Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
kg committed Apr 8, 2024
1 parent f8cc40c commit 8d8d34f
Showing 1 changed file with 89 additions and 89 deletions.
178 changes: 89 additions & 89 deletions src/native/containers/dn-simdhash-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ key_comparer (instance_data_t data, size_t lhs, size_t rhs) {
#define DN_SIMDHASH_KEY_HASHER(data, key) (uint32_t)(key & 0xFFFFFFFFu)
#define DN_SIMDHASH_KEY_EQUALS key_comparer
#define DN_SIMDHASH_INSTANCE_DATA_T instance_data_t
#define DN_SIMDHASH_ON_REMOVE(data, key, value) ; // printf("remove [%zd, %zd], f==%f\n", key, value, data.f)
#define DN_SIMDHASH_ON_REPLACE(data, key, old_value, new_value) ; // printf("replace [%zd, %zd] with [%zd, %zd] i==%i\n", key, old_value, key, new_value, data.i)
#define DN_SIMDHASH_ON_REMOVE(data, key, value) // printf("remove [%zd, %zd], f==%f\n", key, value, data.f)
#define DN_SIMDHASH_ON_REPLACE(data, old_key, new_key, old_value, new_value) // printf("replace [%zd, %zd] with [%zd, %zd] i==%i\n", key, old_value, key, new_value, data.i)

#include "dn-simdhash-specialization.h"

Expand Down Expand Up @@ -79,7 +79,7 @@ int main () {

for (int i = 0; i < c; i++) {
DN_SIMDHASH_VALUE_T value = (i * 2) + 1;
DN_SIMDHASH_KEY_T key;
DN_SIMDHASH_KEY_T key;

retry: {
key = rand();
Expand All @@ -92,92 +92,92 @@ retry: {
dn_vector_push_back(values, value);
}

for (int iter = 0; iter < 100; iter++) {
if (!tasserteq(dn_simdhash_count(test), c, "count did not match"))
return 1;

printf("Calling foreach:\n");
uint32_t foreach_count = 0;
dn_simdhash_size_t_size_t_foreach(test, foreach_callback, &foreach_count);
printf("Foreach iterated %u time(s)\n", foreach_count);
printf("Count: %u, Capacity: %u, Cascaded item count: %u\n", dn_simdhash_count(test), dn_simdhash_capacity(test), count_cascaded_buckets(test));

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value, expected_value = *dn_vector_index_t(values, DN_SIMDHASH_VALUE_T, i);

uint8_t ok = dn_simdhash_size_t_size_t_try_get_value(test, key, &value);
if (tassert1(ok, key, "did not find key"))
tasserteq(value, expected_value, "value did not match");
}

// NOTE: Adding duplicates could grow the table if we're unlucky, since the add operation
// eagerly grows before doing a table scan if we're at the grow threshold.
for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value = *dn_vector_index_t(values, DN_SIMDHASH_VALUE_T, i);

uint8_t ok = dn_simdhash_size_t_size_t_try_add(test, key, value);
tassert1(!ok, key, "added duplicate key successfully");
}

printf("After adding dupes: Count: %u, Capacity: %u, Cascaded item count: %u\n", dn_simdhash_count(test), dn_simdhash_capacity(test), count_cascaded_buckets(test));
uint32_t final_capacity = dn_simdhash_capacity(test);

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
uint8_t ok = dn_simdhash_size_t_size_t_try_remove(test, key);
tassert1(ok, key, "could not remove key");

DN_SIMDHASH_VALUE_T value;
ok = dn_simdhash_size_t_size_t_try_get_value(test, key, &value);
tassert1(!ok, key, "found key after removal");
}

if (!tasserteq(dn_simdhash_count(test), 0, "was not empty"))
return 1;
if (!tasserteq(dn_simdhash_capacity(test), final_capacity, "capacity changed by emptying"))
return 1;

printf ("Calling foreach after emptying:\n");
foreach_count = 0;
dn_simdhash_size_t_size_t_foreach(test, foreach_callback, &foreach_count);
printf("Foreach iterated %u time(s)\n", foreach_count);
printf("Count: %u, Capacity: %u, Cascaded item count: %u\n", dn_simdhash_count(test), dn_simdhash_capacity(test), count_cascaded_buckets(test));

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value;
uint8_t ok = dn_simdhash_size_t_size_t_try_get_value(test, key, &value);
tassert1(!ok, key, "found key after removal");
}

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value = *dn_vector_index_t(values, DN_SIMDHASH_VALUE_T, i);

uint8_t ok = dn_simdhash_size_t_size_t_try_add(test, key, value);
tassert1(ok, key, "could not re-insert key after emptying");
}

if (!tasserteq(dn_simdhash_capacity(test), final_capacity, "expected capacity not to change after refilling"))
return 1;

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value, expected_value = *dn_vector_index_t(values, DN_SIMDHASH_VALUE_T, i);

uint8_t ok = dn_simdhash_size_t_size_t_try_get_value(test, key, &value);
if (tassert1(ok, key, "did not find key after refilling"))
tasserteq(value, expected_value, "value did not match after refilling");
}

printf("Calling foreach after refilling:\n");
foreach_count = 0;
dn_simdhash_size_t_size_t_foreach(test, foreach_callback, &foreach_count);
printf("Foreach iterated %u time(s)\n", foreach_count);
printf("Count: %u, Capacity: %u, Cascaded item count: %u\n", dn_simdhash_count(test), dn_simdhash_capacity(test), count_cascaded_buckets(test));
}
for (int iter = 0; iter < 100; iter++) {
if (!tasserteq(dn_simdhash_count(test), c, "count did not match"))
return 1;

printf("Calling foreach:\n");
uint32_t foreach_count = 0;
dn_simdhash_size_t_size_t_foreach(test, foreach_callback, &foreach_count);
printf("Foreach iterated %u time(s)\n", foreach_count);
printf("Count: %u, Capacity: %u, Cascaded item count: %u\n", dn_simdhash_count(test), dn_simdhash_capacity(test), count_cascaded_buckets(test));

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value, expected_value = *dn_vector_index_t(values, DN_SIMDHASH_VALUE_T, i);

uint8_t ok = dn_simdhash_size_t_size_t_try_get_value(test, key, &value);
if (tassert1(ok, key, "did not find key"))
tasserteq(value, expected_value, "value did not match");
}

// NOTE: Adding duplicates could grow the table if we're unlucky, since the add operation
// eagerly grows before doing a table scan if we're at the grow threshold.
for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value = *dn_vector_index_t(values, DN_SIMDHASH_VALUE_T, i);

uint8_t ok = dn_simdhash_size_t_size_t_try_add(test, key, value);
tassert1(!ok, key, "added duplicate key successfully");
}

printf("After adding dupes: Count: %u, Capacity: %u, Cascaded item count: %u\n", dn_simdhash_count(test), dn_simdhash_capacity(test), count_cascaded_buckets(test));
uint32_t final_capacity = dn_simdhash_capacity(test);

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
uint8_t ok = dn_simdhash_size_t_size_t_try_remove(test, key);
tassert1(ok, key, "could not remove key");

DN_SIMDHASH_VALUE_T value;
ok = dn_simdhash_size_t_size_t_try_get_value(test, key, &value);
tassert1(!ok, key, "found key after removal");
}

if (!tasserteq(dn_simdhash_count(test), 0, "was not empty"))
return 1;
if (!tasserteq(dn_simdhash_capacity(test), final_capacity, "capacity changed by emptying"))
return 1;

printf ("Calling foreach after emptying:\n");
foreach_count = 0;
dn_simdhash_size_t_size_t_foreach(test, foreach_callback, &foreach_count);
printf("Foreach iterated %u time(s)\n", foreach_count);
printf("Count: %u, Capacity: %u, Cascaded item count: %u\n", dn_simdhash_count(test), dn_simdhash_capacity(test), count_cascaded_buckets(test));

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value;
uint8_t ok = dn_simdhash_size_t_size_t_try_get_value(test, key, &value);
tassert1(!ok, key, "found key after removal");
}

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value = *dn_vector_index_t(values, DN_SIMDHASH_VALUE_T, i);

uint8_t ok = dn_simdhash_size_t_size_t_try_add(test, key, value);
tassert1(ok, key, "could not re-insert key after emptying");
}

if (!tasserteq(dn_simdhash_capacity(test), final_capacity, "expected capacity not to change after refilling"))
return 1;

for (int i = 0; i < c; i++) {
DN_SIMDHASH_KEY_T key = *dn_vector_index_t(keys, DN_SIMDHASH_KEY_T, i);
DN_SIMDHASH_VALUE_T value, expected_value = *dn_vector_index_t(values, DN_SIMDHASH_VALUE_T, i);

uint8_t ok = dn_simdhash_size_t_size_t_try_get_value(test, key, &value);
if (tassert1(ok, key, "did not find key after refilling"))
tasserteq(value, expected_value, "value did not match after refilling");
}

printf("Calling foreach after refilling:\n");
foreach_count = 0;
dn_simdhash_size_t_size_t_foreach(test, foreach_callback, &foreach_count);
printf("Foreach iterated %u time(s)\n", foreach_count);
printf("Count: %u, Capacity: %u, Cascaded item count: %u\n", dn_simdhash_count(test), dn_simdhash_capacity(test), count_cascaded_buckets(test));
}

printf("done\n");

Expand Down

0 comments on commit 8d8d34f

Please sign in to comment.