// vim:fileencoding=utf-8:foldmethod=marker #include "wapp.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_find(void) { b8 result = true; WpHashSet set = wpHashSet(WpStr8, encoder, key_eq, 8); WpStr8Array keys = wpArray( WpStr8, wpStr8Lit("key1"), wpStr8Lit("key2"), wpStr8Lit("key3"), wpStr8Lit("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); WpStr8Array keys = wpArray( WpStr8, wpStr8Lit("key1"), wpStr8Lit("key2"), wpStr8Lit("key3"), wpStr8Lit("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(wpArrayGet(WpStr8, set.keys, 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); WpStr8Array keys = wpArray( WpStr8, wpStr8Lit("key1"), wpStr8Lit("key2"), wpStr8Lit("key3"), wpStr8Lit("key4") ); WpStr8Array set_keys = 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 < 3; ++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); WpStr8Array keys = wpArray( WpStr8, wpStr8Lit("key1"), wpStr8Lit("key2"), wpStr8Lit("key3"), wpStr8Lit("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); WpStr8Array keys = wpArray( WpStr8, wpStr8Lit("key1"), wpStr8Lit("key2"), wpStr8Lit("key3"), wpStr8Lit("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); }