tests/test_hash_map.c

changeset 856
6bbbf219251d
parent 855
35bcb3216c0d
     1.1 --- a/tests/test_hash_map.c	Thu May 23 20:31:37 2024 +0200
     1.2 +++ b/tests/test_hash_map.c	Thu May 23 20:43:04 2024 +0200
     1.3 @@ -42,19 +42,19 @@
     1.4          for(size_t i = 0 ; i < hmap->bucket_count ; i++) {
     1.5              CX_TEST_ASSERT(hmap->buckets[i] == NULL);
     1.6          }
     1.7 -        CX_TEST_ASSERT(map->base.elem_size == 1);
     1.8 -        CX_TEST_ASSERT(map->base.size == 0);
     1.9 -        CX_TEST_ASSERT(map->base.allocator == allocator);
    1.10 -        CX_TEST_ASSERT(!map->base.store_pointer);
    1.11 -        CX_TEST_ASSERT(map->base.cmpfunc == NULL);
    1.12 -        CX_TEST_ASSERT(map->base.simple_destructor == NULL);
    1.13 -        CX_TEST_ASSERT(map->base.advanced_destructor == NULL);
    1.14 -        CX_TEST_ASSERT(map->base.destructor_data == NULL);
    1.15 +        CX_TEST_ASSERT(map->collection.elem_size == 1);
    1.16 +        CX_TEST_ASSERT(map->collection.size == 0);
    1.17 +        CX_TEST_ASSERT(map->collection.allocator == allocator);
    1.18 +        CX_TEST_ASSERT(!map->collection.store_pointer);
    1.19 +        CX_TEST_ASSERT(map->collection.cmpfunc == NULL);
    1.20 +        CX_TEST_ASSERT(map->collection.simple_destructor == NULL);
    1.21 +        CX_TEST_ASSERT(map->collection.advanced_destructor == NULL);
    1.22 +        CX_TEST_ASSERT(map->collection.destructor_data == NULL);
    1.23          cxMapStorePointers(map);
    1.24 -        CX_TEST_ASSERT(map->base.store_pointer);
    1.25 -        CX_TEST_ASSERT(map->base.elem_size == sizeof(void *));
    1.26 +        CX_TEST_ASSERT(map->collection.store_pointer);
    1.27 +        CX_TEST_ASSERT(map->collection.elem_size == sizeof(void *));
    1.28          cxMapStoreObjects(map);
    1.29 -        CX_TEST_ASSERT(!map->base.store_pointer);
    1.30 +        CX_TEST_ASSERT(!map->collection.store_pointer);
    1.31  
    1.32          cxMapDestroy(map);
    1.33          CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
    1.34 @@ -73,10 +73,10 @@
    1.35          for (size_t i = 0; i < hmap->bucket_count; i++) {
    1.36              CX_TEST_ASSERT(hmap->buckets[i] == NULL);
    1.37          }
    1.38 -        CX_TEST_ASSERT(map->base.size == 0);
    1.39 -        CX_TEST_ASSERT(map->base.allocator == allocator);
    1.40 -        CX_TEST_ASSERT(map->base.store_pointer);
    1.41 -        CX_TEST_ASSERT(map->base.elem_size == sizeof(void *));
    1.42 +        CX_TEST_ASSERT(map->collection.size == 0);
    1.43 +        CX_TEST_ASSERT(map->collection.allocator == allocator);
    1.44 +        CX_TEST_ASSERT(map->collection.store_pointer);
    1.45 +        CX_TEST_ASSERT(map->collection.elem_size == sizeof(void *));
    1.46  
    1.47          cxMapDestroy(map);
    1.48          CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
    1.49 @@ -106,7 +106,7 @@
    1.50          int result = cxMapRehash(map);
    1.51          CX_TEST_ASSERT(result == 0);
    1.52          CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 25);
    1.53 -        CX_TEST_ASSERT(map->base.size == 10);
    1.54 +        CX_TEST_ASSERT(map->collection.size == 10);
    1.55  
    1.56          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 1"), "val 1"));
    1.57          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 2"), "val 2"));
    1.58 @@ -161,11 +161,11 @@
    1.59          cxMapPut(map, "key 2", (void *) "val 2");
    1.60          cxMapPut(map, "key 3", (void *) "val 3");
    1.61  
    1.62 -        CX_TEST_ASSERT(map->base.size == 3);
    1.63 +        CX_TEST_ASSERT(map->collection.size == 3);
    1.64  
    1.65          cxMapClear(map);
    1.66  
    1.67 -        CX_TEST_ASSERT(map->base.size == 0);
    1.68 +        CX_TEST_ASSERT(map->collection.size == 0);
    1.69          CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL);
    1.70          CX_TEST_ASSERT(cxMapGet(map, "key 2") == NULL);
    1.71          CX_TEST_ASSERT(cxMapGet(map, "key 3") == NULL);
    1.72 @@ -208,7 +208,7 @@
    1.73  
    1.74          // remove a string
    1.75          cxMapRemove(map, "s2");
    1.76 -        CX_TEST_ASSERT(map->base.size == 3);
    1.77 +        CX_TEST_ASSERT(map->collection.size == 3);
    1.78  
    1.79          // iterate
    1.80          bool s3found = false, s4found = false, s5found = false;
    1.81 @@ -244,8 +244,8 @@
    1.82          cx_foreach(CxMapEntry*, entry, iter) {
    1.83              if (((char const *)entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter);
    1.84          }
    1.85 -        CX_TEST_ASSERT(map->base.size == 3);
    1.86 -        CX_TEST_ASSERT(iter.index == map->base.size);
    1.87 +        CX_TEST_ASSERT(map->collection.size == 3);
    1.88 +        CX_TEST_ASSERT(iter.index == map->collection.size);
    1.89  
    1.90          CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL);
    1.91          CX_TEST_ASSERT(cxMapGet(map, "key 2") != NULL);
    1.92 @@ -348,7 +348,7 @@
    1.93      CxAllocator *allocator = &talloc.base;
    1.94      CX_TEST_DO {
    1.95          CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
    1.96 -        map->base.simple_destructor = test_simple_destructor;
    1.97 +        map->collection.simple_destructor = test_simple_destructor;
    1.98          CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map);
    1.99          CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   1.100      }
   1.101 @@ -361,7 +361,7 @@
   1.102      CxAllocator *allocator = &talloc.base;
   1.103      CX_TEST_DO {
   1.104          CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
   1.105 -        map->base.advanced_destructor = test_advanced_destructor;
   1.106 +        map->collection.advanced_destructor = test_advanced_destructor;
   1.107          CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map);
   1.108          CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   1.109      }
   1.110 @@ -370,7 +370,8 @@
   1.111  
   1.112  CX_TEST(test_empty_map_size) {
   1.113      CX_TEST_DO {
   1.114 -        CX_TEST_ASSERT(cxEmptyMap->base.size == 0);
   1.115 +        CX_TEST_ASSERT(cxEmptyMap->collection.size == 0);
   1.116 +        CX_TEST_ASSERT(cxMapSize(cxEmptyMap) == 0);
   1.117      }
   1.118  }
   1.119  
   1.120 @@ -430,7 +431,7 @@
   1.121          cxMapPut(map, cx_mutstr("foo"), "bar");
   1.122          cxMapPut(map, cx_str("hallo"), "welt");
   1.123  
   1.124 -        CX_TEST_ASSERT(map->base.size == 3);
   1.125 +        CX_TEST_ASSERT(map->collection.size == 3);
   1.126          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "test"), "test"));
   1.127          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
   1.128          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt"));
   1.129 @@ -441,16 +442,16 @@
   1.130          cxMapDetach(map, hallo);
   1.131          cxMapPut(map, cx_hash_key_str("key"), "value");
   1.132  
   1.133 -        CX_TEST_ASSERT(map->base.size == 2);
   1.134 +        CX_TEST_ASSERT(map->collection.size == 2);
   1.135          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value"));
   1.136          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
   1.137  
   1.138          void *r;
   1.139          r = cxMapRemoveAndGet(map, "key");
   1.140          r = cxMapRemoveAndGet(map, cx_str("foo"));
   1.141 -        if (r != NULL) map->base.size = 47;
   1.142 +        if (r != NULL) map->collection.size = 47;
   1.143  
   1.144 -        CX_TEST_ASSERT(map->base.size == 0);
   1.145 +        CX_TEST_ASSERT(map->collection.size == 0);
   1.146  
   1.147          cxMapDestroy(map);
   1.148          CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   1.149 @@ -539,21 +540,21 @@
   1.150  
   1.151  static CX_TEST_SUBROUTINE(verify_map_contents, CxMap *map) {
   1.152      // verify that the reference map has same size (i.e. no other keys are mapped)
   1.153 -    CX_TEST_ASSERT(map->base.size == test_map_reference_size());
   1.154 +    CX_TEST_ASSERT(map->collection.size == test_map_reference_size());
   1.155  
   1.156      // verify key iterator
   1.157      {
   1.158          // collect the keys from the map iterator
   1.159          CxIterator keyiter = cxMapIteratorKeys(map);
   1.160          CX_TEST_ASSERT(keyiter.elem_size == sizeof(CxHashKey));
   1.161 -        CX_TEST_ASSERT(keyiter.elem_count == map->base.size);
   1.162 -        CxHashKey *keys = calloc(map->base.size, sizeof(CxHashKey));
   1.163 +        CX_TEST_ASSERT(keyiter.elem_count == map->collection.size);
   1.164 +        CxHashKey *keys = calloc(map->collection.size, sizeof(CxHashKey));
   1.165          cx_foreach(CxHashKey*, elem, keyiter) {
   1.166              keys[keyiter.index] = *elem;
   1.167          }
   1.168 -        CX_TEST_ASSERT(keyiter.index == map->base.size);
   1.169 +        CX_TEST_ASSERT(keyiter.index == map->collection.size);
   1.170          // verify that all keys are mapped to values in reference map
   1.171 -        for (size_t i = 0 ; i < map->base.size ; i++) {
   1.172 +        for (size_t i = 0 ; i < map->collection.size ; i++) {
   1.173              cxmutstr ksz = cx_strdup(cx_strn(keys[i].data, keys[i].len));
   1.174              CX_TEST_ASSERT(test_map_reference_get(ksz.ptr) != NULL);
   1.175              cx_strfree(&ksz);
   1.176 @@ -566,15 +567,15 @@
   1.177          // by using that the values in our test data are unique strings
   1.178          // we can re-use a similar approach as above
   1.179          CxIterator valiter = cxMapIteratorValues(map);
   1.180 -        CX_TEST_ASSERT(valiter.elem_size == map->base.elem_size);
   1.181 -        CX_TEST_ASSERT(valiter.elem_count == map->base.size);
   1.182 -        char const** values = calloc(map->base.size, sizeof(char const*));
   1.183 +        CX_TEST_ASSERT(valiter.elem_size == map->collection.elem_size);
   1.184 +        CX_TEST_ASSERT(valiter.elem_count == map->collection.size);
   1.185 +        char const** values = calloc(map->collection.size, sizeof(char const*));
   1.186          cx_foreach(char const*, elem, valiter) {
   1.187              values[valiter.index] = elem;
   1.188          }
   1.189 -        CX_TEST_ASSERT(valiter.index == map->base.size);
   1.190 +        CX_TEST_ASSERT(valiter.index == map->collection.size);
   1.191          // verify that all values are present in the reference map
   1.192 -        for (size_t i = 0 ; i < map->base.size ; i++) {
   1.193 +        for (size_t i = 0 ; i < map->collection.size ; i++) {
   1.194              bool found = false;
   1.195              for (size_t j = 0; j < test_map_reference_len ; j++) {
   1.196                  if (test_map_reference[j].value == values[i]) {
   1.197 @@ -591,16 +592,16 @@
   1.198      {
   1.199          CxIterator pairiter = cxMapIterator(map);
   1.200          CX_TEST_ASSERT(pairiter.elem_size == sizeof(CxMapEntry));
   1.201 -        CX_TEST_ASSERT(pairiter.elem_count == map->base.size);
   1.202 -        struct test_map_kv *pairs = calloc(map->base.size, sizeof(struct test_map_kv));
   1.203 +        CX_TEST_ASSERT(pairiter.elem_count == map->collection.size);
   1.204 +        struct test_map_kv *pairs = calloc(map->collection.size, sizeof(struct test_map_kv));
   1.205          cx_foreach(CxMapEntry*, entry, pairiter) {
   1.206              CxHashKey const *key = entry->key;
   1.207              pairs[pairiter.index].key = cx_strdup(cx_strn(key->data, key->len)).ptr;
   1.208              pairs[pairiter.index].value = entry->value;
   1.209          }
   1.210 -        CX_TEST_ASSERT(pairiter.index == map->base.size);
   1.211 +        CX_TEST_ASSERT(pairiter.index == map->collection.size);
   1.212          // verify that all pairs are present in the reference map
   1.213 -        for (size_t i = 0 ; i < map->base.size ; i++) {
   1.214 +        for (size_t i = 0 ; i < map->collection.size ; i++) {
   1.215              CX_TEST_ASSERT(test_map_reference_get(pairs[i].key) == pairs[i].value);
   1.216              // this was strdup'ed
   1.217              free((void*)pairs[i].key);

mercurial