70 } |
70 } |
71 CX_TEST_ASSERT(map->collection.size == 0); |
71 CX_TEST_ASSERT(map->collection.size == 0); |
72 CX_TEST_ASSERT(map->collection.allocator == allocator); |
72 CX_TEST_ASSERT(map->collection.allocator == allocator); |
73 CX_TEST_ASSERT(map->collection.store_pointer); |
73 CX_TEST_ASSERT(map->collection.store_pointer); |
74 CX_TEST_ASSERT(map->collection.elem_size == sizeof(void *)); |
74 CX_TEST_ASSERT(map->collection.elem_size == sizeof(void *)); |
|
75 |
|
76 cxMapFree(map); |
|
77 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
78 } |
|
79 cx_testing_allocator_destroy(&talloc); |
|
80 } |
|
81 |
|
82 CX_TEST(test_hash_map_emplace) { |
|
83 CxTestingAllocator talloc; |
|
84 cx_testing_allocator_init(&talloc); |
|
85 CxAllocator *allocator = &talloc.base; |
|
86 CX_TEST_DO { |
|
87 CxMap *map = cxHashMapCreate(allocator, 20, 4); |
|
88 |
|
89 char *v = cxMapEmplace(map, "key 1"); |
|
90 CX_TEST_ASSERT(v != NULL); |
|
91 strcpy(v, "val 1"); |
|
92 |
|
93 char *tv = cxMapGet(map, "key 1"); |
|
94 CX_TEST_ASSERT(tv != NULL); |
|
95 CX_TEST_ASSERT(0 == strcmp(tv, "val 1")); |
|
96 |
|
97 v = cxMapEmplace(map, "key 1"); |
|
98 CX_TEST_ASSERT(v != NULL); |
|
99 CX_TEST_ASSERT(0 != strcmp(v, "val 1")); |
|
100 |
|
101 tv = cxMapGet(map, "key 1"); |
|
102 CX_TEST_ASSERT(tv != NULL); |
|
103 CX_TEST_ASSERT(0 != strcmp(tv, "val 1")); |
|
104 |
|
105 cxMapFree(map); |
|
106 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
107 } |
|
108 cx_testing_allocator_destroy(&talloc); |
|
109 } |
|
110 |
|
111 CX_TEST(test_hash_map_emplace_pointers) { |
|
112 CxTestingAllocator talloc; |
|
113 cx_testing_allocator_init(&talloc); |
|
114 CxAllocator *allocator = &talloc.base; |
|
115 CX_TEST_DO { |
|
116 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 4); |
|
117 cxDefineAdvancedDestructor(map, cxFree, allocator); |
|
118 |
|
119 char *val1 = cxMalloc(allocator, 8); |
|
120 strcpy(val1, "val 1"); |
|
121 char *val2 = cxMalloc(allocator, 8); |
|
122 strcpy(val2, "val 2"); |
|
123 |
|
124 char **v1 = cxMapEmplace(map, "key 1"); |
|
125 CX_TEST_ASSERT(v1 != NULL); |
|
126 *v1 = val1; |
|
127 |
|
128 char *tv = cxMapGet(map, "key 1"); |
|
129 CX_TEST_ASSERT(tv != NULL); |
|
130 CX_TEST_ASSERT(0 == strcmp(tv, "val 1")); |
|
131 |
|
132 // this will call the destructor of former v1 |
|
133 char **v2 = cxMapEmplace(map, "key 1"); |
|
134 CX_TEST_ASSERT(v2 != NULL); |
|
135 *v2 = val2; |
|
136 tv = cxMapGet(map, "key 1"); |
|
137 CX_TEST_ASSERT(tv != NULL); |
|
138 CX_TEST_ASSERT(0 == strcmp(tv, "val 2")); |
75 |
139 |
76 cxMapFree(map); |
140 cxMapFree(map); |
77 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
141 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
78 } |
142 } |
79 cx_testing_allocator_destroy(&talloc); |
143 cx_testing_allocator_destroy(&talloc); |
729 CxTestSuite *suite = cx_test_suite_new("map"); |
793 CxTestSuite *suite = cx_test_suite_new("map"); |
730 |
794 |
731 cx_test_register(suite, test_hash_map_create); |
795 cx_test_register(suite, test_hash_map_create); |
732 cx_test_register(suite, test_hash_map_create_store_pointers); |
796 cx_test_register(suite, test_hash_map_create_store_pointers); |
733 cx_test_register(suite, test_hash_map_basic_operations); |
797 cx_test_register(suite, test_hash_map_basic_operations); |
|
798 cx_test_register(suite, test_hash_map_emplace); |
|
799 cx_test_register(suite, test_hash_map_emplace_pointers); |
734 cx_test_register(suite, test_hash_map_rehash); |
800 cx_test_register(suite, test_hash_map_rehash); |
735 cx_test_register(suite, test_hash_map_rehash_not_required); |
801 cx_test_register(suite, test_hash_map_rehash_not_required); |
736 cx_test_register(suite, test_hash_map_clear); |
802 cx_test_register(suite, test_hash_map_clear); |
737 cx_test_register(suite, test_hash_map_store_ucx_strings); |
803 cx_test_register(suite, test_hash_map_store_ucx_strings); |
738 cx_test_register(suite, test_hash_map_remove_via_iterator); |
804 cx_test_register(suite, test_hash_map_remove_via_iterator); |