Debian Codegraph @

tests/random_string.cpp

raw
1 #include "fast_float/fast_float.h"
2
3 #include <cstdint>
4 #include <ios>
5 #include <iostream>
6 #include <random>
7 #include <system_error>
8 #include <utility>
9
10 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
11 defined(sun) || defined(__sun)
12 // Anything at all that is related to cygwin, msys and so forth will
13 // always use this fallback because we cannot rely on it behaving as normal
14 // gcc.
15 #include <locale>
16 #include <sstream>
17
18 // workaround for CYGWIN
19 double cygwin_strtod_l(char const *start, char **end) {
20 double d;
21 std::stringstream ss;
22 ss.imbue(std::locale::classic());
23 ss << start;
24 ss >> d;
25 if (ss.fail()) {
26 *end = nullptr;
27 }
28 if (ss.eof()) {
29 ss.clear();
30 }
31 auto nread = ss.tellg();
32 *end = const_cast<char *>(start) + nread;
33 return d;
34 }
35
36 float cygwin_strtof_l(char const *start, char **end) {
37 float d;
38 std::stringstream ss;
39 ss.imbue(std::locale::classic());
40 ss << start;
41 ss >> d;
42 if (ss.fail()) {
43 *end = nullptr;
44 }
45 if (ss.eof()) {
46 ss.clear();
47 }
48 auto nread = ss.tellg();
49 *end = const_cast<char *>(start) + nread;
50 return d;
51 }
52 #endif
53
54 class RandomEngine {
55 public:
56 RandomEngine() = delete;
57 RandomEngine(uint64_t new_seed) : wyhash64_x_(new_seed){};
58
59 uint64_t next() {
60 // Adapted from https://github.com/wangyi-fudan/wyhash/blob/master/wyhash.h
61 // Inspired from
62 // https://github.com/lemire/testingRNG/blob/master/source/wyhash.h
63 wyhash64_x_ += UINT64_C(0x60bee2bee120fc15);
64 fast_float::value128 tmp = fast_float::full_multiplication(
65 wyhash64_x_, UINT64_C(0xa3b195354a39b70d));
66 uint64_t m1 = (tmp.high) ^ tmp.low;
67 tmp = fast_float::full_multiplication(m1, UINT64_C(0x1b03738712fad5c9));
68 uint64_t m2 = (tmp.high) ^ tmp.low;
69 return m2;
70 }
71
72 bool next_bool() { return (next() & 1) == 1; }
73
74 int next_int() { return static_cast<int>(next()); }
75
76 char next_char() { return static_cast<char>(next()); }
77
78 double next_double() { return static_cast<double>(next()); }
79
80 int next_ranged_int(int min, int max) { // min and max are included
81 // Adapted from
82 // https://lemire.me/blog/2019/06/06/nearly-divisionless-random-integer-generation-on-various-systems/
83 /* if (min == max) {
84 return min;
85 }*/
86 uint64_t s = uint64_t(max - min + 1);
87 uint64_t x = next();
88 fast_float::value128 m = fast_float::full_multiplication(x, s);
89 uint64_t l = m.low;
90 if (l < s) {
91 uint64_t t = -s % s;
92 while (l < t) {
93 x = next();
94 m = fast_float::full_multiplication(x, s);
95 l = m.low;
96 }
97 }
98 return int(m.high) + min;
99 }
100
101 int next_digit() { return next_ranged_int(0, 9); }
102
103 private:
104 uint64_t wyhash64_x_;
105 };
106
107 size_t build_random_string(RandomEngine &rand, char *buffer) {
108 size_t pos{0};
109 if (rand.next_bool()) {
110 buffer[pos++] = '-';
111 }
112 int number_of_digits = rand.next_ranged_int(1, 100);
113 if (number_of_digits == 100) {
114 // With low probability, we want to allow very long strings just to stress
115 // the system.
116 number_of_digits = rand.next_ranged_int(1, 2000);
117 }
118 int location_of_decimal_separator = rand.next_ranged_int(1, number_of_digits);
119 for (size_t i = 0; i < size_t(number_of_digits); i++) {
120 if (i == size_t(location_of_decimal_separator)) {
121 buffer[pos++] = '.';
122 }
123 buffer[pos] = char(rand.next_digit() + '0');
124 // We can have a leading zero only if location_of_decimal_separator = 1.
125 while (i == 0 && 1 != size_t(location_of_decimal_separator) &&
126 buffer[pos] == '0') {
127 buffer[pos] = char(rand.next_digit() + '0');
128 }
129 pos++;
130 }
131 if (rand.next_bool()) {
132 if (rand.next_bool()) {
133 buffer[pos++] = 'e';
134 } else {
135 buffer[pos++] = 'E';
136 }
137 if (rand.next_bool()) {
138 buffer[pos++] = '-';
139 } else {
140 if (rand.next_bool()) {
141 buffer[pos++] = '+';
142 }
143 }
144 number_of_digits = rand.next_ranged_int(1, 3);
145 for (size_t i = 0; i < size_t(number_of_digits); i++) {
146 buffer[pos++] = char(rand.next_digit() + '0');
147 }
148 }
149 buffer[pos] = '\0'; // null termination
150 return pos;
151 }
152
153 std::pair<double, bool> strtod_from_string(char *st) {
154 double d;
155 char *pr;
156 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
157 defined(sun) || defined(__sun)
158 d = cygwin_strtod_l(st, &pr);
159 #elif defined(_WIN32)
160 static _locale_t c_locale = _create_locale(LC_ALL, "C");
161 d = _strtod_l(st, &pr, c_locale);
162 #else
163 static locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL);
164 d = strtod_l(st, &pr, c_locale);
165 #endif
166 if (st == pr) {
167 std::cerr << "strtod_l could not parse '" << st << std::endl;
168 return std::make_pair(0, false);
169 }
170 return std::make_pair(d, true);
171 }
172
173 std::pair<float, bool> strtof_from_string(char *st) {
174 float d;
175 char *pr;
176 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
177 defined(sun) || defined(__sun)
178 d = cygwin_strtof_l(st, &pr);
179 #elif defined(_WIN32)
180 static _locale_t c_locale = _create_locale(LC_ALL, "C");
181 d = _strtof_l(st, &pr, c_locale);
182 #else
183 static locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL);
184 d = strtof_l(st, &pr, c_locale);
185 #endif
186 if (st == pr) {
187 std::cerr << "strtof_l could not parse '" << st << std::endl;
188 return std::make_pair(0.0f, false);
189 }
190 return std::make_pair(d, true);
191 }
192
193 /**
194 * We generate random strings and we try to parse them with both strtod/strtof,
195 * and we verify that we get the same answer with with fast_float::from_chars.
196 */
197 bool tester(uint64_t seed, size_t volume) {
198 char buffer[4096]; // large buffer (can't overflow)
199 RandomEngine rand(seed);
200 for (size_t i = 0; i < volume; i++) {
201 if ((i % 100000) == 0) {
202 std::cout << ".";
203 std::cout.flush();
204 }
205 size_t length = build_random_string(rand, buffer);
206 std::pair<double, bool> expected_double = strtod_from_string(buffer);
207 if (expected_double.second) {
208 double result_value;
209 auto result =
210 fast_float::from_chars(buffer, buffer + length, result_value);
211 if (result.ec != std::errc() &&
212 result.ec != std::errc::result_out_of_range) {
213 printf("parsing %.*s\n", int(length), buffer);
214 std::cerr << " I could not parse " << std::endl;
215 return false;
216 }
217 if (result.ptr != buffer + length) {
218 printf("parsing %.*s\n", int(length), buffer);
219 std::cerr << " Did not get to the end " << std::endl;
220 return false;
221 }
222 if (result_value != expected_double.first) {
223 printf("parsing %.*s\n", int(length), buffer);
224 std::cerr << std::hexfloat << result_value << std::endl;
225 std::cerr << std::hexfloat << expected_double.first << std::endl;
226 std::cerr << " Mismatch " << std::endl;
227 return false;
228 }
229 }
230 std::pair<float, bool> expected_float = strtof_from_string(buffer);
231 if (expected_float.second) {
232 float result_value;
233 auto result =
234 fast_float::from_chars(buffer, buffer + length, result_value);
235 if (result.ec != std::errc() &&
236 result.ec != std::errc::result_out_of_range) {
237 printf("parsing %.*s\n", int(length), buffer);
238 std::cerr << " I could not parse " << std::endl;
239 return false;
240 }
241 if (result.ptr != buffer + length) {
242 printf("parsing %.*s\n", int(length), buffer);
243 std::cerr << " Did not get to the end " << std::endl;
244 return false;
245 }
246 if (result_value != expected_float.first) {
247 printf("parsing %.*s\n", int(length), buffer);
248 std::cerr << std::hexfloat << result_value << std::endl;
249 std::cerr << std::hexfloat << expected_float.first << std::endl;
250 std::cerr << " Mismatch " << std::endl;
251 return false;
252 }
253 }
254 }
255 return true;
256 }
257
258 int main() {
259
260 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
261 defined(sun) || defined(__sun)
262 std::cout << "Warning: msys/cygwin or solaris detected." << std::endl;
263 return EXIT_SUCCESS;
264 #else
265 if (tester(1234344, 100000000)) {
266 std::cout << "All tests ok." << std::endl;
267 return EXIT_SUCCESS;
268 }
269 std::cout << "Failure." << std::endl;
270 return EXIT_FAILURE;
271
272 #endif
273 }