OSDN Git Service

2011-02-28 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 20_util / hash / chi2_quality.cc
1 // { dg-options "-std=gnu++0x" }
2
3 // Use smaller statistics when running on simulators, so it takes less time.
4 // { dg-options "-std=gnu++0x -DSAMPLES=10000" { target simulator } }
5
6 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library.  This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this library; see the file COPYING3.  If not see
21 // <http://www.gnu.org/licenses/>.
22
23 // This file uses the chi^2 test to measure the quality of a hash
24 // function, by computing the uniformity with which it distributes a set
25 // of N strings into k buckets (where k is significantly greater than N).
26 //
27 // Each bucket has B[i] strings in it. The expected value of each bucket
28 // for a uniform distribution is z = N/k, so
29 //   chi^2 = Sum_i (B[i] - z)^2 / z.
30 //
31 // We check whether chi^2 is small enough to be consistent with the
32 // hypothesis of a uniform distribution. If F(chi^2, k-1) is close to
33 // 0 (where F is the cumulative probability distribution), we can
34 // reject that hypothesis. So we don't want F to be too small, which
35 // for large k, means we want chi^2 to be not too much larger than k.
36 //
37 // We use the chi^2 test for several sets of strings. Any non-horrible
38 // hash function should do well with purely random strings. A really
39 // good hash function will also do well with more structured sets,
40 // including ones where the strings differ by only a few bits.
41
42 #include <algorithm>
43 #include <cstdlib>
44 #include <cstdio>
45 #include <fstream>
46 #include <functional>
47 #include <iostream>
48 #include <iterator>
49 #include <string>
50 #include <unordered_set>
51 #include <vector>
52 #include <testsuite_hooks.h>
53
54 #ifndef SAMPLES
55 #define SAMPLES 300000
56 #endif
57
58 template <typename Container>
59   double
60   chi2_hash(const Container& c, long buckets)
61   {
62     std::vector<int> counts(buckets);
63     std::hash<std::string> hasher;
64     double elements = 0;
65     for (auto i = c.begin(); i != c.end(); ++i)
66       {
67         ++counts[hasher(*i) % buckets];
68         ++elements;
69       }
70
71     const double z = elements / buckets;
72     double sum = 0;
73     for (long i = 0; i < buckets; ++i)
74       {
75         double delta = counts[i] - z;
76         sum += delta*delta;
77       }
78     return sum/z;
79   }
80
81 // Tests chi^2 for a distribution of uniformly generated random strings.
82 void
83 test_uniform_random()
84 {
85   bool test __attribute__((unused)) = true;
86   std::srand(137);
87   std::unordered_set<std::string> set;
88   std::string s;
89   const unsigned long N = SAMPLES;
90   const unsigned long k = N/100;
91   const unsigned int len = 25;
92   while (set.size() < N)
93     {
94       s.clear();
95       for (unsigned int i = 0; i < len; ++i)
96         s.push_back(rand() % 128);
97       set.insert(s);
98     }
99
100   double chi2 = chi2_hash(set, k);
101   VERIFY( chi2 < k*1.1 );
102 }
103
104 // Tests chi^2 for a distribution of strings that differ from each
105 // other by only a few bits. We start with an arbitrary base string, and
106 // flip three random bits for each member of the set.
107 void
108 test_bit_flip_set()
109 {
110   bool test __attribute__((unused)) = true;
111   const unsigned long N = SAMPLES;
112   const unsigned long k = N/100;
113   const unsigned int len = 67;
114   const unsigned int bitlen = len * 8;
115   const unsigned int bits_to_flip = 3;
116   const char base[len+1] = "abcdefghijklmnopqrstuvwxyz"
117                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
118                            "0123456789!@#$%";
119
120   std::unordered_set<std::string> set;
121   while (set.size() < N)
122     {
123       std::string s(base, base+len);
124       for (unsigned int i = 0; i < bits_to_flip; ++i)
125         {
126           int bit = rand() % bitlen;
127           s[bit/8] ^= (1 << (bit%8));
128         }
129       set.insert(s);
130     }
131
132   double chi2 = chi2_hash(set, k);
133   VERIFY( chi2 < k*1.1 );
134 }
135
136 // Tests chi^2 of a set of strings that all have a similar pattern,
137 // intended to mimic some sort of ID string.
138 void
139 test_numeric_pattern_set()
140 {
141   bool test __attribute__((unused)) = true;
142   const unsigned long N = SAMPLES;
143   const unsigned long k = N/100;
144   std::vector<std::string> set;
145   for (unsigned long i = 0; i < N; ++i)
146     {
147       long i1 = i % 100000;
148       long i2 = i / 100000;
149       char buf[16];
150       std::sprintf(buf, "XX-%05lu-%05lu", i1, i2);
151       set.push_back(buf);
152     }
153
154   double chi2 = chi2_hash(set, k);
155   VERIFY( chi2 < k*1.1 );
156 }
157
158 // Tests chi^2 for a set of strings that all consist of '1' and '0'.
159 void
160 test_bit_string_set()
161 {
162   bool test __attribute__((unused)) = true;
163   const unsigned long N = SAMPLES;
164   const unsigned long k = N/100;
165   std::vector<std::string> set;
166   std::string s;
167   for (unsigned long i = 0; i < N; ++i)
168     {
169       s.clear();
170       for (unsigned int j = 0; j < sizeof(unsigned long) * 8; ++j)
171         {
172           const bool bit = (1UL << j) & i;
173           s.push_back(bit ? '1' : '0');
174         }
175       set.push_back(s);
176     }
177
178   double chi2 = chi2_hash(set, k);
179   VERIFY( chi2 < k*1.1 );
180 }
181
182 // Tests chi^2 for a set of words taken from a document written in English.
183 void
184 test_document_words()
185 {
186   bool test __attribute__((unused)) = true;
187   const std::string f_name = "thirty_years_among_the_dead_preproc.txt";
188   std::ifstream in(f_name);
189   VERIFY( in.is_open() );
190   std::vector<std::string> words;
191   words.assign(std::istream_iterator<std::string>(in),
192                std::istream_iterator<std::string>());
193   VERIFY( words.size() > 100000 );
194   std::sort(words.begin(), words.end());
195   auto it = std::unique(words.begin(), words.end());
196   words.erase(it, words.end());
197   VERIFY( words.size() > 5000 );
198
199   const unsigned long k = words.size() / 20;
200   double chi2 = chi2_hash(words, k);
201   VERIFY( chi2 < k*1.1 );
202 }
203
204 int
205 main()
206 {
207   test_uniform_random();
208   test_bit_flip_set();
209   test_numeric_pattern_set();
210   test_bit_string_set();
211   test_document_words();
212   return 0;
213 }