tests/test_hash_map.c

changeset 854
fe0d69d72bcd
parent 853
d4baf4dd55c3
child 855
35bcb3216c0d
     1.1 --- a/tests/test_hash_map.c	Thu May 23 19:29:14 2024 +0200
     1.2 +++ b/tests/test_hash_map.c	Thu May 23 20:29:28 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->item_size == 1);
     1.8 -        CX_TEST_ASSERT(map->size == 0);
     1.9 -        CX_TEST_ASSERT(map->allocator == allocator);
    1.10 -        CX_TEST_ASSERT(!map->store_pointer);
    1.11 -        CX_TEST_ASSERT(map->cmpfunc == NULL);
    1.12 -        CX_TEST_ASSERT(map->simple_destructor == NULL);
    1.13 -        CX_TEST_ASSERT(map->advanced_destructor == NULL);
    1.14 -        CX_TEST_ASSERT(map->destructor_data == NULL);
    1.15 +        CX_TEST_ASSERT(map->base.item_size == 1);
    1.16 +        CX_TEST_ASSERT(map->base.size == 0);
    1.17 +        CX_TEST_ASSERT(map->base.allocator == allocator);
    1.18 +        CX_TEST_ASSERT(!map->base.store_pointer);
    1.19 +        CX_TEST_ASSERT(map->base.cmpfunc == NULL);
    1.20 +        CX_TEST_ASSERT(map->base.simple_destructor == NULL);
    1.21 +        CX_TEST_ASSERT(map->base.advanced_destructor == NULL);
    1.22 +        CX_TEST_ASSERT(map->base.destructor_data == NULL);
    1.23          cxMapStorePointers(map);
    1.24 -        CX_TEST_ASSERT(map->store_pointer);
    1.25 -        CX_TEST_ASSERT(map->item_size == sizeof(void *));
    1.26 +        CX_TEST_ASSERT(map->base.store_pointer);
    1.27 +        CX_TEST_ASSERT(map->base.item_size == sizeof(void *));
    1.28          cxMapStoreObjects(map);
    1.29 -        CX_TEST_ASSERT(!map->store_pointer);
    1.30 +        CX_TEST_ASSERT(!map->base.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->size == 0);
    1.39 -        CX_TEST_ASSERT(map->allocator == allocator);
    1.40 -        CX_TEST_ASSERT(map->store_pointer);
    1.41 -        CX_TEST_ASSERT(map->item_size == sizeof(void *));
    1.42 +        CX_TEST_ASSERT(map->base.size == 0);
    1.43 +        CX_TEST_ASSERT(map->base.allocator == allocator);
    1.44 +        CX_TEST_ASSERT(map->base.store_pointer);
    1.45 +        CX_TEST_ASSERT(map->base.item_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->size == 10);
    1.54 +        CX_TEST_ASSERT(map->base.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->size == 3);
    1.63 +        CX_TEST_ASSERT(map->base.size == 3);
    1.64  
    1.65          cxMapClear(map);
    1.66  
    1.67 -        CX_TEST_ASSERT(map->size == 0);
    1.68 +        CX_TEST_ASSERT(map->base.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->size == 3);
    1.77 +        CX_TEST_ASSERT(map->base.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->size == 3);
    1.86 -        CX_TEST_ASSERT(iter.index == map->size);
    1.87 +        CX_TEST_ASSERT(map->base.size == 3);
    1.88 +        CX_TEST_ASSERT(iter.index == map->base.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->simple_destructor = test_simple_destructor;
    1.97 +        map->base.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->advanced_destructor = test_advanced_destructor;
   1.106 +        map->base.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,7 @@
   1.111  
   1.112  CX_TEST(test_empty_map_size) {
   1.113      CX_TEST_DO {
   1.114 -        CX_TEST_ASSERT(cxEmptyMap->size == 0);
   1.115 +        CX_TEST_ASSERT(cxEmptyMap->base.size == 0);
   1.116      }
   1.117  }
   1.118  
   1.119 @@ -430,7 +430,7 @@
   1.120          cxMapPut(map, cx_mutstr("foo"), "bar");
   1.121          cxMapPut(map, cx_str("hallo"), "welt");
   1.122  
   1.123 -        CX_TEST_ASSERT(map->size == 3);
   1.124 +        CX_TEST_ASSERT(map->base.size == 3);
   1.125          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "test"), "test"));
   1.126          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
   1.127          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt"));
   1.128 @@ -441,16 +441,16 @@
   1.129          cxMapDetach(map, hallo);
   1.130          cxMapPut(map, cx_hash_key_str("key"), "value");
   1.131  
   1.132 -        CX_TEST_ASSERT(map->size == 2);
   1.133 +        CX_TEST_ASSERT(map->base.size == 2);
   1.134          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value"));
   1.135          CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
   1.136  
   1.137          void *r;
   1.138          r = cxMapRemoveAndGet(map, "key");
   1.139          r = cxMapRemoveAndGet(map, cx_str("foo"));
   1.140 -        if (r != NULL) map->size = 47;
   1.141 +        if (r != NULL) map->base.size = 47;
   1.142  
   1.143 -        CX_TEST_ASSERT(map->size == 0);
   1.144 +        CX_TEST_ASSERT(map->base.size == 0);
   1.145  
   1.146          cxMapDestroy(map);
   1.147          CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
   1.148 @@ -539,21 +539,21 @@
   1.149  
   1.150  static CX_TEST_SUBROUTINE(verify_map_contents, CxMap *map) {
   1.151      // verify that the reference map has same size (i.e. no other keys are mapped)
   1.152 -    CX_TEST_ASSERT(map->size == test_map_reference_size());
   1.153 +    CX_TEST_ASSERT(map->base.size == test_map_reference_size());
   1.154  
   1.155      // verify key iterator
   1.156      {
   1.157          // collect the keys from the map iterator
   1.158          CxIterator keyiter = cxMapIteratorKeys(map);
   1.159          CX_TEST_ASSERT(keyiter.elem_size == sizeof(CxHashKey));
   1.160 -        CX_TEST_ASSERT(keyiter.elem_count == map->size);
   1.161 -        CxHashKey *keys = calloc(map->size, sizeof(CxHashKey));
   1.162 +        CX_TEST_ASSERT(keyiter.elem_count == map->base.size);
   1.163 +        CxHashKey *keys = calloc(map->base.size, sizeof(CxHashKey));
   1.164          cx_foreach(CxHashKey*, elem, keyiter) {
   1.165              keys[keyiter.index] = *elem;
   1.166          }
   1.167 -        CX_TEST_ASSERT(keyiter.index == map->size);
   1.168 +        CX_TEST_ASSERT(keyiter.index == map->base.size);
   1.169          // verify that all keys are mapped to values in reference map
   1.170 -        for (size_t i = 0 ; i < map->size ; i++) {
   1.171 +        for (size_t i = 0 ; i < map->base.size ; i++) {
   1.172              cxmutstr ksz = cx_strdup(cx_strn(keys[i].data, keys[i].len));
   1.173              CX_TEST_ASSERT(test_map_reference_get(ksz.ptr) != NULL);
   1.174              cx_strfree(&ksz);
   1.175 @@ -566,15 +566,15 @@
   1.176          // by using that the values in our test data are unique strings
   1.177          // we can re-use a similar approach as above
   1.178          CxIterator valiter = cxMapIteratorValues(map);
   1.179 -        CX_TEST_ASSERT(valiter.elem_size == map->item_size);
   1.180 -        CX_TEST_ASSERT(valiter.elem_count == map->size);
   1.181 -        char const** values = calloc(map->size, sizeof(char const*));
   1.182 +        CX_TEST_ASSERT(valiter.elem_size == map->base.item_size);
   1.183 +        CX_TEST_ASSERT(valiter.elem_count == map->base.size);
   1.184 +        char const** values = calloc(map->base.size, sizeof(char const*));
   1.185          cx_foreach(char const*, elem, valiter) {
   1.186              values[valiter.index] = elem;
   1.187          }
   1.188 -        CX_TEST_ASSERT(valiter.index == map->size);
   1.189 +        CX_TEST_ASSERT(valiter.index == map->base.size);
   1.190          // verify that all values are present in the reference map
   1.191 -        for (size_t i = 0 ; i < map->size ; i++) {
   1.192 +        for (size_t i = 0 ; i < map->base.size ; i++) {
   1.193              bool found = false;
   1.194              for (size_t j = 0; j < test_map_reference_len ; j++) {
   1.195                  if (test_map_reference[j].value == values[i]) {
   1.196 @@ -591,16 +591,16 @@
   1.197      {
   1.198          CxIterator pairiter = cxMapIterator(map);
   1.199          CX_TEST_ASSERT(pairiter.elem_size == sizeof(CxMapEntry));
   1.200 -        CX_TEST_ASSERT(pairiter.elem_count == map->size);
   1.201 -        struct test_map_kv *pairs = calloc(map->size, sizeof(struct test_map_kv));
   1.202 +        CX_TEST_ASSERT(pairiter.elem_count == map->base.size);
   1.203 +        struct test_map_kv *pairs = calloc(map->base.size, sizeof(struct test_map_kv));
   1.204          cx_foreach(CxMapEntry*, entry, pairiter) {
   1.205              CxHashKey const *key = entry->key;
   1.206              pairs[pairiter.index].key = cx_strdup(cx_strn(key->data, key->len)).ptr;
   1.207              pairs[pairiter.index].value = entry->value;
   1.208          }
   1.209 -        CX_TEST_ASSERT(pairiter.index == map->size);
   1.210 +        CX_TEST_ASSERT(pairiter.index == map->base.size);
   1.211          // verify that all pairs are present in the reference map
   1.212 -        for (size_t i = 0 ; i < map->size ; i++) {
   1.213 +        for (size_t i = 0 ; i < map->base.size ; i++) {
   1.214              CX_TEST_ASSERT(test_map_reference_get(pairs[i].key) == pairs[i].value);
   1.215              // this was strdup'ed
   1.216              free((void*)pairs[i].key);

mercurial