tests/test_map.cpp

changeset 658
56c62780582e
parent 653
e081643aae2a
child 659
4a06fd63909a
     1.1 --- a/tests/test_map.cpp	Mon Feb 20 19:55:42 2023 +0100
     1.2 +++ b/tests/test_map.cpp	Thu Feb 23 18:58:15 2023 +0100
     1.3 @@ -28,6 +28,7 @@
     1.4  
     1.5  #include "cx/hash_map.h"
     1.6  #include "cx/utils.h"
     1.7 +#include "cx/string.h"
     1.8  #include "util_allocator.h"
     1.9  
    1.10  #include <gtest/gtest.h>
    1.11 @@ -114,14 +115,21 @@
    1.12  
    1.13  TEST(CxHashMap, Create) {
    1.14      CxTestingAllocator allocator;
    1.15 -    auto map = cxHashMapCreate(&allocator, 0);
    1.16 +    auto map = cxHashMapCreate(&allocator, 1, 0);
    1.17      auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map);
    1.18      EXPECT_GT(hmap->bucket_count, 0);
    1.19      cx_for_n(i, hmap->bucket_count) {
    1.20          EXPECT_EQ(hmap->buckets[i], nullptr);
    1.21      }
    1.22 +    EXPECT_EQ(map->itemsize, 1);
    1.23      EXPECT_EQ(map->size, 0);
    1.24      EXPECT_EQ(map->allocator, &allocator);
    1.25 +    EXPECT_FALSE(map->store_pointers);
    1.26 +    cxMapStorePointers(map);
    1.27 +    EXPECT_TRUE(map->store_pointers);
    1.28 +    EXPECT_EQ(map->itemsize, sizeof(void *));
    1.29 +    cxMapStoreObjects(map);
    1.30 +    EXPECT_FALSE(map->store_pointers);
    1.31  
    1.32      cxMapDestroy(map);
    1.33      EXPECT_TRUE(allocator.verify());
    1.34 @@ -130,7 +138,7 @@
    1.35  TEST(CxHashMap, BasicOperations) {
    1.36      // create the map
    1.37      CxTestingAllocator allocator;
    1.38 -    auto map = cxHashMapCreate(&allocator, 8);
    1.39 +    auto map = cxHashMapCreateForPointers(&allocator, 8);
    1.40  
    1.41      // create a reference map
    1.42      std::unordered_map<std::string, std::string> refmap;
    1.43 @@ -174,7 +182,7 @@
    1.44  
    1.45  TEST(CxHashMap, RemoveViaIterator) {
    1.46      CxTestingAllocator allocator;
    1.47 -    auto map = cxHashMapCreate(&allocator, 4);
    1.48 +    auto map = cxHashMapCreateForPointers(&allocator, 4);
    1.49  
    1.50      cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
    1.51      cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
    1.52 @@ -203,7 +211,7 @@
    1.53  
    1.54  TEST(CxHashMap, RehashNotRequired) {
    1.55      CxTestingAllocator allocator;
    1.56 -    auto map = cxHashMapCreate(&allocator, 8);
    1.57 +    auto map = cxHashMapCreateForPointers(&allocator, 8);
    1.58  
    1.59      cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
    1.60      cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
    1.61 @@ -223,7 +231,7 @@
    1.62  
    1.63  TEST(CxHashMap, Rehash) {
    1.64      CxTestingAllocator allocator;
    1.65 -    auto map = cxHashMapCreate(&allocator, 8);
    1.66 +    auto map = cxHashMapCreateForPointers(&allocator, 8);
    1.67  
    1.68      cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
    1.69      cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
    1.70 @@ -252,7 +260,7 @@
    1.71  
    1.72  TEST(CxHashMap, Clear) {
    1.73      CxTestingAllocator allocator;
    1.74 -    auto map = cxHashMapCreate(&allocator, 0);
    1.75 +    auto map = cxHashMapCreateForPointers(&allocator, 0);
    1.76      
    1.77      cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
    1.78      cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
    1.79 @@ -269,4 +277,49 @@
    1.80  
    1.81      cxMapDestroy(map);
    1.82      EXPECT_TRUE(allocator.verify());
    1.83 -}
    1.84 \ No newline at end of file
    1.85 +}
    1.86 +
    1.87 +TEST(CxHashMap, StoreUcxStrings) {
    1.88 +    // create the map
    1.89 +    CxTestingAllocator allocator;
    1.90 +    auto map = cxHashMapCreate(&allocator, sizeof(cxstring), 8);
    1.91 +
    1.92 +    // define some strings
    1.93 +    cxstring s1 = CX_STR("this");
    1.94 +    cxstring s2 = CX_STR("is");
    1.95 +    cxstring s3 = CX_STR("a");
    1.96 +    cxstring s4 = CX_STR("test");
    1.97 +    cxstring s5 = CX_STR("setup");
    1.98 +
    1.99 +    // put them into the map
   1.100 +    cxMapPut(map, cx_hash_key_str("s1"), &s1);
   1.101 +    cxMapPut(map, cx_hash_key_str("s2"), &s2);
   1.102 +    cxMapPut(map, cx_hash_key_str("s3"), &s3);
   1.103 +    cxMapPut(map, cx_hash_key_str("s4"), &s4);
   1.104 +
   1.105 +    // overwrite a value
   1.106 +    cxMapPut(map, cx_hash_key_str("s1"), &s5);
   1.107 +
   1.108 +    // look up a string
   1.109 +    auto s3p = reinterpret_cast<cxstring *>(cxMapGet(map, cx_hash_key_str("s3")));
   1.110 +    EXPECT_EQ(s3p->length, s3.length);
   1.111 +    EXPECT_EQ(s3p->ptr, s3.ptr);
   1.112 +    EXPECT_NE(s3p, &s3);
   1.113 +
   1.114 +    // remove a string
   1.115 +    auto r = cxMapRemove(map, cx_hash_key_str("s2"));
   1.116 +    EXPECT_EQ(r, nullptr);
   1.117 +
   1.118 +    // iterate
   1.119 +    auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr};
   1.120 +    auto iter = cxMapIteratorValues(map);
   1.121 +    cx_foreach(cxstring*, s, iter) {
   1.122 +        auto found = std::find(ref.begin(), ref.end(), s->ptr);
   1.123 +        ASSERT_NE(found, ref.end());
   1.124 +        ref.erase(found);
   1.125 +    }
   1.126 +    EXPECT_EQ(ref.size(), 0);
   1.127 +
   1.128 +    cxMapDestroy(map);
   1.129 +    EXPECT_TRUE(allocator.verify());
   1.130 +}

mercurial