tests/test_kv_list.c

changeset 1386
748d0d40881e
parent 1384
a2cfbff39e5d
equal deleted inserted replaced
1385:4ebb27ff6f76 1386:748d0d40881e
854 cxIteratorNext(it); 854 cxIteratorNext(it);
855 CX_TEST_ASSERT(!cxIteratorValid(it)); 855 CX_TEST_ASSERT(!cxIteratorValid(it));
856 856
857 // remove the first element (which was skipped anyway) and try again 857 // remove the first element (which was skipped anyway) and try again
858 cxListRemove(cxKvListAsList(map), 0); 858 cxListRemove(cxKvListAsList(map), 0);
859 it = cxMapIterator(map); 859 it = cxMapIteratorKeys(map);
860 CX_TEST_ASSERT(it.elem_count == 3); 860 CX_TEST_ASSERT(it.elem_count == 3);
861 CX_TEST_ASSERT(it.elem_size == sizeof(CxMapEntry)); 861 CX_TEST_ASSERT(it.elem_size == sizeof(CxHashKey));
862 862
863 CX_TEST_ASSERT(cxIteratorValid(it)); 863 CX_TEST_ASSERT(cxIteratorValid(it));
864 CX_TEST_ASSERT(it.index == 0); 864 CX_TEST_ASSERT(it.index == 0);
865 entry = cxIteratorCurrent(it); 865 CxHashKey *key = cxIteratorCurrent(it);
866 CX_TEST_ASSERT(*(int*)entry->value == 815); 866 CX_TEST_ASSERT(key->len == 3);
867 CX_TEST_ASSERT(entry->key->len == 3); 867 CX_TEST_ASSERT(strncmp(key->data, "xyz", 3) == 0);
868 CX_TEST_ASSERT(strncmp(entry->key->data, "xyz", 3) == 0); 868 }
869 cxMapFree(map);
870 }
871
872 CX_TEST(test_kv_list_map_iterator_remove) {
873 CxMap *map = cxKvListCreateAsMapSimple(CX_STORE_POINTERS);
874 int x, y, z;
875 CX_TEST_DO {
876 cxDefineDestructor(map, kv_list_test_destr);
877 x = 815;
878 cxMapPut(map, "xyz", &x);
879 y = 8016;
880 cxMapPut(map, "abcd", &y);
881 z = 80017;
882 cxMapPut(map, "efghi", &z);
883
884 kv_list_test_destr_val = 0;
885 CxMapIterator iter = cxMapMutIteratorValues(map);
886 cx_foreach(int *, elem, iter) {
887 if (*elem == 8016) {
888 cxIteratorFlagRemoval(iter);
889 }
890 }
891
892 CX_TEST_ASSERT(kv_list_test_destr_val == 8016);
893 CX_TEST_ASSERT(cxMapSize(map) == 2);
894 CX_TEST_ASSERT(cxMapGet(map, "abcd") == NULL);
895 CxList *list = cxKvListAsList(map);
896 CX_TEST_ASSERT(cxListSize(list) == 2);
897 CX_TEST_ASSERT(*(int*)cxListAt(list, 0) == 815);
898 CX_TEST_ASSERT(*(int*)cxListAt(list, 1) == 80017);
899 }
900 cxMapFree(map);
901 }
902
903 CX_TEST(test_kv_list_list_iterator_remove) {
904 CxMap *map = cxKvListCreateAsMapSimple(sizeof(int));
905 CX_TEST_DO {
906 cxDefineAdvancedDestructor(map, kv_list_test_destr2, (void*)0xf00);
907 int x;
908 x = 815;
909 cxMapPut(map, "xyz", &x);
910 x = 8016;
911 cxMapPut(map, "abcd", &x);
912 x = 80017;
913 cxMapPut(map, "efghi", &x);
914
915 CxList *list = cxKvListAsList(map);
916 kv_list_test_destr2_val = 0;
917 kv_list_test_destr2_extra = NULL;
918 CxIterator iter = cxListMutIterator(list);
919 cx_foreach(int *, elem, iter) {
920 if (*elem == 8016) {
921 cxIteratorFlagRemoval(iter);
922 }
923 }
924
925 CX_TEST_ASSERT(kv_list_test_destr2_val == 8016);
926 CX_TEST_ASSERT(kv_list_test_destr2_extra == (void*)0xf00);
927 CX_TEST_ASSERT(cxMapSize(map) == 2);
928 CX_TEST_ASSERT(cxMapGet(map, "abcd") == NULL);
929 CX_TEST_ASSERT(cxListSize(list) == 2);
930 CX_TEST_ASSERT(*(int*)cxMapGet(map, "xyz") == 815);
931 CX_TEST_ASSERT(*(int*)cxMapGet(map, "efghi") == 80017);
869 } 932 }
870 cxMapFree(map); 933 cxMapFree(map);
871 } 934 }
872 935
873 CxTestSuite *cx_test_suite_kv_list_specifics(void) { 936 CxTestSuite *cx_test_suite_kv_list_specifics(void) {
905 cx_test_register(suite, test_kv_list_list_clear_destr_in_map); 968 cx_test_register(suite, test_kv_list_list_clear_destr_in_map);
906 cx_test_register(suite, test_kv_list_map_clear_destr_in_list); 969 cx_test_register(suite, test_kv_list_map_clear_destr_in_list);
907 cx_test_register(suite, test_kv_list_map_clear_destr_in_map); 970 cx_test_register(suite, test_kv_list_map_clear_destr_in_map);
908 cx_test_register(suite, test_kv_list_destr_ptr); 971 cx_test_register(suite, test_kv_list_destr_ptr);
909 cx_test_register(suite, test_kv_list_map_iterator); 972 cx_test_register(suite, test_kv_list_map_iterator);
973 cx_test_register(suite, test_kv_list_map_iterator_remove);
974 cx_test_register(suite, test_kv_list_list_iterator_remove);
910 975
911 return suite; 976 return suite;
912 } 977 }

mercurial