Debian Codegraph @

tests/json_fmt.cpp

raw
1
2 #include <cstdlib>
3 #include <iostream>
4 #include <vector>
5 #include "fast_float/fast_float.h"
6
7 int main_readme() {
8 std::string const input = "+.1"; // not valid
9 double result;
10 fast_float::parse_options options{
11 fast_float::chars_format::json |
12 fast_float::chars_format::allow_leading_plus}; // should be ignored
13 auto answer = fast_float::from_chars_advanced(
14 input.data(), input.data() + input.size(), result, options);
15 if (answer.ec == std::errc()) {
16 std::cerr << "should have failed\n";
17 return EXIT_FAILURE;
18 }
19 return EXIT_SUCCESS;
20 }
21
22 int main_readme2() {
23 std::string const input = "inf"; // not valid in JSON
24 double result;
25 fast_float::parse_options options{
26 fast_float::chars_format::json |
27 fast_float::chars_format::allow_leading_plus}; // should be ignored
28 auto answer = fast_float::from_chars_advanced(
29 input.data(), input.data() + input.size(), result, options);
30 if (answer.ec == std::errc()) {
31 std::cerr << "should have failed\n";
32 return EXIT_FAILURE;
33 }
34 return EXIT_SUCCESS;
35 }
36
37 int main_readme3() {
38 std::string const input =
39 "inf"; // not valid in JSON but we allow it with json_or_infnan
40 double result;
41 fast_float::parse_options options{
42 fast_float::chars_format::json_or_infnan |
43 fast_float::chars_format::allow_leading_plus}; // should be ignored
44 auto answer = fast_float::from_chars_advanced(
45 input.data(), input.data() + input.size(), result, options);
46 if (answer.ec != std::errc() || (!std::isinf(result))) {
47 std::cerr << "should have parsed infinity\n";
48 return EXIT_FAILURE;
49 }
50 return EXIT_SUCCESS;
51 }
52
53 struct ExpectedResult {
54 double value;
55 std::string junk_chars;
56 };
57
58 struct AcceptedValue {
59 std::string input;
60 ExpectedResult expected;
61 };
62
63 struct RejectReason {
64 fast_float::parse_error error;
65 intptr_t location_offset;
66 };
67
68 struct RejectedValue {
69 std::string input;
70 RejectReason reason;
71 };
72
73 int main() {
74 std::vector<AcceptedValue> const accept{
75 {"-0.2", {-0.2, ""}},
76 {"0.02", {0.02, ""}},
77 {"0.002", {0.002, ""}},
78 {"1e+0000", {1., ""}},
79 {"0e-2", {0., ""}},
80 {"1e", {1., "e"}},
81 {"1e+", {1., "e+"}},
82 {"inf", {std::numeric_limits<double>::infinity(), ""}}};
83 std::vector<RejectedValue> const reject{
84 {"-.2", {fast_float::parse_error::missing_integer_after_sign, 1}},
85 {"00.02", {fast_float::parse_error::leading_zeros_in_integer_part, 0}},
86 {"0.e+1", {fast_float::parse_error::no_digits_in_fractional_part, 2}},
87 {"00.e+1", {fast_float::parse_error::leading_zeros_in_integer_part, 0}},
88 {".25", {fast_float::parse_error::no_digits_in_integer_part, 0}},
89 // The following cases already start as invalid JSON, so they are
90 // handled as trailing junk and the error is for not having digits in the
91 // empty string before the invalid token.
92 {"+0.25", {fast_float::parse_error::no_digits_in_integer_part, 0}},
93 {"inf", {fast_float::parse_error::no_digits_in_integer_part, 0}},
94 {"nan(snan)", {fast_float::parse_error::no_digits_in_integer_part, 0}}};
95
96 for (std::size_t i = 0; i < accept.size(); ++i) {
97 auto const &s = accept[i].input;
98 auto const &expected = accept[i].expected;
99 double result;
100 auto answer =
101 fast_float::from_chars(s.data(), s.data() + s.size(), result,
102 fast_float::chars_format::json_or_infnan);
103 if (answer.ec != std::errc()) {
104 std::cerr << "json fmt rejected valid json " << s << std::endl;
105 return EXIT_FAILURE;
106 }
107 if (result != expected.value) {
108 std::cerr << "json fmt gave wrong result " << s << " (expected "
109 << expected.value << " got " << result << ")" << std::endl;
110 return EXIT_FAILURE;
111 }
112 if (std::string(answer.ptr) != expected.junk_chars) {
113 std::cerr << "json fmt has wrong trailing characters " << s
114 << " (expected " << expected.junk_chars << " got " << answer.ptr
115 << ")" << std::endl;
116 return EXIT_FAILURE;
117 }
118 }
119
120 for (std::size_t i = 0; i < reject.size(); ++i) {
121 auto const &s = reject[i].input;
122 double result;
123 auto answer = fast_float::from_chars(s.data(), s.data() + s.size(), result,
124 fast_float::chars_format::json);
125 if (answer.ec == std::errc()) {
126 std::cerr << "json fmt accepted invalid json " << s << std::endl;
127 return EXIT_FAILURE;
128 }
129 }
130
131 for (std::size_t i = 0; i < reject.size(); ++i) {
132 auto const &f = reject[i].input;
133 auto const &expected_reason = reject[i].reason;
134 auto answer = fast_float::parse_number_string<true>(
135 f.data(), f.data() + f.size(),
136 fast_float::parse_options(
137 fast_float::chars_format::json |
138 fast_float::chars_format::allow_leading_plus)); // should be ignored
139 if (answer.valid) {
140 std::cerr << "json parse accepted invalid json " << f << std::endl;
141 return EXIT_FAILURE;
142 }
143 if (answer.error != expected_reason.error) {
144 std::cerr << "json parse failure had invalid error reason " << f
145 << std::endl;
146 return EXIT_FAILURE;
147 }
148 intptr_t error_location = answer.lastmatch - f.data();
149 if (error_location != expected_reason.location_offset) {
150 std::cerr << "json parse failure had invalid error location " << f
151 << " (expected " << expected_reason.location_offset << " got "
152 << error_location << ")" << std::endl;
153 return EXIT_FAILURE;
154 }
155 }
156
157 if (main_readme() != EXIT_SUCCESS) {
158 return EXIT_FAILURE;
159 }
160 if (main_readme2() != EXIT_SUCCESS) {
161 return EXIT_FAILURE;
162 }
163 #ifndef __FAST_MATH__
164 if (main_readme3() != EXIT_SUCCESS) {
165 return EXIT_FAILURE;
166 }
167 #endif
168
169 return EXIT_SUCCESS;
170 }