tests/test_map.cpp

changeset 686
64919f63f059
parent 685
2dd841e364af
child 687
612ed521b1c5
equal deleted inserted replaced
685:2dd841e364af 686:64919f63f059
280 } 280 }
281 281
282 TEST(CxHashMap, Clear) { 282 TEST(CxHashMap, Clear) {
283 CxTestingAllocator allocator; 283 CxTestingAllocator allocator;
284 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); 284 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0);
285 285
286 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1"); 286 cxMapPut(map, cx_hash_key_str("key 1"), (void *) "val 1");
287 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2"); 287 cxMapPut(map, cx_hash_key_str("key 2"), (void *) "val 2");
288 cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3"); 288 cxMapPut(map, cx_hash_key_str("key 3"), (void *) "val 3");
289 289
290 EXPECT_EQ(map->size, 3); 290 EXPECT_EQ(map->size, 3);
342 342
343 cxMapDestroy(map); 343 cxMapDestroy(map);
344 EXPECT_TRUE(allocator.verify()); 344 EXPECT_TRUE(allocator.verify());
345 } 345 }
346 346
347 static void test_simple_destructor(void *data) {
348 strcpy((char *) data, "OK");
349 }
350
351 static void test_advanced_destructor(
352 [[maybe_unused]] void *unused,
353 void *data
354 ) {
355 strcpy((char *) data, "OK");
356 }
357
358 static void verify_any_destructor(CxMap *map) {
359 auto k1 = cx_hash_key_str("key 1");
360 auto k2 = cx_hash_key_str("key 2");
361 auto k3 = cx_hash_key_str("key 3");
362 auto k4 = cx_hash_key_str("key 4");
363 auto k5 = cx_hash_key_str("key 5");
364
365 char v1[] = "val 1";
366 char v2[] = "val 2";
367 char v3[] = "val 3";
368 char v4[] = "val 4";
369 char v5[] = "val 5";
370
371 cxMapPut(map, k1, (void *) v1);
372 cxMapPut(map, k2, (void *) v2);
373 cxMapPut(map, k3, (void *) v3);
374 cxMapPut(map, k4, (void *) v4);
375
376 cxMapRemove(map, k2);
377 auto r = cxMapRemoveAndGet(map, k3);
378 cxMapDetach(map, k1);
379
380 EXPECT_STREQ(v1, "val 1");
381 EXPECT_STREQ(v2, "OK");
382 EXPECT_STREQ(v3, "val 3");
383 EXPECT_STREQ(v4, "val 4");
384 EXPECT_STREQ(v5, "val 5");
385 EXPECT_EQ(r, v3);
386
387 cxMapClear(map);
388
389 EXPECT_STREQ(v1, "val 1");
390 EXPECT_STREQ(v2, "OK");
391 EXPECT_STREQ(v3, "val 3");
392 EXPECT_STREQ(v4, "OK");
393 EXPECT_STREQ(v5, "val 5");
394
395 cxMapPut(map, k1, (void *) v1);
396 cxMapPut(map, k3, (void *) v3);
397 cxMapPut(map, k5, (void *) v5);
398
399 {
400 auto iter = cxMapMutIteratorKeys(map);
401 cx_foreach(CxHashKey*, key, iter) {
402 if (key->data.cstr[4] == '1') cxIteratorFlagRemoval(iter);
403 }
404 }
405 {
406 auto iter = cxMapMutIteratorValues(map);
407 cx_foreach(char*, v, iter) {
408 if (v[4] == '5') cxIteratorFlagRemoval(iter);
409 }
410 }
411
412 EXPECT_STREQ(v1, "OK");
413 EXPECT_STREQ(v2, "OK");
414 EXPECT_STREQ(v3, "val 3");
415 EXPECT_STREQ(v4, "OK");
416 EXPECT_STREQ(v5, "OK");
417
418 v1[0] = v2[0] = v4[0] = v5[0] = 'c';
419
420 cxMapDestroy(map);
421
422 EXPECT_STREQ(v1, "cK");
423 EXPECT_STREQ(v2, "cK");
424 EXPECT_STREQ(v3, "OK");
425 EXPECT_STREQ(v4, "cK");
426 EXPECT_STREQ(v5, "cK");
427 }
428
429 TEST(CxHashMap, SimpleDestructor) {
430 CxTestingAllocator allocator;
431 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0);
432 map->simple_destructor = test_simple_destructor;
433 verify_any_destructor(map);
434 EXPECT_TRUE(allocator.verify());
435 }
436
437 TEST(CxHashMap, AdvancedDestructor) {
438 CxTestingAllocator allocator;
439 auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0);
440 map->advanced_destructor = test_advanced_destructor;
441 verify_any_destructor(map);
442 EXPECT_TRUE(allocator.verify());
443 }

mercurial