Added resize function to hashmap and hashset
This commit is contained in:
1 parent
f0b7b125b9
commit
9f8671f75f
5 files changed
+1893
-1464
No files matched your search
+709
-674
File diff suppressed because it is too large.
Load diff
+816
-788
File diff suppressed because it is too large.
Load diff
+2
-2
@@ -9,8 +9,8 @@ main: FORCE
|
||||
./main
|
||||
|
||||
dev: FORCE
|
||||
$(CC) main.c -o main $(CFLAGS) -I $(INCLUDE)
|
||||
./main
|
||||
$(CC) main.c -o main.exe $(CFLAGS) -I $(INCLUDE)
|
||||
./main.exe
|
||||
rm *.exe
|
||||
|
||||
windev: FORCE
|
||||
|
||||
@@ -124,6 +124,30 @@ CMC_CREATE_UNIT(hashmap_test, true, {
|
||||
hm_free(map, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[buffer growth and item preservation], {
|
||||
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));
|
||||
|
||||
size_t p = 0;
|
||||
for (size_t i = 0; i < 100000; i++)
|
||||
{
|
||||
if (hm_full(map))
|
||||
p++;
|
||||
|
||||
cmc_assert(hm_insert(map, i, i));
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[p], hm_capacity(map));
|
||||
|
||||
for (size_t i = 0; i < 100000; i++)
|
||||
cmc_assert(hm_contains(map, i));
|
||||
|
||||
hm_free(map, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(update, {
|
||||
hashmap *map = hm_new(100, 0.6, cmp, hash);
|
||||
|
||||
@@ -166,6 +190,17 @@ CMC_CREATE_UNIT(hashmap_test, true, {
|
||||
hm_free(map, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove[edge_case count = 0], {
|
||||
hashmap *map = hm_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
for (size_t i = 100; i < 200; 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);
|
||||
|
||||
@@ -327,6 +362,27 @@ CMC_CREATE_UNIT(hashmap_test, true, {
|
||||
hm_free(map, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(full, {
|
||||
hashmap *map = hm_new(cmc_hashtable_primes[0], 0.99999, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[0], hm_capacity(map));
|
||||
|
||||
for (size_t i = 0; i < hm_capacity(map); i++)
|
||||
cmc_assert(hm_insert(map, i, i));
|
||||
|
||||
cmc_assert(hm_full(map));
|
||||
|
||||
cmc_assert(hm_insert(map, 10000, 10000));
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[1], hm_capacity(map));
|
||||
|
||||
cmc_assert(!hm_full(map));
|
||||
|
||||
hm_free(map, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(count, {
|
||||
hashmap *map = hm_new(100, 0.6, cmp, hash);
|
||||
|
||||
@@ -360,4 +416,14 @@ CMC_CREATE_UNIT(hashmap_test, true, {
|
||||
|
||||
hm_free(map, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(load, {
|
||||
hashmap *map = hm_new(1, 0.99, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
cmc_assert_in_range(double, 0.99 - 0.0001, 0.99 + 0.0001, hm_load(map));
|
||||
|
||||
hm_free(map, NULL);
|
||||
});
|
||||
});
|
||||
@@ -47,4 +47,304 @@ CMC_CREATE_UNIT(hashset_test, true, {
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert, {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(hs_insert(set, 1));
|
||||
cmc_assert_equals(size_t, 1, hs_count(set));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[edge_case smallest capacity], {
|
||||
hashset *set = hs_new(1, 0.99, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[0], hs_capacity(set));
|
||||
cmc_assert(hs_insert(set, 1));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[element position], {
|
||||
// Using the numhash the key is the hash itself
|
||||
hashset *set = hs_new(1, 0.99, cmp, numhash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
size_t capacity = hs_capacity(set);
|
||||
|
||||
cmc_assert_greater(size_t, 0, capacity);
|
||||
|
||||
cmc_assert(hs_insert(set, capacity - 1));
|
||||
cmc_assert(hs_insert(set, capacity));
|
||||
|
||||
cmc_assert_equals(size_t, capacity - 1, set->buffer[capacity - 1].value);
|
||||
cmc_assert_equals(size_t, capacity, set->buffer[0].value);
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[distance], {
|
||||
hashset *set = hs_new(500, 0.6, cmp, hash0);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 0; i < 200; i++)
|
||||
cmc_assert(hs_insert(set, 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, set->buffer[i].dist);
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[distance wrap], {
|
||||
hashset *set = hs_new(1, 0.99, cmp, hashcapminus1);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
size_t capacity = hs_capacity(set);
|
||||
|
||||
// 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(hs_insert(set, 0));
|
||||
cmc_assert(hs_insert(set, 1));
|
||||
|
||||
cmc_assert_equals(size_t, 0, set->buffer[capacity - 1].dist);
|
||||
cmc_assert_equals(size_t, 1, set->buffer[0].dist);
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(insert[buffer growth and item preservation], {
|
||||
hashset *set = hs_new(1, 0.99, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[0], hs_capacity(set));
|
||||
|
||||
size_t p = 0;
|
||||
for (size_t i = 0; i < 100000; i++)
|
||||
{
|
||||
if (hs_full(set))
|
||||
p++;
|
||||
|
||||
cmc_assert(hs_insert(set, i));
|
||||
}
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[p], hs_capacity(set));
|
||||
|
||||
for (size_t i = 0; i < 100000; i++)
|
||||
cmc_assert(hs_contains(set, i));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove, {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
cmc_assert(hs_insert(set, i));
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
cmc_assert(hs_remove(set, i));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove[edge_case count = 0], {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 100; i < 200; i++)
|
||||
cmc_assert(!hs_remove(set, i));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(remove[key not found], {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 0; i < 100; i++)
|
||||
cmc_assert(hs_insert(set, i));
|
||||
|
||||
for (size_t i = 100; i < 200; i++)
|
||||
cmc_assert(!hs_remove(set, i));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(max, {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert(hs_insert(set, i));
|
||||
|
||||
size_t val;
|
||||
cmc_assert(hs_max(set, &val));
|
||||
|
||||
cmc_assert_equals(size_t, 100, val);
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(max[edge_case count = 0], {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(!hs_min(set, NULL));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(min, {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 1; i <= 100; i++)
|
||||
cmc_assert(hs_insert(set, i));
|
||||
|
||||
size_t val;
|
||||
cmc_assert(hs_min(set, &val));
|
||||
|
||||
cmc_assert_equals(size_t, 1, val);
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(min[edge_case count = 0], {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(!hs_min(set, NULL));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(contains, {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(hs_insert(set, 987654321));
|
||||
|
||||
cmc_assert(hs_contains(set, 987654321));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(contains[sum], {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 101; i <= 200; i++)
|
||||
cmc_assert(hs_insert(set, i));
|
||||
|
||||
size_t sum = 0;
|
||||
for (size_t i = 1; i <= 300; i++)
|
||||
if (hs_contains(set, i))
|
||||
sum += i;
|
||||
|
||||
cmc_assert_equals(size_t, 15050, sum);
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(empty, {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert(hs_empty(set));
|
||||
|
||||
cmc_assert(hs_insert(set, 1));
|
||||
|
||||
cmc_assert(!hs_empty(set));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(full, {
|
||||
hashset *set = hs_new(cmc_hashtable_primes[0], 0.99999, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[0], hs_capacity(set));
|
||||
|
||||
for (size_t i = 0; i < hs_capacity(set); i++)
|
||||
cmc_assert(hs_insert(set, i));
|
||||
|
||||
cmc_assert(hs_full(set));
|
||||
|
||||
cmc_assert(hs_insert(set, 10000));
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[1], hs_capacity(set));
|
||||
|
||||
cmc_assert(!hs_full(set));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(count, {
|
||||
hashset *set = hs_new(100, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
for (size_t i = 1; i < 100; i++)
|
||||
{
|
||||
cmc_assert(hs_insert(set, i));
|
||||
cmc_assert_equals(size_t, i, hs_count(set));
|
||||
}
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(capacity, {
|
||||
hashset *set = hs_new(2500, 0.6, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6), hs_capacity(set));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(capacity[small], {
|
||||
hashset *set = hs_new(1, 0.99, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert_equals(size_t, cmc_hashtable_primes[0], hs_capacity(set));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(load, {
|
||||
hashset *set = hs_new(1, 0.99, cmp, hash);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, set);
|
||||
|
||||
cmc_assert_in_range(double, 0.99 - 0.0001, 0.99 + 0.0001, hs_load(set));
|
||||
|
||||
hs_free(set, NULL);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user