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, sizeof(array), 0);
23 for (i = 0; i < sizeof(uint32_t); i++) {
24 memcpy(array2 + i, array, sizeof(array));
25 ok(hash(array2 + i, sizeof(array), 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, sizeof(array), 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), 0) >> i*8)&0xFF]++;
67 for (j = 0; j < 256; j++) {
68 if (results[j] < lowest)
70 if (results[j] > highest)
73 /* Expect within 20% */
74 ok(lowest > 800, "Byte %i lowest %i", i, lowest);
75 ok(highest < 1200, "Byte %i highest %i", i, highest);
76 diag("Byte %i, range %u-%u", i, lowest, highest);
79 /* Hash of pointer values should also have random distribution. */
80 for (i = 0; i < sizeof(uint32_t); i++) {
81 unsigned int lowest = -1U, highest = 0;
82 char *p = malloc(256000);
84 memset(results, 0, sizeof(results));
86 for (j = 0; j < 256000; j++)
87 results[(hash_pointer(p + j, 0) >> i*8)&0xFF]++;
90 for (j = 0; j < 256; j++) {
91 if (results[j] < lowest)
93 if (results[j] > highest)
96 /* Expect within 20% */
97 ok(lowest > 800, "hash_pointer byte %i lowest %i", i, lowest);
98 ok(highest < 1200, "hash_pointer byte %i highest %i",
100 diag("hash_pointer byte %i, range %u-%u", i, lowest, highest);
103 if (sizeof(long) == sizeof(uint32_t))
104 ok1(hashl(array, sizeof(array), 0)
105 == hash(array, sizeof(array), 0));
107 ok1(hashl(array, sizeof(array), 0)
108 == hash64(array, sizeof(array), 0));
110 /* String hash: weak, so only test bottom byte */
111 for (i = 0; i < 1; i++) {
112 unsigned int num = 0, cursor, lowest = -1U, highest = 0;
115 memset(results, 0, sizeof(results));
117 memset(p, 'A', sizeof(p));
118 p[sizeof(p)-1] = '\0';
121 for (cursor = 0; cursor < sizeof(p)-1; cursor++) {
123 if (p[cursor] <= 'z')
127 if (cursor == sizeof(p)-1)
130 results[(hash_string(p) >> i*8)&0xFF]++;
134 for (j = 0; j < 256; j++) {
135 if (results[j] < lowest)
137 if (results[j] > highest)
138 highest = results[j];
140 /* Expect within 20% */
141 ok(lowest > 35000, "hash_pointer byte %i lowest %i", i, lowest);
142 ok(highest < 53000, "hash_pointer byte %i highest %i",
144 diag("hash_pointer byte %i, range %u-%u", i, lowest, highest);
147 return exit_status();