This repository has been archived on 2026-04-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
bhlib/test/src/TestHashmap.c

235 lines
6.3 KiB
C
Raw Normal View History

#include <BH/Hashmap.h>
#include <BH/Unit.h>
2025-01-18 17:24:36 +03:00
static size_t DBG_PtrIntHash(const void *ptr)
2025-01-18 17:24:36 +03:00
{
return BH_PTR2INT(ptr);
}
static int DBG_PtrIntEqual(const void *lhs, const void *rhs)
2025-01-18 17:24:36 +03:00
{
return BH_PTR2INT(lhs) - BH_PTR2INT(rhs);
}
static int NewFree(void)
2025-01-18 17:24:36 +03:00
{
BH_Hashmap *hashmap;
2025-01-18 17:24:36 +03:00
hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
2025-01-18 17:24:36 +03:00
BH_VERIFY(hashmap != NULL);
BH_VERIFY(BH_HashmapEmpty(hashmap) != 0);
BH_VERIFY(BH_HashmapSize(hashmap) == 0);
BH_VERIFY(BH_HashmapCapacity(hashmap) == 0);
BH_VERIFY(BH_HashmapFactor(hashmap) >= 0.15f);
BH_VERIFY(BH_HashmapFactor(hashmap) <= 1.0f);
2025-01-18 17:24:36 +03:00
BH_HashmapFree(hashmap);
2025-01-18 17:24:36 +03:00
return 0;
}
static int GrowShrink(void)
2025-01-18 17:24:36 +03:00
{
BH_Hashmap *hashmap;
2025-01-18 17:24:36 +03:00
void *iter;
hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
2025-01-18 17:24:36 +03:00
BH_VERIFY(hashmap != NULL);
/* Allocate space for 1024 entries and insert 1 element */
BH_VERIFY(BH_HashmapReserve(hashmap, 1024) == 0);
BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(1337), BH_INT2PTR(80085)) == 0);
2025-01-18 17:24:36 +03:00
/* Check hashmap contents */
iter = BH_HashmapIterNext(hashmap, NULL);
2025-01-18 17:24:36 +03:00
BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085);
BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL);
BH_VERIFY(BH_HashmapEmpty(hashmap) == 0);
BH_VERIFY(BH_HashmapSize(hashmap) == 1);
BH_VERIFY(BH_HashmapCapacity(hashmap) >= 1024);
2025-01-18 17:24:36 +03:00
/* Change factor and grow */
BH_HashmapSetFactor(hashmap, 0.35f);
2025-01-18 17:24:36 +03:00
/* Check hashmap contents */
iter = BH_HashmapIterNext(hashmap, NULL);
2025-01-18 17:24:36 +03:00
BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085);
BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL);
BH_VERIFY(BH_HashmapReserve(hashmap, 8192) == 0);
BH_VERIFY(BH_HashmapEmpty(hashmap) == 0);
BH_VERIFY(BH_HashmapSize(hashmap) == 1);
BH_VERIFY(BH_HashmapCapacity(hashmap) >= 8192);
BH_VERIFY_DELTA(BH_HashmapFactor(hashmap), 0.35f, 0.001f);
2025-01-18 17:24:36 +03:00
/* Shrink */
BH_VERIFY(BH_HashmapReserve(hashmap, 0) == 0);
2025-01-18 17:24:36 +03:00
/* Check hashmap contents */
iter = BH_HashmapIterNext(hashmap, NULL);
2025-01-18 17:24:36 +03:00
BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085);
BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL);
BH_VERIFY(BH_HashmapEmpty(hashmap) == 0);
BH_VERIFY(BH_HashmapSize(hashmap) == 1);
BH_VERIFY(BH_HashmapCapacity(hashmap) >= 1);
BH_VERIFY(BH_HashmapCapacity(hashmap) < 8192);
BH_VERIFY_DELTA(BH_HashmapFactor(hashmap), 0.35f, 0.001f);
2025-01-18 17:24:36 +03:00
/* Shrink to 0 (deallocate) */
BH_HashmapClear(hashmap);
BH_VERIFY(BH_HashmapEmpty(hashmap) != 0);
BH_VERIFY(BH_HashmapSize(hashmap) == 0);
BH_VERIFY(BH_HashmapCapacity(hashmap) > 0);
2025-01-18 17:24:36 +03:00
BH_VERIFY(BH_HashmapReserve(hashmap, 0) == 0);
BH_VERIFY(BH_HashmapEmpty(hashmap) != 0);
BH_VERIFY(BH_HashmapSize(hashmap) == 0);
BH_VERIFY(BH_HashmapCapacity(hashmap) == 0);
2025-01-18 17:24:36 +03:00
/* Check hashmap contents */
iter = BH_HashmapIterNext(hashmap, NULL);
2025-01-18 17:24:36 +03:00
BH_VERIFY(iter == NULL);
BH_HashmapFree(hashmap);
2025-01-18 17:24:36 +03:00
return 0;
}
static int InsertRemove(void)
2025-01-18 17:24:36 +03:00
{
BH_Hashmap *hashmap;
2025-01-18 17:24:36 +03:00
size_t i, added, removed;
void *iter;
hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
2025-01-18 17:24:36 +03:00
BH_VERIFY(hashmap != NULL);
BH_HashmapSetFactor(hashmap, 1.0f);
2025-01-18 17:24:36 +03:00
/* Insert elements into hashmap */
added = 0;
for (i = 1024; i > 0; i--)
{
added += (i - 1) / 4;
BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR((i - 1) / 4), BH_INT2PTR(i)) == 0);
2025-01-18 17:24:36 +03:00
}
/* Remove elements */
iter = BH_HashmapIterNext(hashmap, NULL);
2025-01-18 17:24:36 +03:00
removed = 0;
while (iter)
{
removed += BH_PTR2INT(BH_HashmapIterKey(iter));
BH_HashmapIterRemove(hashmap, iter);
2025-01-18 17:24:36 +03:00
iter = BH_HashmapIterNext(hashmap, NULL);
2025-01-18 17:24:36 +03:00
}
/* Check inserted elements are equal to removed */
BH_VERIFY(added == removed);
BH_HashmapFree(hashmap);
2025-01-18 17:24:36 +03:00
return 0;
}
static int Lookup(void)
2025-01-18 17:24:36 +03:00
{
BH_Hashmap *hashmap;
2025-01-18 17:24:36 +03:00
size_t i;
hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
2025-01-18 17:24:36 +03:00
BH_VERIFY(hashmap != NULL);
/* Insert elements into hashmap */
for (i = 0; i < 256; i++)
BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(i * 4), BH_INT2PTR(i)) == 0);
2025-01-18 17:24:36 +03:00
/* Lookup inserted elements */
for (i = 0; i < 256; i++)
{
void *value;
BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), NULL) == BH_OK);
BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == (int)i);
2025-01-18 17:24:36 +03:00
}
/* Lookup non-existing elements */
for (i = 256; i < 512; i++)
{
void *value;
BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), NULL) != BH_OK);
BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), &value) != BH_OK);
2025-01-18 17:24:36 +03:00
}
BH_HashmapFree(hashmap);
2025-01-18 17:24:36 +03:00
return 0;
}
static int Clear(void)
2025-01-18 17:24:36 +03:00
{
BH_Hashmap *hashmap;
2025-01-18 17:24:36 +03:00
size_t i;
hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
2025-01-18 17:24:36 +03:00
BH_VERIFY(hashmap != NULL);
/* Insert elements into hashmap */
for (i = 0; i < 128; i++)
BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(i), 0) == 0);
2025-01-18 17:24:36 +03:00
BH_HashmapClear(hashmap);
2025-01-18 17:24:36 +03:00
/* Remove non-existing elements */
for (i = 0; i < 128; i++)
BH_HashmapRemove(hashmap, BH_INT2PTR(i));
2025-01-18 17:24:36 +03:00
BH_HashmapFree(hashmap);
2025-01-18 17:24:36 +03:00
return 0;
}
static int Fields(void)
2025-01-18 17:24:36 +03:00
{
BH_Hashmap *hashmap;
2025-01-18 17:24:36 +03:00
size_t i;
hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
2025-01-18 17:24:36 +03:00
BH_VERIFY(hashmap != NULL);
BH_VERIFY(BH_HashmapEmpty(hashmap) == 1);
2025-01-18 17:24:36 +03:00
/* Insert elements into hashmap */
for (i = 0; i < 14; i++)
BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(i), NULL) == 0);
2025-01-18 17:24:36 +03:00
/* Check hashmap fields correspond to getter functions */
BH_VERIFY(BH_HashmapSize(hashmap) == 14);
BH_VERIFY(BH_HashmapCapacity(hashmap) >= 14);
BH_VERIFY(BH_HashmapEmpty(hashmap) == 0);
2025-01-18 17:24:36 +03:00
BH_HashmapFree(hashmap);
2025-01-18 17:24:36 +03:00
return 0;
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
/* Add unit tests */
BH_UnitAdd("NewFree", NewFree);
BH_UnitAdd("GrowShrink", GrowShrink);
BH_UnitAdd("InsertRemove", InsertRemove);
BH_UnitAdd("Lookup", Lookup);
BH_UnitAdd("Clear", Clear);
BH_UnitAdd("Fields", Fields);
return BH_UnitRun();
2025-01-18 17:24:36 +03:00
}