| 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, 19); |
| 113 |
int location_of_decimal_separator = rand.next_ranged_int(1, number_of_digits); |
| 114 |
for (size_t i = 0; i < size_t(number_of_digits); i++) { |
| 115 |
if (i == size_t(location_of_decimal_separator)) { |
| 116 |
buffer[pos++] = '.'; |
| 117 |
} |
| 118 |
buffer[pos] = char(rand.next_digit() + '0'); |
| 119 |
// We can have a leading zero only if location_of_decimal_separator = 1. |
| 120 |
while (i == 0 && 1 != size_t(location_of_decimal_separator) && |
| 121 |
buffer[pos] == '0') { |
| 122 |
buffer[pos] = char(rand.next_digit() + '0'); |
| 123 |
} |
| 124 |
pos++; |
| 125 |
} |
| 126 |
if (rand.next_bool()) { |
| 127 |
if (rand.next_bool()) { |
| 128 |
buffer[pos++] = 'e'; |
| 129 |
} else { |
| 130 |
buffer[pos++] = 'E'; |
| 131 |
} |
| 132 |
if (rand.next_bool()) { |
| 133 |
buffer[pos++] = '-'; |
| 134 |
} else { |
| 135 |
if (rand.next_bool()) { |
| 136 |
buffer[pos++] = '+'; |
| 137 |
} |
| 138 |
} |
| 139 |
number_of_digits = rand.next_ranged_int(1, 3); |
| 140 |
for (size_t i = 0; i < size_t(number_of_digits); i++) { |
| 141 |
buffer[pos++] = char(rand.next_digit() + '0'); |
| 142 |
} |
| 143 |
} |
| 144 |
buffer[pos] = '\0'; // null termination |
| 145 |
return pos; |
| 146 |
} |
| 147 |
|
| 148 |
std::pair<double, bool> strtod_from_string(char *st) { |
| 149 |
double d; |
| 150 |
char *pr; |
| 151 |
#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \ |
| 152 |
defined(sun) || defined(__sun) |
| 153 |
d = cygwin_strtod_l(st, &pr); |
| 154 |
#elif defined(_WIN32) |
| 155 |
static _locale_t c_locale = _create_locale(LC_ALL, "C"); |
| 156 |
d = _strtod_l(st, &pr, c_locale); |
| 157 |
#else |
| 158 |
static locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL); |
| 159 |
d = strtod_l(st, &pr, c_locale); |
| 160 |
#endif |
| 161 |
if (st == pr) { |
| 162 |
std::cerr << "strtod_l could not parse '" << st << std::endl; |
| 163 |
return std::make_pair(0, false); |
| 164 |
} |
| 165 |
return std::make_pair(d, true); |
| 166 |
} |
| 167 |
|
| 168 |
std::pair<float, bool> strtof_from_string(char *st) { |
| 169 |
float d; |
| 170 |
char *pr; |
| 171 |
#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \ |
| 172 |
defined(sun) || defined(__sun) |
| 173 |
d = cygwin_strtof_l(st, &pr); |
| 174 |
#elif defined(_WIN32) |
| 175 |
static _locale_t c_locale = _create_locale(LC_ALL, "C"); |
| 176 |
d = _strtof_l(st, &pr, c_locale); |
| 177 |
#else |
| 178 |
static locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL); |
| 179 |
d = strtof_l(st, &pr, c_locale); |
| 180 |
#endif |
| 181 |
if (st == pr) { |
| 182 |
std::cerr << "strtof_l could not parse '" << st << std::endl; |
| 183 |
return std::make_pair(0.0f, false); |
| 184 |
} |
| 185 |
return std::make_pair(d, true); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* We generate random strings and we try to parse them with both strtod/strtof, |
| 190 |
* and we verify that we get the same answer with with fast_float::from_chars. |
| 191 |
*/ |
| 192 |
bool tester(uint64_t seed, size_t volume) { |
| 193 |
char buffer[4096]; // large buffer (can't overflow) |
| 194 |
RandomEngine rand(seed); |
| 195 |
for (size_t i = 0; i < volume; i++) { |
| 196 |
if ((i % 1000000) == 0) { |
| 197 |
std::cout << "."; |
| 198 |
std::cout.flush(); |
| 199 |
} |
| 200 |
size_t length = build_random_string(rand, buffer); |
| 201 |
std::pair<double, bool> expected_double = strtod_from_string(buffer); |
| 202 |
if (expected_double.second) { |
| 203 |
double result_value; |
| 204 |
auto result = |
| 205 |
fast_float::from_chars(buffer, buffer + length, result_value); |
| 206 |
if (result.ec != std::errc() && |
| 207 |
result.ec != std::errc::result_out_of_range) { |
| 208 |
printf("parsing %.*s\n", int(length), buffer); |
| 209 |
std::cerr << " I could not parse " << std::endl; |
| 210 |
return false; |
| 211 |
} |
| 212 |
if (result.ptr != buffer + length) { |
| 213 |
printf("parsing %.*s\n", int(length), buffer); |
| 214 |
std::cerr << " Did not get to the end " << std::endl; |
| 215 |
return false; |
| 216 |
} |
| 217 |
if (result_value != expected_double.first) { |
| 218 |
printf("parsing %.*s\n", int(length), buffer); |
| 219 |
std::cerr << std::hexfloat << result_value << std::endl; |
| 220 |
std::cerr << std::hexfloat << expected_double.first << std::endl; |
| 221 |
std::cerr << " Mismatch " << std::endl; |
| 222 |
return false; |
| 223 |
} |
| 224 |
} |
| 225 |
std::pair<float, bool> expected_float = strtof_from_string(buffer); |
| 226 |
if (expected_float.second) { |
| 227 |
float result_value; |
| 228 |
auto result = |
| 229 |
fast_float::from_chars(buffer, buffer + length, result_value); |
| 230 |
if (result.ec != std::errc() && |
| 231 |
result.ec != std::errc::result_out_of_range) { |
| 232 |
printf("parsing %.*s\n", int(length), buffer); |
| 233 |
std::cerr << " I could not parse " << std::endl; |
| 234 |
return false; |
| 235 |
} |
| 236 |
if (result.ptr != buffer + length) { |
| 237 |
printf("parsing %.*s\n", int(length), buffer); |
| 238 |
std::cerr << " Did not get to the end " << std::endl; |
| 239 |
return false; |
| 240 |
} |
| 241 |
if (result_value != expected_float.first) { |
| 242 |
printf("parsing %.*s\n", int(length), buffer); |
| 243 |
std::cerr << std::hexfloat << result_value << std::endl; |
| 244 |
std::cerr << std::hexfloat << expected_float.first << std::endl; |
| 245 |
std::cerr << " Mismatch " << std::endl; |
| 246 |
return false; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
int main() { |
| 254 |
#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \ |
| 255 |
defined(sun) || defined(__sun) |
| 256 |
std::cout << "Warning: msys/cygwin detected. This particular test is likely " |
| 257 |
"to generate false failures due to our reliance on the " |
| 258 |
"underlying runtime library." |
| 259 |
<< std::endl; |
| 260 |
return EXIT_SUCCESS; |
| 261 |
#else |
| 262 |
if (tester(1234344, 100000000)) { |
| 263 |
std::cout << "All tests ok." << std::endl; |
| 264 |
return EXIT_SUCCESS; |
| 265 |
} |
| 266 |
return EXIT_FAILURE; |
| 267 |
|
| 268 |
#endif |
| 269 |
} |