1- #include "test-lib .h"
1+ #include "unit-test .h"
22#include "hashmap.h"
33#include "strbuf.h"
44
@@ -83,23 +83,23 @@ static void t_replace(struct hashmap *map, unsigned int ignore_case)
8383 struct test_entry * entry ;
8484
8585 entry = alloc_test_entry ("key1" , "value1" , ignore_case );
86- check_pointer_eq (hashmap_put_entry (map , entry , ent ), NULL );
86+ cl_assert_equal_p (hashmap_put_entry (map , entry , ent ), NULL );
8787
8888 entry = alloc_test_entry (ignore_case ? "Key1" : "key1" , "value2" ,
8989 ignore_case );
9090 entry = hashmap_put_entry (map , entry , ent );
91- if ( check ( entry != NULL ))
92- check_str (get_value (entry ), "value1" );
91+ cl_assert ( entry != NULL );
92+ cl_assert_equal_s (get_value (entry ), "value1" );
9393 free (entry );
9494
9595 entry = alloc_test_entry ("fooBarFrotz" , "value3" , ignore_case );
96- check_pointer_eq (hashmap_put_entry (map , entry , ent ), NULL );
96+ cl_assert_equal_p (hashmap_put_entry (map , entry , ent ), NULL );
9797
9898 entry = alloc_test_entry (ignore_case ? "FOObarFrotz" : "fooBarFrotz" ,
9999 "value4" , ignore_case );
100100 entry = hashmap_put_entry (map , entry , ent );
101- if ( check ( entry != NULL ))
102- check_str (get_value (entry ), "value3" );
101+ cl_assert ( entry != NULL );
102+ cl_assert_equal_s (get_value (entry ), "value3" );
103103 free (entry );
104104}
105105
@@ -122,20 +122,18 @@ static void t_get(struct hashmap *map, unsigned int ignore_case)
122122 for (size_t i = 0 ; i < ARRAY_SIZE (key_val ); i ++ ) {
123123 entry = alloc_test_entry (key_val [i ][0 ], key_val [i ][1 ],
124124 ignore_case );
125- check_pointer_eq (hashmap_put_entry (map , entry , ent ), NULL );
125+ cl_assert_equal_p (hashmap_put_entry (map , entry , ent ), NULL );
126126 }
127127
128128 for (size_t i = 0 ; i < ARRAY_SIZE (query ); i ++ ) {
129129 entry = get_test_entry (map , query [i ][0 ], ignore_case );
130- if (check (entry != NULL ))
131- check_str (get_value (entry ), query [i ][1 ]);
132- else
133- test_msg ("query key: %s" , query [i ][0 ]);
130+ cl_assert (entry != NULL );
131+ cl_assert_equal_s (get_value (entry ), query [i ][1 ]);
134132 }
135133
136- check_pointer_eq (get_test_entry (map , "notInMap" , ignore_case ), NULL );
137- check_int (map -> tablesize , = = , 64 );
138- check_int (hashmap_get_size (map ), = = , ARRAY_SIZE (key_val ));
134+ cl_assert_equal_p (get_test_entry (map , "notInMap" , ignore_case ), NULL );
135+ cl_assert_equal_i (map -> tablesize , 64 );
136+ cl_assert_equal_i (hashmap_get_size (map ), ARRAY_SIZE (key_val ));
139137}
140138
141139static void t_add (struct hashmap * map , unsigned int ignore_case )
@@ -165,39 +163,19 @@ static void t_add(struct hashmap *map, unsigned int ignore_case)
165163
166164 hashmap_for_each_entry_from (map , entry , ent )
167165 {
168- int ret ;
169- if (!check_int ((ret = key_val_contains (
170- key_val , seen ,
171- ARRAY_SIZE (key_val ), entry )),
172- = = , 0 )) {
173- switch (ret ) {
174- case 1 :
175- test_msg ("found entry was not given in the input\n"
176- " key: %s\n value: %s" ,
177- entry -> key , get_value (entry ));
178- break ;
179- case 2 :
180- test_msg ("duplicate entry detected\n"
181- " key: %s\n value: %s" ,
182- entry -> key , get_value (entry ));
183- break ;
184- }
185- } else {
186- count ++ ;
187- }
166+ int ret = key_val_contains (key_val , seen ,
167+ ARRAY_SIZE (key_val ), entry );
168+ cl_assert_equal_i (ret , 0 );
169+ count ++ ;
188170 }
189- check_int (count , = = , 2 );
171+ cl_assert_equal_i (count , 2 );
190172 }
191173
192- for (size_t i = 0 ; i < ARRAY_SIZE (seen ); i ++ ) {
193- if (!check_int (seen [i ], = = , 1 ))
194- test_msg ("following key-val pair was not iterated over:\n"
195- " key: %s\n value: %s" ,
196- key_val [i ][0 ], key_val [i ][1 ]);
197- }
174+ for (size_t i = 0 ; i < ARRAY_SIZE (seen ); i ++ )
175+ cl_assert_equal_i (seen [i ], 1 );
198176
199- check_int (hashmap_get_size (map ), = = , ARRAY_SIZE (key_val ));
200- check_pointer_eq (get_test_entry (map , "notInMap" , ignore_case ), NULL );
177+ cl_assert_equal_i (hashmap_get_size (map ), ARRAY_SIZE (key_val ));
178+ cl_assert_equal_p (get_test_entry (map , "notInMap" , ignore_case ), NULL );
201179}
202180
203181static void t_remove (struct hashmap * map , unsigned int ignore_case )
@@ -211,24 +189,25 @@ static void t_remove(struct hashmap *map, unsigned int ignore_case)
211189
212190 for (size_t i = 0 ; i < ARRAY_SIZE (key_val ); i ++ ) {
213191 entry = alloc_test_entry (key_val [i ][0 ], key_val [i ][1 ], ignore_case );
214- check_pointer_eq (hashmap_put_entry (map , entry , ent ), NULL );
192+ cl_assert_equal_p (hashmap_put_entry (map , entry , ent ), NULL );
215193 }
216194
217195 for (size_t i = 0 ; i < ARRAY_SIZE (remove ); i ++ ) {
218196 entry = alloc_test_entry (remove [i ][0 ], "" , ignore_case );
219197 removed = hashmap_remove_entry (map , entry , ent , remove [i ][0 ]);
220- if ( check ( removed != NULL ))
221- check_str (get_value (removed ), remove [i ][1 ]);
198+ cl_assert ( removed != NULL );
199+ cl_assert_equal_s (get_value (removed ), remove [i ][1 ]);
222200 free (entry );
223201 free (removed );
224202 }
225203
226204 entry = alloc_test_entry ("notInMap" , "" , ignore_case );
227- check_pointer_eq (hashmap_remove_entry (map , entry , ent , "notInMap" ), NULL );
205+ cl_assert_equal_p (hashmap_remove_entry (map , entry , ent , "notInMap" ), NULL );
228206 free (entry );
229207
230- check_int (map -> tablesize , = = , 64 );
231- check_int (hashmap_get_size (map ), = = , ARRAY_SIZE (key_val ) - ARRAY_SIZE (remove ));
208+ cl_assert_equal_i (map -> tablesize , 64 );
209+ cl_assert_equal_i (hashmap_get_size (map ),
210+ ARRAY_SIZE (key_val ) - ARRAY_SIZE (remove ));
232211}
233212
234213static void t_iterate (struct hashmap * map , unsigned int ignore_case )
@@ -242,38 +221,21 @@ static void t_iterate(struct hashmap *map, unsigned int ignore_case)
242221
243222 for (size_t i = 0 ; i < ARRAY_SIZE (key_val ); i ++ ) {
244223 entry = alloc_test_entry (key_val [i ][0 ], key_val [i ][1 ], ignore_case );
245- check_pointer_eq (hashmap_put_entry (map , entry , ent ), NULL );
224+ cl_assert_equal_p (hashmap_put_entry (map , entry , ent ), NULL );
246225 }
247226
248227 hashmap_for_each_entry (map , & iter , entry , ent /* member name */ )
249228 {
250- int ret ;
251- if (!check_int ((ret = key_val_contains (key_val , seen ,
252- ARRAY_SIZE (key_val ),
253- entry )), = = , 0 )) {
254- switch (ret ) {
255- case 1 :
256- test_msg ("found entry was not given in the input\n"
257- " key: %s\n value: %s" ,
258- entry -> key , get_value (entry ));
259- break ;
260- case 2 :
261- test_msg ("duplicate entry detected\n"
262- " key: %s\n value: %s" ,
263- entry -> key , get_value (entry ));
264- break ;
265- }
266- }
229+ int ret = key_val_contains (key_val , seen ,
230+ ARRAY_SIZE (key_val ),
231+ entry );
232+ cl_assert (ret == 0 );
267233 }
268234
269- for (size_t i = 0 ; i < ARRAY_SIZE (seen ); i ++ ) {
270- if (!check_int (seen [i ], = = , 1 ))
271- test_msg ("following key-val pair was not iterated over:\n"
272- " key: %s\n value: %s" ,
273- key_val [i ][0 ], key_val [i ][1 ]);
274- }
235+ for (size_t i = 0 ; i < ARRAY_SIZE (seen ); i ++ )
236+ cl_assert_equal_i (seen [i ], 1 );
275237
276- check_int (hashmap_get_size (map ), = = , ARRAY_SIZE (key_val ));
238+ cl_assert_equal_i (hashmap_get_size (map ), ARRAY_SIZE (key_val ));
277239}
278240
279241static void t_alloc (struct hashmap * map , unsigned int ignore_case )
@@ -284,78 +246,114 @@ static void t_alloc(struct hashmap *map, unsigned int ignore_case)
284246 char * key = xstrfmt ("key%d" , i );
285247 char * value = xstrfmt ("value%d" , i );
286248 entry = alloc_test_entry (key , value , ignore_case );
287- check_pointer_eq (hashmap_put_entry (map , entry , ent ), NULL );
249+ cl_assert_equal_p (hashmap_put_entry (map , entry , ent ), NULL );
288250 free (key );
289251 free (value );
290252 }
291- check_int (map -> tablesize , = = , 64 );
292- check_int (hashmap_get_size (map ), = = , 51 );
253+ cl_assert_equal_i (map -> tablesize , 64 );
254+ cl_assert_equal_i (hashmap_get_size (map ), 51 );
293255
294256 entry = alloc_test_entry ("key52" , "value52" , ignore_case );
295- check_pointer_eq (hashmap_put_entry (map , entry , ent ), NULL );
296- check_int (map -> tablesize , = = , 256 );
297- check_int (hashmap_get_size (map ), = = , 52 );
257+ cl_assert_equal_p (hashmap_put_entry (map , entry , ent ), NULL );
258+ cl_assert_equal_i (map -> tablesize , 256 );
259+ cl_assert_equal_i (hashmap_get_size (map ), 52 );
298260
299261 for (int i = 1 ; i <= 12 ; i ++ ) {
300262 char * key = xstrfmt ("key%d" , i );
301263 char * value = xstrfmt ("value%d" , i );
302264
303265 entry = alloc_test_entry (key , "" , ignore_case );
304266 removed = hashmap_remove_entry (map , entry , ent , key );
305- if ( check ( removed != NULL ))
306- check_str (value , get_value (removed ));
267+ cl_assert ( removed != NULL );
268+ cl_assert_equal_s (value , get_value (removed ));
307269 free (key );
308270 free (value );
309271 free (entry );
310272 free (removed );
311273 }
312- check_int (map -> tablesize , = = , 256 );
313- check_int (hashmap_get_size (map ), = = , 40 );
274+ cl_assert_equal_i (map -> tablesize , 256 );
275+ cl_assert_equal_i (hashmap_get_size (map ), 40 );
314276
315277 entry = alloc_test_entry ("key40" , "" , ignore_case );
316278 removed = hashmap_remove_entry (map , entry , ent , "key40" );
317- if ( check ( removed != NULL ))
318- check_str ("value40" , get_value (removed ));
319- check_int (map -> tablesize , = = , 64 );
320- check_int (hashmap_get_size (map ), = = , 39 );
279+ cl_assert ( removed != NULL );
280+ cl_assert_equal_s ("value40" , get_value (removed ));
281+ cl_assert_equal_i (map -> tablesize , 64 );
282+ cl_assert_equal_i (hashmap_get_size (map ), 39 );
321283 free (entry );
322284 free (removed );
323285}
324286
325- static void t_intern (void )
287+ void test_hashmap__intern (void )
326288{
327289 const char * values [] = { "value1" , "Value1" , "value2" , "value2" };
328290
329291 for (size_t i = 0 ; i < ARRAY_SIZE (values ); i ++ ) {
330292 const char * i1 = strintern (values [i ]);
331293 const char * i2 = strintern (values [i ]);
332294
333- if (!check (!strcmp (i1 , values [i ])))
334- test_msg ("strintern(%s) returns %s\n" , values [i ], i1 );
335- else if (!check (i1 != values [i ]))
336- test_msg ("strintern(%s) returns input pointer\n" ,
337- values [i ]);
338- else if (!check_pointer_eq (i1 , i2 ))
339- test_msg ("address('%s') != address('%s'), so strintern('%s') != strintern('%s')" ,
340- i1 , i2 , values [i ], values [i ]);
341- else
342- check_str (i1 , values [i ]);
295+ cl_assert_equal_s (i1 , values [i ]);
296+ cl_assert (i1 != values [i ]);
297+ cl_assert_equal_p (i1 , i2 );
343298 }
344299}
345300
346- int cmd_main (int argc UNUSED , const char * * argv UNUSED )
301+ void test_hashmap__replace_case_sensitive (void )
302+ {
303+ setup (t_replace , 0 );
304+ }
305+
306+ void test_hashmap__replace_case_insensitive (void )
307+ {
308+ setup (t_replace , 1 );
309+ }
310+
311+ void test_hashmap__get_case_sensitive (void )
312+ {
313+ setup (t_get , 0 );
314+ }
315+
316+ void test_hashmap__get_case_insensitive (void )
317+ {
318+ setup (t_get , 1 );
319+ }
320+
321+ void test_hashmap__add_case_sensitive (void )
322+ {
323+ setup (t_add , 0 );
324+ }
325+
326+ void test_hashmap__add_case_insensitive (void )
327+ {
328+ setup (t_add , 1 );
329+ }
330+
331+ void test_hashmap__remove_case_sensitive (void )
332+ {
333+ setup (t_remove , 0 );
334+ }
335+
336+ void test_hashmap__remove_case_insensitive (void )
337+ {
338+ setup (t_remove , 1 );
339+ }
340+
341+ void test_hashmap__iterate_case_sensitive (void )
342+ {
343+ setup (t_iterate , 0 );
344+ }
345+
346+ void test_hashmap__iterate_case_insensitive (void )
347+ {
348+ setup (t_iterate , 1 );
349+ }
350+
351+ void test_hashmap__alloc_case_sensitive (void )
352+ {
353+ setup (t_alloc , 0 );
354+ }
355+
356+ void test_hashmap__alloc_case_insensitive (void )
347357{
348- TEST (setup (t_replace , 0 ), "replace works" );
349- TEST (setup (t_replace , 1 ), "replace (case insensitive) works" );
350- TEST (setup (t_get , 0 ), "get works" );
351- TEST (setup (t_get , 1 ), "get (case insensitive) works" );
352- TEST (setup (t_add , 0 ), "add works" );
353- TEST (setup (t_add , 1 ), "add (case insensitive) works" );
354- TEST (setup (t_remove , 0 ), "remove works" );
355- TEST (setup (t_remove , 1 ), "remove (case insensitive) works" );
356- TEST (setup (t_iterate , 0 ), "iterate works" );
357- TEST (setup (t_iterate , 1 ), "iterate (case insensitive) works" );
358- TEST (setup (t_alloc , 0 ), "grow / shrink works" );
359- TEST (t_intern (), "string interning works" );
360- return test_done ();
358+ setup (t_alloc , 1 );
361359}
0 commit comments