1 // { dg-options "-std=gnu++0x" }
3 // Copyright (C) 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // This file uses the chi^2 test to measure the quality of a hash
21 // function, by computing the uniformity with which it distributes a set
22 // of N strings into k buckets (where k is significantly greater than N).
24 // Each bucket has B[i] strings in it. The expected value of each bucket
25 // for a uniform distribution is z = N/k, so
26 // chi^2 = Sum_i (B[i] - z)^2 / z.
28 // We check whether chi^2 is small enough to be consistent with the
29 // hypothesis of a uniform distribution. If F(chi^2, k-1) is close to
30 // 0 (where F is the cumulative probability distribution), we can
31 // reject that hypothesis. So we don't want F to be too small, which
32 // for large k, means we want chi^2 to be not too much larger than k.
34 // We use the chi^2 test for several sets of strings. Any non-horrible
35 // hash function should do well with purely random strings. A really
36 // good hash function will also do well with more structured sets,
37 // including ones where the strings differ by only a few bits.
47 #include <unordered_set>
49 #include <testsuite_hooks.h>
51 // Use smaller statistics when running on simulators, so it takes less time.
52 // { dg-options "-DSAMPLES=10000" { target simulator } }
54 #define SAMPLES 300000
57 template <typename Container>
59 chi2_hash(const Container& c, long buckets)
61 std::vector<int> counts(buckets);
62 std::hash<std::string> hasher;
64 for (auto i = c.begin(); i != c.end(); ++i)
66 ++counts[hasher(*i) % buckets];
70 const double z = elements / buckets;
72 for (long i = 0; i < buckets; ++i)
74 double delta = counts[i] - z;
80 // Tests chi^2 for a distribution of uniformly generated random strings.
84 bool test __attribute__((unused)) = true;
86 std::unordered_set<std::string> set;
88 const unsigned long N = SAMPLES;
89 const unsigned long k = N/100;
90 const unsigned int len = 25;
91 while (set.size() < N)
94 for (unsigned int i = 0; i < len; ++i)
95 s.push_back(rand() % 128);
99 double chi2 = chi2_hash(set, k);
100 VERIFY( chi2 < k*1.1 );
103 // Tests chi^2 for a distribution of strings that differ from each
104 // other by only a few bits. We start with an arbitrary base string, and
105 // flip three random bits for each member of the set.
109 bool test __attribute__((unused)) = true;
110 const unsigned long N = SAMPLES;
111 const unsigned long k = N/100;
112 const unsigned int len = 67;
113 const unsigned int bitlen = len * 8;
114 const unsigned int bits_to_flip = 3;
115 const char base[len+1] = "abcdefghijklmnopqrstuvwxyz"
116 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
119 std::unordered_set<std::string> set;
120 while (set.size() < N)
122 std::string s(base, base+len);
123 for (unsigned int i = 0; i < bits_to_flip; ++i)
125 int bit = rand() % bitlen;
126 s[bit/8] ^= (1 << (bit%8));
131 double chi2 = chi2_hash(set, k);
132 VERIFY( chi2 < k*1.1 );
135 // Tests chi^2 of a set of strings that all have a similar pattern,
136 // intended to mimic some sort of ID string.
138 test_numeric_pattern_set()
140 bool test __attribute__((unused)) = true;
141 const unsigned long N = SAMPLES;
142 const unsigned long k = N/100;
143 std::vector<std::string> set;
144 for (unsigned long i = 0; i < N; ++i)
146 long i1 = i % 100000;
147 long i2 = i / 100000;
149 std::sprintf(buf, "XX-%05lu-%05lu", i1, i2);
153 double chi2 = chi2_hash(set, k);
154 VERIFY( chi2 < k*1.1 );
157 // Tests chi^2 for a set of strings that all consist of '1' and '0'.
159 test_bit_string_set()
161 bool test __attribute__((unused)) = true;
162 const unsigned long N = SAMPLES;
163 const unsigned long k = N/100;
164 std::vector<std::string> set;
166 for (unsigned long i = 0; i < N; ++i)
169 for (unsigned int j = 0; j < sizeof(unsigned long) * 8; ++j)
171 const bool bit = (1UL << j) & i;
172 s.push_back(bit ? '1' : '0');
177 double chi2 = chi2_hash(set, k);
178 VERIFY( chi2 < k*1.1 );
181 // Tests chi^2 for a set of words taken from a document written in English.
183 test_document_words()
185 bool test __attribute__((unused)) = true;
186 const std::string f_name = "thirty_years_among_the_dead_preproc.txt";
187 std::ifstream in(f_name);
188 VERIFY( in.is_open() );
189 std::vector<std::string> words;
190 words.assign(std::istream_iterator<std::string>(in),
191 std::istream_iterator<std::string>());
192 VERIFY( words.size() > 100000 );
193 std::sort(words.begin(), words.end());
194 auto it = std::unique(words.begin(), words.end());
195 words.erase(it, words.end());
196 VERIFY( words.size() > 5000 );
198 const unsigned long k = words.size() / 20;
199 double chi2 = chi2_hash(words, k);
200 VERIFY( chi2 < k*1.1 );
206 test_uniform_random();
208 test_numeric_pattern_set();
209 test_bit_string_set();
210 test_document_words();