Basic tests implemented using cmocka
This commit is contained in:
parent
1e76066400
commit
bfffbb9211
2 changed files with 108 additions and 1 deletions
8
Makefile
8
Makefile
|
@ -27,6 +27,9 @@ ASMS = $(C_SOURCES:.c=.s)
|
||||||
|
|
||||||
all: $(OBJECTS)
|
all: $(OBJECTS)
|
||||||
|
|
||||||
|
test: test/test.elf
|
||||||
|
./test/test.elf
|
||||||
|
|
||||||
%.o: %.c $(C_HEADERS)
|
%.o: %.c $(C_HEADERS)
|
||||||
@echo "CC $<"
|
@echo "CC $<"
|
||||||
@$(CC) $(CFLAGS) -c $< -o $@
|
@$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
@ -41,6 +44,9 @@ driver: $(OBJECTS)
|
||||||
run: driver
|
run: driver
|
||||||
@./driver
|
@./driver
|
||||||
|
|
||||||
|
test/test.elf: test/test_ringbuf.o ringbuf.o
|
||||||
|
@$(CC) $(CFLAGS) $^ -o $@ -lpthread -lcmocka
|
||||||
|
|
||||||
lib: $(OBJECTS)
|
lib: $(OBJECTS)
|
||||||
@ar rcs librbuf.a ringbuf.o
|
@ar rcs librbuf.a ringbuf.o
|
||||||
|
|
||||||
|
@ -64,4 +70,4 @@ tidy:
|
||||||
format:
|
format:
|
||||||
clang-format -i $(C_SOURCES) $(C_HEADERS)
|
clang-format -i $(C_SOURCES) $(C_HEADERS)
|
||||||
|
|
||||||
.PHONY: all clean format asm
|
.PHONY: all clean format asm test
|
||||||
|
|
101
test/test_ringbuf.c
Normal file
101
test/test_ringbuf.c
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <cmocka.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#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);
|
||||||
|
}
|
Loading…
Reference in a new issue