Skip to content

Commit 3e26cf4

Browse files
committed
add failing test for wide chars
1 parent 1e188d9 commit 3e26cf4

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

tests/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ cc_test(
8888
],
8989
)
9090

91+
cc_test(
92+
name = "wide_char_test",
93+
srcs = ["wide_char_test.cpp"],
94+
deps = [
95+
"//:fast_float",
96+
"@doctest//doctest",
97+
],
98+
)
99+
91100
cc_test(
92101
name = "string_test",
93102
srcs = ["string_test.cpp"],

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function(fast_float_add_cpp_test TEST_NAME)
6767
endfunction(fast_float_add_cpp_test)
6868

6969
fast_float_add_cpp_test(rcppfastfloat_test)
70+
fast_float_add_cpp_test(wide_char_test)
7071
fast_float_add_cpp_test(example_test)
7172
fast_float_add_cpp_test(example_comma_test)
7273
fast_float_add_cpp_test(basictest)

tests/wide_char_test.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)