| 1059 } |
1059 } |
| 1060 cxMapFree(dst); |
1060 cxMapFree(dst); |
| 1061 cxMapFree(s1); |
1061 cxMapFree(s1); |
| 1062 cxMapFree(s2); |
1062 cxMapFree(s2); |
| 1063 cx_testing_allocator_destroy(&talloc); |
1063 cx_testing_allocator_destroy(&talloc); |
| |
1064 } |
| |
1065 |
| |
1066 CX_TEST(test_hash_map_union) { |
| |
1067 CxMap *s1 = cxHashMapCreateSimple(sizeof(int)); |
| |
1068 CxMap *s2 = cxHashMapCreateSimple(sizeof(int)); |
| |
1069 const char *s1_keys[] = {"k1", "k2", "k3", "k4"}; |
| |
1070 int s1_values[] = {1, 3, 4, 6}; |
| |
1071 const char *s2_keys[] = {"k4", "k5", "k2", "k6"}; |
| |
1072 int s2_values[] = {5, 9, 15, 23}; |
| |
1073 for (unsigned int i = 0 ; i < 4 ; i++) { |
| |
1074 cxMapPut(s1, s1_keys[i], &s1_values[i]); |
| |
1075 cxMapPut(s2, s2_keys[i], &s2_values[i]); |
| |
1076 } |
| |
1077 CX_TEST_DO { |
| |
1078 int c = 4; |
| |
1079 CX_TEST_ASSERT(0 == cxMapUnion(s1, s2, test_hash_map_clone_func, NULL, &c)); |
| |
1080 CX_TEST_ASSERT(cxMapSize(s1) == 6); |
| |
1081 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k1")) == 1); |
| |
1082 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k2")) == 3); |
| |
1083 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k3")) == 4); |
| |
1084 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k4")) == 6); |
| |
1085 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k5")) == 13); |
| |
1086 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k6")) == 27); |
| |
1087 } |
| |
1088 cxMapFree(s1); |
| |
1089 cxMapFree(s2); |
| |
1090 } |
| |
1091 |
| |
1092 CX_TEST(test_hash_map_union_ptr) { |
| |
1093 CxTestingAllocator talloc; |
| |
1094 cx_testing_allocator_init(&talloc); |
| |
1095 CxAllocator *allocator = &talloc.base; |
| |
1096 CxMap *dst = cxHashMapCreateSimple(CX_STORE_POINTERS); |
| |
1097 cxDefineAdvancedDestructor(dst, cxFree, allocator); |
| |
1098 |
| |
1099 CxMap *s1 = cxHashMapCreateSimple(sizeof(int)); |
| |
1100 CxMap *s2 = cxHashMapCreateSimple(sizeof(int)); |
| |
1101 const char *s1_keys[] = {"k1", "k2", "k3", "k4"}; |
| |
1102 int s1_values[] = {1, 3, 4, 6}; |
| |
1103 const char *s2_keys[] = {"k4", "k5", "k2", "k6"}; |
| |
1104 int s2_values[] = {5, 9, 15, 23}; |
| |
1105 for (unsigned int i = 0 ; i < 4 ; i++) { |
| |
1106 cxMapPut(s1, s1_keys[i], &s1_values[i]); |
| |
1107 cxMapPut(s2, s2_keys[i], &s2_values[i]); |
| |
1108 } |
| |
1109 CX_TEST_DO { |
| |
1110 int c = 4; |
| |
1111 CX_TEST_ASSERT(0 == cxMapClone(dst, s1, test_hash_map_clone_func, allocator, &c)); |
| |
1112 CX_TEST_ASSERT(0 == cxMapUnion(dst, s2, test_hash_map_clone_func, allocator, &c)); |
| |
1113 CX_TEST_ASSERT(cxMapSize(dst) == 6); |
| |
1114 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 5); |
| |
1115 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 7); |
| |
1116 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 8); |
| |
1117 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k4")) == 10); |
| |
1118 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k5")) == 13); |
| |
1119 CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k6")) == 27); |
| |
1120 CX_TEST_ASSERT(cx_testing_allocator_used(&talloc)); |
| |
1121 } |
| |
1122 cxMapFree(dst); |
| |
1123 cxMapFree(s1); |
| |
1124 cxMapFree(s2); |
| |
1125 cx_testing_allocator_destroy(&talloc); |
| |
1126 } |
| |
1127 |
| |
1128 CX_TEST(test_hash_map_union_alloc_fail) { |
| |
1129 CxMap *s1 = cxHashMapCreateSimple(sizeof(int)); |
| |
1130 CxMap *s2 = cxHashMapCreateSimple(sizeof(int)); |
| |
1131 const char *s1_keys[] = {"k1", "k2", "k3", "k4"}; |
| |
1132 int s1_values[] = {1, 3, 4, 6}; |
| |
1133 const char *s2_keys[] = {"k4", "k5", "k2", "k6"}; |
| |
1134 int s2_values[] = {5, 9, 15, 23}; |
| |
1135 for (unsigned int i = 0 ; i < 4 ; i++) { |
| |
1136 cxMapPut(s1, s1_keys[i], &s1_values[i]); |
| |
1137 cxMapPut(s2, s2_keys[i], &s2_values[i]); |
| |
1138 } |
| |
1139 CX_TEST_DO { |
| |
1140 int c = 4; |
| |
1141 test_hash_map_clone_func_max_enabled = true; |
| |
1142 test_hash_map_clone_func_max_clones = 1; |
| |
1143 CX_TEST_ASSERT(0 != cxMapUnion(s1, s2, test_hash_map_clone_func, NULL, &c)); |
| |
1144 test_hash_map_clone_func_max_enabled = false; |
| |
1145 CX_TEST_ASSERT(cxMapSize(s1) == 5); |
| |
1146 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k1")) == 1); |
| |
1147 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k2")) == 3); |
| |
1148 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k3")) == 4); |
| |
1149 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k4")) == 6); |
| |
1150 // the concrete element which is affected might change when the hash function changes |
| |
1151 CX_TEST_ASSERT(cxMapGet(s1, "k5") == NULL); |
| |
1152 CX_TEST_ASSERT(*((int*)cxMapGet(s1, "k6")) == 27); |
| |
1153 } |
| |
1154 cxMapFree(s1); |
| |
1155 cxMapFree(s2); |
| 1064 } |
1156 } |
| 1065 |
1157 |
| 1066 CX_TEST(test_empty_map_size) { |
1158 CX_TEST(test_empty_map_size) { |
| 1067 CX_TEST_DO { |
1159 CX_TEST_DO { |
| 1068 CX_TEST_ASSERT(cxEmptyMap->collection.size == 0); |
1160 CX_TEST_ASSERT(cxEmptyMap->collection.size == 0); |
| 1423 cx_test_register(suite, test_hash_map_list_intersection); |
1515 cx_test_register(suite, test_hash_map_list_intersection); |
| 1424 cx_test_register(suite, test_hash_map_intersection_alloc_fail); |
1516 cx_test_register(suite, test_hash_map_intersection_alloc_fail); |
| 1425 cx_test_register(suite, test_hash_map_list_intersection_alloc_fail); |
1517 cx_test_register(suite, test_hash_map_list_intersection_alloc_fail); |
| 1426 cx_test_register(suite, test_hash_map_intersection_non_empty_target); |
1518 cx_test_register(suite, test_hash_map_intersection_non_empty_target); |
| 1427 cx_test_register(suite, test_hash_map_list_intersection_non_empty_target); |
1519 cx_test_register(suite, test_hash_map_list_intersection_non_empty_target); |
| |
1520 cx_test_register(suite, test_hash_map_union); |
| |
1521 cx_test_register(suite, test_hash_map_union_ptr); |
| |
1522 cx_test_register(suite, test_hash_map_union_alloc_fail); |
| 1428 cx_test_register(suite, test_empty_map_no_ops); |
1523 cx_test_register(suite, test_empty_map_no_ops); |
| 1429 cx_test_register(suite, test_empty_map_size); |
1524 cx_test_register(suite, test_empty_map_size); |
| 1430 cx_test_register(suite, test_empty_map_get); |
1525 cx_test_register(suite, test_empty_map_get); |
| 1431 cx_test_register(suite, test_empty_map_iterator); |
1526 cx_test_register(suite, test_empty_map_iterator); |
| 1432 cx_test_register(suite, test_null_map_iterator); |
1527 cx_test_register(suite, test_null_map_iterator); |