diff options
author | Petr Štetiar <ynezz@true.cz> | 2019-11-19 16:31:44 +0300 |
---|---|---|
committer | Petr Štetiar <ynezz@true.cz> | 2019-11-24 15:26:58 +0300 |
commit | b0a5cd8a28bf1d1883317ceac6cb8967d840d6ae (patch) | |
tree | 8600f4d551a16671af14f1d086014b111316ea10 /tests/test-list.c | |
parent | 1fefb7c4d7f90464940143c93e1b98f44ecf6590 (diff) |
add cram based unit tests
For improved QA etc. For the start with initial test cases for avl,
base64, jshn and list components. Moved runqueue and blobmsg from
examples to tests. Converted just a few first test cases from
json-script example into the new cram based unit test, more to come.
Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'tests/test-list.c')
-rw-r--r-- | tests/test-list.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/test-list.c b/tests/test-list.c new file mode 100644 index 0000000..cb0f231 --- /dev/null +++ b/tests/test-list.c @@ -0,0 +1,91 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "list.h" +#include "utils.h" + +struct item { + const char *name; + struct list_head list; +}; + +#define OUT(fmt, ...) do { \ + fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \ +} while (0); + +static void test_basics() +{ + size_t i; + struct item *tmp; + struct item *item; + struct item *last; + struct item *first; + static struct list_head test_list = LIST_HEAD_INIT(test_list); + + const char *vals[] = { + "zero", "one", "two", "three", "four", "five", "six", + "seven", "eight", "nine", "ten", "eleven", "twelve" + }; + + OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no"); + OUT("list_add_tail: "); + for (i=0; i<ARRAY_SIZE(vals); i++) { + struct item *e = malloc(sizeof(struct item)); + e->name = vals[i]; + list_add_tail(&e->list, &test_list); + fprintf(stdout, "%s ", vals[i]); + } + fprintf(stdout, "\n"); + OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no"); + + first = list_first_entry(&test_list, struct item, list); + last = list_last_entry(&test_list, struct item, list); + OUT("first=%s last=%s\n", first->name, last->name); + OUT("'zero' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no"); + OUT("'twelve' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no"); + + OUT("removing 'twelve' and 'zero'\n"); + list_del(&first->list); + list_del(&last->list); + free(first); + free(last); + first = list_first_entry(&test_list, struct item, list); + last = list_last_entry(&test_list, struct item, list); + OUT("first=%s last=%s\n", first->name, last->name); + OUT("'one' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no"); + OUT("'eleven' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no"); + + OUT("moving 'one' to the tail\n"); + list_move_tail(&first->list, &test_list); + first = list_first_entry(&test_list, struct item, list); + last = list_last_entry(&test_list, struct item, list); + OUT("first=%s last=%s\n", first->name, last->name); + OUT("'two' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no"); + OUT("'one' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no"); + + OUT("list_for_each_entry: "); + list_for_each_entry(item, &test_list, list) { + fprintf(stdout, "%s ", item->name); + } + fprintf(stdout, "\n"); + + OUT("list_for_each_entry_reverse: "); + list_for_each_entry_reverse(item, &test_list, list) { + fprintf(stdout, "%s ", item->name); + } + fprintf(stdout, "\n"); + + OUT("delete all entries\n"); + list_for_each_entry_safe(item, tmp, &test_list, list) { + list_del(&item->list); + free(item); + } + OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no"); +} + +int main() +{ + test_basics(); + return 0; +} |