]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/hash/chi2_q_bit_flip_set.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / hash / chi2_q_bit_flip_set.cc
1 // Use smaller statistics when running on simulators, so it takes less time.
2 // { dg-options "-DSAMPLES=30000" { target simulator } }
3 // { dg-do run { target c++11 } }
4
5 // Copyright (C) 2010-2024 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
21
22 #include "chi2_quality.h"
23
24 // Tests chi^2 for a distribution of strings that differ from each
25 // other by only a few bits. We start with an arbitrary base string, and
26 // flip three random bits for each member of the set.
27 void
28 test_bit_flip_set()
29 {
30 const unsigned long N = SAMPLES;
31 const unsigned long k = N/100;
32 const unsigned int len = 67;
33 const unsigned int bitlen = len * 8;
34 const unsigned int bits_to_flip = 3;
35 const char base[len+1] = "abcdefghijklmnopqrstuvwxyz"
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37 "0123456789!@#$%";
38
39 std::unordered_set<std::string> set;
40 while (set.size() < N)
41 {
42 std::string s(base, base+len);
43 for (unsigned int i = 0; i < bits_to_flip; ++i)
44 {
45 int bit = rand() % bitlen;
46 s[bit/8] ^= (1 << (bit%8));
47 }
48 set.insert(s);
49 }
50
51 double chi2 = chi2_hash(set, k);
52 VERIFY( chi2 < k*1.1 );
53 }
54
55 int
56 main()
57 {
58 test_bit_flip_set();
59 return 0;
60 }