1 #include <ccan/hash/hash.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/hash/hash.c>
9 int main(int argc, char *argv[])
12 uint32_t array[ARRAY_WORDS], val;
13 char array2[sizeof(array) + sizeof(uint32_t)];
14 uint32_t results[256];
16 /* Initialize array. */
17 for (i = 0; i < ARRAY_WORDS; i++)
21 /* Hash should be the same, indep of memory alignment. */
22 val = hash(array, ARRAY_WORDS, 0);
23 for (i = 0; i < sizeof(uint32_t); i++) {
24 memcpy(array2 + i, array, sizeof(array));
25 ok(hash(array2 + i, ARRAY_WORDS, 0) != val,
26 "hash matched at offset %i", i);
29 /* Hash of random values should have random distribution:
30 * check one byte at a time. */
31 for (i = 0; i < sizeof(uint32_t); i++) {
32 unsigned int lowest = -1U, highest = 0;
34 memset(results, 0, sizeof(results));
36 for (j = 0; j < 256000; j++) {
37 for (k = 0; k < ARRAY_WORDS; k++)
39 results[(hash(array, ARRAY_WORDS, 0) >> i*8)&0xFF]++;
42 for (j = 0; j < 256; j++) {
43 if (results[j] < lowest)
45 if (results[j] > highest)
48 /* Expect within 20% */
49 ok(lowest > 800, "Byte %i lowest %i", i, lowest);
50 ok(highest < 1200, "Byte %i highest %i", i, highest);
51 diag("Byte %i, range %u-%u", i, lowest, highest);
54 /* Hash of random values should have random distribution:
55 * check one byte at a time. */
56 for (i = 0; i < sizeof(uint64_t); i++) {
57 unsigned int lowest = -1U, highest = 0;
59 memset(results, 0, sizeof(results));
61 for (j = 0; j < 256000; j++) {
62 for (k = 0; k < ARRAY_WORDS; k++)
64 results[(hash64(array, sizeof(array)/sizeof(uint64_t),
68 for (j = 0; j < 256; j++) {
69 if (results[j] < lowest)
71 if (results[j] > highest)
74 /* Expect within 20% */
75 ok(lowest > 800, "Byte %i lowest %i", i, lowest);
76 ok(highest < 1200, "Byte %i highest %i", i, highest);
77 diag("Byte %i, range %u-%u", i, lowest, highest);
80 /* Hash of pointer values should also have random distribution. */
81 for (i = 0; i < sizeof(uint32_t); i++) {
82 unsigned int lowest = -1U, highest = 0;
83 char *p = malloc(256000);
85 memset(results, 0, sizeof(results));
87 for (j = 0; j < 256000; j++)
88 results[(hash_pointer(p + j, 0) >> i*8)&0xFF]++;
91 for (j = 0; j < 256; j++) {
92 if (results[j] < lowest)
94 if (results[j] > highest)
97 /* Expect within 20% */
98 ok(lowest > 800, "hash_pointer byte %i lowest %i", i, lowest);
99 ok(highest < 1200, "hash_pointer byte %i highest %i",
101 diag("hash_pointer byte %i, range %u-%u", i, lowest, highest);
104 if (sizeof(long) == sizeof(uint32_t))
105 ok1(hashl(array, ARRAY_WORDS, 0)
106 == hash(array, ARRAY_WORDS, 0));
108 ok1(hashl(array, ARRAY_WORDS, 0)
109 == hash64(array, ARRAY_WORDS, 0));
111 /* String hash: weak, so only test bottom byte */
112 for (i = 0; i < 1; i++) {
113 unsigned int num = 0, cursor, lowest = -1U, highest = 0;
116 memset(results, 0, sizeof(results));
118 memset(p, 'A', sizeof(p));
119 p[sizeof(p)-1] = '\0';
122 for (cursor = 0; cursor < sizeof(p)-1; cursor++) {
124 if (p[cursor] <= 'z')
128 if (cursor == sizeof(p)-1)
131 results[(hash_string(p) >> i*8)&0xFF]++;
135 for (j = 0; j < 256; j++) {
136 if (results[j] < lowest)
138 if (results[j] > highest)
139 highest = results[j];
141 /* Expect within 20% */
142 ok(lowest > 35000, "hash_pointer byte %i lowest %i", i, lowest);
143 ok(highest < 53000, "hash_pointer byte %i highest %i",
145 diag("hash_pointer byte %i, range %u-%u", i, lowest, highest);
148 return exit_status();