1
- #include "test-lib .h"
1
+ #include "unit-test .h"
2
2
#include "hashmap.h"
3
3
#include "strbuf.h"
4
4
@@ -83,23 +83,23 @@ static void t_replace(struct hashmap *map, unsigned int ignore_case)
83
83
struct test_entry * entry ;
84
84
85
85
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 );
87
87
88
88
entry = alloc_test_entry (ignore_case ? "Key1" : "key1" , "value2" ,
89
89
ignore_case );
90
90
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" );
93
93
free (entry );
94
94
95
95
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 );
97
97
98
98
entry = alloc_test_entry (ignore_case ? "FOObarFrotz" : "fooBarFrotz" ,
99
99
"value4" , ignore_case );
100
100
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" );
103
103
free (entry );
104
104
}
105
105
@@ -122,20 +122,18 @@ static void t_get(struct hashmap *map, unsigned int ignore_case)
122
122
for (size_t i = 0 ; i < ARRAY_SIZE (key_val ); i ++ ) {
123
123
entry = alloc_test_entry (key_val [i ][0 ], key_val [i ][1 ],
124
124
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 );
126
126
}
127
127
128
128
for (size_t i = 0 ; i < ARRAY_SIZE (query ); i ++ ) {
129
129
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 ]);
134
132
}
135
133
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 ));
139
137
}
140
138
141
139
static 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)
165
163
166
164
hashmap_for_each_entry_from (map , entry , ent )
167
165
{
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 ++ ;
188
170
}
189
- check_int (count , = = , 2 );
171
+ cl_assert_equal_i (count , 2 );
190
172
}
191
173
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 );
198
176
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 );
201
179
}
202
180
203
181
static 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)
211
189
212
190
for (size_t i = 0 ; i < ARRAY_SIZE (key_val ); i ++ ) {
213
191
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 );
215
193
}
216
194
217
195
for (size_t i = 0 ; i < ARRAY_SIZE (remove ); i ++ ) {
218
196
entry = alloc_test_entry (remove [i ][0 ], "" , ignore_case );
219
197
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 ]);
222
200
free (entry );
223
201
free (removed );
224
202
}
225
203
226
204
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 );
228
206
free (entry );
229
207
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 ));
232
211
}
233
212
234
213
static 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)
242
221
243
222
for (size_t i = 0 ; i < ARRAY_SIZE (key_val ); i ++ ) {
244
223
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 );
246
225
}
247
226
248
227
hashmap_for_each_entry (map , & iter , entry , ent /* member name */ )
249
228
{
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 );
267
233
}
268
234
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 );
275
237
276
- check_int (hashmap_get_size (map ), = = , ARRAY_SIZE (key_val ));
238
+ cl_assert_equal_i (hashmap_get_size (map ), ARRAY_SIZE (key_val ));
277
239
}
278
240
279
241
static 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)
284
246
char * key = xstrfmt ("key%d" , i );
285
247
char * value = xstrfmt ("value%d" , i );
286
248
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 );
288
250
free (key );
289
251
free (value );
290
252
}
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 );
293
255
294
256
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 );
298
260
299
261
for (int i = 1 ; i <= 12 ; i ++ ) {
300
262
char * key = xstrfmt ("key%d" , i );
301
263
char * value = xstrfmt ("value%d" , i );
302
264
303
265
entry = alloc_test_entry (key , "" , ignore_case );
304
266
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 ));
307
269
free (key );
308
270
free (value );
309
271
free (entry );
310
272
free (removed );
311
273
}
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 );
314
276
315
277
entry = alloc_test_entry ("key40" , "" , ignore_case );
316
278
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 );
321
283
free (entry );
322
284
free (removed );
323
285
}
324
286
325
- static void t_intern (void )
287
+ void test_hashmap__intern (void )
326
288
{
327
289
const char * values [] = { "value1" , "Value1" , "value2" , "value2" };
328
290
329
291
for (size_t i = 0 ; i < ARRAY_SIZE (values ); i ++ ) {
330
292
const char * i1 = strintern (values [i ]);
331
293
const char * i2 = strintern (values [i ]);
332
294
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 );
343
298
}
344
299
}
345
300
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 )
347
357
{
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 );
361
359
}
0 commit comments