tests/test_hash_map.c

changeset 1638
14ae6a039af7
parent 1635
4983b6a34996
equal deleted inserted replaced
1637:34c2e1436945 1638:14ae6a039af7
1271 cxListFree(kl2); 1271 cxListFree(kl2);
1272 cxMapFree(d1); 1272 cxMapFree(d1);
1273 cxMapFree(d2); 1273 cxMapFree(d2);
1274 } 1274 }
1275 1275
1276 static CX_TEST_SUBROUTINE(test_hash_map_compare_verify, CxMap *map1, CxMap *map2) {
1277 // some ints we can point to in the pointer map
1278 int x13 = 13;
1279 int x15 = 15;
1280 int x42 = 42;
1281 int x1337 = 1337;
1282 int x4711 = 4711;
1283 int z13 = 13;
1284 int z15 = 15;
1285 int z42 = 42;
1286 int z1337 = 1337;
1287 int z4711 = 4711;
1288 int *y13;
1289 int *y15;
1290 int *y42;
1291 int *y1337;
1292 int *y4711;
1293 if (map1->collection.simple_cmp == cx_cmp_ptr) {
1294 // for the defaulted compare func, the pointers must be exactly equal
1295 y13 = &x13;
1296 y15 = &x15;
1297 y42 = &x42;
1298 y1337 = &x1337;
1299 y4711 = &x4711;
1300 } else {
1301 y13 = &z13;
1302 y15 = &z15;
1303 y42 = &z42;
1304 y1337 = &z1337;
1305 y4711 = &z4711;
1306 }
1307
1308 // empty maps are equal
1309 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1310 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1311
1312 // left has fewer keys than right
1313 cxMapPut(map1, "first key", &x13);
1314 cxMapPut(map1, "second key", &x15);
1315 cxMapPut(map2, "first key", y13);
1316 cxMapPut(map2, "second key", y15);
1317 cxMapPut(map2, "third key", y42);
1318 CX_TEST_ASSERT(cxMapCompare(map1, map2) < 0);
1319 CX_TEST_ASSERT(cxMapCompare(map2, map1) > 0);
1320
1321 // both are equal
1322 cxMapPut(map1, "third key", &x42);
1323 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1324 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1325
1326 // left has more keys than right
1327 cxMapPut(map1, "fourth key", &x1337);
1328 CX_TEST_ASSERT(cxMapCompare(map1, map2) > 0);
1329 CX_TEST_ASSERT(cxMapCompare(map2, map1) < 0);
1330
1331 // key sets differ
1332 cxMapPut(map2, "wrong key", y1337);
1333 CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0);
1334 CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0);
1335
1336 // values differ
1337 cxMapRemove(map2, "wrong key");
1338 cxMapPut(map2, "fourth key", y4711);
1339 CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0);
1340 CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0);
1341
1342 // equal again (by overwriting value in map 1)
1343 cxMapPut(map1, "fourth key", &x4711);
1344 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1345 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1346 }
1347
1276 CX_TEST(test_hash_map_compare) { 1348 CX_TEST(test_hash_map_compare) {
1277 CxMap *map1 = cxHashMapCreate(NULL, sizeof(int), 0); 1349 CxMap *map1 = cxHashMapCreate(NULL, sizeof(int), 0);
1278 CxMap *map2 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); 1350 CxMap *map2 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
1279 cxSetCompareFunc(map1, cx_cmp_int); 1351 cxSetCompareFunc(map1, cx_cmp_int);
1280 cxSetCompareFunc(map2, cx_cmp_int); 1352 cxSetCompareFunc(map2, cx_cmp_int);
1281 1353 CX_TEST_DO {
1282 // some ints we can point to in the pointer map 1354 CX_TEST_CALL_SUBROUTINE(test_hash_map_compare_verify, map1, map2);
1283 int z13 = 13;
1284 int z15 = 15;
1285 int z42 = 42;
1286 int z1337 = 1337;
1287 int z4711 = 4711;
1288
1289 CX_TEST_DO {
1290 // empty maps are equal
1291 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1292 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1293
1294 // left has fewer keys than right
1295 cxMapPut(map1, "first key", &z13);
1296 cxMapPut(map1, "second key", &z15);
1297 cxMapPut(map2, "first key", &z13);
1298 cxMapPut(map2, "second key", &z15);
1299 cxMapPut(map2, "third key", &z42);
1300 CX_TEST_ASSERT(cxMapCompare(map1, map2) < 0);
1301 CX_TEST_ASSERT(cxMapCompare(map2, map1) > 0);
1302
1303 // both are equal
1304 cxMapPut(map1, "third key", &z42);
1305 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1306 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1307
1308 // left has more keys than right
1309 cxMapPut(map1, "fourth key", &z1337);
1310 CX_TEST_ASSERT(cxMapCompare(map1, map2) > 0);
1311 CX_TEST_ASSERT(cxMapCompare(map2, map1) < 0);
1312
1313 // key sets differ
1314 cxMapPut(map2, "wrong key", &z1337);
1315 CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0);
1316 CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0);
1317
1318 // values differ
1319 cxMapRemove(map2, "wrong key");
1320 cxMapPut(map2, "fourth key", &z4711);
1321 CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0);
1322 CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0);
1323
1324 // equal again (by overwriting value in map 1)
1325 cxMapPut(map1, "fourth key", &z4711);
1326 CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0);
1327 CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0);
1328 } 1355 }
1329 cxMapFree(map1); 1356 cxMapFree(map1);
1330 cxMapFree(map2); 1357 cxMapFree(map2);
1358 }
1359
1360 static int test_ccmp_int(const void *l, const void *r, void *c) {
1361 int *z = c;
1362 // return bullshit to make the test fail when c was not passed correctly
1363 if (z == NULL || *z != 1337) return -1;
1364 return cx_cmp_int(l, r);
1365 }
1366
1367 CX_TEST(test_hash_map_compare2) {
1368 CxMap *map1 = cxHashMapCreate(NULL, sizeof(int), 0);
1369 CxMap *map2 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
1370 int z = 1337;
1371 cxSetAdvancedCompareFunc(map1, test_ccmp_int, &z);
1372 cxSetAdvancedCompareFunc(map2, test_ccmp_int, &z);
1373 CX_TEST_DO {
1374 CX_TEST_CALL_SUBROUTINE(test_hash_map_compare_verify, map1, map2);
1375 }
1376 cxMapFree(map1);
1377 cxMapFree(map2);
1378 }
1379
1380 CX_TEST(test_hash_map_compare_default_cmp_funcs) {
1381 CxMap *map1 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
1382 CxMap *map2 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
1383 CxMap *map3 = cxHashMapCreate(NULL, sizeof(int), 0);
1384 CxMap *map4 = cxHashMapCreate(NULL, sizeof(int), 0);
1385 CX_TEST_DO {
1386 CX_TEST_CALL_SUBROUTINE(test_hash_map_compare_verify, map1, map2);
1387 CX_TEST_CALL_SUBROUTINE(test_hash_map_compare_verify, map3, map4);
1388 }
1389 cxMapFree(map1);
1390 cxMapFree(map2);
1391 cxMapFree(map3);
1392 cxMapFree(map4);
1331 } 1393 }
1332 1394
1333 CX_TEST(test_empty_map_size) { 1395 CX_TEST(test_empty_map_size) {
1334 CX_TEST_DO { 1396 CX_TEST_DO {
1335 CX_TEST_ASSERT(cxEmptyMap->collection.size == 0); 1397 CX_TEST_ASSERT(cxEmptyMap->collection.size == 0);
1695 cx_test_register(suite, test_hash_map_union); 1757 cx_test_register(suite, test_hash_map_union);
1696 cx_test_register(suite, test_hash_map_union_ptr); 1758 cx_test_register(suite, test_hash_map_union_ptr);
1697 cx_test_register(suite, test_hash_map_union_alloc_fail); 1759 cx_test_register(suite, test_hash_map_union_alloc_fail);
1698 cx_test_register(suite, test_hash_map_shallow_clones); 1760 cx_test_register(suite, test_hash_map_shallow_clones);
1699 cx_test_register(suite, test_hash_map_compare); 1761 cx_test_register(suite, test_hash_map_compare);
1762 cx_test_register(suite, test_hash_map_compare2);
1763 cx_test_register(suite, test_hash_map_compare_default_cmp_funcs);
1700 cx_test_register(suite, test_empty_map_no_ops); 1764 cx_test_register(suite, test_empty_map_no_ops);
1701 cx_test_register(suite, test_empty_map_size); 1765 cx_test_register(suite, test_empty_map_size);
1702 cx_test_register(suite, test_empty_map_get); 1766 cx_test_register(suite, test_empty_map_get);
1703 cx_test_register(suite, test_empty_map_iterator); 1767 cx_test_register(suite, test_empty_map_iterator);
1704 cx_test_register(suite, test_null_map_iterator); 1768 cx_test_register(suite, test_null_map_iterator);

mercurial