Added callbacks to heap and intervaheap, and reworked some min and max functions
This commit is contained in:
1 parent
cf5ceb9c48
commit
5b8f64b421
11 files changed
+237
-130
No files matched your search
@@ -512,7 +512,8 @@ CMC_CREATE_UNIT(bidimap_test, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct bidimap *map = bm_new_custom(100, 0.7, bm_ftab_key, bm_ftab_val, NULL, callbacks);
|
||||
struct bidimap *map =
|
||||
bm_new_custom(100, 0.7, bm_ftab_key, bm_ftab_val, NULL, callbacks);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
cmc_assert_equals(ptr, callbacks, map->callbacks);
|
||||
|
||||
@@ -660,8 +660,7 @@ CMC_CREATE_UNIT(deque_test, true, {
|
||||
|
||||
// customize
|
||||
d->flag = cmc_flags.ERROR;
|
||||
d_customize(d, &cmc_alloc_node_default,
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
d_customize(d, &cmc_alloc_node_default, &(struct cmc_callbacks){ 0 });
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, d_flag(d));
|
||||
|
||||
// push_front
|
||||
|
||||
@@ -604,7 +604,8 @@ CMC_CREATE_UNIT(hashmap_test, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct hashmap *map = hm_new_custom(100, 0.6, hm_ftab_key, hm_ftab_val, NULL, callbacks);
|
||||
struct hashmap *map =
|
||||
hm_new_custom(100, 0.6, hm_ftab_key, hm_ftab_val, NULL, callbacks);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
|
||||
@@ -458,7 +458,7 @@ CMC_CREATE_UNIT(hashset_test, true, {
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hs_flag(set));
|
||||
|
||||
// copy
|
||||
set->flag = cmc_flags.ERROR;\
|
||||
set->flag = cmc_flags.ERROR;
|
||||
struct hashset *set2 = hs_copy_of(set);
|
||||
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, hs_flag(set));
|
||||
@@ -483,7 +483,8 @@ CMC_CREATE_UNIT(hashset_test, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct hashset *set = hs_new_custom(100, 0.6, hs_ftab_val, NULL, callbacks);
|
||||
struct hashset *set =
|
||||
hs_new_custom(100, 0.6, hs_ftab_val, NULL, callbacks);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
|
||||
+43
-12
@@ -104,10 +104,9 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
for (size_t i = 0; i <= 100; i++)
|
||||
cmc_assert(h_insert(h, i));
|
||||
|
||||
size_t r;
|
||||
cmc_assert(h_remove(h, &r));
|
||||
|
||||
cmc_assert_equals(size_t, 100, r);
|
||||
cmc_assert_equals(size_t, 100, h_peek(h));
|
||||
cmc_assert(h_remove(h));
|
||||
cmc_assert_equals(size_t, 99, h_peek(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
@@ -117,8 +116,7 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
size_t _;
|
||||
cmc_assert(!h_remove(h, &_));
|
||||
cmc_assert(!h_remove(h));
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
@@ -136,9 +134,8 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
size_t arr[100];
|
||||
while (!h_empty(h))
|
||||
{
|
||||
cmc_assert(h_remove(h, &r));
|
||||
|
||||
arr[i++] = r;
|
||||
arr[i++] = h_peek(h);
|
||||
cmc_assert(h_remove(h));
|
||||
}
|
||||
|
||||
cmc_assert_array_sorted_any(size_t, arr, cmp, 0, 99);
|
||||
@@ -184,17 +181,17 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
h->flag = cmc_flags.ERROR;
|
||||
|
||||
// remove
|
||||
cmc_assert(h_remove(h, NULL));
|
||||
cmc_assert(h_remove(h));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, h_flag(h));
|
||||
|
||||
cmc_assert(!h_remove(h, NULL));
|
||||
cmc_assert(!h_remove(h));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, h_flag(h));
|
||||
|
||||
// peek
|
||||
cmc_assert(h_insert(h, 10));
|
||||
h_peek(h);
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, h_flag(h));
|
||||
cmc_assert(h_remove(h, NULL));
|
||||
cmc_assert(h_remove(h));
|
||||
h_peek(h);
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, h_flag(h));
|
||||
|
||||
@@ -226,4 +223,38 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
h_free(h);
|
||||
h_free(h2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct heap *h =
|
||||
h_new_custom(100, cmc_max_heap, h_ftab_val, NULL, callbacks);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
|
||||
cmc_assert(h_insert(h, 10));
|
||||
cmc_assert_equals(int32_t, 1, total_create);
|
||||
|
||||
cmc_assert(h_remove(h));
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
|
||||
cmc_assert(h_insert(h, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
|
||||
cmc_assert_equals(size_t, 10, h_peek(h));
|
||||
cmc_assert_equals(int32_t, 1, total_read);
|
||||
|
||||
cmc_assert(h_contains(h, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
|
||||
cmc_assert(h_resize(h, 1000));
|
||||
cmc_assert_equals(int32_t, 1, total_resize);
|
||||
|
||||
cmc_assert(h_resize(h, 100));
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
h_free(h);
|
||||
});
|
||||
});
|
||||
@@ -127,10 +127,7 @@ CMC_CREATE_UNIT(intervalheap_test, true, {
|
||||
cmc_assert(ih_insert(ih, 1));
|
||||
cmc_assert(ih_insert(ih, 2));
|
||||
|
||||
size_t r;
|
||||
cmc_assert(ih_max(ih, &r));
|
||||
|
||||
cmc_assert_equals(size_t, 2, r);
|
||||
cmc_assert_equals(size_t, 2, ih_max(ih));
|
||||
|
||||
ih_free(ih);
|
||||
});
|
||||
@@ -142,10 +139,7 @@ CMC_CREATE_UNIT(intervalheap_test, true, {
|
||||
cmc_assert(ih_insert(ih, 1));
|
||||
cmc_assert(ih_insert(ih, 2));
|
||||
|
||||
size_t r;
|
||||
cmc_assert(ih_min(ih, &r));
|
||||
|
||||
cmc_assert_equals(size_t, 0, r);
|
||||
cmc_assert_equals(size_t, 0, ih_min(ih));
|
||||
|
||||
ih_free(ih);
|
||||
});
|
||||
@@ -228,22 +222,21 @@ CMC_CREATE_UNIT(intervalheap_test, true, {
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, ih_flag(ih));
|
||||
|
||||
// max min
|
||||
size_t r;
|
||||
ih->flag = cmc_flags.ERROR;
|
||||
cmc_assert(!ih_max(ih, &r));
|
||||
cmc_assert_equals(size_t, (size_t){ 0 }, ih_max(ih));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, ih_flag(ih));
|
||||
|
||||
ih->flag = cmc_flags.ERROR;
|
||||
cmc_assert(!ih_min(ih, &r));
|
||||
cmc_assert_equals(size_t, (size_t){ 0 }, ih_min(ih));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, ih_flag(ih));
|
||||
|
||||
cmc_assert(ih_insert(ih, 10));
|
||||
ih->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ih_max(ih, &r));
|
||||
cmc_assert_equals(size_t, 10, ih_max(ih));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ih_flag(ih));
|
||||
|
||||
ih->flag = cmc_flags.ERROR;
|
||||
cmc_assert(ih_max(ih, &r));
|
||||
cmc_assert_equals(size_t, 10, ih_min(ih));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, ih_flag(ih));
|
||||
|
||||
// contains
|
||||
@@ -267,4 +260,54 @@ CMC_CREATE_UNIT(intervalheap_test, true, {
|
||||
ih_free(ih);
|
||||
ih_free(ih2);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(callbacks, {
|
||||
struct intervalheap *ih =
|
||||
ih_new_custom(100, ih_ftab_val, NULL, callbacks);
|
||||
|
||||
total_create = 0;
|
||||
total_read = 0;
|
||||
total_update = 0;
|
||||
total_delete = 0;
|
||||
total_resize = 0;
|
||||
|
||||
cmc_assert(ih_insert(ih, 10));
|
||||
cmc_assert_equals(int32_t, 1, total_create);
|
||||
|
||||
cmc_assert(ih_remove_max(ih));
|
||||
cmc_assert_equals(int32_t, 1, total_delete);
|
||||
|
||||
cmc_assert(ih_insert(ih, 10));
|
||||
cmc_assert_equals(int32_t, 2, total_create);
|
||||
|
||||
cmc_assert(ih_remove_min(ih));
|
||||
cmc_assert_equals(int32_t, 2, total_delete);
|
||||
|
||||
cmc_assert(ih_insert(ih, 1));
|
||||
cmc_assert(ih_insert(ih, 2));
|
||||
cmc_assert_equals(int32_t, 4, total_create);
|
||||
|
||||
cmc_assert(ih_update_max(ih, 1));
|
||||
cmc_assert_equals(int32_t, 1, total_update);
|
||||
|
||||
cmc_assert(ih_update_min(ih, 2));
|
||||
cmc_assert_equals(int32_t, 2, total_update);
|
||||
|
||||
cmc_assert_equals(size_t, 2, ih_max(ih));
|
||||
cmc_assert_equals(int32_t, 1, total_read);
|
||||
|
||||
cmc_assert_equals(size_t, 1, ih_min(ih));
|
||||
cmc_assert_equals(int32_t, 2, total_read);
|
||||
|
||||
cmc_assert(ih_contains(ih, 1));
|
||||
cmc_assert_equals(int32_t, 3, total_read);
|
||||
|
||||
cmc_assert(ih_resize(ih, 1000));
|
||||
cmc_assert_equals(int32_t, 1, total_resize);
|
||||
|
||||
cmc_assert(ih_resize(ih, 50));
|
||||
cmc_assert_equals(int32_t, 2, total_resize);
|
||||
|
||||
ih_free(ih);
|
||||
});
|
||||
});
|
||||
@@ -179,18 +179,18 @@ CMC_CREATE_UNIT(sortedlist_test, true, {
|
||||
|
||||
// max min
|
||||
sl->flag = cmc_flags.ERROR;
|
||||
cmc_assert(!sl_max(sl, NULL));
|
||||
cmc_assert_equals(size_t, (size_t){ 0 }, sl_max(sl));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, sl_flag(sl));
|
||||
sl->flag = cmc_flags.ERROR;
|
||||
cmc_assert(!sl_min(sl, NULL));
|
||||
cmc_assert_equals(size_t, (size_t){ 0 }, sl_min(sl));
|
||||
cmc_assert_equals(int32_t, cmc_flags.EMPTY, sl_flag(sl));
|
||||
|
||||
cmc_assert(sl_insert(sl, 1));
|
||||
sl->flag = cmc_flags.ERROR;
|
||||
cmc_assert(sl_max(sl, NULL));
|
||||
cmc_assert_equals(size_t, 1, sl_max(sl));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, sl_flag(sl));
|
||||
sl->flag = cmc_flags.ERROR;
|
||||
cmc_assert(sl_min(sl, NULL));
|
||||
cmc_assert_equals(size_t, 1, sl_min(sl));
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, sl_flag(sl));
|
||||
|
||||
// get
|
||||
|
||||
@@ -98,12 +98,11 @@ void callback_resize(void)
|
||||
total_resize++;
|
||||
}
|
||||
|
||||
struct cmc_callbacks *callbacks = &(struct cmc_callbacks) {
|
||||
.create = callback_create,
|
||||
.read = callback_read,
|
||||
.update = callback_update,
|
||||
.delete = callback_delete,
|
||||
.resize = callback_resize
|
||||
};
|
||||
struct cmc_callbacks *callbacks =
|
||||
&(struct cmc_callbacks){ .create = callback_create,
|
||||
.read = callback_read,
|
||||
.update = callback_update,
|
||||
.delete = callback_delete,
|
||||
.resize = callback_resize };
|
||||
|
||||
#endif /* CMC_UNIT_TEST_UTL__ */
|
||||
Reference in new issue
Block a user