diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-04-26 19:18:11 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-04-28 21:57:58 +0200 |
commit | ab4685070515230c9aa64f9e69eec7fc061ee8d7 (patch) | |
tree | a051dd2237119d9094dff919bff6548536e67db6 /tests/common | |
parent | f09e36acfe2db50e0452ba1e19b13f4395a8d704 (diff) | |
download | blogc-ab4685070515230c9aa64f9e69eec7fc061ee8d7.tar.gz blogc-ab4685070515230c9aa64f9e69eec7fc061ee8d7.tar.bz2 blogc-ab4685070515230c9aa64f9e69eec7fc061ee8d7.zip |
common: added bc_slist_sort
Diffstat (limited to 'tests/common')
-rw-r--r-- | tests/common/check_sort.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/tests/common/check_sort.c b/tests/common/check_sort.c new file mode 100644 index 0000000..3c4764f --- /dev/null +++ b/tests/common/check_sort.c @@ -0,0 +1,147 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br> + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +#include <stdlib.h> +#include "../../src/common/utils.h" +#include "../../src/common/sort.h" + + +static int +sort_func(void *a, void *b) +{ + return strcmp((char*) a, (char*) b); +} + + +static void +test_slist_sort_empty(void **state) +{ + bc_slist_t *l = NULL; + assert_null(bc_slist_sort(l, sort_func)); +} + + +static void +test_slist_sort_single(void **state) +{ + bc_slist_t *l = NULL; + l = bc_slist_append(l, bc_strdup("a")); + + l = bc_slist_sort(l, sort_func); + + assert_non_null(l); + assert_string_equal(l->data, "a"); + assert_null(l->next); + + bc_slist_free_full(l, free); +} + + +static void +test_slist_sort_sorted(void **state) +{ + bc_slist_t *l = NULL; + l = bc_slist_append(l, bc_strdup("a")); + l = bc_slist_append(l, bc_strdup("b")); + l = bc_slist_append(l, bc_strdup("c")); + + l = bc_slist_sort(l, sort_func); + + assert_non_null(l); + assert_string_equal(l->data, "a"); + assert_string_equal(l->next->data, "b"); + assert_string_equal(l->next->next->data, "c"); + assert_null(l->next->next->next); + + bc_slist_free_full(l, free); +} + + +static void +test_slist_sort_reverse(void **state) +{ + bc_slist_t *l = NULL; + l = bc_slist_append(l, bc_strdup("d")); + l = bc_slist_append(l, bc_strdup("c")); + l = bc_slist_append(l, bc_strdup("b")); + l = bc_slist_append(l, bc_strdup("a")); + + l = bc_slist_sort(l, sort_func); + + assert_non_null(l); + assert_string_equal(l->data, "a"); + assert_string_equal(l->next->data, "b"); + assert_string_equal(l->next->next->data, "c"); + assert_string_equal(l->next->next->next->data, "d"); + assert_null(l->next->next->next->next); + + bc_slist_free_full(l, free); +} + + +static void +test_slist_sort_mixed1(void **state) +{ + bc_slist_t *l = NULL; + l = bc_slist_append(l, bc_strdup("a")); + l = bc_slist_append(l, bc_strdup("d")); + l = bc_slist_append(l, bc_strdup("c")); + l = bc_slist_append(l, bc_strdup("b")); + + l = bc_slist_sort(l, sort_func); + + assert_non_null(l); + assert_string_equal(l->data, "a"); + assert_string_equal(l->next->data, "b"); + assert_string_equal(l->next->next->data, "c"); + assert_string_equal(l->next->next->next->data, "d"); + assert_null(l->next->next->next->next); + + bc_slist_free_full(l, free); +} + + +static void +test_slist_sort_mixed2(void **state) +{ + bc_slist_t *l = NULL; + l = bc_slist_append(l, bc_strdup("c")); + l = bc_slist_append(l, bc_strdup("b")); + l = bc_slist_append(l, bc_strdup("a")); + l = bc_slist_append(l, bc_strdup("d")); + + l = bc_slist_sort(l, sort_func); + + assert_non_null(l); + assert_string_equal(l->data, "a"); + assert_string_equal(l->next->data, "b"); + assert_string_equal(l->next->next->data, "c"); + assert_string_equal(l->next->next->next->data, "d"); + assert_null(l->next->next->next->next); + + bc_slist_free_full(l, free); +} + + +int +main(void) +{ + const UnitTest tests[] = { + unit_test(test_slist_sort_empty), + unit_test(test_slist_sort_single), + unit_test(test_slist_sort_sorted), + unit_test(test_slist_sort_reverse), + unit_test(test_slist_sort_mixed1), + unit_test(test_slist_sort_mixed2), + }; + return run_tests(tests); +} |