|
| 1 | +#include "fast_float/fast_float.h" |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | +#include <system_error> |
| 5 | + |
| 6 | +bool tester(std::string s, double expected, |
| 7 | + fast_float::chars_format fmt = fast_float::chars_format::general) { |
| 8 | + std::wstring input(s.begin(), s.end()); |
| 9 | + double result; |
| 10 | + auto answer = fast_float::from_chars( |
| 11 | + input.data(), input.data() + input.size(), result, fmt); |
| 12 | + if (answer.ec != std::errc()) { |
| 13 | + std::cerr << "parsing of \"" << s << "\" should succeed\n"; |
| 14 | + return false; |
| 15 | + } |
| 16 | + if (result != expected && !(std::isnan(result) && std::isnan(expected))) { |
| 17 | + std::cerr << "parsing of \"" << s << "\" succeeded, expected " << expected |
| 18 | + << " got " << result << "\n"; |
| 19 | + return false; |
| 20 | + } |
| 21 | + input[0] += 256; |
| 22 | + answer = fast_float::from_chars(input.data(), input.data() + input.size(), |
| 23 | + result, fmt); |
| 24 | + if (answer.ec == std::errc()) { |
| 25 | + std::cerr << "parsing of altered \"" << s << "\" should fail\n"; |
| 26 | + return false; |
| 27 | + } |
| 28 | + return true; |
| 29 | +} |
| 30 | + |
| 31 | +bool test_minus() { return tester("-42", -42); } |
| 32 | + |
| 33 | +bool test_plus() { |
| 34 | + return tester("+42", 42, |
| 35 | + fast_float::chars_format::general | |
| 36 | + fast_float::chars_format::allow_leading_plus); |
| 37 | +} |
| 38 | + |
| 39 | +bool test_space() { |
| 40 | + return tester(" 42", 42, |
| 41 | + fast_float::chars_format::general | |
| 42 | + fast_float::chars_format::skip_white_space); |
| 43 | +} |
| 44 | + |
| 45 | +bool test_nan() { |
| 46 | + return tester("nan", std::numeric_limits<double>::quiet_NaN()); |
| 47 | +} |
| 48 | + |
| 49 | +int main() { |
| 50 | + if (test_minus() && test_plus() && test_space() && test_nan()) { |
| 51 | + std::cout << "all ok" << std::endl; |
| 52 | + return EXIT_SUCCESS; |
| 53 | + } |
| 54 | + |
| 55 | + std::cout << "test failure" << std::endl; |
| 56 | + return EXIT_FAILURE; |
| 57 | +} |
0 commit comments