@@ -0,0 +1,266 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "wapp.h"
|
||||
#include "test_hashmap.h"
|
||||
|
||||
wp_persist inline WpU8Stream encoder(void *data);
|
||||
wp_persist inline b8 key_eq(void *a, void *b);
|
||||
|
||||
WpTestFuncResult test_hashmap(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
result = result && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 0;
|
||||
map = wpHashMap(WpStr8, i32, encoder, key_eq, 3);
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 0;
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_alloc(void) {
|
||||
WpAllocator arena = wpMemArenaAllocatorInit(KiB(4));
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMapAlloc(WpStr8, i32, &arena, encoder, key_eq, 8);
|
||||
result = result && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 0;
|
||||
map = wpHashMapAlloc(WpStr8, i32, &arena, encoder, key_eq, 3);
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 0;
|
||||
|
||||
wpMemArenaAllocatorDestroy(&arena);
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_get_key_size(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
result = result && wpHashMapKeySize(&map) == sizeof(WpStr8);
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_get_value_size(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
result = result && wpHashMapValueSize(&map) == sizeof(i32);
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_find(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
WpStr8Array keys = wpArray(
|
||||
WpStr8,
|
||||
wpStr8Lit("key1"),
|
||||
wpStr8Lit("key2"),
|
||||
wpStr8Lit("key3"),
|
||||
wpStr8Lit("key4")
|
||||
);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
WpHashLookupResult res = wpHashMapFind(WpStr8, &map, &keys[3]);
|
||||
|
||||
result = result && res.found && res.ref.dist_and_tag > 0;
|
||||
|
||||
WpStr8 key = wpStr8Lit("key0");
|
||||
res = wpHashMapFind(WpStr8, &map, &key);
|
||||
result = result && !res.found;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_update(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8Array keys = wpArray(
|
||||
WpStr8,
|
||||
wpStr8Lit("key1"),
|
||||
wpStr8Lit("key2"),
|
||||
wpStr8Lit("key3"),
|
||||
wpStr8Lit("key4")
|
||||
);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
i32 new_value = 0;
|
||||
wpHashMapUpdate(WpStr8, i32, &map, &keys[1], &new_value);
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 3 &&
|
||||
wpHashMapValues(i32, &map)[1] == new_value;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_insert_capped(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8Array keys = wpArray(
|
||||
WpStr8,
|
||||
wpStr8Lit("key1"),
|
||||
wpStr8Lit("key2"),
|
||||
wpStr8Lit("key3"),
|
||||
wpStr8Lit("key4")
|
||||
);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 3;
|
||||
for (u64 i = 0; i < 3; ++i) {
|
||||
result = result && wpStr8Equal(wpArrayGet(WpStr8, map.keys, i), &keys[i]);
|
||||
}
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_insert_or_update_capped(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8Array keys = wpArray(
|
||||
WpStr8,
|
||||
wpStr8Lit("key1"),
|
||||
wpStr8Lit("key2"),
|
||||
wpStr8Lit("key3"),
|
||||
wpStr8Lit("key4")
|
||||
);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertOrUpdateCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 3;
|
||||
for (u64 i = 0; i < 3; ++i) {
|
||||
result = result && wpStr8Equal(&wpHashMapKeys(WpStr8, &map)[i], &keys[i]);
|
||||
}
|
||||
|
||||
i32 new_value = 0;
|
||||
wpHashMapInsertOrUpdateCapped(WpStr8, i32, &map, &keys[1], &new_value);
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 3 &&
|
||||
wpHashMapValues(i32, &map)[1] == new_value;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_insert_alloc(void) {
|
||||
WpAllocator arena = wpMemArenaAllocatorInit(KiB(4));
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8Array keys = wpArray(
|
||||
WpStr8,
|
||||
wpStr8Lit("key1"),
|
||||
wpStr8Lit("key2"),
|
||||
wpStr8Lit("key3"),
|
||||
wpStr8Lit("key4")
|
||||
);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
WpStr8Array map_keys = map.keys;
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertAlloc(WpStr8, i32, &arena, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
result = result && map.keys != map_keys && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 4;
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
result = result && wpStr8Equal(&wpHashMapKeys(WpStr8, &map)[i], &keys[i]) &&
|
||||
wpHashMapValues(i32, &map)[i] == values[i];
|
||||
}
|
||||
|
||||
wpMemArenaAllocatorDestroy(&arena);
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_insert_or_update_alloc(void) {
|
||||
WpAllocator arena = wpMemArenaAllocatorInit(KiB(4));
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8Array keys = wpArray(
|
||||
WpStr8,
|
||||
wpStr8Lit("key1"),
|
||||
wpStr8Lit("key2"),
|
||||
wpStr8Lit("key3"),
|
||||
wpStr8Lit("key4")
|
||||
);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertOrUpdateAlloc(WpStr8, i32, &arena, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
result = result && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 4;
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
result = result && wpStr8Equal(&wpHashMapKeys(WpStr8, &map)[i], &keys[i]);
|
||||
}
|
||||
|
||||
i32 new_value = 0;
|
||||
wpHashMapInsertOrUpdateAlloc(WpStr8, i32, &arena, &map, &keys[1], &new_value);
|
||||
result = result && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 4 &&
|
||||
wpHashMapValues(i32, &map)[1] == new_value;
|
||||
|
||||
wpMemArenaAllocatorDestroy(&arena);
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_remove(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8Array keys = wpArray(
|
||||
WpStr8,
|
||||
wpStr8Lit("key1"),
|
||||
wpStr8Lit("key2"),
|
||||
wpStr8Lit("key3"),
|
||||
wpStr8Lit("key4")
|
||||
);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
wpHashMapRemove(WpStr8, i32, &map, &keys[0]);
|
||||
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 2;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_clear(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
WpStr8Array keys = wpArray(
|
||||
WpStr8,
|
||||
wpStr8Lit("key1"),
|
||||
wpStr8Lit("key2"),
|
||||
wpStr8Lit("key3"),
|
||||
wpStr8Lit("key4")
|
||||
);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
wpHashMapClear(WpStr8, i32, &map);
|
||||
|
||||
result = result && wpHashMapCount(&map) == 0;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
wp_persist inline WpU8Stream encoder(void *data) {
|
||||
WpStr8 *str = (WpStr8 *)data;
|
||||
return wpStream(u8, str->buf, str->size);
|
||||
}
|
||||
|
||||
wp_persist inline b8 key_eq(void *a, void *b) {
|
||||
WpStr8 *a_str = (WpStr8 *)a;
|
||||
WpStr8 *b_str = (WpStr8 *)b;
|
||||
return wpStr8Equal(a_str, b_str);
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "wapp.h"
|
||||
#include "test_hashmap.h"
|
||||
|
||||
wp_persist inline WpU8Stream encoder(void *data);
|
||||
wp_persist inline b8 key_eq(void *a, void *b);
|
||||
|
||||
WpTestFuncResult test_hashmap(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
result = result && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 0;
|
||||
map = wpHashMap(WpStr8, i32, encoder, key_eq, 3);
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 0;
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_alloc(void) {
|
||||
WpAllocator arena = wpMemArenaAllocatorInit(KiB(4));
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMapAlloc(WpStr8, i32, &arena, encoder, key_eq, 8);
|
||||
result = result && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 0;
|
||||
map = wpHashMapAlloc(WpStr8, i32, &arena, encoder, key_eq, 3);
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 0;
|
||||
|
||||
wpMemArenaAllocatorDestroy(&arena);
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_get_key_size(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
result = result && wpHashMapKeySize(&map) == sizeof(WpStr8);
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_get_value_size(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
result = result && wpHashMapValueSize(&map) == sizeof(i32);
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_find(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
WpStr8 key1 = wpStr8Lit("key1");
|
||||
WpStr8 key2 = wpStr8Lit("key2");
|
||||
WpStr8 key3 = wpStr8Lit("key3");
|
||||
WpStr8 key4 = wpStr8Lit("key4");
|
||||
WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
WpHashLookupResult res = wpHashMapFind(WpStr8, &map, &keys[3]);
|
||||
|
||||
result = result && res.found && res.ref.dist_and_tag > 0;
|
||||
|
||||
WpStr8 key = wpStr8Lit("key0");
|
||||
res = wpHashMapFind(WpStr8, &map, &key);
|
||||
result = result && !res.found;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_update(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8 key1 = wpStr8Lit("key1");
|
||||
WpStr8 key2 = wpStr8Lit("key2");
|
||||
WpStr8 key3 = wpStr8Lit("key3");
|
||||
WpStr8 key4 = wpStr8Lit("key4");
|
||||
WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
i32 new_value = 0;
|
||||
wpHashMapUpdate(WpStr8, i32, &map, &keys[1], &new_value);
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 3 &&
|
||||
wpHashMapValues(i32, &map)[1] == new_value;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_insert_capped(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8 key1 = wpStr8Lit("key1");
|
||||
WpStr8 key2 = wpStr8Lit("key2");
|
||||
WpStr8 key3 = wpStr8Lit("key3");
|
||||
WpStr8 key4 = wpStr8Lit("key4");
|
||||
WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 3;
|
||||
for (u64 i = 0; i < 3; ++i) {
|
||||
result = result && wpStr8Equal(wpArrayGet(WpStr8, map.keys, i), &keys[i]);
|
||||
}
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_insert_or_update_capped(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8 key1 = wpStr8Lit("key1");
|
||||
WpStr8 key2 = wpStr8Lit("key2");
|
||||
WpStr8 key3 = wpStr8Lit("key3");
|
||||
WpStr8 key4 = wpStr8Lit("key4");
|
||||
WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertOrUpdateCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 3;
|
||||
for (u64 i = 0; i < 3; ++i) {
|
||||
result = result && wpStr8Equal(&wpHashMapKeys(WpStr8, &map)[i], &keys[i]);
|
||||
}
|
||||
|
||||
i32 new_value = 0;
|
||||
wpHashMapInsertOrUpdateCapped(WpStr8, i32, &map, &keys[1], &new_value);
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 3 &&
|
||||
wpHashMapValues(i32, &map)[1] == new_value;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_insert_alloc(void) {
|
||||
WpAllocator arena = wpMemArenaAllocatorInit(KiB(4));
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8 key1 = wpStr8Lit("key1");
|
||||
WpStr8 key2 = wpStr8Lit("key2");
|
||||
WpStr8 key3 = wpStr8Lit("key3");
|
||||
WpStr8 key4 = wpStr8Lit("key4");
|
||||
WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
WpStr8Array map_keys = (WpStr8Array)map.keys;
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertAlloc(WpStr8, i32, &arena, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
result = result && map.keys != map_keys && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 4;
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
result = result && wpStr8Equal(&wpHashMapKeys(WpStr8, &map)[i], &keys[i]) &&
|
||||
wpHashMapValues(i32, &map)[i] == values[i];
|
||||
}
|
||||
|
||||
wpMemArenaAllocatorDestroy(&arena);
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_insert_or_update_alloc(void) {
|
||||
WpAllocator arena = wpMemArenaAllocatorInit(KiB(4));
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8 key1 = wpStr8Lit("key1");
|
||||
WpStr8 key2 = wpStr8Lit("key2");
|
||||
WpStr8 key3 = wpStr8Lit("key3");
|
||||
WpStr8 key4 = wpStr8Lit("key4");
|
||||
WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertOrUpdateAlloc(WpStr8, i32, &arena, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
result = result && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 4;
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
result = result && wpStr8Equal(&wpHashMapKeys(WpStr8, &map)[i], &keys[i]);
|
||||
}
|
||||
|
||||
i32 new_value = 0;
|
||||
wpHashMapInsertOrUpdateAlloc(WpStr8, i32, &arena, &map, &keys[1], &new_value);
|
||||
result = result && wpHashMapCapacity(&map) == 8 && wpHashMapCount(&map) == 4 &&
|
||||
wpHashMapValues(i32, &map)[1] == new_value;
|
||||
|
||||
wpMemArenaAllocatorDestroy(&arena);
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_remove(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 4);
|
||||
WpStr8 key1 = wpStr8Lit("key1");
|
||||
WpStr8 key2 = wpStr8Lit("key2");
|
||||
WpStr8 key3 = wpStr8Lit("key3");
|
||||
WpStr8 key4 = wpStr8Lit("key4");
|
||||
WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
wpHashMapRemove(WpStr8, i32, &map, &keys[0]);
|
||||
|
||||
result = result && wpHashMapCapacity(&map) == 4 && wpHashMapCount(&map) == 2;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashmap_clear(void) {
|
||||
b8 result = true;
|
||||
WpHashMap map = wpHashMap(WpStr8, i32, encoder, key_eq, 8);
|
||||
WpStr8 key1 = wpStr8Lit("key1");
|
||||
WpStr8 key2 = wpStr8Lit("key2");
|
||||
WpStr8 key3 = wpStr8Lit("key3");
|
||||
WpStr8 key4 = wpStr8Lit("key4");
|
||||
WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4);
|
||||
WpI32Array values = wpArray(i32, 1, 2, 3, 4);
|
||||
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
wpHashMapInsertCapped(WpStr8, i32, &map, &keys[i], &values[i]);
|
||||
}
|
||||
|
||||
wpHashMapClear(WpStr8, i32, &map);
|
||||
|
||||
result = result && wpHashMapCount(&map) == 0;
|
||||
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
wp_persist inline WpU8Stream encoder(void *data) {
|
||||
WpStr8 *str = (WpStr8 *)data;
|
||||
return wpStream(u8, str->buf, str->size);
|
||||
}
|
||||
|
||||
wp_persist inline b8 key_eq(void *a, void *b) {
|
||||
WpStr8 *a_str = (WpStr8 *)a;
|
||||
WpStr8 *b_str = (WpStr8 *)b;
|
||||
return wpStr8Equal(a_str, b_str);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#ifndef TEST_HASHMAP_H
|
||||
#define TEST_HASHMAP_H
|
||||
|
||||
#include "wapp.h"
|
||||
|
||||
WpTestFuncResult test_hashmap(void);
|
||||
WpTestFuncResult test_hashmap_alloc(void);
|
||||
WpTestFuncResult test_hashmap_get_key_size(void);
|
||||
WpTestFuncResult test_hashmap_get_value_size(void);
|
||||
WpTestFuncResult test_hashmap_find(void);
|
||||
WpTestFuncResult test_hashmap_update(void);
|
||||
WpTestFuncResult test_hashmap_insert_capped(void);
|
||||
WpTestFuncResult test_hashmap_insert_or_update_capped(void);
|
||||
WpTestFuncResult test_hashmap_insert_alloc(void);
|
||||
WpTestFuncResult test_hashmap_insert_or_update_alloc(void);
|
||||
WpTestFuncResult test_hashmap_remove(void);
|
||||
WpTestFuncResult test_hashmap_clear(void);
|
||||
|
||||
#endif // !TEST_HASHMAP_H
|
||||
@@ -1,6 +1,7 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "wapp.h"
|
||||
#include "test_hashset.h"
|
||||
|
||||
wp_persist inline WpU8Stream encoder(void *data);
|
||||
wp_persist inline b8 key_eq(void *a, void *b);
|
||||
@@ -27,6 +28,13 @@ WpTestFuncResult test_hashset_alloc(void) {
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashset_get_item_size(void) {
|
||||
b8 result = true;
|
||||
WpHashSet set = wpHashSet(WpStr8, encoder, key_eq, 8);
|
||||
result = result && wpHashSetItemSize(&set) == sizeof(WpStr8);
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashset_find(void) {
|
||||
b8 result = true;
|
||||
WpHashSet set = wpHashSet(WpStr8, encoder, key_eq, 8);
|
||||
@@ -70,7 +78,7 @@ WpTestFuncResult test_hashset_insert_capped(void) {
|
||||
|
||||
result = result && wpHashSetCapacity(&set) == 4 && wpHashSetCount(&set) == 3;
|
||||
for (u64 i = 0; i < 3; ++i) {
|
||||
result = result && wpStr8Equal(wpArrayGet(WpStr8, set.keys, i), &keys[i]);
|
||||
result = result && wpStr8Equal(&wpHashSetItems(WpStr8, &set)[i], &keys[i]);
|
||||
}
|
||||
|
||||
return wpTesterResult(result);
|
||||
@@ -95,7 +103,7 @@ WpTestFuncResult test_hashset_insert_alloc(void) {
|
||||
}
|
||||
|
||||
result = result && set.keys != set_keys && wpHashSetCapacity(&set) == 8 && wpHashSetCount(&set) == 4;
|
||||
for (u64 i = 0; i < 3; ++i) {
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
result = result && wpStr8Equal(&wpHashSetItems(WpStr8, &set)[i], &keys[i]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "wapp.h"
|
||||
#include "test_hashset.h"
|
||||
|
||||
wp_persist inline WpU8Stream encoder(void *data);
|
||||
wp_persist inline b8 key_eq(void *a, void *b);
|
||||
@@ -27,6 +28,13 @@ WpTestFuncResult test_hashset_alloc(void) {
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashset_get_item_size(void) {
|
||||
b8 result = true;
|
||||
WpHashSet set = wpHashSet(WpStr8, encoder, key_eq, 8);
|
||||
result = result && wpHashSetItemSize(&set) == sizeof(WpStr8);
|
||||
return wpTesterResult(result);
|
||||
}
|
||||
|
||||
WpTestFuncResult test_hashset_find(void) {
|
||||
b8 result = true;
|
||||
WpHashSet set = wpHashSet(WpStr8, encoder, key_eq, 8);
|
||||
@@ -66,7 +74,7 @@ WpTestFuncResult test_hashset_insert_capped(void) {
|
||||
|
||||
result = result && wpHashSetCapacity(&set) == 4 && wpHashSetCount(&set) == 3;
|
||||
for (u64 i = 0; i < 3; ++i) {
|
||||
result = result && wpStr8Equal(wpArrayGet(WpStr8, set.keys, i), &keys[i]);
|
||||
result = result && wpStr8Equal(&wpHashSetItems(WpStr8, &set)[i], &keys[i]);
|
||||
}
|
||||
|
||||
return wpTesterResult(result);
|
||||
@@ -89,7 +97,7 @@ WpTestFuncResult test_hashset_insert_alloc(void) {
|
||||
}
|
||||
|
||||
result = result && set.keys != set_keys && wpHashSetCapacity(&set) == 8 && wpHashSetCount(&set) == 4;
|
||||
for (u64 i = 0; i < 3; ++i) {
|
||||
for (u64 i = 0; i < wpArrayCount(keys); ++i) {
|
||||
result = result && wpStr8Equal(&wpHashSetItems(WpStr8, &set)[i], &keys[i]);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
WpTestFuncResult test_hashset(void);
|
||||
WpTestFuncResult test_hashset_alloc(void);
|
||||
WpTestFuncResult test_hashset_get_item_size(void);
|
||||
WpTestFuncResult test_hashset_find(void);
|
||||
WpTestFuncResult test_hashset_insert_capped(void);
|
||||
WpTestFuncResult test_hashset_insert_alloc(void);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "test_file.h"
|
||||
#include "test_hasher.h"
|
||||
#include "test_hashset.h"
|
||||
#include "test_hashmap.h"
|
||||
#include "test_shell_commander.h"
|
||||
#include "wapp.h"
|
||||
#include <stdlib.h>
|
||||
@@ -108,11 +109,24 @@ int main(void) {
|
||||
test_siphash_128,
|
||||
test_hashset,
|
||||
test_hashset_alloc,
|
||||
test_hashset_get_item_size,
|
||||
test_hashset_find,
|
||||
test_hashset_insert_capped,
|
||||
test_hashset_insert_alloc,
|
||||
test_hashset_remove,
|
||||
test_hashset_clear,
|
||||
test_hashmap,
|
||||
test_hashmap_alloc,
|
||||
test_hashmap_get_key_size,
|
||||
test_hashmap_get_value_size,
|
||||
test_hashmap_find,
|
||||
test_hashmap_update,
|
||||
test_hashmap_insert_capped,
|
||||
test_hashmap_insert_or_update_capped,
|
||||
test_hashmap_insert_alloc,
|
||||
test_hashmap_insert_or_update_alloc,
|
||||
test_hashmap_remove,
|
||||
test_hashmap_clear,
|
||||
test_commander_cmd_success,
|
||||
test_commander_cmd_failure,
|
||||
test_commander_cmd_out_buf_success,
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "test_file.h"
|
||||
#include "test_hasher.h"
|
||||
#include "test_hashset.h"
|
||||
#include "test_hashmap.h"
|
||||
#include "test_shell_commander.h"
|
||||
#include "wapp.h"
|
||||
#include <stdlib.h>
|
||||
@@ -108,11 +109,24 @@ int main(void) {
|
||||
test_siphash_128,
|
||||
test_hashset,
|
||||
test_hashset_alloc,
|
||||
test_hashset_get_item_size,
|
||||
test_hashset_find,
|
||||
test_hashset_insert_capped,
|
||||
test_hashset_insert_alloc,
|
||||
test_hashset_remove,
|
||||
test_hashset_clear,
|
||||
test_hashmap,
|
||||
test_hashmap_alloc,
|
||||
test_hashmap_get_key_size,
|
||||
test_hashmap_get_value_size,
|
||||
test_hashmap_find,
|
||||
test_hashmap_update,
|
||||
test_hashmap_insert_capped,
|
||||
test_hashmap_insert_or_update_capped,
|
||||
test_hashmap_insert_alloc,
|
||||
test_hashmap_insert_or_update_alloc,
|
||||
test_hashmap_remove,
|
||||
test_hashmap_clear,
|
||||
test_commander_cmd_success,
|
||||
test_commander_cmd_failure,
|
||||
test_commander_cmd_out_buf_success,
|
||||
|
||||
Reference in New Issue
Block a user