| 1 | // { dg-options "-DNTESTS=1 -DNSTRINGS=100 -DSTRSIZE=21" { target simulator } } |
| 2 | // { dg-do run { target c++11 } } |
| 3 | // { dg-timeout-factor 3 } |
| 4 | |
| 5 | // Copyright (C) 2010-2025 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 <cstdlib> |
| 23 | #include <unordered_set> |
| 24 | #include <string> |
| 25 | #include <functional> |
| 26 | #include <vector> |
| 27 | #include <testsuite_hooks.h> |
| 28 | |
| 29 | using namespace std; |
| 30 | |
| 31 | #ifndef NTESTS |
| 32 | #define NTESTS 5 |
| 33 | #endif |
| 34 | #ifndef NSTRINGS |
| 35 | #define NSTRINGS 200 |
| 36 | #endif |
| 37 | #ifndef STRSIZE |
| 38 | #define STRSIZE 42 |
| 39 | #endif |
| 40 | |
| 41 | const unsigned int num_quality_tests = NTESTS; |
| 42 | const unsigned int num_strings_for_quality_tests = NSTRINGS; |
| 43 | const unsigned int string_size = STRSIZE; |
| 44 | |
| 45 | vector<string> |
| 46 | random_strings(unsigned int n, unsigned int len) |
| 47 | { |
| 48 | string s(len, '\0'); |
| 49 | unordered_set<string> result_set; |
| 50 | while (result_set.size() < n) |
| 51 | { |
| 52 | result_set.insert(s); |
| 53 | unsigned int tmp = rand(); |
| 54 | tmp %= len * 256; |
| 55 | s[tmp / 256] = tmp % 256; |
| 56 | } |
| 57 | return vector<string>(result_set.begin(), result_set.end()); |
| 58 | } |
| 59 | |
| 60 | double |
| 61 | score_from_varying_position(string s, unsigned int index) |
| 62 | { |
| 63 | unsigned int bits_in_hash_code = sizeof(size_t) * 8; |
| 64 | |
| 65 | // We'll iterate through all 256 vals for s[index], leaving the rest |
| 66 | // of s fixed. Then, for example, out of the 128 times that |
| 67 | // s[index] has its 3rd bit equal to 0 we would like roughly half 1s |
| 68 | // and half 0s in bit 9 of the hash codes. |
| 69 | // |
| 70 | // Bookkeeping: Conceptually we want a 3D array of ints. We want to |
| 71 | // count the number of times each output position (of which there are |
| 72 | // bits_in_hash_code) is 1 for each bit position within s[index] (of |
| 73 | // which there are 8) and value of that bit (of which there are 2). |
| 74 | const unsigned int jj = 2; |
| 75 | const unsigned int kk = jj * bits_in_hash_code; |
| 76 | const unsigned int array_size = 8 * kk; |
| 77 | vector<int> ones(array_size, 0); |
| 78 | |
| 79 | for (int i = 0; i < 256; i++) |
| 80 | { |
| 81 | s[index] = i; |
| 82 | size_t h = hash<string>()(s); |
| 83 | for (int j = 0; h != 0; j++, h >>= 1) |
| 84 | { |
| 85 | if (h & 1) |
| 86 | { |
| 87 | for (int k = 0; k < 8; k++) |
| 88 | ++ones[k * kk + j * jj + ((i >> k) & 1)]; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // At most, the innermost statement in the above loop nest can |
| 94 | // execute 256 * bits_in_hash_code * 8 times. If the hash is good, |
| 95 | // it'll execute about half that many times, with a pretty even |
| 96 | // spread across the elements of ones[]. |
| 97 | VERIFY( 256 * bits_in_hash_code * 8 / array_size == 128 ); |
| 98 | int max_ones_possible = 128; |
| 99 | int good = 0, bad = 0; |
| 100 | for (int bit = 0; bit <= 1; bit++) |
| 101 | { |
| 102 | for (unsigned int j = 0; j < bits_in_hash_code; j++) |
| 103 | { |
| 104 | for (int bitpos = 0; bitpos < 8; bitpos++) |
| 105 | { |
| 106 | int z = ones[bitpos * kk + j * jj + bit]; |
| 107 | if (z <= max_ones_possible / 6 |
| 108 | || z >= max_ones_possible * 5 / 6) |
| 109 | { |
| 110 | // The hash function screwed up, or was just unlucky, |
| 111 | // as 128 flips of a perfect coin occasionally yield |
| 112 | // far from 64 heads. |
| 113 | bad++; |
| 114 | } |
| 115 | else |
| 116 | good++; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return good / (double)(good + bad); |
| 121 | } |
| 122 | |
| 123 | double |
| 124 | score_from_varying_position(const vector<string>& v, unsigned int index) |
| 125 | { |
| 126 | double score = 0; |
| 127 | for (unsigned int i = 0; i < v.size(); i++) |
| 128 | score += score_from_varying_position(v[i], index); |
| 129 | return score / v.size(); |
| 130 | } |
| 131 | |
| 132 | double |
| 133 | quality_test(unsigned int num_strings, unsigned int string_size) |
| 134 | { |
| 135 | // Construct random strings. |
| 136 | vector<string> v = random_strings(num_strings, string_size); |
| 137 | double sum_of_scores = 0; |
| 138 | for (unsigned int i = 0; i < string_size; i++) |
| 139 | sum_of_scores += score_from_varying_position(v, i); |
| 140 | |
| 141 | // A good hash function should have a score very close to 1, and a bad |
| 142 | // hash function will have a score close to 0. |
| 143 | return sum_of_scores / string_size; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | quality_test() |
| 148 | { |
| 149 | srand(137); |
| 150 | double sum_of_scores = 0; |
| 151 | for (unsigned int i = 0; i < num_quality_tests; i++) |
| 152 | { |
| 153 | double score = quality_test(num_strings_for_quality_tests, |
| 154 | string_size); |
| 155 | sum_of_scores += score; |
| 156 | VERIFY( score > 0.99 ); |
| 157 | } |
| 158 | |
| 159 | if (num_quality_tests > 1) |
| 160 | { |
| 161 | double mean_quality = sum_of_scores / num_quality_tests; |
| 162 | VERIFY( mean_quality > 0.9999 ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | int |
| 167 | main() |
| 168 | { |
| 169 | quality_test(); |
| 170 | return 0; |
| 171 | } |