tests/test_kv_list.c

changeset 1389
bbdc4aee8534
parent 1386
748d0d40881e
equal deleted inserted replaced
1388:edc34e904fe3 1389:bbdc4aee8534
418 418
419 CX_TEST_ASSERT(0 == cxMapRemove(map, "xyz")); 419 CX_TEST_ASSERT(0 == cxMapRemove(map, "xyz"));
420 CX_TEST_ASSERT(cxMapGet(map, "xyz") == NULL); 420 CX_TEST_ASSERT(cxMapGet(map, "xyz") == NULL);
421 421
422 CX_TEST_ASSERT(cxListSize(list) == 0); 422 CX_TEST_ASSERT(cxListSize(list) == 0);
423
424 // now check with a non-prehashed key
425 x = 85;
426 cxListAdd(list, &x);
427 CxHashKey key = {};
428 key.data = "abc";
429 key.len = 3;
430 CX_TEST_ASSERT(0 == cxKvListSetKey(list, 0, key));
431 y = cxMapGet(map, "abc");
432 CX_TEST_ASSERT(y != NULL);
433 CX_TEST_ASSERT(*y == 85);
434
435 CX_TEST_ASSERT(0 == cxMapRemove(map, "abc"));
436 CX_TEST_ASSERT(cxMapGet(map, "abc") == NULL);
437
438 CX_TEST_ASSERT(cxListSize(list) == 0);
423 } 439 }
424 cxListFree(list); 440 cxListFree(list);
425 } 441 }
426 442
427 CX_TEST(test_kv_list_set_key_already_exists) { 443 CX_TEST(test_kv_list_set_key_already_exists) {
514 CX_TEST_ASSERT(*y == 47); 530 CX_TEST_ASSERT(*y == 47);
515 531
516 y = cxMapGet(map, "efg"); 532 y = cxMapGet(map, "efg");
517 CX_TEST_ASSERT(y != NULL); 533 CX_TEST_ASSERT(y != NULL);
518 CX_TEST_ASSERT(*y == 1337); 534 CX_TEST_ASSERT(*y == 1337);
535
536 // idempotence
537 CX_TEST_ASSERT(0 == cxKvListRemoveKey(list, 1));
519 538
520 // index out of bounds: 539 // index out of bounds:
521 CX_TEST_ASSERT(0 != cxKvListRemoveKey(list, 3)); 540 CX_TEST_ASSERT(0 != cxKvListRemoveKey(list, 3));
522 } 541 }
523 cxListFree(list); 542 cxListFree(list);
863 CX_TEST_ASSERT(cxIteratorValid(it)); 882 CX_TEST_ASSERT(cxIteratorValid(it));
864 CX_TEST_ASSERT(it.index == 0); 883 CX_TEST_ASSERT(it.index == 0);
865 CxHashKey *key = cxIteratorCurrent(it); 884 CxHashKey *key = cxIteratorCurrent(it);
866 CX_TEST_ASSERT(key->len == 3); 885 CX_TEST_ASSERT(key->len == 3);
867 CX_TEST_ASSERT(strncmp(key->data, "xyz", 3) == 0); 886 CX_TEST_ASSERT(strncmp(key->data, "xyz", 3) == 0);
887 }
888 cxMapFree(map);
889 }
890
891 CX_TEST(test_kv_list_map_iterator_no_keys) {
892 CxMap *map = cxKvListCreateAsMapSimple(sizeof(int));
893 CX_TEST_DO {
894 CxList *list = cxKvListAsList(map);
895 int x;
896 x = 815;
897 cxListAdd(list, &x);
898 x = 8016;
899 cxListAdd(list, &x);
900 x = 80017;
901 cxMapPut(map, "xyz", &x);
902 cxKvListRemoveKey(list, 2);
903
904 // only contains items without keys
905 CxMapIterator it = cxMapIterator(map);
906 CX_TEST_ASSERT(it.elem_count == 0);
907 CX_TEST_ASSERT(!cxIteratorValid(it));
868 } 908 }
869 cxMapFree(map); 909 cxMapFree(map);
870 } 910 }
871 911
872 CX_TEST(test_kv_list_map_iterator_remove) { 912 CX_TEST(test_kv_list_map_iterator_remove) {
968 cx_test_register(suite, test_kv_list_list_clear_destr_in_map); 1008 cx_test_register(suite, test_kv_list_list_clear_destr_in_map);
969 cx_test_register(suite, test_kv_list_map_clear_destr_in_list); 1009 cx_test_register(suite, test_kv_list_map_clear_destr_in_list);
970 cx_test_register(suite, test_kv_list_map_clear_destr_in_map); 1010 cx_test_register(suite, test_kv_list_map_clear_destr_in_map);
971 cx_test_register(suite, test_kv_list_destr_ptr); 1011 cx_test_register(suite, test_kv_list_destr_ptr);
972 cx_test_register(suite, test_kv_list_map_iterator); 1012 cx_test_register(suite, test_kv_list_map_iterator);
1013 cx_test_register(suite, test_kv_list_map_iterator_no_keys);
973 cx_test_register(suite, test_kv_list_map_iterator_remove); 1014 cx_test_register(suite, test_kv_list_map_iterator_remove);
974 cx_test_register(suite, test_kv_list_list_iterator_remove); 1015 cx_test_register(suite, test_kv_list_list_iterator_remove);
975 1016
976 return suite; 1017 return suite;
977 } 1018 }

mercurial