HAMMER Utilities: Sync with 59A
[dragonfly.git] / sbin / hammer / test_dupkey.c
blob56916f5fa27a38fa427b9e67af905735b06de407
1 /*
2 * This is a really simple stupid standalone program which will find two
3 * filenames with the same CRC, used to test the directory iterator.
5 * cc -I /usr/src/sys test_dupkey.c /usr/src/sys/libkern/crc32.c -o test_dupkey
7 * $DragonFly: src/sbin/hammer/test_dupkey.c,v 1.1 2008/06/26 04:07:57 dillon Exp $
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include "hammer_util.h"
15 static u_int32_t namekey(const char *name);
16 static void randomname(char *name);
18 u_int32_t bitmap[0x80000000U / 32];
20 int
21 main(int ac, char **av)
23 char name[32];
24 u_int32_t key;
25 u_int32_t *ptr;
26 u_int32_t mask;
27 u_int32_t count;
28 u_int32_t saved;
30 srandom(0); /* reproducable random sequence number */
31 count = 0;
32 for (;;) {
33 randomname(name);
34 key = namekey(name);
35 ptr = &bitmap[key / 32];
36 mask = 1 << (key & 31);
37 if (*ptr & mask) {
38 break;
40 *ptr |= mask;
41 ++count;
43 printf("duplicate found at count %d key %08x\n", count, key);
44 printf("'%s' and", name);
45 saved = key;
47 srandom(0);
48 count = 0;
49 for (;;) {
50 randomname(name);
51 key = namekey(name);
52 if (saved == key)
53 break;
54 ++count;
56 printf(" '%s'\n", name);
59 static
60 u_int32_t
61 namekey(const char *name)
63 u_int32_t key;
65 key = crc32(name, strlen(name)) & 0x7FFFFFFF;
66 if (key == 0)
67 key = 1;
68 return(key);
71 static
72 void
73 randomname(char *name)
75 int len = random() % 16 + 8;
76 int i;
78 for (i = 0; i < len; ++i)
79 name[i] = random() % 26 + 'a';
80 name[i] = 0;