xref: /dflybsd-src/sbin/hammer/test_dupkey.c (revision eccac4ffb5982fd205a1701631a4c37bfa42cc25)
1a7fbbf91SMatthew Dillon /*
2a7fbbf91SMatthew Dillon  * This is a really simple stupid standalone program which will find two
3a7fbbf91SMatthew Dillon  * filenames with the same CRC, used to test the directory iterator.
4a7fbbf91SMatthew Dillon  *
5f970d47bSTomohiro Kusumi  * cc -I /usr/src/sys test_dupkey.c /usr/src/sys/libkern/crc32.c \
6f970d47bSTomohiro Kusumi  * /usr/src/sys/libkern/icrc32.c -o test_dupkey
7a7fbbf91SMatthew Dillon  *
8a7fbbf91SMatthew Dillon  * $DragonFly: src/sbin/hammer/test_dupkey.c,v 1.1 2008/06/26 04:07:57 dillon Exp $
9a7fbbf91SMatthew Dillon  */
10a7fbbf91SMatthew Dillon 
11a7fbbf91SMatthew Dillon #include "hammer_util.h"
12a7fbbf91SMatthew Dillon 
1346137e17STomohiro Kusumi static uint32_t namekey(const char *name);
14a7fbbf91SMatthew Dillon static void randomname(char *name);
15a7fbbf91SMatthew Dillon 
1646137e17STomohiro Kusumi uint32_t bitmap[0x80000000U / 32];
17a7fbbf91SMatthew Dillon 
18a7fbbf91SMatthew Dillon int
main(int ac,char ** av)19a7fbbf91SMatthew Dillon main(int ac, char **av)
20a7fbbf91SMatthew Dillon {
21a7fbbf91SMatthew Dillon 	char name[32];
2246137e17STomohiro Kusumi 	uint32_t key;
2346137e17STomohiro Kusumi 	uint32_t *ptr;
2446137e17STomohiro Kusumi 	uint32_t mask;
2546137e17STomohiro Kusumi 	uint32_t count;
2646137e17STomohiro Kusumi 	uint32_t saved;
27a7fbbf91SMatthew Dillon 
28a7fbbf91SMatthew Dillon 	srandom(0);	/* reproducable random sequence number */
29a7fbbf91SMatthew Dillon 	count = 0;
30a7fbbf91SMatthew Dillon 	for (;;) {
31a7fbbf91SMatthew Dillon 		randomname(name);
32a7fbbf91SMatthew Dillon 		key = namekey(name);
33a7fbbf91SMatthew Dillon 		ptr = &bitmap[key / 32];
34a7fbbf91SMatthew Dillon 		mask = 1 << (key & 31);
3552e2f1b5STomohiro Kusumi 		if (*ptr & mask)
36a7fbbf91SMatthew Dillon 			break;
37a7fbbf91SMatthew Dillon 		*ptr |= mask;
38a7fbbf91SMatthew Dillon 		++count;
39a7fbbf91SMatthew Dillon 	}
40a7fbbf91SMatthew Dillon 	printf("duplicate found at count %d key %08x\n", count, key);
41a7fbbf91SMatthew Dillon 	printf("'%s' and", name);
42a7fbbf91SMatthew Dillon 	saved = key;
43a7fbbf91SMatthew Dillon 
44a7fbbf91SMatthew Dillon 	srandom(0);
45a7fbbf91SMatthew Dillon 	count = 0;
46a7fbbf91SMatthew Dillon 	for (;;) {
47a7fbbf91SMatthew Dillon 		randomname(name);
48a7fbbf91SMatthew Dillon 		key = namekey(name);
49a7fbbf91SMatthew Dillon 		if (saved == key)
50a7fbbf91SMatthew Dillon 			break;
51a7fbbf91SMatthew Dillon 		++count;
52a7fbbf91SMatthew Dillon 	}
53a7fbbf91SMatthew Dillon 	printf(" '%s'\n", name);
54*eccac4ffSTomohiro Kusumi 
55*eccac4ffSTomohiro Kusumi 	return(0);
56a7fbbf91SMatthew Dillon }
57a7fbbf91SMatthew Dillon 
58a7fbbf91SMatthew Dillon static
5946137e17STomohiro Kusumi uint32_t
namekey(const char * name)60a7fbbf91SMatthew Dillon namekey(const char *name)
61a7fbbf91SMatthew Dillon {
6246137e17STomohiro Kusumi 	uint32_t key;
63a7fbbf91SMatthew Dillon 
64a7fbbf91SMatthew Dillon 	key = crc32(name, strlen(name)) & 0x7FFFFFFF;
65a7fbbf91SMatthew Dillon 	if (key == 0)
66a7fbbf91SMatthew Dillon 		key = 1;
67a7fbbf91SMatthew Dillon 	return(key);
68a7fbbf91SMatthew Dillon }
69a7fbbf91SMatthew Dillon 
70a7fbbf91SMatthew Dillon static
71a7fbbf91SMatthew Dillon void
randomname(char * name)72a7fbbf91SMatthew Dillon randomname(char *name)
73a7fbbf91SMatthew Dillon {
74a7fbbf91SMatthew Dillon 	int len = random() % 16 + 8;
75a7fbbf91SMatthew Dillon 	int i;
76a7fbbf91SMatthew Dillon 
77a7fbbf91SMatthew Dillon 	for (i = 0; i < len; ++i)
78a7fbbf91SMatthew Dillon 		name[i] = random() % 26 + 'a';
79a7fbbf91SMatthew Dillon 	name[i] = 0;
80a7fbbf91SMatthew Dillon }
81a7fbbf91SMatthew Dillon 
82a7fbbf91SMatthew Dillon 
83