diff options
author | Kenny Levinsen <kl@kl.wtf> | 2020-08-31 14:30:28 +0200 |
---|---|---|
committer | Kenny Levinsen <kl@kl.wtf> | 2020-08-31 14:40:58 +0200 |
commit | aef19fe383a70f47fa83a70cfce70fa4a713c1d1 (patch) | |
tree | 521611c3bb6f44b2b8978380fd53248025c621e1 | |
parent | 8610ec4aac6027e702d0aaea0331ccaa41489834 (diff) |
test: Add test_run and test_assert macros
test_run and test_assert replaces regular assert with better logging
which include the currently running test name. The tests can now also be
built without DEBUG.
-rw-r--r-- | include/test.h | 23 | ||||
-rw-r--r-- | tests/linked_list.c | 146 |
2 files changed, 96 insertions, 73 deletions
diff --git a/include/test.h b/include/test.h new file mode 100644 index 0000000..64716cc --- /dev/null +++ b/include/test.h @@ -0,0 +1,23 @@ +#ifndef _TEST_H +#define _TEST_H + +char *__curtestname = "<none>"; + +#define test_run(func) \ + do { \ + char *orig = __curtestname; \ + __curtestname = #func; \ + func(); \ + __curtestname = orig; \ + } while (0) + +#define test_assert(cond) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, "%s:%d: %s: test_assert failed: %s\n", __FILE__, __LINE__, \ + __curtestname, #cond); \ + abort(); \ + } \ + } while (0) + +#endif diff --git a/tests/linked_list.c b/tests/linked_list.c index 71322b8..6d7a995 100644 --- a/tests/linked_list.c +++ b/tests/linked_list.c @@ -1,9 +1,9 @@ -#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "linked_list.h" +#include "test.h" struct list_elem { struct linked_list link; @@ -15,10 +15,10 @@ static void test_linked_list_init(void) { linked_list_init(&list); // Both next and prev should point to self - assert(list.next == &list && list.prev == &list); + test_assert(list.next == &list && list.prev == &list); // The list should be empty - assert(linked_list_empty(&list)); + test_assert(linked_list_empty(&list)); } static void test_linked_list_single_insert(void) { @@ -29,14 +29,14 @@ static void test_linked_list_single_insert(void) { linked_list_insert(&list, &elem1.link); // Both next and prev on list should point to the elem - assert(list.next == &elem1.link && list.prev == &elem1.link); + test_assert(list.next == &elem1.link && list.prev == &elem1.link); // Both next and prev on elem should point to the list - assert(elem1.link.next == &list && elem1.link.prev == &list); + test_assert(elem1.link.next == &list && elem1.link.prev == &list); // The list and element should not be empty - assert(!linked_list_empty(&list)); - assert(!linked_list_empty(&elem1.link)); + test_assert(!linked_list_empty(&list)); + test_assert(!linked_list_empty(&elem1.link)); } static void test_linked_list_single_remove(void) { @@ -48,13 +48,13 @@ static void test_linked_list_single_remove(void) { linked_list_remove(&elem1.link); // Both next and prev on elem be NULL - assert(elem1.link.next == NULL && elem1.link.prev == NULL); + test_assert(elem1.link.next == NULL && elem1.link.prev == NULL); // Both next and prev should point to self - assert(list.next == &list && list.prev == &list); + test_assert(list.next == &list && list.prev == &list); // The list should be empty - assert(linked_list_empty(&list)); + test_assert(linked_list_empty(&list)); } static void test_linked_list_alternate_remove(void) { @@ -66,13 +66,13 @@ static void test_linked_list_alternate_remove(void) { linked_list_remove(&list); // Both next and prev on list be NULL - assert(list.next == NULL && list.prev == NULL); + test_assert(list.next == NULL && list.prev == NULL); // Both next and prev should point to self - assert(elem1.link.next == &elem1.link && elem1.link.prev == &elem1.link); + test_assert(elem1.link.next == &elem1.link && elem1.link.prev == &elem1.link); // The elem should be empty - assert(linked_list_empty(&elem1.link)); + test_assert(linked_list_empty(&elem1.link)); } static void test_linked_list_sequential_remove(void) { @@ -85,35 +85,35 @@ static void test_linked_list_sequential_remove(void) { linked_list_insert(&elem2.link, &elem3.link); // The order should now be list→elem1→elem2→elem3→list - assert(list.next == &elem1.link && list.prev == &elem3.link); - assert(elem1.link.next == &elem2.link && elem1.link.prev == &list); - assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link); - assert(elem3.link.next == &list && elem3.link.prev == &elem2.link); + test_assert(list.next == &elem1.link && list.prev == &elem3.link); + test_assert(elem1.link.next == &elem2.link && elem1.link.prev == &list); + test_assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link); + test_assert(elem3.link.next == &list && elem3.link.prev == &elem2.link); linked_list_remove(list.next); // The order should now be list→elem2→elem3→list - assert(list.next == &elem2.link && list.prev == &elem3.link); - assert(elem2.link.next == &elem3.link && elem2.link.prev == &list); - assert(elem3.link.next == &list && elem3.link.prev == &elem2.link); - assert(elem1.link.next == NULL && elem1.link.prev == NULL); + test_assert(list.next == &elem2.link && list.prev == &elem3.link); + test_assert(elem2.link.next == &elem3.link && elem2.link.prev == &list); + test_assert(elem3.link.next == &list && elem3.link.prev == &elem2.link); + test_assert(elem1.link.next == NULL && elem1.link.prev == NULL); linked_list_remove(list.next); // The order should now be list→elem3→list - assert(list.next == &elem3.link && list.prev == &elem3.link); - assert(elem3.link.next == &list && elem3.link.prev == &list); - assert(elem1.link.next == NULL && elem1.link.prev == NULL); - assert(elem2.link.next == NULL && elem2.link.prev == NULL); + test_assert(list.next == &elem3.link && list.prev == &elem3.link); + test_assert(elem3.link.next == &list && elem3.link.prev == &list); + test_assert(elem1.link.next == NULL && elem1.link.prev == NULL); + test_assert(elem2.link.next == NULL && elem2.link.prev == NULL); linked_list_remove(list.next); // The list should now be empty - assert(elem1.link.next == NULL && elem1.link.prev == NULL); - assert(elem2.link.next == NULL && elem2.link.prev == NULL); - assert(elem3.link.next == NULL && elem3.link.prev == NULL); - assert(list.next == &list && list.prev == &list); - assert(linked_list_empty(&list)); + test_assert(elem1.link.next == NULL && elem1.link.prev == NULL); + test_assert(elem2.link.next == NULL && elem2.link.prev == NULL); + test_assert(elem3.link.next == NULL && elem3.link.prev == NULL); + test_assert(list.next == &list && list.prev == &list); + test_assert(linked_list_empty(&list)); } static void test_linked_list_insert_after(void) { @@ -126,10 +126,10 @@ static void test_linked_list_insert_after(void) { linked_list_insert(&elem1.link, &elem2.link); // The order should now be list→elem1→elem2→elem3→list - assert(list.next == &elem1.link && list.prev == &elem3.link); - assert(elem1.link.next == &elem2.link && elem1.link.prev == &list); - assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link); - assert(elem3.link.next == &list && elem3.link.prev == &elem2.link); + test_assert(list.next == &elem1.link && list.prev == &elem3.link); + test_assert(elem1.link.next == &elem2.link && elem1.link.prev == &list); + test_assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link); + test_assert(elem3.link.next == &list && elem3.link.prev == &elem2.link); } static void test_linked_list_remove_loop(void) { @@ -147,13 +147,13 @@ static void test_linked_list_remove_loop(void) { linked_list_remove(&elem->link); cnt++; } - assert(cnt == 3); + test_assert(cnt == 3); // Link should now be empty, and next and prev on all elements hsould be NULL - assert(linked_list_empty(&list)); - assert(elem1.link.next == NULL && elem1.link.prev == NULL); - assert(elem2.link.next == NULL && elem2.link.prev == NULL); - assert(elem3.link.next == NULL && elem3.link.prev == NULL); + test_assert(linked_list_empty(&list)); + test_assert(elem1.link.next == NULL && elem1.link.prev == NULL); + test_assert(elem2.link.next == NULL && elem2.link.prev == NULL); + test_assert(elem3.link.next == NULL && elem3.link.prev == NULL); } static void test_linked_list_manual_iterate(void) { @@ -170,15 +170,15 @@ static void test_linked_list_manual_iterate(void) { struct list_elem *ptr = NULL; ptr = (struct list_elem *)list.next; - assert(strcmp("elem1", ptr->content) == 0); + test_assert(strcmp("elem1", ptr->content) == 0); ptr = (struct list_elem *)ptr->link.next; - assert(strcmp("elem2", ptr->content) == 0); + test_assert(strcmp("elem2", ptr->content) == 0); ptr = (struct list_elem *)ptr->link.next; - assert(strcmp("elem3", ptr->content) == 0); + test_assert(strcmp("elem3", ptr->content) == 0); - assert(ptr->link.next == &list); + test_assert(ptr->link.next == &list); } static void test_linked_list_loop_iterate(void) { @@ -195,10 +195,10 @@ static void test_linked_list_loop_iterate(void) { size_t cnt = 0; for (struct linked_list *ptr = list.next; ptr != &list; ptr = ptr->next) { struct list_elem *elem = (struct list_elem *)ptr; - assert(strcmp("elem", elem->content) == 0); + test_assert(strcmp("elem", elem->content) == 0); cnt++; } - assert(cnt == 3); + test_assert(cnt == 3); } static void test_linked_list_take_empty(void) { @@ -208,8 +208,8 @@ static void test_linked_list_take_empty(void) { linked_list_take(&list2, &list1); - assert(linked_list_empty(&list1)); - assert(linked_list_empty(&list2)); + test_assert(linked_list_empty(&list1)); + test_assert(linked_list_empty(&list2)); } static void test_linked_list_take_single(void) { @@ -222,9 +222,9 @@ static void test_linked_list_take_single(void) { linked_list_take(&list2, &list1); - assert(linked_list_empty(&list1)); - assert(list2.next == &elem1.link && list2.prev == &elem1.link); - assert(elem1.link.next == &list2 && elem1.link.prev == &list2); + test_assert(linked_list_empty(&list1)); + test_assert(list2.next == &elem1.link && list2.prev == &elem1.link); + test_assert(elem1.link.next == &list2 && elem1.link.prev == &list2); } static void test_linked_list_take_many(void) { @@ -239,10 +239,10 @@ static void test_linked_list_take_many(void) { linked_list_take(&list2, &list1); - assert(linked_list_empty(&list1)); - assert(list2.next == &elem1.link && list2.prev == &elem2.link); - assert(elem1.link.next == &elem2.link && elem1.link.prev == &list2); - assert(elem2.link.next == &list2 && elem2.link.prev == &elem1.link); + test_assert(linked_list_empty(&list1)); + test_assert(list2.next == &elem1.link && list2.prev == &elem2.link); + test_assert(elem1.link.next == &elem2.link && elem1.link.prev == &list2); + test_assert(elem2.link.next == &list2 && elem2.link.prev == &elem1.link); } static void test_linked_list_take_concat(void) { @@ -261,31 +261,31 @@ static void test_linked_list_take_concat(void) { linked_list_take(&list2, &list1); - assert(linked_list_empty(&list1)); - assert(list2.next == &elem1.link && list2.prev == &elem4.link); - assert(elem1.link.next == &elem2.link && elem1.link.prev == &list2); - assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link); - assert(elem3.link.next == &elem4.link && elem3.link.prev == &elem2.link); - assert(elem4.link.next == &list2 && elem4.link.prev == &elem3.link); + test_assert(linked_list_empty(&list1)); + test_assert(list2.next == &elem1.link && list2.prev == &elem4.link); + test_assert(elem1.link.next == &elem2.link && elem1.link.prev == &list2); + test_assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link); + test_assert(elem3.link.next == &elem4.link && elem3.link.prev == &elem2.link); + test_assert(elem4.link.next == &list2 && elem4.link.prev == &elem3.link); } int main(int argc, char *argv[]) { (void)argc; (void)argv; - test_linked_list_init(); - test_linked_list_single_insert(); - test_linked_list_single_remove(); - test_linked_list_alternate_remove(); - test_linked_list_sequential_remove(); - test_linked_list_insert_after(); - test_linked_list_remove_loop(); - test_linked_list_manual_iterate(); - test_linked_list_loop_iterate(); - test_linked_list_take_empty(); - test_linked_list_take_single(); - test_linked_list_take_many(); - test_linked_list_take_concat(); + test_run(test_linked_list_init); + test_run(test_linked_list_single_insert); + test_run(test_linked_list_single_remove); + test_run(test_linked_list_alternate_remove); + test_run(test_linked_list_sequential_remove); + test_run(test_linked_list_insert_after); + test_run(test_linked_list_remove_loop); + test_run(test_linked_list_manual_iterate); + test_run(test_linked_list_loop_iterate); + test_run(test_linked_list_take_empty); + test_run(test_linked_list_take_single); + test_run(test_linked_list_take_many); + test_run(test_linked_list_take_concat); return 0; } |