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 pointer values should also have random distribution. */
55 for (i = 0; i < sizeof(uint32_t); i++) {
56 unsigned int lowest = -1U, highest = 0;
57 char *p = malloc(256000);
59 memset(results, 0, sizeof(results));
61 for (j = 0; j < 256000; j++)
62 results[(hash_pointer(p + j, 0) >> i*8)&0xFF]++;
65 for (j = 0; j < 256; j++) {
66 if (results[j] < lowest)
68 if (results[j] > highest)
71 /* Expect within 20% */
72 ok(lowest > 800, "hash_pointer byte %i lowest %i", i, lowest);
73 ok(highest < 1200, "hash_pointer byte %i highest %i",
75 diag("hash_pointer byte %i, range %u-%u", i, lowest, highest);
78 /* String hash: weak, so only test bottom byte */
79 for (i = 0; i < 1; i++) {
80 unsigned int num = 0, cursor, lowest = -1U, highest = 0;
83 memset(results, 0, sizeof(results));
85 memset(p, 'A', sizeof(p));
86 p[sizeof(p)-1] = '\0';
89 for (cursor = 0; cursor < sizeof(p)-1; cursor++) {
95 if (cursor == sizeof(p)-1)
98 results[(hash_string(p) >> i*8)&0xFF]++;
102 for (j = 0; j < 256; j++) {
103 if (results[j] < lowest)
105 if (results[j] > highest)
106 highest = results[j];
108 /* Expect within 20% */
109 ok(lowest > 35000, "hash_pointer byte %i lowest %i", i, lowest);
110 ok(highest < 53000, "hash_pointer byte %i highest %i",
112 diag("hash_pointer byte %i, range %u-%u", i, lowest, highest);
115 return exit_status();