From f52a9ff702717f6b785bba112999f41fe0ea2a59 Mon Sep 17 00:00:00 2001 From: Leonardo Date: Mon, 30 Sep 2019 18:27:19 -0300 Subject: [PATCH] Refactoring tests and added sortedlist test --- tests/main/main.c | 2 ++ tests/main/unt/hashmap.c | 5 ++- tests/main/unt/hashset.c | 5 ++- tests/main/unt/heap.c | 7 ++-- tests/main/unt/linkedlist.c | 3 +- tests/main/unt/list.c | 22 ++++++------ tests/main/unt/multimap.c | 9 +++-- tests/main/unt/multiset.c | 5 ++- tests/main/unt/queue.c | 7 ++-- tests/main/unt/sortedlist.c | 68 +++++++++++++++++++++++++++++++++++++ tests/main/unt/stack.c | 41 +++++++++++----------- tests/main/unt/treemap.c | 1 - tests/main/unt/treeset.c | 1 - 13 files changed, 118 insertions(+), 58 deletions(-) create mode 100644 tests/main/unt/sortedlist.c diff --git a/tests/main/main.c b/tests/main/main.c index 24cae15..9967ada 100644 --- a/tests/main/main.c +++ b/tests/main/main.c @@ -14,6 +14,7 @@ #include "unt/intervalheap.c" #include "unt/multimap.c" #include "unt/multiset.c" +#include "unt/sortedlist.c" int main(void) { @@ -37,6 +38,7 @@ int main(void) failed += intervalheap_test(); failed += multimap_test(); failed += multiset_test(); + failed += sortedlist_test(); cmc_timer_stop(timer); cmc_timer_calc(timer); diff --git a/tests/main/unt/hashmap.c b/tests/main/unt/hashmap.c index f80445d..c8ccddd 100644 --- a/tests/main/unt/hashmap.c +++ b/tests/main/unt/hashmap.c @@ -19,13 +19,13 @@ CMC_CREATE_UNIT(hashmap_test, true, { hm_free(map, NULL); }); - CMC_CREATE_TEST(new[edge_case:capacity = 0], { + CMC_CREATE_TEST(new [edge_case:capacity = 0], { hashmap *map = hm_new(0, 0.6, cmp, hash); cmc_assert_equals(ptr, NULL, map); }); - CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], { + CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], { hashmap *map = hm_new(UINT64_MAX, 0.6, cmp, hash); cmc_assert_equals(ptr, NULL, map); @@ -47,5 +47,4 @@ CMC_CREATE_UNIT(hashmap_test, true, { hm_free(map, NULL); }); - }); diff --git a/tests/main/unt/hashset.c b/tests/main/unt/hashset.c index fa5e911..df2c7cb 100644 --- a/tests/main/unt/hashset.c +++ b/tests/main/unt/hashset.c @@ -19,13 +19,13 @@ CMC_CREATE_UNIT(hashset_test, true, { hs_free(set, NULL); }); - CMC_CREATE_TEST(new[edge_case:capacity = 0], { + CMC_CREATE_TEST(new [edge_case:capacity = 0], { hashset *set = hs_new(0, 0.6, cmp, hash); cmc_assert_equals(ptr, NULL, set); }); - CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], { + CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], { hashset *set = hs_new(UINT64_MAX, 0.6, cmp, hash); cmc_assert_equals(ptr, NULL, set); @@ -47,5 +47,4 @@ CMC_CREATE_UNIT(hashset_test, true, { hs_free(set, NULL); }); - }); diff --git a/tests/main/unt/heap.c b/tests/main/unt/heap.c index 84b4aaf..4d6e931 100644 --- a/tests/main/unt/heap.c +++ b/tests/main/unt/heap.c @@ -19,13 +19,13 @@ CMC_CREATE_UNIT(heap_test, true, { h_free(h, NULL); }); - CMC_CREATE_TEST(new[edge_case:capacity = 0], { + CMC_CREATE_TEST(new [edge_case:capacity = 0], { heap *h = h_new(0, cmc_max_heap, cmp); cmc_assert_equals(ptr, NULL, h); }); - CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], { + CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], { heap *h = h_new(UINT64_MAX, cmc_max_heap, cmp); cmc_assert_equals(ptr, NULL, h); @@ -49,7 +49,7 @@ CMC_CREATE_UNIT(heap_test, true, { h_free(h, NULL); }); - CMC_CREATE_TEST(buffer_growth[edge_case:capacity = 1], { + CMC_CREATE_TEST(buffer_growth [edge_case:capacity = 1], { heap *h = h_new(1, cmc_max_heap, cmp); cmc_assert_not_equals(ptr, NULL, h); @@ -61,5 +61,4 @@ CMC_CREATE_UNIT(heap_test, true, { h_free(h, NULL); }); - }); diff --git a/tests/main/unt/linkedlist.c b/tests/main/unt/linkedlist.c index 0d7b5e3..fbe28bb 100644 --- a/tests/main/unt/linkedlist.c +++ b/tests/main/unt/linkedlist.c @@ -36,7 +36,7 @@ CMC_CREATE_UNIT(linkedlist_test, true, { ll_free(ll, NULL); }); - CMC_CREATE_TEST(clear[edge_case:count = 0], { + CMC_CREATE_TEST(clear [edge_case:count = 0], { linkedlist *ll = ll_new(); cmc_assert_not_equals(ptr, NULL, ll); @@ -75,5 +75,4 @@ CMC_CREATE_UNIT(linkedlist_test, true, { ll_free(ll, NULL); }); - }); diff --git a/tests/main/unt/list.c b/tests/main/unt/list.c index c5e25c9..c72a055 100644 --- a/tests/main/unt/list.c +++ b/tests/main/unt/list.c @@ -19,13 +19,13 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(new[edge_case:capacity = 0], { + CMC_CREATE_TEST(new [edge_case:capacity = 0], { list *l = l_new(0); cmc_assert_equals(ptr, NULL, l); }); - CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], { + CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], { list *l = l_new(UINT64_MAX); cmc_assert_equals(ptr, NULL, l); @@ -49,7 +49,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(new_from[edge_case:size = 1], { + CMC_CREATE_TEST(new_from [edge_case:size = 1], { size_t a = 10; size_t *values = &a; @@ -79,7 +79,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(buffer_growth[edge_case:capacity = 1], { + CMC_CREATE_TEST(buffer_growth [edge_case:capacity = 1], { list *l = l_new(1); cmc_assert_not_equals(ptr, NULL, l); @@ -253,7 +253,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(pop_front[edge_case:count = 0], { + CMC_CREATE_TEST(pop_front [edge_case:count = 0], { list *l = l_new(100); cmc_assert_not_equals(ptr, NULL, l); @@ -262,7 +262,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(pop_front[edge_case:count = 1], { + CMC_CREATE_TEST(pop_front [edge_case:count = 1], { list *l = l_new(100); cmc_assert_not_equals(ptr, NULL, l); @@ -286,7 +286,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(pop[edge_case:count = 0], { + CMC_CREATE_TEST(pop_at [edge_case:count = 0], { list *l = l_new(100); cmc_assert_not_equals(ptr, NULL, l); @@ -295,7 +295,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(pop[edge_case:count = 1], { + CMC_CREATE_TEST(pop_at [edge_case:count = 1], { list *l = l_new(100); cmc_assert_not_equals(ptr, NULL, l); @@ -307,7 +307,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(pop[buffer_clear], { + CMC_CREATE_TEST(pop_at[buffer_clear], { list *l = l_new(100); cmc_assert_not_equals(ptr, NULL, l); @@ -319,7 +319,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(pop_back[edge_case:count = 0], { + CMC_CREATE_TEST(pop_back [edge_case:count = 0], { list *l = l_new(100); cmc_assert_not_equals(ptr, NULL, l); @@ -328,7 +328,7 @@ CMC_CREATE_UNIT(list_test, true, { l_free(l, NULL); }); - CMC_CREATE_TEST(pop_back[edge_case:count = 1], { + CMC_CREATE_TEST(pop_back [edge_case:count = 1], { list *l = l_new(100); cmc_assert_not_equals(ptr, NULL, l); diff --git a/tests/main/unt/multimap.c b/tests/main/unt/multimap.c index 5ee74fa..364ee5f 100644 --- a/tests/main/unt/multimap.c +++ b/tests/main/unt/multimap.c @@ -19,13 +19,13 @@ CMC_CREATE_UNIT(multimap_test, true, { mm_free(map, NULL); }); - CMC_CREATE_TEST(new[edge_case:capacity = 0], { + CMC_CREATE_TEST(new [edge_case:capacity = 0], { multimap *map = mm_new(0, 0.8, cmp, hash); cmc_assert_equals(ptr, NULL, map); }); - CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], { + CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], { multimap *map = mm_new(UINT64_MAX, 0.8, cmp, hash); cmc_assert_equals(ptr, NULL, map); @@ -79,7 +79,7 @@ CMC_CREATE_UNIT(multimap_test, true, { mm_free(map, NULL); }); - CMC_CREATE_TEST(remove[edge_case:count = 0], { + CMC_CREATE_TEST(remove [edge_case:count = 0], { multimap *map = mm_new(100, 0.8, cmp, hash); cmc_assert_not_equals(ptr, NULL, map); @@ -91,7 +91,7 @@ CMC_CREATE_UNIT(multimap_test, true, { mm_free(map, NULL); }); - CMC_CREATE_TEST(remove[edge_case:count = 1], { + CMC_CREATE_TEST(remove [edge_case:count = 1], { multimap *map = mm_new(100, 0.8, cmp, hash); cmc_assert_not_equals(ptr, NULL, map); @@ -119,5 +119,4 @@ CMC_CREATE_UNIT(multimap_test, true, { mm_free(map, NULL); }); - }); diff --git a/tests/main/unt/multiset.c b/tests/main/unt/multiset.c index f793d02..7774440 100644 --- a/tests/main/unt/multiset.c +++ b/tests/main/unt/multiset.c @@ -19,13 +19,13 @@ CMC_CREATE_UNIT(multiset_test, true, { ms_free(set, NULL); }); - CMC_CREATE_TEST(new[edge_case:capacity = 0], { + CMC_CREATE_TEST(new [edge_case:capacity = 0], { multiset *set = ms_new(0, 0.6, cmp, hash); cmc_assert_equals(ptr, NULL, set); }); - CMC_CREATE_TEST(new[edge_case:capacity = UINTMAX_MAX], { + CMC_CREATE_TEST(new [edge_case:capacity = UINTMAX_MAX], { multiset *set = ms_new(UINTMAX_MAX, 0.99, cmp, hash); cmc_assert_equals(ptr, NULL, set); @@ -85,5 +85,4 @@ CMC_CREATE_UNIT(multiset_test, true, { ms_free(set, NULL); }); - }); diff --git a/tests/main/unt/queue.c b/tests/main/unt/queue.c index 4811fcb..271108b 100644 --- a/tests/main/unt/queue.c +++ b/tests/main/unt/queue.c @@ -20,13 +20,13 @@ CMC_CREATE_UNIT(queue_test, true, { q_free(q, NULL); }); - CMC_CREATE_TEST(new[edge_case:capacity = 0], { + CMC_CREATE_TEST(new [edge_case:capacity = 0], { queue *q = q_new(0); cmc_assert_equals(ptr, NULL, q); }); - CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], { + CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], { queue *q = q_new(UINT64_MAX); cmc_assert_equals(ptr, NULL, q); @@ -50,7 +50,7 @@ CMC_CREATE_UNIT(queue_test, true, { q_free(q, NULL); }); - CMC_CREATE_TEST(buffer_growth[edge_case:capacity = 1], { + CMC_CREATE_TEST(buffer_growth [edge_case:capacity = 1], { queue *q = q_new(1); cmc_assert_not_equals(ptr, NULL, q); @@ -62,5 +62,4 @@ CMC_CREATE_UNIT(queue_test, true, { q_free(q, NULL); }); - }); diff --git a/tests/main/unt/sortedlist.c b/tests/main/unt/sortedlist.c new file mode 100644 index 0000000..fce3781 --- /dev/null +++ b/tests/main/unt/sortedlist.c @@ -0,0 +1,68 @@ +#include "utl.c" +#include +#include +#include + +#include + +CMC_GENERATE_SORTEDLIST(sl, sortedlist, size_t) + +CMC_CREATE_UNIT(sortedlist_test, true, { + CMC_CREATE_TEST(new, { + sortedlist *sl = sl_new(1000000, cmp); + + cmc_assert_not_equals(ptr, NULL, sl); + cmc_assert_not_equals(ptr, NULL, sl->buffer); + cmc_assert_equals(size_t, 1000000, sl_capacity(sl)); + cmc_assert_equals(size_t, 0, sl_count(sl)); + cmc_assert_not_equals(ptr, NULL, sl->buffer); + + sl_free(sl, NULL); + }); + + CMC_CREATE_TEST(new [edge_case:capacity = 0], { + sortedlist *sl = sl_new(0, cmp); + + cmc_assert_equals(ptr, NULL, sl); + }); + + CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], { + sortedlist *sl = sl_new(UINT64_MAX, cmp); + + cmc_assert_equals(ptr, NULL, sl); + }); + + CMC_CREATE_TEST(clear[count capacity], { + sortedlist *sl = sl_new(100, cmp); + + cmc_assert_not_equals(ptr, NULL, sl); + + for (size_t i = 0; i < 50; i++) + cmc_assert(sl_insert(sl, i)); + + cmc_assert_equals(size_t, 50, sl_count(sl)); + + sl_clear(sl, NULL); + + cmc_assert_equals(size_t, 0, sl_count(sl)); + cmc_assert_equals(size_t, 100, sl_capacity(sl)); + + sl_free(sl, NULL); + }); + + CMC_CREATE_TEST(buffer_growth [edge_case:capacity = 1], { + sortedlist *sl = sl_new(1, cmp); + + cmc_assert_not_equals(ptr, NULL, sl); + + for (size_t i = 0; i < 50; i++) + cmc_assert(sl_insert(sl, i)); + + sl_sort(sl); + + cmc_assert_equals(size_t, 50, sl_count(sl)); + cmc_assert_array_sorted_any(size_t, sl->buffer, cmp, 0, sl->count - 1); + + sl_free(sl, NULL); + }); +}); diff --git a/tests/main/unt/stack.c b/tests/main/unt/stack.c index a05d1b1..73e2dda 100644 --- a/tests/main/unt/stack.c +++ b/tests/main/unt/stack.c @@ -20,13 +20,13 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(new[edge_case:capacity = 0], { + CMC_CREATE_TEST(new [edge_case:capacity = 0], { stack *s = s_new(0); cmc_assert_equals(ptr, NULL, s); }); - CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], { + CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], { stack *s = s_new(UINT64_MAX); cmc_assert_equals(ptr, NULL, s); @@ -50,7 +50,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(buffer_growth[edge_case:capacity = 1], { + CMC_CREATE_TEST(buffer_growth [edge_case:capacity = 1], { stack *s = s_new(1); cmc_assert_not_equals(ptr, NULL, s); @@ -151,7 +151,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(pop[edge_case:count = 0], { + CMC_CREATE_TEST(pop [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -177,7 +177,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(top[edge_case:count = 0], { + CMC_CREATE_TEST(top [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -191,7 +191,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(top[edge_case:count = 1], { + CMC_CREATE_TEST(top [edge_case:count = 1], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -217,7 +217,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(contains[edge_case:count = 0], { + CMC_CREATE_TEST(contains [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -262,7 +262,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(full[edge_case:capacity = 1], { + CMC_CREATE_TEST(full [edge_case:capacity = 1], { stack *s = s_new(1); cmc_assert_not_equals(ptr, NULL, s); @@ -287,7 +287,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iter_init[edge_case:count = 0], { + CMC_CREATE_TEST(iter_init [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -304,7 +304,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iter_start[edge_case:count = 0], { + CMC_CREATE_TEST(iter_start [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -318,7 +318,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iter_end[edge_case:count = 0], { + CMC_CREATE_TEST(iter_end [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -389,7 +389,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iteration[edge_case:count = 1], { + CMC_CREATE_TEST(iteration [edge_case:count = 1], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -412,7 +412,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iteration[edge_case:count = capacity], { + CMC_CREATE_TEST(iteration [edge_case:count = capacity], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -476,7 +476,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iteration[boundaries:count = 0], { + CMC_CREATE_TEST(iteration [boundaries:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -495,7 +495,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iteration[boundaries:count > 0], { + CMC_CREATE_TEST(iteration [boundaries:count > 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -517,7 +517,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iter_value[edge_case:count = 0], { + CMC_CREATE_TEST(iter_value [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -531,7 +531,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(iter_rvalue[edge_case:count = 0], { + CMC_CREATE_TEST(iter_rvalue [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -557,12 +557,12 @@ CMC_CREATE_UNIT(stack_test, true, { size_t total = 0; - for (stack_iter it = s->it_start(s); false; ) + for (stack_iter it = s->it_start(s); false;) { cmc_assert_equals(size_t, 0, s_iter_index(&it)); } - for (stack_iter it = s->it_start(s); false; ) + for (stack_iter it = s->it_start(s); false;) { cmc_assert_equals(size_t, s_count(s) - 1, s_iter_index(&it)); } @@ -584,7 +584,7 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - CMC_CREATE_TEST(index[edge_case:count = 0], { + CMC_CREATE_TEST(index [edge_case:count = 0], { stack *s = s_new(100); cmc_assert_not_equals(ptr, NULL, s); @@ -596,5 +596,4 @@ CMC_CREATE_UNIT(stack_test, true, { s_free(s, NULL); }); - }); diff --git a/tests/main/unt/treemap.c b/tests/main/unt/treemap.c index 34e174f..a6c423b 100644 --- a/tests/main/unt/treemap.c +++ b/tests/main/unt/treemap.c @@ -33,5 +33,4 @@ CMC_CREATE_UNIT(treemap_test, true, { tm_free(map, NULL); }); - }); diff --git a/tests/main/unt/treeset.c b/tests/main/unt/treeset.c index f4e32c4..7972d8f 100644 --- a/tests/main/unt/treeset.c +++ b/tests/main/unt/treeset.c @@ -33,5 +33,4 @@ CMC_CREATE_UNIT(treeset_test, true, { ts_free(set, NULL); }); - });