tests/test_hash_map.c

changeset 1579
0393c67556ec
parent 1510
89cf6b4a5792
child 1582
32b82c424252
equal deleted inserted replaced
1578:fb73c4d69317 1579:0393c67556ec
1259 cxMapFree(c); 1259 cxMapFree(c);
1260 cxListFree(kl1); 1260 cxListFree(kl1);
1261 cxListFree(kl2); 1261 cxListFree(kl2);
1262 cxMapFree(d1); 1262 cxMapFree(d1);
1263 cxMapFree(d2); 1263 cxMapFree(d2);
1264 }
1265
1266 CX_TEST(test_hash_map_compare) {
1267 CxMap *map1 = cxHashMapCreateSimple(sizeof(int));
1268 CxMap *map2 = cxHashMapCreateSimple(CX_STORE_POINTERS);
1269 // TODO: fix specification of compare function once #622 is realized
1270 map1->collection.cmpfunc = cx_cmp_int;
1271 map2->collection.cmpfunc = cx_cmp_int;
1272
1273 // some ints we can point to in the pointer map
1274 int z13 = 13;
1275 int z15 = 15;
1276 int z42 = 42;
1277 int z1337 = 1337;
1278 int z4711 = 4711;
1279
1280 CX_TEST_DO {
1281 // empty maps are equal
1282 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1283 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1284
1285 // left has fewer keys than right
1286 cxMapPut(map1, "first key", &z13);
1287 cxMapPut(map1, "second key", &z15);
1288 cxMapPut(map2, "first key", &z13);
1289 cxMapPut(map2, "second key", &z15);
1290 cxMapPut(map2, "third key", &z42);
1291 CX_TEST_ASSERT(cxMapCompare(map1, map2) < 0);
1292 CX_TEST_ASSERT(cxMapCompare(map2, map1) > 0);
1293
1294 // both are equal
1295 cxMapPut(map1, "third key", &z42);
1296 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1297 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1298
1299 // left has more keys than right
1300 cxMapPut(map1, "fourth key", &z1337);
1301 CX_TEST_ASSERT(cxMapCompare(map1, map2) > 0);
1302 CX_TEST_ASSERT(cxMapCompare(map2, map1) < 0);
1303
1304 // key sets differ
1305 cxMapPut(map2, "wrong key", &z1337);
1306 CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0);
1307 CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0);
1308
1309 // values differ
1310 cxMapRemove(map2, "wrong key");
1311 cxMapPut(map2, "fourth key", &z4711);
1312 CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0);
1313 CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0);
1314
1315 // equal again (by overwriting value in map 1)
1316 cxMapPut(map1, "fourth key", &z4711);
1317 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1318 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1319 }
1320 cxMapFree(map1);
1321 cxMapFree(map2);
1264 } 1322 }
1265 1323
1266 CX_TEST(test_empty_map_size) { 1324 CX_TEST(test_empty_map_size) {
1267 CX_TEST_DO { 1325 CX_TEST_DO {
1268 CX_TEST_ASSERT(cxEmptyMap->collection.size == 0); 1326 CX_TEST_ASSERT(cxEmptyMap->collection.size == 0);
1627 cx_test_register(suite, test_hash_map_list_intersection_non_empty_target); 1685 cx_test_register(suite, test_hash_map_list_intersection_non_empty_target);
1628 cx_test_register(suite, test_hash_map_union); 1686 cx_test_register(suite, test_hash_map_union);
1629 cx_test_register(suite, test_hash_map_union_ptr); 1687 cx_test_register(suite, test_hash_map_union_ptr);
1630 cx_test_register(suite, test_hash_map_union_alloc_fail); 1688 cx_test_register(suite, test_hash_map_union_alloc_fail);
1631 cx_test_register(suite, test_hash_map_simple_clones); 1689 cx_test_register(suite, test_hash_map_simple_clones);
1690 cx_test_register(suite, test_hash_map_compare);
1632 cx_test_register(suite, test_empty_map_no_ops); 1691 cx_test_register(suite, test_empty_map_no_ops);
1633 cx_test_register(suite, test_empty_map_size); 1692 cx_test_register(suite, test_empty_map_size);
1634 cx_test_register(suite, test_empty_map_get); 1693 cx_test_register(suite, test_empty_map_get);
1635 cx_test_register(suite, test_empty_map_iterator); 1694 cx_test_register(suite, test_empty_map_iterator);
1636 cx_test_register(suite, test_null_map_iterator); 1695 cx_test_register(suite, test_null_map_iterator);

mercurial