46 lines
1.6 KiB
C
46 lines
1.6 KiB
C
|
#include <stdlib.h>
|
||
|
|
||
|
#include "util.h"
|
||
|
#include "unit.h"
|
||
|
|
||
|
#define TEST_LEN 32768
|
||
|
#define SIZE_MAX (size_t)-1
|
||
|
|
||
|
int tests_deque_pop(int argc, char **argv) {
|
||
|
int *expected_front = xmalloc(TEST_LEN / 2 * sizeof(int));
|
||
|
int *expected_back = xmalloc(TEST_LEN / 2 * sizeof(int));
|
||
|
for (size_t i = 0; i < TEST_LEN / 2; i++)
|
||
|
expected_front[i] = rand();
|
||
|
for (size_t i = 0; i < TEST_LEN / 2; i++)
|
||
|
expected_back[i] = rand();
|
||
|
int_deque_t deq;
|
||
|
deque_init(int_deque_t, deq);
|
||
|
for (size_t i = 0; i < TEST_LEN / 2; i++) {
|
||
|
if (rand() % 2 == 0) {
|
||
|
deque_push_back(deq, expected_back[i]);
|
||
|
deque_push_front(deq, expected_front[TEST_LEN / 2 - i - 1]);
|
||
|
}
|
||
|
else {
|
||
|
deque_push_front(deq, expected_front[TEST_LEN / 2 - i - 1]);
|
||
|
deque_push_back(deq, expected_back[i]);
|
||
|
}
|
||
|
}
|
||
|
int_deque_t from_back, from_front;
|
||
|
deque_clone(from_back, deq);
|
||
|
deque_clone(from_front, deq);
|
||
|
deque_destroy(deq);
|
||
|
for (size_t i = 0; i < 2 * (TEST_LEN / 2); i++) {
|
||
|
int exp = (i >= TEST_LEN / 2) ? expected_back[i - TEST_LEN / 2] : expected_front[i];
|
||
|
chi_assert("from_front value doesnt match", deque_pop_front(from_front) == exp);
|
||
|
}
|
||
|
for (size_t i = 2 * (TEST_LEN / 2) - 1; i != SIZE_MAX; i--) {
|
||
|
int exp = (i >= TEST_LEN / 2) ? expected_back[i - TEST_LEN / 2] : expected_front[i];
|
||
|
chi_assert("from_back value doesnt match", deque_pop_back(from_back) == exp);
|
||
|
}
|
||
|
deque_destroy(from_front);
|
||
|
deque_destroy(from_back);
|
||
|
xfree(expected_back);
|
||
|
xfree(expected_front);
|
||
|
return 0;
|
||
|
}
|