// 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); WpTestFuncResult test_hashset(void) { b8 result = true; WpHashSet set = wpHashSet(WpStr8, encoder, key_eq, 8); result = result && wpHashSetCapacity(&set) == 8 && wpHashSetCount(&set) == 0; set = wpHashSet(WpStr8, encoder, key_eq, 3); result = result && wpHashSetCapacity(&set) == 4 && wpHashSetCount(&set) == 0; return wpTesterResult(result); } WpTestFuncResult test_hashset_alloc(void) { WpAllocator arena = wpMemArenaAllocatorInit(KiB(4)); b8 result = true; WpHashSet set = wpHashSetAlloc(WpStr8, &arena, encoder, key_eq, 8); result = result && wpHashSetCapacity(&set) == 8 && wpHashSetCount(&set) == 0; set = wpHashSetAlloc(WpStr8, &arena, encoder, key_eq, 3); result = result && wpHashSetCapacity(&set) == 4 && wpHashSetCount(&set) == 0; wpMemArenaAllocatorDestroy(&arena); 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); WpStr8 key1 = wpStr8Lit("key1"); WpStr8 key2 = wpStr8Lit("key2"); WpStr8 key3 = wpStr8Lit("key3"); WpStr8 key4 = wpStr8Lit("key4"); WpStr8Array keys = wpArray(WpStr8, key1, key2, key3, key4); for (u64 i = 0; i < wpArrayCount(keys); ++i) { wpHashSetInsertCapped(WpStr8, &set, &keys[i]); } WpHashLookupResult res = wpHashSetFind(WpStr8, &set, &keys[3]); result = result && res.found && res.ref.dist_and_tag > 0; WpStr8 key = wpStr8Lit("key0"); res = wpHashSetFind(WpStr8, &set, &key); result = result && !res.found; return wpTesterResult(result); } WpTestFuncResult test_hashset_insert_capped(void) { b8 result = true; WpHashSet set = wpHashSet(WpStr8, 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); for (u64 i = 0; i < wpArrayCount(keys); ++i) { wpHashSetInsertCapped(WpStr8, &set, &keys[i]); } result = result && wpHashSetCapacity(&set) == 4 && wpHashSetCount(&set) == 3; for (u64 i = 0; i < 3; ++i) { result = result && wpStr8Equal(&wpHashSetItems(WpStr8, &set)[i], &keys[i]); } return wpTesterResult(result); } WpTestFuncResult test_hashset_insert_alloc(void) { WpAllocator arena = wpMemArenaAllocatorInit(KiB(4)); b8 result = true; WpHashSet set = wpHashSet(WpStr8, 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); WpStr8Array set_keys = (WpStr8Array)set.keys; for (u64 i = 0; i < wpArrayCount(keys); ++i) { wpHashSetInsertAlloc(WpStr8, &arena, &set, &keys[i]); } result = result && set.keys != set_keys && wpHashSetCapacity(&set) == 8 && wpHashSetCount(&set) == 4; for (u64 i = 0; i < wpArrayCount(keys); ++i) { result = result && wpStr8Equal(&wpHashSetItems(WpStr8, &set)[i], &keys[i]); } wpMemArenaAllocatorDestroy(&arena); return wpTesterResult(result); } WpTestFuncResult test_hashset_remove(void) { b8 result = true; WpHashSet set = wpHashSet(WpStr8, 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); for (u64 i = 0; i < wpArrayCount(keys); ++i) { wpHashSetInsertCapped(WpStr8, &set, &keys[i]); } wpHashSetRemove(WpStr8, &set, &keys[0]); result = result && wpHashSetCapacity(&set) == 4 && wpHashSetCount(&set) == 2; return wpTesterResult(result); } WpTestFuncResult test_hashset_clear(void) { b8 result = true; WpHashSet set = wpHashSet(WpStr8, 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); for (u64 i = 0; i < wpArrayCount(keys); ++i) { wpHashSetInsertCapped(WpStr8, &set, &keys[i]); } wpHashSetClear(WpStr8, &set); result = result && wpHashSetCount(&set) == 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); }