Spider2/tests/deque_push.c

59 lines
2.5 KiB
C

#include "util.h"
#include "unit.h"
#define SIZE_MAX (size_t)-1
int tests_deque_push(int argc, char **argv) {
int_deque_t deq;
// push back and front
deque_init(int_deque_t, deq);
deque_push_back(deq, 10);
chi_assert("incorrect back value", deq.base[0] == 10);
deque_push_front(deq, 69);
chi_assert("incorrect back value", deq.base[0] == 10);
chi_assert("incorrect front value", deq.base[deq.cap-1] == 69);
for(size_t i = 0; i < DEQUE_INIT_CAP - 2; i++)
deque_push_front(deq, i);
chi_assert("length and/or capacity incorrect", deq.len == deq.cap && deq.cap == DEQUE_INIT_CAP);
chi_assert("incorrect back value", deq.base[0] == 10);
chi_assert("incorrect front value ", deq.base[deq.cap-1] == 69);
chi_assert("incorrect back", deq.back == 1);
chi_assert("incorrect front", deq.front == 1);
for (size_t i = 2; i < deq.cap - 1; i++)
chi_assert("incorrect ordering", deq.base[i-1] > deq.base[i]);
deque_push_back(deq, 0xee);
chi_assert("length and/or capacity incorrect correct",
deq.len == DEQUE_INIT_CAP + 1 && deq.cap == DEQUE_INIT_CAP * 2);
chi_assert("incorrect back value 1", deq.base[deq.len - 1] == 0xee);
chi_assert("incorrect back value 2", deq.base[deq.len - 2] == 10);
chi_assert("incorrect front value", deq.base[deq.len - 3] == 69);
chi_assert("incorrect back", deq.back == DEQUE_INIT_CAP + 1);
chi_assert("incorrect front", deq.front == 0);
for (size_t i = 1; i < deq.len - 3; i++)
chi_assert("incorrect ordering", deq.base[i-1] > deq.base[i]);
deque_destroy(deq);
// push front
deque_init(int_deque_t, deq);
for (size_t i = 4 * DEQUE_INIT_CAP - 1; i != SIZE_MAX; i--)
deque_push_front(deq, i);
chi_assert("incorrect back and/or front", deq.back == deq.front && deq.back == DEQUE_INIT_CAP * 2);
chi_assert("incorrect length and/or capcity", deq.len == deq.cap && deq.cap == 4 * DEQUE_INIT_CAP);
for (size_t i = 0; i < deq.cap; i++)
chi_assert("incorrect value", *deque_get(deq, i) == i);
deque_destroy(deq);
// push back
deque_init(int_deque_t, deq);
for (size_t i = 0; i < 4 * DEQUE_INIT_CAP; i++)
deque_push_back(deq, i);
chi_assert("incorrect back and/or front", deq.back == deq.front && deq.back == 0);
chi_assert("incorrect length and/or capcity", deq.len == deq.cap && deq.cap == 4 * DEQUE_INIT_CAP);
for (size_t i = 0; i < deq.cap; i++)
chi_assert("incorrect value", *deque_get(deq, i) == i);
deque_destroy(deq);
return 0;
}