mirror of
https://github.com/Ed94/metadesk.git
synced 2026-08-05 15:18:46 +00:00
Test and avoid MD_StringMap collisions.
This commit is contained in:
@@ -488,5 +488,60 @@ int main(void)
|
||||
}
|
||||
}
|
||||
|
||||
Test("Hash maps")
|
||||
{
|
||||
MD_String8 keys[] =
|
||||
{
|
||||
MD_S8Lit("\xed\x80\x73\x71\x78\xba\xff\xd6\x87\x83\xcd\x20\x28\xf7\x1c\xc1\x5f\xca\x98\x9c\x5a\xab\x0c\xae\x9a\x60\x57\x03\xeb\x1f\xde\x99"),
|
||||
MD_S8Lit("\x4c\x80\xb7\x8b\xbf\x65\x5a\x4b\xc1\x2a\xc3\x5f\xe1\x66\xfb\x0d\x72\x83\x1c\x63\xba\xb5\x97\x02\x3f\x6a\xe0\x2a\x1b\x82\x07\x76"),
|
||||
MD_S8Lit("\xd8\xfd\x11\x4b\x04\xdf\xe5\x20\x5b\xd6\x4f\x87\x00\x70\x6a\xc8\xde\xed\xc7\x79\xdb\x87\x24\x36\xa8\x7a\x31\x41\x00\x57\xbd\x8d"),
|
||||
};
|
||||
|
||||
// NOTE(mal): True for MD_HashString is djb2
|
||||
{
|
||||
TestResult(MD_HashString(keys[0]) == 1 && MD_HashString(keys[1]) == 1 && MD_HashString(keys[2]) == 3);
|
||||
}
|
||||
|
||||
{
|
||||
MD_MapCollisionRule rules[] = { MD_MapCollisionRule_Chain, MD_MapCollisionRule_Overwrite };
|
||||
for(int i_rule = 0; i_rule < MD_ArrayCount(rules); ++i_rule)
|
||||
{
|
||||
MD_StringMap map = {0};
|
||||
MD_StringMap_Insert(&map, rules[i_rule], keys[0], (void *)0);
|
||||
MD_StringMap_Insert(&map, rules[i_rule], keys[1], (void *)1);
|
||||
MD_StringMap_Insert(&map, rules[i_rule], keys[2], (void *)2);
|
||||
|
||||
MD_MapSlot *slot0 = MD_StringMap_Lookup(&map, keys[0]);
|
||||
MD_MapSlot *slot1 = MD_StringMap_Lookup(&map, keys[1]);
|
||||
MD_MapSlot *slot2 = MD_StringMap_Lookup(&map, keys[2]);
|
||||
|
||||
TestResult(slot0 && slot0->value == (void *)0 &&
|
||||
slot1 && slot1->value == (void *)1 &&
|
||||
slot2 && slot2->value == (void *)2);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
MD_MapCollisionRule rules[] = { MD_MapCollisionRule_Chain, MD_MapCollisionRule_Overwrite };
|
||||
for(int i_rule = 0; i_rule < MD_ArrayCount(rules); ++i_rule)
|
||||
{
|
||||
MD_PtrMap map = {0};
|
||||
MD_PtrMap_Insert(&map, rules[i_rule], (void *)0, (void *)0);
|
||||
MD_PtrMap_Insert(&map, rules[i_rule], (void *)1, (void *)1);
|
||||
MD_PtrMap_Insert(&map, rules[i_rule], (void *)2, (void *)2);
|
||||
|
||||
MD_MapSlot *slot0 = MD_PtrMap_Lookup(&map, (void *)0);
|
||||
MD_MapSlot *slot1 = MD_PtrMap_Lookup(&map, (void *)1);
|
||||
MD_MapSlot *slot2 = MD_PtrMap_Lookup(&map, (void *)2);
|
||||
|
||||
TestResult(slot0 && slot0->value == (void *) 0 &&
|
||||
slot1 && slot1->value == (void *) 1 &&
|
||||
slot2 && slot2->value == (void *) 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user