Added deque.h resize and added more deque and hashmap tests

This commit is contained in:
Leonardo committed 2019-10-05 00:31:29 -03:00
1 parent 8483eda701
commit f0b7b125b9
4 files changed
+1005 -468

No files matched your search

+174 -7
View File
@@ -19,13 +19,13 @@ CMC_CREATE_UNIT(deque_test, true, {
d_free(d, NULL);
});
CMC_CREATE_TEST(new [edge_case:capacity = 0], {
CMC_CREATE_TEST(new[edge_case capacity = 0], {
deque *d = d_new(0);
cmc_assert_equals(ptr, NULL, d);
});
CMC_CREATE_TEST(new [edge_case:capacity = UINT64_MAX], {
CMC_CREATE_TEST(new[edge_case capacity = UINT64_MAX], {
deque *d = d_new(UINT64_MAX);
cmc_assert_equals(ptr, NULL, d);
@@ -49,7 +49,7 @@ CMC_CREATE_UNIT(deque_test, true, {
d_free(d, NULL);
});
CMC_CREATE_TEST(buffer_growth [edge_case:capacity = 1], {
CMC_CREATE_TEST(buffer_growth[edge_case capacity = 1], {
deque *d = d_new(1);
cmc_assert_not_equals(ptr, NULL, d);
@@ -63,6 +63,29 @@ CMC_CREATE_UNIT(deque_test, true, {
d_free(d, NULL);
});
CMC_CREATE_TEST(buffer_growth[item_preservation], {
deque *d = d_new(100);
cmc_assert_not_equals(ptr, NULL, d);
for (size_t i = 1; i <= 1200; i++)
{
if (i % 2 == 0)
d_push_front(d, i);
else
d_push_back(d, i);
}
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
sum += d->buffer[i];
cmc_assert_equals(size_t, 1200, d_count(d));
cmc_assert_equals(size_t, 720600, sum);
d_free(d, NULL);
});
CMC_CREATE_TEST(push_front[count capacity], {
deque *d = d_new(100);
@@ -277,7 +300,7 @@ CMC_CREATE_UNIT(deque_test, true, {
d_free(d, NULL);
});
CMC_CREATE_TEST(front [edge_case:count = 0], {
CMC_CREATE_TEST(front[edge_case count = 0], {
deque *d = d_new(100);
cmc_assert_not_equals(ptr, NULL, d);
@@ -318,7 +341,7 @@ CMC_CREATE_UNIT(deque_test, true, {
d_free(d, NULL);
});
CMC_CREATE_TEST(back [edge_case:count = 0], {
CMC_CREATE_TEST(back[edge_case count = 0], {
deque *d = d_new(100);
cmc_assert_not_equals(ptr, NULL, d);
@@ -381,7 +404,7 @@ CMC_CREATE_UNIT(deque_test, true, {
d_free(d, NULL);
});
CMC_CREATE_TEST(contains [edge_case:count = 0], {
CMC_CREATE_TEST(contains[edge_case count = 0], {
deque *d = d_new(100);
cmc_assert_not_equals(ptr, NULL, d);
@@ -391,7 +414,7 @@ CMC_CREATE_UNIT(deque_test, true, {
d_free(d, NULL);
});
CMC_CREATE_TEST(contains [edge_case:count = 1], {
CMC_CREATE_TEST(contains[edge_case count = 1], {
deque *d = d_new(100);
cmc_assert_not_equals(ptr, NULL, d);
@@ -459,8 +482,152 @@ CMC_CREATE_UNIT(deque_test, true, {
CMC_CREATE_TEST(capacity, {
deque *d = d_new(100);
cmc_assert_not_equals(ptr, NULL, d);
cmc_assert_equals(size_t, 100, d_capacity(d));
d_free(d, NULL);
});
CMC_CREATE_TEST(resize, {
deque *d = d_new(100);
cmc_assert_not_equals(ptr, NULL, d);
for (size_t i = 0; i < 150; i++)
{
if (i % 2 == 0)
cmc_assert(d_push_back(d, i));
else
cmc_assert(d_push_front(d, i));
}
cmc_assert_equals(size_t, 150, d_count(d));
cmc_assert(d_resize(d, d_count(d)));
cmc_assert_equals(size_t, 150, d_capacity(d));
d_free(d, NULL);
});
CMC_CREATE_TEST(resize[item_preservation], {
deque *d = d_new(100);
cmc_assert_not_equals(ptr, NULL, d);
for (size_t i = 1; i <= 150; i++)
{
if (i % 2 != 0)
cmc_assert(d_push_back(d, i));
else
cmc_assert(d_push_front(d, i));
}
cmc_assert(d_resize(d, d_count(d)));
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
{
sum += d->buffer[i];
}
cmc_assert_equals(size_t, 11325, sum);
d_free(d, NULL);
});
CMC_CREATE_TEST(copy_of, {
deque *d1 = d_new(100);
cmc_assert_not_equals(ptr, NULL, d1);
for (size_t i = 1; i <= 150; i++)
{
if (i % 2 != 0)
cmc_assert(d_push_back(d1, i));
else
cmc_assert(d_push_front(d1, i));
}
deque *d2 = d_copy_of(d1, NULL);
cmc_assert_not_equals(ptr, NULL, d2);
cmc_assert_equals(size_t, d_count(d1), d_count(d2));
for (size_t i1 = d1->front, i2 = d2->front, j = 0; j < d1->count; j++)
{
cmc_assert_equals(size_t, d1->buffer[i1], d2->buffer[i2]);
i1 = (i1 + 1) % d1->capacity;
i2 = (i2 + 1) % d2->capacity;
}
d_free(d1, NULL);
d_free(d2, NULL);
});
CMC_CREATE_TEST(equals, {
deque *d1 = d_new(100);
deque *d2 = d_new(100);
cmc_assert_not_equals(ptr, NULL, d1);
cmc_assert_not_equals(ptr, NULL, d2);
for (size_t i = 1; i <= 150; i++)
{
if (i % 2 != 0)
{
cmc_assert(d_push_back(d1, i));
cmc_assert(d_push_back(d2, i));
}
else
{
cmc_assert(d_push_front(d1, i));
cmc_assert(d_push_front(d2, i));
}
}
cmc_assert(d_equals(d1, d2, cmp));
d_free(d1, NULL);
d_free(d2, NULL);
});
CMC_CREATE_TEST(equals[from copy], {
deque *d1 = d_new(100);
cmc_assert_not_equals(ptr, NULL, d1);
for (size_t i = 1; i <= 150; i++)
{
if (i % 2 != 0)
cmc_assert(d_push_back(d1, i));
else
cmc_assert(d_push_front(d1, i));
}
deque *d2 = d_copy_of(d1, NULL);
cmc_assert_not_equals(ptr, NULL, d2);
cmc_assert(d_equals(d1, d2, cmp));
d_free(d1, NULL);
d_free(d2, NULL);
});
CMC_CREATE_TEST(equals[edge_case count = 0], {
deque *d1 = d_new(100);
cmc_assert_not_equals(ptr, NULL, d1);
deque *d2 = d_copy_of(d1, NULL);
cmc_assert_not_equals(ptr, NULL, d2);
cmc_assert(d_equals(d1, d2, cmp));
d_free(d1, NULL);
d_free(d2, NULL);
});
});
+313
View File
@@ -47,4 +47,317 @@ CMC_CREATE_UNIT(hashmap_test, true, {
hm_free(map, NULL);
});
CMC_CREATE_TEST(insert, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert(hm_insert(map, 1, 1));
cmc_assert_equals(size_t, 1, hm_count(map));
hm_free(map, NULL);
});
CMC_CREATE_TEST(insert[edge_case smallest capacity], {
hashmap *map = hm_new(1, 0.99, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_equals(size_t, cmc_hashtable_primes[0], hm_capacity(map));
cmc_assert(hm_insert(map, 1, 1));
hm_free(map, NULL);
});
CMC_CREATE_TEST(insert[element position], {
// Using the numhash the key is the hash itself
hashmap *map = hm_new(1, 0.99, cmp, numhash);
cmc_assert_not_equals(ptr, NULL, map);
size_t capacity = hm_capacity(map);
cmc_assert_greater(size_t, 0, capacity);
cmc_assert(hm_insert(map, capacity - 1, capacity - 1));
cmc_assert(hm_insert(map, capacity, capacity));
cmc_assert_equals(size_t, capacity - 1, map->buffer[capacity - 1].key);
cmc_assert_equals(size_t, capacity, map->buffer[0].key);
hm_free(map, NULL);
});
CMC_CREATE_TEST(insert[distance], {
hashmap *map = hm_new(500, 0.6, cmp, hash0);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 0; i < 200; i++)
cmc_assert(hm_insert(map, i, i));
// Everything is hashed to 0 so key 0 should have dist 0, key 1 dist 1, etc
for (size_t i = 0; i < 200; i++)
cmc_assert_equals(size_t, i, map->buffer[i].dist);
hm_free(map, NULL);
});
CMC_CREATE_TEST(insert[distance wrap], {
hashmap *map = hm_new(1, 0.99, cmp, hashcapminus1);
cmc_assert_not_equals(ptr, NULL, map);
size_t capacity = hm_capacity(map);
// Just to be sure, not part of the test
cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1, hashcapminus1(0));
cmc_assert_equals(size_t, capacity - 1, hashcapminus1(0));
cmc_assert(hm_insert(map, 0, 0));
cmc_assert(hm_insert(map, 1, 1));
cmc_assert_equals(size_t, 0, map->buffer[capacity - 1].dist);
cmc_assert_equals(size_t, 1, map->buffer[0].dist);
hm_free(map, NULL);
});
CMC_CREATE_TEST(update, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert(hm_insert(map, 1, 1));
size_t old;
cmc_assert(hm_update(map, 1, 2, &old));
cmc_assert_equals(size_t, 1, old);
hm_free(map, NULL);
});
CMC_CREATE_TEST(update[key not found], {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 0; i < 100; i++)
cmc_assert(hm_insert(map, i, i));
cmc_assert(!hm_update(map, 120, 120, NULL));
hm_free(map, NULL);
});
CMC_CREATE_TEST(remove, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 0; i < 100; i++)
cmc_assert(hm_insert(map, i, i));
for (size_t i = 0; i < 100; i++)
cmc_assert(hm_remove(map, i, NULL));
hm_free(map, NULL);
});
CMC_CREATE_TEST(remove[key not found], {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 0; i < 100; i++)
cmc_assert(hm_insert(map, i, i));
for (size_t i = 100; i < 200; i++)
cmc_assert(!hm_remove(map, i, NULL));
hm_free(map, NULL);
});
CMC_CREATE_TEST(max, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 1; i <= 100; i++)
cmc_assert(hm_insert(map, i, i));
size_t key;
size_t val;
cmc_assert(hm_max(map, &key, &val));
cmc_assert_equals(size_t, 100, key);
cmc_assert_equals(size_t, 100, val);
hm_free(map, NULL);
});
CMC_CREATE_TEST(max[edge_case count = 0], {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert(!hm_min(map, NULL, NULL));
hm_free(map, NULL);
});
CMC_CREATE_TEST(min, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 1; i <= 100; i++)
cmc_assert(hm_insert(map, i, i));
size_t key;
size_t val;
cmc_assert(hm_min(map, &key, &val));
cmc_assert_equals(size_t, 1, key);
cmc_assert_equals(size_t, 1, val);
hm_free(map, NULL);
});
CMC_CREATE_TEST(min[edge_case count = 0], {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert(!hm_min(map, NULL, NULL));
hm_free(map, NULL);
});
CMC_CREATE_TEST(get, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert(hm_insert(map, 4321, 1234));
cmc_assert_equals(size_t, 1234, hm_get(map, 4321));
hm_free(map, NULL);
});
CMC_CREATE_TEST(get[edge_case count = 0], {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_equals(size_t, hm_impl_default_value(), hm_get(map, 4321));
hm_free(map, NULL);
});
CMC_CREATE_TEST(get_ref, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert(hm_insert(map, 4321, 1234));
size_t *result = hm_get_ref(map, 4321);
cmc_assert_not_equals(ptr, NULL, result);
cmc_assert_equals(size_t, 1234, *result);
hm_free(map, NULL);
});
CMC_CREATE_TEST(get_ref[edge_case count = 0], {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_equals(ptr, NULL, hm_get_ref(map, 4321));
hm_free(map, NULL);
});
CMC_CREATE_TEST(contains, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert(hm_insert(map, 987654321, 1));
cmc_assert(hm_contains(map, 987654321));
hm_free(map, NULL);
});
CMC_CREATE_TEST(contains[sum], {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 101; i <= 200; i++)
cmc_assert(hm_insert(map, i, i));
size_t sum = 0;
for (size_t i = 1; i <= 300; i++)
if (hm_contains(map, i))
sum += i;
cmc_assert_equals(size_t, 15050, sum);
hm_free(map, NULL);
});
CMC_CREATE_TEST(empty, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert(hm_empty(map));
cmc_assert(hm_insert(map, 1, 1));
cmc_assert(!hm_empty(map));
hm_free(map, NULL);
});
CMC_CREATE_TEST(count, {
hashmap *map = hm_new(100, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 1; i < 100; i++)
{
cmc_assert(hm_insert(map, i, i));
cmc_assert_equals(size_t, i, hm_count(map));
}
hm_free(map, NULL);
});
CMC_CREATE_TEST(capacity, {
hashmap *map = hm_new(2500, 0.6, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6), hm_capacity(map));
hm_free(map, NULL);
});
CMC_CREATE_TEST(capacity[small], {
hashmap *map = hm_new(1, 0.99, cmp, hash);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_equals(size_t, cmc_hashtable_primes[0], hm_capacity(map));
hm_free(map, NULL);
});
});
+21
View File
@@ -3,6 +3,7 @@
#define CMC_UNIT_TEST_UTL__
#include <stdlib.h>
#include <cmc/hashmap.h>
int cmp(size_t a, size_t b)
{
@@ -20,4 +21,24 @@ size_t hash(size_t a)
return a;
}
size_t numhash(size_t a)
{
return a;
}
size_t hashcapminus1(size_t a)
{
return cmc_hashtable_primes[0] - 1;
}
size_t hash0(size_t a)
{
return 0;
}
size_t hashmod40(size_t a)
{
return a % 40;
}
#endif /* CMC_UNIT_TEST_UTL__ */