--- a/tests/test_map.cpp Mon Feb 20 19:55:42 2023 +0100 +++ b/tests/test_map.cpp Thu Feb 23 18:58:15 2023 +0100 @@ -28,6 +28,7 @@ #include "cx/hash_map.h" #include "cx/utils.h" +#include "cx/string.h" #include "util_allocator.h" #include <gtest/gtest.h> @@ -114,14 +115,21 @@ TEST(CxHashMap, Create) { CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, 0); + auto map = cxHashMapCreate(&allocator, 1, 0); auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map); EXPECT_GT(hmap->bucket_count, 0); cx_for_n(i, hmap->bucket_count) { EXPECT_EQ(hmap->buckets[i], nullptr); } + EXPECT_EQ(map->itemsize, 1); EXPECT_EQ(map->size, 0); EXPECT_EQ(map->allocator, &allocator); + EXPECT_FALSE(map->store_pointers); + cxMapStorePointers(map); + EXPECT_TRUE(map->store_pointers); + EXPECT_EQ(map->itemsize, sizeof(void *)); + cxMapStoreObjects(map); + EXPECT_FALSE(map->store_pointers); cxMapDestroy(map); EXPECT_TRUE(allocator.verify()); @@ -130,7 +138,7 @@ TEST(CxHashMap, BasicOperations) { // create the map CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, 8); + auto map = cxHashMapCreateForPointers(&allocator, 8); // create a reference map std::unordered_map<std::string, std::string> refmap; @@ -174,7 +182,7 @@ TEST(CxHashMap, RemoveViaIterator) { CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, 4); + auto map = cxHashMapCreateForPointers(&allocator, 4); cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); @@ -203,7 +211,7 @@ TEST(CxHashMap, RehashNotRequired) { CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, 8); + auto map = cxHashMapCreateForPointers(&allocator, 8); cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); @@ -223,7 +231,7 @@ TEST(CxHashMap, Rehash) { CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, 8); + auto map = cxHashMapCreateForPointers(&allocator, 8); cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); @@ -252,7 +260,7 @@ TEST(CxHashMap, Clear) { CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, 0); + auto map = cxHashMapCreateForPointers(&allocator, 0); cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); @@ -269,4 +277,49 @@ cxMapDestroy(map); EXPECT_TRUE(allocator.verify()); -} \ No newline at end of file +} + +TEST(CxHashMap, StoreUcxStrings) { + // create the map + CxTestingAllocator allocator; + auto map = cxHashMapCreate(&allocator, sizeof(cxstring), 8); + + // define some strings + cxstring s1 = CX_STR("this"); + cxstring s2 = CX_STR("is"); + cxstring s3 = CX_STR("a"); + cxstring s4 = CX_STR("test"); + cxstring s5 = CX_STR("setup"); + + // put them into the map + cxMapPut(map, cx_hash_key_str("s1"), &s1); + cxMapPut(map, cx_hash_key_str("s2"), &s2); + cxMapPut(map, cx_hash_key_str("s3"), &s3); + cxMapPut(map, cx_hash_key_str("s4"), &s4); + + // overwrite a value + cxMapPut(map, cx_hash_key_str("s1"), &s5); + + // look up a string + auto s3p = reinterpret_cast<cxstring *>(cxMapGet(map, cx_hash_key_str("s3"))); + EXPECT_EQ(s3p->length, s3.length); + EXPECT_EQ(s3p->ptr, s3.ptr); + EXPECT_NE(s3p, &s3); + + // remove a string + auto r = cxMapRemove(map, cx_hash_key_str("s2")); + EXPECT_EQ(r, nullptr); + + // iterate + auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr}; + auto iter = cxMapIteratorValues(map); + cx_foreach(cxstring*, s, iter) { + auto found = std::find(ref.begin(), ref.end(), s->ptr); + ASSERT_NE(found, ref.end()); + ref.erase(found); + } + EXPECT_EQ(ref.size(), 0); + + cxMapDestroy(map); + EXPECT_TRUE(allocator.verify()); +}