Skip to content

Commit

Permalink
Fixed table tests, ready for some benchmarking, see #131
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Oct 18, 2024
1 parent 98ea0e5 commit 787a1cc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
73 changes: 37 additions & 36 deletions tests/cases/test_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int test_table_contents_no_expansion() {

//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(1), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(2), TOY_VALUE_FROM_INTEGER(69)); //hash: 8
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(2), TOY_VALUE_FROM_INTEGER(69)); //hash: 0
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(3), TOY_VALUE_FROM_INTEGER(420)); //hash: 5

//check the state
Expand All @@ -128,11 +128,11 @@ int test_table_contents_no_expansion() {
table->count != 3 ||

TEST_ENTRY_STATE(7, 1, 42, 0) ||
TEST_ENTRY_STATE(8, 2, 69, 0) ||
TEST_ENTRY_STATE(0, 2, 69, 0) ||
TEST_ENTRY_STATE(5, 3, 420, 0)
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, multiple inserts, no collisions {1:42},{2:69},{3:420}\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, multiple inserts, no collisions\n" TOY_CC_RESET);
Toy_freeTable(table);
return -1;
}
Expand All @@ -147,23 +147,23 @@ int test_table_contents_no_expansion() {
Toy_Table* table = Toy_allocateTable();

//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(1), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(14), TOY_VALUE_FROM_INTEGER(69)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(76), TOY_VALUE_FROM_INTEGER(420)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(80), TOY_VALUE_FROM_INTEGER(8891)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(5), TOY_VALUE_FROM_INTEGER(42)); //hash: 2
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(19), TOY_VALUE_FROM_INTEGER(69)); //hash: 2
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(37), TOY_VALUE_FROM_INTEGER(420)); //hash: 2
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(65), TOY_VALUE_FROM_INTEGER(8891)); //hash: 2

//check the state
if (table == NULL ||
table->capacity != 8 ||
table->count != 4 ||

TEST_ENTRY_STATE(7, 1, 42, 0) ||
TEST_ENTRY_STATE(8, 14, 69, 1) ||
TEST_ENTRY_STATE(9, 76, 420, 2) ||
TEST_ENTRY_STATE(10, 80, 8891, 3)
TEST_ENTRY_STATE(2, 5, 42, 0) ||
TEST_ENTRY_STATE(3, 19, 69, 1) ||
TEST_ENTRY_STATE(4, 37, 420, 2) ||
TEST_ENTRY_STATE(5, 65, 8891, 3)
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, muiltiple inserts, with collisions {1:42},{14:69},{76:420},{80:8891}\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, muiltiple inserts, with collisions\n" TOY_CC_RESET);
Toy_freeTable(table);
return -1;
}
Expand All @@ -178,21 +178,24 @@ int test_table_contents_no_expansion() {
Toy_Table* table = Toy_allocateTable();

//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(33), TOY_VALUE_FROM_INTEGER(69)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(70), TOY_VALUE_FROM_INTEGER(420)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(1), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(14), TOY_VALUE_FROM_INTEGER(69)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(76), TOY_VALUE_FROM_INTEGER(420)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(80), TOY_VALUE_FROM_INTEGER(8891)); //hash: 7

//check the state
if (table == NULL ||
table->capacity != 8 ||
table->count != 3 ||
table->count != 4 ||

TEST_ENTRY_STATE(7, 1, 42, 0) ||
TEST_ENTRY_STATE(0, 14, 69, 1) ||
TEST_ENTRY_STATE(1, 76, 420, 2) ||
TEST_ENTRY_STATE(2, 80, 8891, 3)

TEST_ENTRY_STATE(15, 17, 42, 0) ||
TEST_ENTRY_STATE(0, 33, 69, 1) ||
TEST_ENTRY_STATE(1, 70, 420, 2)
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, muiltiple inserts, with collisions, modulo wrap {17:42},{33:69},{70:420}\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, muiltiple inserts, with collisions, modulo wrap\n" TOY_CC_RESET);
Toy_freeTable(table);
return -1;
}
Expand All @@ -207,9 +210,9 @@ int test_table_contents_no_expansion() {
Toy_Table* table = Toy_allocateTable();

//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(33), TOY_VALUE_FROM_INTEGER(69)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(70), TOY_VALUE_FROM_INTEGER(420)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(33), TOY_VALUE_FROM_INTEGER(69)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(70), TOY_VALUE_FROM_INTEGER(420)); //hash: 7

//lookup
Toy_Value result = Toy_lookupTable(&table, TOY_VALUE_FROM_INTEGER(33));
Expand Down Expand Up @@ -238,23 +241,23 @@ int test_table_contents_no_expansion() {
Toy_Table* table = Toy_allocateTable();

//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(33), TOY_VALUE_FROM_INTEGER(69)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(70), TOY_VALUE_FROM_INTEGER(420)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(33), TOY_VALUE_FROM_INTEGER(69)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(70), TOY_VALUE_FROM_INTEGER(420)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(35), TOY_VALUE_FROM_INTEGER(8891)); //hash: 1

//check the state
if (table == NULL ||
table->capacity != 8 ||
table->count != 4 ||

TEST_ENTRY_STATE(15, 17, 42, 0) ||
TEST_ENTRY_STATE(7, 17, 42, 0) ||
TEST_ENTRY_STATE(0, 33, 69, 1) ||
TEST_ENTRY_STATE(1, 70, 420, 2) ||
TEST_ENTRY_STATE(2, 35, 8891, 1)
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, muiltiple inserts, with collisions, modulo wrap, psl overlap {17:42},{33:69},{70:420},{35:8891}\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, muiltiple inserts, with collisions, modulo wrap, psl overlap\n" TOY_CC_RESET);
Toy_freeTable(table);
return -1;
}
Expand All @@ -269,9 +272,9 @@ int test_table_contents_no_expansion() {
Toy_Table* table = Toy_allocateTable();

//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(33), TOY_VALUE_FROM_INTEGER(69)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(70), TOY_VALUE_FROM_INTEGER(420)); //hash: 15
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(33), TOY_VALUE_FROM_INTEGER(69)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(70), TOY_VALUE_FROM_INTEGER(420)); //hash: 7
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(35), TOY_VALUE_FROM_INTEGER(8891)); //hash: 1

//remove
Expand All @@ -282,12 +285,12 @@ int test_table_contents_no_expansion() {
table->capacity != 8 ||
table->count != 3 ||

TEST_ENTRY_STATE(15, 17, 42, 0) ||
TEST_ENTRY_STATE(7, 17, 42, 0) ||
TEST_ENTRY_STATE(0, 70, 420, 1) ||
TEST_ENTRY_STATE(1, 35, 8891, 0)
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, muiltiple inserts, with collisions, modulo wrap, psl overlap, psl shift {17:42},{*33:69},{70:420},{35:8891}\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: Unrecognized state from table data, muiltiple inserts, with collisions, modulo wrap, psl overlap, psl shift\n" TOY_CC_RESET);
Toy_freeTable(table);
return -1;
}
Expand Down Expand Up @@ -561,7 +564,7 @@ int test_table_contents_with_expansions() {
//Skipped: multiple inserts, with collisions, modulo wrap, psl overlap
//Skipped: multiple inserts, with collisions, modulo wrap, psl overlap, psl shift

//Note: since psl overlap and psl shift both work without expansion, I'm leaving these tests unimplemented due to exhaustion.
//Note: since psl overlap and psl shift both work without expansion, I'm leaving these tests unimplemented.

return 0;
}
Expand Down Expand Up @@ -606,8 +609,6 @@ int main() {
//run each test set, returning the total errors given
int total = 0, res = 0;

//Note: there's some utility c programs in .notes called "hash_generator" that can help

{
res = test_table_allocation();
if (res == 0) {
Expand Down
3 changes: 2 additions & 1 deletion tests/standalone/hash_generator_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ static unsigned int hashUInt(unsigned int x) {
int main() {
//print the index/hash pairs
for (unsigned int i = 0; i < 100; i++) {
printf("{%u:%u}\n", i, hashUInt(i));
unsigned int h = hashUInt(i);
printf("%u: %u %% 8 = %u\n", i, h, h % 8);
}

return 0;
Expand Down

0 comments on commit 787a1cc

Please sign in to comment.