tests/test_hash_map.c

changeset 1115
6db21dee4929
parent 1114
ad5eeb256242
equal deleted inserted replaced
1114:ad5eeb256242 1115:6db21dee4929
207 CX_TEST_ASSERT(map->collection.size == 3); 207 CX_TEST_ASSERT(map->collection.size == 3);
208 CX_TEST_ASSERT(0 == cx_strcmp(ret, cx_str("is"))); 208 CX_TEST_ASSERT(0 == cx_strcmp(ret, cx_str("is")));
209 209
210 // iterate 210 // iterate
211 bool s3found = false, s4found = false, s5found = false; 211 bool s3found = false, s4found = false, s5found = false;
212 CxIterator iter = cxMapIteratorValues(map); 212 CxMapIterator iter = cxMapIteratorValues(map);
213 cx_foreach(cxstring*, s, iter) { 213 cx_foreach(cxstring*, s, iter) {
214 s3found |= s3.ptr == s->ptr; 214 s3found |= s3.ptr == s->ptr;
215 s4found |= s4.ptr == s->ptr; 215 s4found |= s4.ptr == s->ptr;
216 s5found |= s5.ptr == s->ptr; 216 s5found |= s5.ptr == s->ptr;
217 } 217 }
235 cxMapPut(map, "key 3", (void *) "val 3"); 235 cxMapPut(map, "key 3", (void *) "val 3");
236 cxMapPut(map, "key 4", (void *) "val 4"); 236 cxMapPut(map, "key 4", (void *) "val 4");
237 cxMapPut(map, "key 5", (void *) "val 5"); 237 cxMapPut(map, "key 5", (void *) "val 5");
238 cxMapPut(map, "key 6", (void *) "val 6"); 238 cxMapPut(map, "key 6", (void *) "val 6");
239 239
240 CxIterator iter = cxMapMutIterator(map); 240 CxMapIterator iter = cxMapMutIterator(map);
241 cx_foreach(CxMapEntry*, entry, iter) { 241 cx_foreach(CxMapEntry*, entry, iter) {
242 if (((const char *)entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); 242 if (((const char *)entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter);
243 } 243 }
244 CX_TEST_ASSERT(map->collection.size == 3); 244 CX_TEST_ASSERT(map->collection.size == 3);
245 CX_TEST_ASSERT(iter.index == map->collection.size); 245 CX_TEST_ASSERT(iter.index == map->collection.size);
336 CX_TEST_ASSERT(0 == strcmp(v3, "val 3")); 336 CX_TEST_ASSERT(0 == strcmp(v3, "val 3"));
337 337
338 // now remove k1 via key iterator and k5 (val 5) via value iterator 338 // now remove k1 via key iterator and k5 (val 5) via value iterator
339 v1[0] = 'y'; // mark v1 and check that destr is not called for previous val 339 v1[0] = 'y'; // mark v1 and check that destr is not called for previous val
340 { 340 {
341 CxIterator iter = cxMapMutIteratorKeys(map); 341 CxMapIterator iter = cxMapMutIteratorKeys(map);
342 cx_foreach(CxHashKey*, key, iter) { 342 cx_foreach(CxHashKey*, key, iter) {
343 if (((char*)key->data)[4] == '1') cxIteratorFlagRemoval(iter); 343 if (((char*)key->data)[4] == '1') cxIteratorFlagRemoval(iter);
344 } 344 }
345 } 345 }
346 { 346 {
347 CxIterator iter = cxMapMutIteratorValues(map); 347 CxMapIterator iter = cxMapMutIteratorValues(map);
348 cx_foreach(struct test_destr_struct*, v, iter) { 348 cx_foreach(struct test_destr_struct*, v, iter) {
349 if (v->str[4] == '5') cxIteratorFlagRemoval(iter); 349 if (v->str[4] == '5') cxIteratorFlagRemoval(iter);
350 } 350 }
351 } 351 }
352 352
435 } 435 }
436 436
437 CX_TEST(test_empty_map_iterator) { 437 CX_TEST(test_empty_map_iterator) {
438 CxMap *map = cxEmptyMap; 438 CxMap *map = cxEmptyMap;
439 439
440 CxIterator it1 = cxMapIterator(map); 440 CxMapIterator it1 = cxMapIterator(map);
441 CxIterator it2 = cxMapIteratorValues(map); 441 CxMapIterator it2 = cxMapIteratorValues(map);
442 CxIterator it3 = cxMapIteratorKeys(map); 442 CxMapIterator it3 = cxMapIteratorKeys(map);
443 CxIterator it4 = cxMapMutIterator(map); 443 CxMapIterator it4 = cxMapMutIterator(map);
444 CxIterator it5 = cxMapMutIteratorValues(map); 444 CxMapIterator it5 = cxMapMutIteratorValues(map);
445 CxIterator it6 = cxMapMutIteratorKeys(map); 445 CxMapIterator it6 = cxMapMutIteratorKeys(map);
446 446
447 CX_TEST_DO { 447 CX_TEST_DO {
448 CX_TEST_ASSERT(!cxIteratorValid(it1)); 448 CX_TEST_ASSERT(!cxIteratorValid(it1));
449 CX_TEST_ASSERT(!cxIteratorValid(it2)); 449 CX_TEST_ASSERT(!cxIteratorValid(it2));
450 CX_TEST_ASSERT(!cxIteratorValid(it3)); 450 CX_TEST_ASSERT(!cxIteratorValid(it3));
605 CX_TEST_ASSERT(map->collection.size == test_map_reference_size()); 605 CX_TEST_ASSERT(map->collection.size == test_map_reference_size());
606 606
607 // verify key iterator 607 // verify key iterator
608 { 608 {
609 // collect the keys from the map iterator 609 // collect the keys from the map iterator
610 CxIterator keyiter = cxMapIteratorKeys(map); 610 CxMapIterator keyiter = cxMapIteratorKeys(map);
611 CX_TEST_ASSERT(keyiter.elem_size == sizeof(CxHashKey)); 611 CX_TEST_ASSERT(keyiter.elem_size == sizeof(CxHashKey));
612 CX_TEST_ASSERT(keyiter.elem_count == map->collection.size); 612 CX_TEST_ASSERT(keyiter.elem_count == map->collection.size);
613 CxHashKey *keys = calloc(map->collection.size, sizeof(CxHashKey)); 613 CxHashKey *keys = calloc(map->collection.size, sizeof(CxHashKey));
614 cx_foreach(CxHashKey*, elem, keyiter) { 614 cx_foreach(CxHashKey*, elem, keyiter) {
615 keys[keyiter.index] = *elem; 615 keys[keyiter.index] = *elem;
626 626
627 // verify value iterator 627 // verify value iterator
628 { 628 {
629 // by using that the values in our test data are unique strings 629 // by using that the values in our test data are unique strings
630 // we can re-use a similar approach as above 630 // we can re-use a similar approach as above
631 CxIterator valiter = cxMapIteratorValues(map); 631 CxMapIterator valiter = cxMapIteratorValues(map);
632 CX_TEST_ASSERT(valiter.elem_size == map->collection.elem_size); 632 CX_TEST_ASSERT(valiter.elem_size == map->collection.elem_size);
633 CX_TEST_ASSERT(valiter.elem_count == map->collection.size); 633 CX_TEST_ASSERT(valiter.elem_count == map->collection.size);
634 const char ** values = calloc(map->collection.size, sizeof(const char *)); 634 const char ** values = calloc(map->collection.size, sizeof(const char *));
635 cx_foreach(const char *, elem, valiter) { 635 cx_foreach(const char *, elem, valiter) {
636 values[valiter.index] = elem; 636 values[valiter.index] = elem;
650 free(values); 650 free(values);
651 } 651 }
652 652
653 // verify pair iterator 653 // verify pair iterator
654 { 654 {
655 CxIterator pairiter = cxMapIterator(map); 655 CxMapIterator pairiter = cxMapIterator(map);
656 CX_TEST_ASSERT(pairiter.elem_size == sizeof(CxMapEntry)); 656 CX_TEST_ASSERT(pairiter.elem_size == sizeof(CxMapEntry));
657 CX_TEST_ASSERT(pairiter.elem_count == map->collection.size); 657 CX_TEST_ASSERT(pairiter.elem_count == map->collection.size);
658 struct test_map_kv *pairs = calloc(map->collection.size, sizeof(struct test_map_kv)); 658 struct test_map_kv *pairs = calloc(map->collection.size, sizeof(struct test_map_kv));
659 cx_foreach(CxMapEntry*, entry, pairiter) { 659 cx_foreach(CxMapEntry*, entry, pairiter) {
660 const CxHashKey *key = entry->key; 660 const CxHashKey *key = entry->key;

mercurial