Adding functions table to heap and intervalheap

This commit is contained in:
Leonardo committed 2020-02-06 16:12:06 -03:00
1 parent fa49fddd8f
commit 64c3a99922
4 files changed
+345 -261

No files matched your search

+30 -23
View File
@@ -7,32 +7,39 @@
CMC_GENERATE_HEAP(h, heap, size_t)
struct heap_ftab_val *h_ftab_val = &(struct heap_ftab_val){ .cmp = cmp,
.cpy = copy,
.str = str,
.free = custom_free,
.hash = hash,
.pri = pri };
CMC_CREATE_UNIT(heap_test, true, {
CMC_CREATE_TEST(new, {
struct heap *h = h_new(1000000, cmc_max_heap, cmp);
struct heap *h = h_new(1000000, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
cmc_assert_not_equals(ptr, NULL, h->buffer);
cmc_assert_equals(size_t, 1000000, h_capacity(h));
cmc_assert_equals(size_t, 0, h_count(h));
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(new[edge_case capacity = 0], {
struct heap *h = h_new(0, cmc_max_heap, cmp);
struct heap *h = h_new(0, cmc_max_heap, h_ftab_val);
cmc_assert_equals(ptr, NULL, h);
});
CMC_CREATE_TEST(new[edge_case capacity = UINT64_MAX], {
struct heap *h = h_new(UINT64_MAX, cmc_max_heap, cmp);
struct heap *h = h_new(UINT64_MAX, cmc_max_heap, h_ftab_val);
cmc_assert_equals(ptr, NULL, h);
});
CMC_CREATE_TEST(clear[count capacity], {
struct heap *h = h_new(100, cmc_max_heap, cmp);
struct heap *h = h_new(100, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
@@ -41,16 +48,16 @@ CMC_CREATE_UNIT(heap_test, true, {
cmc_assert_equals(size_t, 50, h_count(h));
h_clear(h, NULL);
h_clear(h);
cmc_assert_equals(size_t, 0, h_count(h));
cmc_assert_equals(size_t, 100, h_capacity(h));
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(buffer_growth[edge_case capacity = 1], {
struct heap *h = h_new(1, cmc_max_heap, cmp);
struct heap *h = h_new(1, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
@@ -59,21 +66,21 @@ CMC_CREATE_UNIT(heap_test, true, {
cmc_assert_equals(size_t, 50, h_count(h));
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(insert, {
struct heap *h = h_new(100, cmc_max_heap, cmp);
struct heap *h = h_new(100, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
cmc_assert(h_insert(h, 10));
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(insert[item preservation], {
struct heap *h = h_new(50, cmc_max_heap, cmp);
struct heap *h = h_new(50, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
@@ -86,11 +93,11 @@ CMC_CREATE_UNIT(heap_test, true, {
cmc_assert_equals(size_t, 5050, sum);
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(remove, {
struct heap *h = h_new(50, cmc_max_heap, cmp);
struct heap *h = h_new(50, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
@@ -102,22 +109,22 @@ CMC_CREATE_UNIT(heap_test, true, {
cmc_assert_equals(size_t, 100, r);
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(remove[count = 0], {
struct heap *h = h_new(100, cmc_max_heap, cmp);
struct heap *h = h_new(100, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
size_t _;
cmc_assert(!h_remove(h, &_));
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(remove[sorted], {
struct heap *h = h_new(100, cmc_min_heap, cmp);
struct heap *h = h_new(100, cmc_min_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
@@ -136,11 +143,11 @@ CMC_CREATE_UNIT(heap_test, true, {
cmc_assert_array_sorted_any(size_t, arr, cmp, 0, 99);
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(peek, {
struct heap *h = h_new(100, cmc_max_heap, cmp);
struct heap *h = h_new(100, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
@@ -149,16 +156,16 @@ CMC_CREATE_UNIT(heap_test, true, {
cmc_assert_equals(size_t, 100, h_peek(h));
h_free(h, NULL);
h_free(h);
});
CMC_CREATE_TEST(peek[count = 0], {
struct heap *h = h_new(100, cmc_max_heap, cmp);
struct heap *h = h_new(100, cmc_max_heap, h_ftab_val);
cmc_assert_not_equals(ptr, NULL, h);
cmc_assert_equals(size_t, (size_t){ 0 }, h_peek(h));
h_free(h, NULL);
h_free(h);
});
});
+31 -23
View File
@@ -7,68 +7,76 @@
CMC_GENERATE_INTERVALHEAP(ih, intervalheap, size_t)
struct intervalheap_ftab_val *ih_ftab_val =
&(struct intervalheap_ftab_val){ .cmp = cmp,
.cpy = copy,
.str = str,
.free = custom_free,
.hash = hash,
.pri = pri };
CMC_CREATE_UNIT(intervalheap_test, true, {
CMC_CREATE_TEST(new, {
struct intervalheap *ih = ih_new(1000000, cmp);
struct intervalheap *ih = ih_new(1000000, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
cmc_assert_not_equals(ptr, NULL, ih->buffer);
cmc_assert_equals(size_t, 500000, ih_capacity(ih));
cmc_assert_equals(size_t, 0, ih_count(ih));
ih_free(ih, NULL);
ih_free(ih);
});
CMC_CREATE_TEST(new[capacity = 0], {
struct intervalheap *ih = ih_new(0, cmp);
struct intervalheap *ih = ih_new(0, ih_ftab_val);
cmc_assert_equals(ptr, NULL, ih);
if (ih)
ih_free(ih, NULL);
ih_free(ih);
});
CMC_CREATE_TEST(new[capacity], {
struct intervalheap *ih = ih_new(1, cmp);
struct intervalheap *ih = ih_new(1, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
cmc_assert_equals(size_t, 1, ih_capacity(ih));
ih_free(ih, NULL);
ih_free(ih);
ih = ih_new(2, cmp);
ih = ih_new(2, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
cmc_assert_equals(size_t, 1, ih_capacity(ih));
ih_free(ih, NULL);
ih_free(ih);
ih = ih_new(3, cmp);
ih = ih_new(3, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
cmc_assert_equals(size_t, 2, ih_capacity(ih));
ih_free(ih, NULL);
ih_free(ih);
ih = ih_new(4, cmp);
ih = ih_new(4, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
cmc_assert_equals(size_t, 2, ih_capacity(ih));
ih_free(ih, NULL);
ih_free(ih);
});
CMC_CREATE_TEST(new[capacity = UINT64_MAX], {
struct intervalheap *ih = ih_new(UINT64_MAX, cmp);
struct intervalheap *ih = ih_new(UINT64_MAX, ih_ftab_val);
cmc_assert_equals(ptr, NULL, ih);
if (ih)
ih_free(ih, NULL);
ih_free(ih);
});
CMC_CREATE_TEST(clear[count capacity], {
struct intervalheap *ih = ih_new(100, cmp);
struct intervalheap *ih = ih_new(100, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
@@ -77,16 +85,16 @@ CMC_CREATE_UNIT(intervalheap_test, true, {
cmc_assert_equals(size_t, 50, ih_count(ih));
ih_clear(ih, NULL);
ih_clear(ih);
cmc_assert_equals(size_t, 0, ih_count(ih));
cmc_assert_equals(size_t, 50, ih_capacity(ih));
ih_free(ih, NULL);
ih_free(ih);
});
CMC_CREATE_TEST(buffer_growth[capacity = 1], {
struct intervalheap *ih = ih_new(1, cmp);
struct intervalheap *ih = ih_new(1, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
@@ -96,11 +104,11 @@ CMC_CREATE_UNIT(intervalheap_test, true, {
cmc_assert_equals(size_t, 50, ih_count(ih));
cmc_assert_greater(size_t, 50, ih_capacity(ih) * 2);
ih_free(ih, NULL);
ih_free(ih);
});
CMC_CREATE_TEST(insert[count], {
struct intervalheap *ih = ih_new(100, cmp);
struct intervalheap *ih = ih_new(100, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
@@ -109,11 +117,11 @@ CMC_CREATE_UNIT(intervalheap_test, true, {
cmc_assert_equals(size_t, 2, ih_count(ih));
ih_free(ih, NULL);
ih_free(ih);
});
CMC_CREATE_TEST(insert[max min], {
struct intervalheap *ih = ih_new(1, cmp);
struct intervalheap *ih = ih_new(1, ih_ftab_val);
cmc_assert_not_equals(ptr, NULL, ih);
@@ -129,6 +137,6 @@ CMC_CREATE_UNIT(intervalheap_test, true, {
cmc_assert_equals(size_t, 0, min);
cmc_assert_equals(size_t, 1, max);
ih_free(ih, NULL);
ih_free(ih);
});
});