From bfffbb92115ba4058281306008636a18e0359b96 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 25 Dec 2024 14:05:22 +0100 Subject: [PATCH] Basic tests implemented using cmocka --- Makefile | 8 +++- test/test_ringbuf.c | 101 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 test/test_ringbuf.c diff --git a/Makefile b/Makefile index ad23202..50d4220 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,9 @@ ASMS = $(C_SOURCES:.c=.s) all: $(OBJECTS) +test: test/test.elf + ./test/test.elf + %.o: %.c $(C_HEADERS) @echo "CC $<" @$(CC) $(CFLAGS) -c $< -o $@ @@ -41,6 +44,9 @@ driver: $(OBJECTS) run: driver @./driver +test/test.elf: test/test_ringbuf.o ringbuf.o + @$(CC) $(CFLAGS) $^ -o $@ -lpthread -lcmocka + lib: $(OBJECTS) @ar rcs librbuf.a ringbuf.o @@ -64,4 +70,4 @@ tidy: format: clang-format -i $(C_SOURCES) $(C_HEADERS) -.PHONY: all clean format asm +.PHONY: all clean format asm test diff --git a/test/test_ringbuf.c b/test/test_ringbuf.c new file mode 100644 index 0000000..44756e9 --- /dev/null +++ b/test/test_ringbuf.c @@ -0,0 +1,101 @@ +#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); +}