| 1 |
/** |
| 2 |
* See https://github.com/eddelbuettel/rcppfastfloat/issues/4 |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "fast_float/fast_float.h" |
| 6 |
#include <iostream> |
| 7 |
#include <string> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
struct test_data { |
| 11 |
std::string input; |
| 12 |
bool expected_success; |
| 13 |
double expected_result; |
| 14 |
}; |
| 15 |
|
| 16 |
bool eddelbuettel() { |
| 17 |
std::vector<test_data> const test_datas = { |
| 18 |
{"infinity", true, std::numeric_limits<double>::infinity()}, |
| 19 |
{" \r\n\t\f\v3.16227766016838 \r\n\t\f\v", true, 3.16227766016838}, |
| 20 |
{" \r\n\t\f\v3 \r\n\t\f\v", true, 3.0}, |
| 21 |
{" 1970-01-01", false, 0.0}, |
| 22 |
{"-NaN", true, std::numeric_limits<double>::quiet_NaN()}, |
| 23 |
{"-inf", true, -std::numeric_limits<double>::infinity()}, |
| 24 |
{" \r\n\t\f\v2.82842712474619 \r\n\t\f\v", true, 2.82842712474619}, |
| 25 |
{"nan", true, std::numeric_limits<double>::quiet_NaN()}, |
| 26 |
{" \r\n\t\f\v2.44948974278318 \r\n\t\f\v", true, 2.44948974278318}, |
| 27 |
{"Inf", true, std::numeric_limits<double>::infinity()}, |
| 28 |
{" \r\n\t\f\v2 \r\n\t\f\v", true, 2.0}, |
| 29 |
{"-infinity", true, -std::numeric_limits<double>::infinity()}, |
| 30 |
{" \r\n\t\f\v0 \r\n\t\f\v", true, 0.0}, |
| 31 |
{" \r\n\t\f\v1.73205080756888 \r\n\t\f\v", true, 1.73205080756888}, |
| 32 |
{" \r\n\t\f\v1 \r\n\t\f\v", true, 1.0}, |
| 33 |
{" \r\n\t\f\v1.4142135623731 \r\n\t\f\v", true, 1.4142135623731}, |
| 34 |
{" \r\n\t\f\v2.23606797749979 \r\n\t\f\v", true, 2.23606797749979}, |
| 35 |
{"1970-01-02 ", false, 0.0}, |
| 36 |
{" \r\n\t\f\v2.64575131106459 \r\n\t\f\v", true, 2.64575131106459}, |
| 37 |
{"inf", true, std::numeric_limits<double>::infinity()}, |
| 38 |
{"-nan", true, std::numeric_limits<double>::quiet_NaN()}, |
| 39 |
{"NaN", true, std::numeric_limits<double>::quiet_NaN()}, |
| 40 |
{"", false, 0.0}, |
| 41 |
{"-Inf", true, -std::numeric_limits<double>::infinity()}, |
| 42 |
{"+2.2", true, 2.2}, |
| 43 |
{"1d+4", false, 0.0}, |
| 44 |
{"1d-1", false, 0.0}, |
| 45 |
{"0.", true, 0.0}, |
| 46 |
{"-.1", true, -0.1}, |
| 47 |
{"+.1", true, 0.1}, |
| 48 |
{"1e+1", true, 10.0}, |
| 49 |
{"+1e1", true, 10.0}, |
| 50 |
{"-+0", false, 0.0}, |
| 51 |
{"-+inf", false, 0.0}, |
| 52 |
{"-+nan", false, 0.0}, |
| 53 |
}; |
| 54 |
for (size_t i = 0; i < test_datas.size(); i++) { |
| 55 |
auto const &input = test_datas[i].input; |
| 56 |
auto const expected_success = test_datas[i].expected_success; |
| 57 |
auto const expected_result = test_datas[i].expected_result; |
| 58 |
double result; |
| 59 |
// answer contains a error code and a pointer to the end of the |
| 60 |
// parsed region (on success). |
| 61 |
auto const answer = fast_float::from_chars( |
| 62 |
input.data(), input.data() + input.size(), result, |
| 63 |
fast_float::chars_format::general | |
| 64 |
fast_float::chars_format::allow_leading_plus | |
| 65 |
fast_float::chars_format::skip_white_space); |
| 66 |
if (answer.ec != std::errc()) { |
| 67 |
std::cout << "could not parse" << std::endl; |
| 68 |
if (expected_success) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
continue; |
| 72 |
} |
| 73 |
bool non_space_trailing_content = false; |
| 74 |
if (answer.ptr != input.data() + input.size()) { |
| 75 |
// check that there is no content left |
| 76 |
for (char const *leftover = answer.ptr; |
| 77 |
leftover != input.data() + input.size(); leftover++) { |
| 78 |
if (!fast_float::is_space(*leftover)) { |
| 79 |
non_space_trailing_content = true; |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
if (non_space_trailing_content) { |
| 85 |
std::cout << "found trailing content " << std::endl; |
| 86 |
if (!expected_success) { |
| 87 |
continue; |
| 88 |
} else { |
| 89 |
return false; |
| 90 |
} |
| 91 |
} |
| 92 |
std::cout << "parsed " << result << std::endl; |
| 93 |
if (!expected_success) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
if (result != expected_result && |
| 97 |
!(std::isnan(result) && std::isnan(expected_result))) { |
| 98 |
std::cout << "results do not match. Expected " << expected_result |
| 99 |
<< std::endl; |
| 100 |
return false; |
| 101 |
} |
| 102 |
} |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
int main() { |
| 107 |
if (!eddelbuettel()) { |
| 108 |
printf("Bug.\n"); |
| 109 |
return EXIT_FAILURE; |
| 110 |
} |
| 111 |
printf("All ok.\n"); |
| 112 |
return EXIT_SUCCESS; |
| 113 |
} |