9
9
#include < string>
10
10
#include < vector>
11
11
12
+ struct test_data {
13
+ std::string input;
14
+ bool expected_success;
15
+ double expected_result;
16
+ };
17
+
12
18
bool eddelbuettel () {
13
- std::vector<std::string> inputs = {" infinity" ,
14
- " \r\n\t\f\v 3.16227766016838 \r\n\t\f\v " ,
15
- " \r\n\t\f\v 3 \r\n\t\f\v " ,
16
- " 1970-01-01" ,
17
- " -NaN" ,
18
- " -inf" ,
19
- " \r\n\t\f\v 2.82842712474619 \r\n\t\f\v " ,
20
- " nan" ,
21
- " \r\n\t\f\v 2.44948974278318 \r\n\t\f\v " ,
22
- " Inf" ,
23
- " \r\n\t\f\v 2 \r\n\t\f\v " ,
24
- " -infinity" ,
25
- " \r\n\t\f\v 0 \r\n\t\f\v " ,
26
- " \r\n\t\f\v 1.73205080756888 \r\n\t\f\v " ,
27
- " \r\n\t\f\v 1 \r\n\t\f\v " ,
28
- " \r\n\t\f\v 1.4142135623731 \r\n\t\f\v " ,
29
- " \r\n\t\f\v 2.23606797749979 \r\n\t\f\v " ,
30
- " 1970-01-02 " ,
31
- " \r\n\t\f\v 2.64575131106459 \r\n\t\f\v " ,
32
- " inf" ,
33
- " -nan" ,
34
- " NaN" ,
35
- " " ,
36
- " -Inf" ,
37
- " +2.2" ,
38
- " 1d+4" ,
39
- " 1d-1" ,
40
- " 0." ,
41
- " -.1" ,
42
- " +.1" ,
43
- " 1e+1" ,
44
- " +1e1" ,
45
- " -+0" ,
46
- " -+inf" ,
47
- " -+nan" };
48
- std::vector<std::pair<bool , double >> expected_results = {
49
- {true , std::numeric_limits<double >::infinity ()},
50
- {true , 3.16227766016838 },
51
- {true , 3 },
52
- {false , -1 },
53
- {true , std::numeric_limits<double >::quiet_NaN ()},
54
- {true , -std::numeric_limits<double >::infinity ()},
55
- {true , 2.82842712474619 },
56
- {true , std::numeric_limits<double >::quiet_NaN ()},
57
- {true , 2.44948974278318 },
58
- {true , std::numeric_limits<double >::infinity ()},
59
- {true , 2 },
60
- {true , -std::numeric_limits<double >::infinity ()},
61
- {true , 0 },
62
- {true , 1.73205080756888 },
63
- {true , 1 },
64
- {true , 1.4142135623731 },
65
- {true , 2.23606797749979 },
66
- {false , -1 },
67
- {true , 2.64575131106459 },
68
- {true , std::numeric_limits<double >::infinity ()},
69
- {true , std::numeric_limits<double >::quiet_NaN ()},
70
- {true , std::numeric_limits<double >::quiet_NaN ()},
71
- {false , -1 },
72
- {true , -std::numeric_limits<double >::infinity ()},
73
- {true , 2.2 },
74
- {false , -1 },
75
- {false , -1 },
76
- {true , 0 },
77
- {true , -0.1 },
78
- {true , 0.1 },
79
- {true , 10 },
80
- {true , 10 },
81
- {false , -1 },
82
- {false , -1 },
83
- {false , -1 },
19
+ std::vector<test_data> const test_datas = {
20
+ {" infinity" , true , std::numeric_limits<double >::infinity ()},
21
+ {" \r\n\t\f\v 3.16227766016838 \r\n\t\f\v " , true , 3.16227766016838 },
22
+ {" \r\n\t\f\v 3 \r\n\t\f\v " , true , 3.0 },
23
+ {" 1970-01-01" , false , 0.0 },
24
+ {" -NaN" , true , std::numeric_limits<double >::quiet_NaN ()},
25
+ {" -inf" , true , -std::numeric_limits<double >::infinity ()},
26
+ {" \r\n\t\f\v 2.82842712474619 \r\n\t\f\v " , true , 2.82842712474619 },
27
+ {" nan" , true , std::numeric_limits<double >::quiet_NaN ()},
28
+ {" \r\n\t\f\v 2.44948974278318 \r\n\t\f\v " , true , 2.44948974278318 },
29
+ {" Inf" , true , std::numeric_limits<double >::infinity ()},
30
+ {" \r\n\t\f\v 2 \r\n\t\f\v " , true , 2.0 },
31
+ {" -infinity" , true , -std::numeric_limits<double >::infinity ()},
32
+ {" \r\n\t\f\v 0 \r\n\t\f\v " , true , 0.0 },
33
+ {" \r\n\t\f\v 1.73205080756888 \r\n\t\f\v " , true , 1.73205080756888 },
34
+ {" \r\n\t\f\v 1 \r\n\t\f\v " , true , 1.0 },
35
+ {" \r\n\t\f\v 1.4142135623731 \r\n\t\f\v " , true , 1.4142135623731 },
36
+ {" \r\n\t\f\v 2.23606797749979 \r\n\t\f\v " , true , 2.23606797749979 },
37
+ {" 1970-01-02 " , false , 0.0 },
38
+ {" \r\n\t\f\v 2.64575131106459 \r\n\t\f\v " , true , 2.64575131106459 },
39
+ {" inf" , true , std::numeric_limits<double >::infinity ()},
40
+ {" -nan" , true , std::numeric_limits<double >::quiet_NaN ()},
41
+ {" NaN" , true , std::numeric_limits<double >::quiet_NaN ()},
42
+ {" " , false , 0.0 },
43
+ {" -Inf" , true , -std::numeric_limits<double >::infinity ()},
44
+ {" +2.2" , true , 2.2 },
45
+ {" 1d+4" , false , 0.0 },
46
+ {" 1d-1" , false , 0.0 },
47
+ {" 0." , true , 0.0 },
48
+ {" -.1" , true , -0.1 },
49
+ {" +.1" , true , 0.1 },
50
+ {" 1e+1" , true , 10.0 },
51
+ {" +1e1" , true , 10.0 },
52
+ {" -+0" , false , 0.0 },
53
+ {" -+inf" , false , 0.0 },
54
+ {" -+nan" , false , 0.0 },
84
55
};
85
- for (size_t i = 0 ; i < inputs.size (); i++) {
86
- const std::string &input = inputs[i];
87
- std::pair<bool , double > expected = expected_results[i];
56
+ for (size_t i = 0 ; i < test_datas.size (); i++) {
57
+ auto const &input = test_datas[i].input ;
58
+ auto const expected_success = test_datas[i].expected_success ;
59
+ auto const expected_result = test_datas[i].expected_result ;
88
60
double result;
89
61
// answer contains a error code and a pointer to the end of the
90
62
// parsed region (on success).
91
- auto answer = fast_float::from_chars (input. data (),
92
- input.data () + input.size (), result);
63
+ auto const answer = fast_float::from_chars (
64
+ input. data (), input.data () + input.size (), result);
93
65
if (answer.ec != std::errc ()) {
94
66
std::cout << " could not parse" << std::endl;
95
- if (expected. first ) {
67
+ if (expected_success ) {
96
68
return false ;
97
69
}
98
70
continue ;
@@ -110,24 +82,19 @@ bool eddelbuettel() {
110
82
}
111
83
if (non_space_trailing_content) {
112
84
std::cout << " found trailing content " << std::endl;
113
- }
114
-
115
- if (non_space_trailing_content) {
116
- if (!expected.first ) {
85
+ if (!expected_success) {
117
86
continue ;
118
87
} else {
119
88
return false ;
120
89
}
121
90
}
122
91
std::cout << " parsed " << result << std::endl;
123
- if (!expected. first ) {
92
+ if (!expected_success ) {
124
93
return false ;
125
94
}
126
- if (result != expected.second ) {
127
- if (std::isnan (result) && std::isnan (expected.second )) {
128
- continue ;
129
- }
130
- std::cout << " results do not match. Expected " << expected.second
95
+ if (result != expected_result &&
96
+ !(std::isnan (result) && std::isnan (expected_result))) {
97
+ std::cout << " results do not match. Expected " << expected_result
131
98
<< std::endl;
132
99
return false ;
133
100
}
0 commit comments