mirror of
https://github.com/fastfloat/fast_float.git
synced 2026-02-06 09:49:52 +08:00
update rcppfastfloat_test
This commit is contained in:
parent
d80be1a5b0
commit
cd28b563fc
@ -9,90 +9,62 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
struct test_data {
|
||||||
|
std::string input;
|
||||||
|
bool expected_success;
|
||||||
|
double expected_result;
|
||||||
|
};
|
||||||
|
|
||||||
bool eddelbuettel() {
|
bool eddelbuettel() {
|
||||||
std::vector<std::string> inputs = {"infinity",
|
std::vector<test_data> const test_datas = {
|
||||||
" \r\n\t\f\v3.16227766016838 \r\n\t\f\v",
|
{"infinity", true, std::numeric_limits<double>::infinity()},
|
||||||
" \r\n\t\f\v3 \r\n\t\f\v",
|
{" \r\n\t\f\v3.16227766016838 \r\n\t\f\v", true, 3.16227766016838},
|
||||||
" 1970-01-01",
|
{" \r\n\t\f\v3 \r\n\t\f\v", true, 3.0},
|
||||||
"-NaN",
|
{" 1970-01-01", false, 0.0},
|
||||||
"-inf",
|
{"-NaN", true, std::numeric_limits<double>::quiet_NaN()},
|
||||||
" \r\n\t\f\v2.82842712474619 \r\n\t\f\v",
|
{"-inf", true, -std::numeric_limits<double>::infinity()},
|
||||||
"nan",
|
{" \r\n\t\f\v2.82842712474619 \r\n\t\f\v", true, 2.82842712474619},
|
||||||
" \r\n\t\f\v2.44948974278318 \r\n\t\f\v",
|
{"nan", true, std::numeric_limits<double>::quiet_NaN()},
|
||||||
"Inf",
|
{" \r\n\t\f\v2.44948974278318 \r\n\t\f\v", true, 2.44948974278318},
|
||||||
" \r\n\t\f\v2 \r\n\t\f\v",
|
{"Inf", true, std::numeric_limits<double>::infinity()},
|
||||||
"-infinity",
|
{" \r\n\t\f\v2 \r\n\t\f\v", true, 2.0},
|
||||||
" \r\n\t\f\v0 \r\n\t\f\v",
|
{"-infinity", true, -std::numeric_limits<double>::infinity()},
|
||||||
" \r\n\t\f\v1.73205080756888 \r\n\t\f\v",
|
{" \r\n\t\f\v0 \r\n\t\f\v", true, 0.0},
|
||||||
" \r\n\t\f\v1 \r\n\t\f\v",
|
{" \r\n\t\f\v1.73205080756888 \r\n\t\f\v", true, 1.73205080756888},
|
||||||
" \r\n\t\f\v1.4142135623731 \r\n\t\f\v",
|
{" \r\n\t\f\v1 \r\n\t\f\v", true, 1.0},
|
||||||
" \r\n\t\f\v2.23606797749979 \r\n\t\f\v",
|
{" \r\n\t\f\v1.4142135623731 \r\n\t\f\v", true, 1.4142135623731},
|
||||||
"1970-01-02 ",
|
{" \r\n\t\f\v2.23606797749979 \r\n\t\f\v", true, 2.23606797749979},
|
||||||
" \r\n\t\f\v2.64575131106459 \r\n\t\f\v",
|
{"1970-01-02 ", false, 0.0},
|
||||||
"inf",
|
{" \r\n\t\f\v2.64575131106459 \r\n\t\f\v", true, 2.64575131106459},
|
||||||
"-nan",
|
{"inf", true, std::numeric_limits<double>::infinity()},
|
||||||
"NaN",
|
{"-nan", true, std::numeric_limits<double>::quiet_NaN()},
|
||||||
"",
|
{"NaN", true, std::numeric_limits<double>::quiet_NaN()},
|
||||||
"-Inf",
|
{"", false, 0.0},
|
||||||
"+2.2",
|
{"-Inf", true, -std::numeric_limits<double>::infinity()},
|
||||||
"1d+4",
|
{"+2.2", true, 2.2},
|
||||||
"1d-1",
|
{"1d+4", false, 0.0},
|
||||||
"0.",
|
{"1d-1", false, 0.0},
|
||||||
"-.1",
|
{"0.", true, 0.0},
|
||||||
"+.1",
|
{"-.1", true, -0.1},
|
||||||
"1e+1",
|
{"+.1", true, 0.1},
|
||||||
"+1e1",
|
{"1e+1", true, 10.0},
|
||||||
"-+0",
|
{"+1e1", true, 10.0},
|
||||||
"-+inf",
|
{"-+0", false, 0.0},
|
||||||
"-+nan"};
|
{"-+inf", false, 0.0},
|
||||||
std::vector<std::pair<bool, double>> expected_results = {
|
{"-+nan", false, 0.0},
|
||||||
{true, std::numeric_limits<double>::infinity()},
|
|
||||||
{true, 3.16227766016838},
|
|
||||||
{true, 3},
|
|
||||||
{false, -1},
|
|
||||||
{true, std::numeric_limits<double>::quiet_NaN()},
|
|
||||||
{true, -std::numeric_limits<double>::infinity()},
|
|
||||||
{true, 2.82842712474619},
|
|
||||||
{true, std::numeric_limits<double>::quiet_NaN()},
|
|
||||||
{true, 2.44948974278318},
|
|
||||||
{true, std::numeric_limits<double>::infinity()},
|
|
||||||
{true, 2},
|
|
||||||
{true, -std::numeric_limits<double>::infinity()},
|
|
||||||
{true, 0},
|
|
||||||
{true, 1.73205080756888},
|
|
||||||
{true, 1},
|
|
||||||
{true, 1.4142135623731},
|
|
||||||
{true, 2.23606797749979},
|
|
||||||
{false, -1},
|
|
||||||
{true, 2.64575131106459},
|
|
||||||
{true, std::numeric_limits<double>::infinity()},
|
|
||||||
{true, std::numeric_limits<double>::quiet_NaN()},
|
|
||||||
{true, std::numeric_limits<double>::quiet_NaN()},
|
|
||||||
{false, -1},
|
|
||||||
{true, -std::numeric_limits<double>::infinity()},
|
|
||||||
{true, 2.2},
|
|
||||||
{false, -1},
|
|
||||||
{false, -1},
|
|
||||||
{true, 0},
|
|
||||||
{true, -0.1},
|
|
||||||
{true, 0.1},
|
|
||||||
{true, 10},
|
|
||||||
{true, 10},
|
|
||||||
{false, -1},
|
|
||||||
{false, -1},
|
|
||||||
{false, -1},
|
|
||||||
};
|
};
|
||||||
for (size_t i = 0; i < inputs.size(); i++) {
|
for (size_t i = 0; i < test_datas.size(); i++) {
|
||||||
const std::string &input = inputs[i];
|
auto const &input = test_datas[i].input;
|
||||||
std::pair<bool, double> expected = expected_results[i];
|
auto const expected_success = test_datas[i].expected_success;
|
||||||
|
auto const expected_result = test_datas[i].expected_result;
|
||||||
double result;
|
double result;
|
||||||
// answer contains a error code and a pointer to the end of the
|
// answer contains a error code and a pointer to the end of the
|
||||||
// parsed region (on success).
|
// parsed region (on success).
|
||||||
auto answer = fast_float::from_chars(input.data(),
|
auto const answer = fast_float::from_chars(
|
||||||
input.data() + input.size(), result);
|
input.data(), input.data() + input.size(), result);
|
||||||
if (answer.ec != std::errc()) {
|
if (answer.ec != std::errc()) {
|
||||||
std::cout << "could not parse" << std::endl;
|
std::cout << "could not parse" << std::endl;
|
||||||
if (expected.first) {
|
if (expected_success) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@ -110,24 +82,19 @@ bool eddelbuettel() {
|
|||||||
}
|
}
|
||||||
if (non_space_trailing_content) {
|
if (non_space_trailing_content) {
|
||||||
std::cout << "found trailing content " << std::endl;
|
std::cout << "found trailing content " << std::endl;
|
||||||
}
|
if (!expected_success) {
|
||||||
|
|
||||||
if (non_space_trailing_content) {
|
|
||||||
if (!expected.first) {
|
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << "parsed " << result << std::endl;
|
std::cout << "parsed " << result << std::endl;
|
||||||
if (!expected.first) {
|
if (!expected_success) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (result != expected.second) {
|
if (result != expected_result &&
|
||||||
if (std::isnan(result) && std::isnan(expected.second)) {
|
!(std::isnan(result) && std::isnan(expected_result))) {
|
||||||
continue;
|
std::cout << "results do not match. Expected " << expected_result
|
||||||
}
|
|
||||||
std::cout << "results do not match. Expected " << expected.second
|
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user