File: test_hash.c

package info (click to toggle)
libnih 1.0.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,544 kB
  • sloc: ansic: 188,634; sh: 11,217; makefile: 1,116; yacc: 291; xml: 239; sed: 16
file content (625 lines) | stat: -rw-r--r-- 15,224 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/* libnih
 *
 * test_hash.c - test suite for nih/hash.c
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <nih/test.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/list.h>
#include <nih/hash.h>


typedef struct hash_entry {
	NihList     list;
	const char *key;
} HashEntry;

static NihList *
new_entry (void       *parent,
	   const char *key)
{
	HashEntry *entry;

	entry = nih_new (parent, HashEntry);

	nih_list_init (&entry->list);
	entry->key = key;

	return (NihList *)entry;
}

static const void *
my_key_function (NihList *entry)
{
	return "foo";
}

static uint32_t
my_hash_function (const void *key)
{
	static uint32_t hash = 0;

	return hash++;
}

static int
my_cmp_function (const void *key1,
		 const void *key2)
{
	return 0;
}


void
test_new (void)
{
	NihHash *hash;
	size_t   i;

	TEST_FUNCTION ("nih_hash_new");

	/* Check that we can create a small hash table; a small prime number
	 * should be selected for the actual size, and that number of empty
	 * bins should be allocated as a child of the hash table.
	 */
	TEST_FEATURE ("with zero size");
	TEST_ALLOC_FAIL {
		hash = nih_hash_new (NULL, 0,
				     my_key_function,
				     my_hash_function,
				     my_cmp_function);

		if (test_alloc_failed) {
			TEST_EQ_P (hash, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (hash, sizeof(NihHash));
		TEST_EQ_P (hash->key_function, my_key_function);
		TEST_EQ_P (hash->hash_function, my_hash_function);
		TEST_EQ_P (hash->cmp_function, my_cmp_function);

		TEST_EQ (hash->size, 17);
		TEST_NE_P (hash->bins, NULL);
		TEST_ALLOC_PARENT (hash->bins, hash);

		for (i = 0; i < hash->size; i++)
			TEST_LIST_EMPTY (&hash->bins[i]);

		nih_free (hash);
	}


	/* Check again with a medium size, which should pick a medium prime
	 * number.
	 */
	TEST_FEATURE ("with medium size");
	TEST_ALLOC_FAIL {
		hash = nih_hash_new (NULL, 600,
				     my_key_function,
				     my_hash_function,
				     my_cmp_function);

		if (test_alloc_failed) {
			TEST_EQ_P (hash, NULL);
			continue;
		}

		TEST_EQ (hash->size, 331);
		TEST_NE_P (hash->bins, NULL);
		TEST_ALLOC_PARENT (hash->bins, hash);

		for (i = 0; i < hash->size; i++)
			TEST_LIST_EMPTY (&hash->bins[i]);

		nih_free (hash);
	}


	/* Check with a much larger size, which should pick the largest prime
	 * that we know of.
	 */
	TEST_FEATURE ("with large size");
	TEST_ALLOC_FAIL {
		hash = nih_hash_new (NULL, 40000000,
				     my_key_function,
				     my_hash_function,
				     my_cmp_function);

		if (test_alloc_failed) {
			TEST_EQ_P (hash, NULL);
			continue;
		}

		TEST_EQ (hash->size, 10250323);
		TEST_NE_P (hash->bins, NULL);
		TEST_ALLOC_PARENT (hash->bins, hash);

		for (i = 0; i < hash->size; i++)
			TEST_LIST_EMPTY (&hash->bins[i]);

		nih_free (hash);
	}
}

void
test_string_new (void)
{
	NihHash *hash;
	size_t   i;

	/* Check that we can create a small hash table; a small prime number
	 * should be selected for the actual size, and that number of empty
	 * bins should be allocated as a child of the hash table.
	 */
	TEST_FUNCTION ("nih_hash_string_new");
	TEST_ALLOC_FAIL {
		hash = nih_hash_string_new (NULL, 0);

		if (test_alloc_failed) {
			TEST_EQ_P (hash, NULL);
			continue;
		}

		TEST_ALLOC_SIZE (hash, sizeof(NihHash));
		TEST_EQ_P (hash->key_function,
			   (NihKeyFunction)nih_hash_string_key);
		TEST_EQ_P (hash->hash_function,
			   (NihHashFunction)nih_hash_string_hash);
		TEST_EQ_P (hash->cmp_function,
			   (NihCmpFunction)nih_hash_string_cmp);

		TEST_EQ (hash->size, 17);
		TEST_NE_P (hash->bins, NULL);
		TEST_ALLOC_PARENT (hash->bins, hash);

		for (i = 0; i < hash->size; i++)
			TEST_LIST_EMPTY (&hash->bins[i]);

		nih_free (hash);
	}
}

void
test_add (void)
{
	NihHash *hash;
	NihList *entry1, *entry2, *entry3, *entry4, *ptr;

	TEST_FUNCTION ("nih_hash_add");
	hash = nih_hash_string_new (NULL, 0);
	entry1 = new_entry (hash, "entry 1");
	entry2 = new_entry (hash, "entry 2");
	entry3 = new_entry (hash, "entry 1");
	entry4 = new_entry (hash, "entry 4");

	/* Check that we can add an entry to an empty hash table; it should
	 * be returned and turn up in the appropriate bin.
	 */
	TEST_FEATURE ("with empty hash");
	ptr = nih_hash_add (hash, entry1);

	TEST_EQ_P (ptr, entry1);

	TEST_EQ_P (hash->bins[15].next, entry1);
	TEST_EQ_P (entry1->next, &hash->bins[15]);
	TEST_EQ_P (hash->bins[15].prev, entry1);
	TEST_EQ_P (entry1->prev, &hash->bins[15]);


	/* Check that we can add an entry to a populated hash table. */
	TEST_FEATURE ("with non-empty hash");
	nih_hash_add (hash, entry2);

	TEST_EQ_P (hash->bins[14].next, entry2);
	TEST_EQ_P (entry2->next, &hash->bins[14]);
	TEST_EQ_P (hash->bins[14].prev, entry2);
	TEST_EQ_P (entry2->prev, &hash->bins[14]);


	/* Check that we can add an entry with a duplicate key, and it is
	 * added to the end of the same bin as the previous entry with that
	 * key.
	 */
	TEST_FEATURE ("with duplicate key");
	nih_hash_add (hash, entry3);

	TEST_EQ_P (hash->bins[15].next, entry1);
	TEST_EQ_P (entry1->next, entry3);
	TEST_EQ_P (entry3->next, &hash->bins[15]);
	TEST_EQ_P (hash->bins[15].prev, entry3);
	TEST_EQ_P (entry3->prev, entry1);
	TEST_EQ_P (entry1->prev, &hash->bins[15]);


	/* Check that nih_hash_add can rip an entry out of an existing list
	 * and place it in the hash table.
	 */
	TEST_FEATURE ("with entry already in a list");
	ptr = nih_list_new (NULL);
	nih_list_add (ptr, entry4);
	nih_hash_add (hash, entry4);

	TEST_EQ_P (ptr->next, ptr);
	TEST_EQ_P (ptr->prev, ptr);

	TEST_EQ_P (hash->bins[3].next, entry4);
	TEST_EQ_P (entry4->next, &hash->bins[3]);
	TEST_EQ_P (hash->bins[3].prev, entry4);
	TEST_EQ_P (entry4->prev, &hash->bins[3]);

	nih_free (hash);
	nih_free (ptr);
}

void
test_add_unique (void)
{
	NihHash *hash;
	NihList *entry1, *entry2, *entry3, *entry4, *ptr;

	TEST_FUNCTION ("nih_hash_add_unique");
	hash = nih_hash_string_new (NULL, 0);
	entry1 = new_entry (hash, "entry 1");
	entry2 = new_entry (hash, "entry 2");
	entry3 = new_entry (hash, "entry 1");
	entry4 = new_entry (hash, "entry 4");

	/* Check that we can add an entry to an empty hash table; it should
	 * be returned and turn up in the appropriate bin.
	 */
	TEST_FEATURE ("with empty hash");
	ptr = nih_hash_add_unique (hash, entry1);

	TEST_EQ_P (ptr, entry1);

	TEST_EQ_P (hash->bins[15].next, entry1);
	TEST_EQ_P (entry1->next, &hash->bins[15]);
	TEST_EQ_P (hash->bins[15].prev, entry1);
	TEST_EQ_P (entry1->prev, &hash->bins[15]);


	/* Check that we can add an entry to a populated hash table. */
	TEST_FEATURE ("with non-empty hash");
	nih_hash_add_unique (hash, entry2);

	TEST_EQ_P (hash->bins[14].next, entry2);
	TEST_EQ_P (entry2->next, &hash->bins[14]);
	TEST_EQ_P (hash->bins[14].prev, entry2);
	TEST_EQ_P (entry2->prev, &hash->bins[14]);


	/* Check that we get NULL if we try and add an entry with a
	 * duplicate key, and that the hash table is not altered.
	 */
	TEST_FEATURE ("with duplicate key");
	ptr = nih_hash_add_unique (hash, entry3);

	TEST_EQ_P (ptr, NULL);

	TEST_EQ_P (hash->bins[15].next, entry1);
	TEST_EQ_P (entry1->next, &hash->bins[15]);
	TEST_EQ_P (hash->bins[15].prev, entry1);
	TEST_EQ_P (entry1->prev, &hash->bins[15]);


	/* Check that nih_hash_add can rip an entry out of an existing list
	 * and place it in the hash table.
	 */
	TEST_FEATURE ("with entry already in a list");
	ptr = nih_list_new (NULL);
	nih_list_add (ptr, entry4);
	nih_hash_add_unique (hash, entry4);

	TEST_EQ_P (ptr->next, ptr);
	TEST_EQ_P (ptr->prev, ptr);

	TEST_EQ_P (hash->bins[3].next, entry4);
	TEST_EQ_P (entry4->next, &hash->bins[3]);
	TEST_EQ_P (hash->bins[3].prev, entry4);
	TEST_EQ_P (entry4->prev, &hash->bins[3]);

	nih_free (hash);
	nih_free (ptr);
}

void
test_replace (void)
{
	NihHash *hash;
	NihList *entry1, *entry2, *entry3, *entry4, *ptr;

	TEST_FUNCTION ("nih_hash_replace");
	hash = nih_hash_string_new (NULL, 0);
	entry1 = new_entry (hash, "entry 1");
	entry2 = new_entry (hash, "entry 2");
	entry3 = new_entry (hash, "entry 1");
	entry4 = new_entry (hash, "entry 4");

	/* Check that we can add an entry to an empty hash table; NULL should
	 * be returned (nothing replaced) and the entry should turn up in the
	 * appropriate bin.
	 */
	TEST_FEATURE ("with empty hash");
	ptr = nih_hash_replace (hash, entry1);

	TEST_EQ_P (ptr, NULL);

	TEST_EQ_P (hash->bins[15].next, entry1);
	TEST_EQ_P (entry1->next, &hash->bins[15]);
	TEST_EQ_P (hash->bins[15].prev, entry1);
	TEST_EQ_P (entry1->prev, &hash->bins[15]);


	/* Check that we can add an entry to a populated hash table. */
	TEST_FEATURE ("with non-empty hash");
	nih_hash_replace (hash, entry2);

	TEST_EQ_P (hash->bins[14].next, entry2);
	TEST_EQ_P (entry2->next, &hash->bins[14]);
	TEST_EQ_P (hash->bins[14].prev, entry2);
	TEST_EQ_P (entry2->prev, &hash->bins[14]);


	/* Check that we can add an entry with a duplicate key, replacing
	 * the existing one in the hash.  The replaced entry should be
	 * returned, and removed from the bin.
	 */
	TEST_FEATURE ("with duplicate key");
	ptr = nih_hash_replace (hash, entry3);

	TEST_EQ_P (ptr, entry1);

	TEST_EQ_P (entry1->next, entry1);
	TEST_EQ_P (entry1->prev, entry1);

	TEST_EQ_P (hash->bins[15].next, entry3);
	TEST_EQ_P (entry3->next, &hash->bins[15]);
	TEST_EQ_P (hash->bins[15].prev, entry3);
	TEST_EQ_P (entry3->prev, &hash->bins[15]);


	/* Check that nih_hash_add can rip an entry out of an existing list
	 * and place it in the hash table.
	 */
	TEST_FEATURE ("with entry already in a list");
	ptr = nih_list_new (NULL);
	nih_list_add (ptr, entry4);
	nih_hash_replace (hash, entry4);

	TEST_EQ_P (ptr->next, ptr);
	TEST_EQ_P (ptr->prev, ptr);

	TEST_EQ_P (hash->bins[3].next, entry4);
	TEST_EQ_P (entry4->next, &hash->bins[3]);
	TEST_EQ_P (hash->bins[3].prev, entry4);
	TEST_EQ_P (entry4->prev, &hash->bins[3]);

	nih_free (hash);
	nih_free (ptr);
}

void
test_search (void)
{
	NihHash *hash;
	NihList *entry1, *entry2, *entry3, *ptr;

	TEST_FUNCTION ("nih_hash_search");
	hash = nih_hash_string_new (NULL, 0);
	entry1 = nih_hash_add (hash, new_entry (hash, "entry 1"));
	entry2 = nih_hash_add (hash, new_entry (hash, "entry 2"));
	entry3 = nih_hash_add (hash, new_entry (hash, "entry 2"));

	/* Check that we find the sole matching entry. */
	ptr = nih_hash_search (hash, "entry 1", NULL);

	TEST_EQ_P (ptr, entry1);

	/* Searching again should find nothing */
	ptr = nih_hash_search (hash, "entry 1", ptr);

	TEST_EQ_P (ptr, NULL);


	/* Check that where there's multiple matches, we find the first one. */
	TEST_FEATURE ("with multiple matches");
	ptr = nih_hash_search (hash, "entry 2", NULL);

	TEST_EQ_P (ptr, entry2);

	/* And that searching again finds the second one. */
	ptr = nih_hash_search (hash, "entry 2", ptr);

	TEST_EQ_P (ptr, entry3);

	/* And again finds nothing. */
	ptr = nih_hash_search (hash, "entry 2", ptr);

	TEST_EQ_P (ptr, NULL);


	/* Check that we get NULL if there are no matches. */
	TEST_FEATURE ("with no matches");
	ptr = nih_hash_search (hash, "entry 3", NULL);

	TEST_EQ_P (ptr, NULL);

	nih_free (hash);
}

void
test_lookup (void)
{
	NihHash *hash;
	NihList *entry1, *entry2, *entry3, *ptr;

	TEST_FUNCTION ("nih_hash_lookup");
	hash = nih_hash_string_new (NULL, 0);
	entry1 = nih_hash_add (hash, new_entry (hash, "entry 1"));
	entry2 = nih_hash_add (hash, new_entry (hash, "entry 2"));
	entry3 = new_entry (hash, "entry 3");

	/* Check that we find a single matching entry. */
	TEST_FEATURE ("with single match");
	ptr = nih_hash_lookup (hash, "entry 1");

	TEST_EQ_P (ptr, entry1);


	/* Check that we find the first matching entry. */
	TEST_FEATURE ("with multiple matches");
	ptr = nih_hash_lookup (hash, "entry 2");

	TEST_EQ_P (ptr, entry2);


	/* Check that we get NULL when there are no matching entries. */
	TEST_FEATURE ("with no matches");
	ptr = nih_hash_lookup (hash, "entry 3");

	TEST_EQ_P (ptr, NULL);

	nih_free (hash);
}


void
test_foreach (void)
{
	NihHash *hash;
	NihList *entry[4], *entry0, *entry1, *entry2, *entry3;
	int      i;

	/* Check that NIH_HASH_FOREACH iterates the hash correctly in order,
	 * visiting each entry in each bin.  Note that we stage the entries
	 * in the hash in the order we expect them to come out in, but add
	 * them in a different order for sanity.
	 */
	TEST_FUNCTION ("NIH_HASH_FOREACH");
	hash = nih_hash_string_new (NULL, 0);
	entry0 = entry[2] = new_entry (hash, "entry 1");
	entry1 = entry[1] = new_entry (hash, "entry 2");
	entry2 = entry[3] = new_entry (hash, "entry 1");
	entry3 = entry[0] = new_entry (hash, "entry 4");

	nih_hash_add (hash, entry0);
	nih_hash_add (hash, entry1);
	nih_hash_add (hash, entry2);
	nih_hash_add (hash, entry3);

	i = 0;
	NIH_HASH_FOREACH (hash, iter) {
		if (i > 3)
			TEST_FAILED ("wrong number of iterations, expected %d got %d",
				     4, i + 1);

		if (iter != entry[i])
			TEST_FAILED ("wrong list entry, expected %p got %p",
				     entry[i], iter);

		i++;
	}

	nih_free (hash);
}

void
test_foreach_safe (void)
{
	NihHash *hash;
	NihList *entry[4], *entry0, *entry1, *entry2, *entry3;
	int      i;

	/* Check that NIH_HASH_FOREACH_SAFE iterates the hash correctly in
	 * order, visiting each entry in each bin; and that it's safe to
	 * remove the entries while doing so.
	 */
	TEST_FUNCTION ("NIH_HASH_FOREACH_SAFE");
	hash = nih_hash_string_new (NULL, 0);
	entry0 = entry[2] = new_entry (hash, "entry 1");
	entry1 = entry[1] = new_entry (hash, "entry 2");
	entry2 = entry[3] = new_entry (hash, "entry 1");
	entry3 = entry[0] = new_entry (hash, "entry 4");

	nih_hash_add (hash, entry0);
	nih_hash_add (hash, entry1);
	nih_hash_add (hash, entry2);
	nih_hash_add (hash, entry3);

	i = 0;
	NIH_HASH_FOREACH_SAFE (hash, iter) {
		if (i > 3)
			TEST_FAILED ("wrong number of iterations, expected %d got %d",
				     4, i + 1);

		if (iter != entry[i])
			TEST_FAILED ("wrong list entry, expected %p got %p",
				     entry[i], iter);

		nih_list_remove (entry[i]);

		i++;
	}

	nih_free (hash);
}


void
test_string_key (void)
{
	NihList    *entry;
	const char *key;


	/* Check that the string key function returns a pointer to the
	 * key in our test structure.
	 */
	TEST_FUNCTION ("nih_hash_string_key");
	entry = new_entry (NULL, "my entry");

	key = nih_hash_string_key (entry);

	TEST_EQ_P (key, ((HashEntry *)entry)->key);
	TEST_EQ_STR (key, "my entry");

	nih_free (entry);
}


int
main (int   argc,
      char *argv[])
{
	test_new ();
	test_string_new ();
	test_add ();
	test_add_unique ();
	test_replace ();
	test_search ();
	test_lookup ();
	test_foreach ();
	test_foreach_safe ();
	test_string_key ();

	return 0;
}