d53ba2eadd3a71fb5a233b932db574ce61d74a76
[sliver-openvswitch.git] / tests / test-hash.c
1 /*
2  * Copyright (c) 2009, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include <inttypes.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "hash.h"
23
24 #undef NDEBUG
25 #include <assert.h>
26
27 static void
28 set_bit(uint32_t array[3], int bit)
29 {
30     assert(bit >= 0 && bit <= 96);
31     memset(array, 0, sizeof(uint32_t) * 3);
32     if (bit < 96) {
33         array[bit / 32] = UINT32_C(1) << (bit % 32);
34     }
35 }
36
37 static uint32_t
38 hash_words_cb(uint32_t input)
39 {
40     return hash_words(&input, 1, 0);
41 }
42
43 static uint32_t
44 mhash_words_cb(uint32_t input)
45 {
46     return mhash_words(&input, 1, 0);
47 }
48
49 static uint32_t
50 hash_int_cb(uint32_t input)
51 {
52     return hash_int(input, 0);
53 }
54
55 static void
56 check_word_hash(uint32_t (*hash)(uint32_t), const char *name,
57                 int min_unique)
58 {
59     int i, j;
60
61     for (i = 0; i <= 32; i++) {
62         uint32_t in1 = i < 32 ? UINT32_C(1) << i : 0;
63         for (j = i + 1; j <= 32; j++) {
64             uint32_t in2 = j < 32 ? UINT32_C(1) << j : 0;
65             uint32_t out1 = hash(in1);
66             uint32_t out2 = hash(in2);
67             const uint32_t unique_mask = (UINT32_C(1) << min_unique) - 1;
68             int ofs;
69             for (ofs = 0; ofs < 32 - min_unique; ofs++) {
70                 uint32_t bits1 = (out1 >> ofs) & unique_mask;
71                 uint32_t bits2 = (out2 >> ofs) & unique_mask;
72                 if (bits1 == bits2) {
73                     printf("Partial collision for '%s':\n", name);
74                     printf("%s(%08"PRIx32") = %08"PRIx32"\n", name, in1, out1);
75                     printf("%s(%08"PRIx32") = %08"PRIx32"\n", name, in2, out2);
76                     printf("%d bits of output starting at bit %d "
77                            "are both 0x%"PRIx32"\n", min_unique, ofs, bits1);
78                     exit(1);
79                 }
80             }
81         }
82     }
83 }
84
85 static void
86 check_3word_hash(uint32_t (*hash)(const uint32_t[], size_t, uint32_t),
87                  const char *name)
88 {
89     int i, j;
90
91     for (i = 0; i <= 96; i++) {
92         for (j = i + 1; j <= 96; j++) {
93             uint32_t in1[3], in2[3];
94             uint32_t out1, out2;
95             const int min_unique = 12;
96             const uint32_t unique_mask = (UINT32_C(1) << min_unique) - 1;
97
98             set_bit(in1, i);
99             set_bit(in2, j);
100             out1 = hash(in1, 3, 0);
101             out2 = hash(in2, 3, 0);
102             if ((out1 & unique_mask) == (out2 & unique_mask)) {
103                 printf("%s has a partial collision:\n", name);
104                 printf("hash(1 << %d) == %08"PRIx32"\n", i, out1);
105                 printf("hash(1 << %d) == %08"PRIx32"\n", j, out2);
106                 printf("The low-order %d bits of output are both "
107                        "0x%"PRIx32"\n", min_unique, out1 & unique_mask);
108             }
109         }
110     }
111 }
112
113 int
114 main(void)
115 {
116     /* Check that all hashes computed with hash_words with one 1-bit (or no
117      * 1-bits) set within a single 32-bit word have different values in all
118      * 11-bit consecutive runs.
119      *
120      * Given a random distribution, the probability of at least one collision
121      * in any set of 11 bits is approximately
122      *
123      *                      1 - ((2**11 - 1)/2**11)**C(33,2)
124      *                   == 1 - (2047/2048)**528
125      *                   =~ 0.22
126      *
127      * There are 21 ways to pick 11 consecutive bits in a 32-bit word, so if we
128      * assumed independence then the chance of having no collisions in any of
129      * those 11-bit runs would be (1-0.22)**21 =~ .0044.  Obviously
130      * independence must be a bad assumption :-)
131      */
132     check_word_hash(hash_words_cb, "hash_words", 11);
133     check_word_hash(mhash_words_cb, "mhash_words", 11);
134
135     /* Check that all hash functions of with one 1-bit (or no 1-bits) set
136      * within three 32-bit words have different values in their lowest 12
137      * bits.
138      *
139      * Given a random distribution, the probability of at least one collision
140      * in 12 bits is approximately
141      *
142      *                      1 - ((2**12 - 1)/2**12)**C(97,2)
143      *                   == 1 - (4095/4096)**4656
144      *                   =~ 0.68
145      *
146      * so we are doing pretty well to not have any collisions in 12 bits.
147      */
148     check_3word_hash(hash_words, "hash_words");
149     check_3word_hash(mhash_words, "mhash_words");
150
151     /* Check that all hashes computed with hash_int with one 1-bit (or no
152      * 1-bits) set within a single 32-bit word have different values in all
153      * 14-bit consecutive runs.
154      *
155      * Given a random distribution, the probability of at least one collision
156      * in any set of 14 bits is approximately
157      *
158      *                      1 - ((2**14 - 1)/2**14)**C(33,2)
159      *                   == 1 - (16,383/16,834)**528
160      *                   =~ 0.031
161      *
162      * There are 18 ways to pick 14 consecutive bits in a 32-bit word, so if we
163      * assumed independence then the chance of having no collisions in any of
164      * those 14-bit runs would be (1-0.03)**18 =~ 0.56.  This seems reasonable.
165      */
166     check_word_hash(hash_int_cb, "hash_int", 14);
167
168     return 0;
169 }