#include #include #include #include #include #include #include "ringbuf.h" /** Tests initialization */ static void test_rb_init_empty(void **state) { struct RingBuf rb; rb_size_t capacity = 10; rb_size_t struct_size = sizeof(int); rb_init(&rb, capacity, malloc, struct_size); assert_int_equal(rb.capacity, capacity); assert_int_equal(rb.count, 0); } /** Tests push_back */ static void test_rb_push_back(void **state) { struct RingBuf rb; rb_size_t capacity = 10; rb_size_t struct_size = sizeof(int); rb_init(&rb, capacity, malloc, struct_size); int data = 10; enum WriteResult wr = rb_push_back(&rb, (void *)&data, memcpy); assert_int_equal(wr, WriteOk); assert_int_equal(rb.capacity, capacity); assert_int_equal(rb.count, 1); } /** Tests the destroy function */ static void test_rb_init_destroy(void **state) { struct RingBuf rb = { 0, 0, 0, NULL, NULL, NULL, NULL }; rb_size_t capacity = 10; rb_size_t struct_size = sizeof(int); rb_init(&rb, capacity, malloc, struct_size); int data = 10; rb_push_back(&rb, (void *)&data, memcpy); assert_int_equal(rb.capacity, capacity); assert_int_equal(rb.count, 1); rb_destroy(&rb, free); assert_null(rb.buffer); } /** Tests fill, but not overflow/wrap-around */ static void test_rb_push_back_fill(void **state) { struct RingBuf rb = { 0, 0, 0, NULL, NULL, NULL, NULL }; rb_size_t capacity = 10; rb_size_t struct_size = sizeof(int); rb_init(&rb, capacity, malloc, struct_size); const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Insert them all sequentially for(int i = 0; rb_push_back(&rb, &arr[i], memcpy) == WriteOk; i++); assert_int_equal(rb.count, 10); assert_int_equal(rb.capacity, capacity); // Read them out and assert expected value for(int i = 0, d = __INT_MAX__; rb_pop_front(&rb, &d, memcpy) == ReadOk; i++) { assert_int_equal(d, arr[i]); } assert_int_equal(rb.capacity, capacity); assert_int_equal(rb.count, 0); rb_destroy(&rb, free); assert_null(rb.buffer); } int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_rb_init_empty), cmocka_unit_test(test_rb_push_back), cmocka_unit_test(test_rb_init_destroy), cmocka_unit_test(test_rb_push_back_fill), }; return cmocka_run_group_tests(tests, NULL, NULL); }