-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathwide_char_test.cpp
57 lines (50 loc) · 1.66 KB
/
wide_char_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "fast_float/fast_float.h"
#include <iostream>
#include <string>
#include <system_error>
bool tester(std::string s, double expected,
fast_float::chars_format fmt = fast_float::chars_format::general) {
std::wstring input(s.begin(), s.end());
double result;
auto answer = fast_float::from_chars(
input.data(), input.data() + input.size(), result, fmt);
if (answer.ec != std::errc()) {
std::cerr << "parsing of \"" << s << "\" should succeed\n";
return false;
}
if (result != expected && !(std::isnan(result) && std::isnan(expected))) {
std::cerr << "parsing of \"" << s << "\" succeeded, expected " << expected
<< " got " << result << "\n";
return false;
}
input[0] += 256;
answer = fast_float::from_chars(input.data(), input.data() + input.size(),
result, fmt);
if (answer.ec == std::errc()) {
std::cerr << "parsing of altered \"" << s << "\" should fail\n";
return false;
}
return true;
}
bool test_minus() { return tester("-42", -42); }
bool test_plus() {
return tester("+42", 42,
fast_float::chars_format::general |
fast_float::chars_format::allow_leading_plus);
}
bool test_space() {
return tester(" 42", 42,
fast_float::chars_format::general |
fast_float::chars_format::skip_white_space);
}
bool test_nan() {
return tester("nan", std::numeric_limits<double>::quiet_NaN());
}
int main() {
if (test_minus() && test_plus() && test_space() && test_nan()) {
std::cout << "all ok" << std::endl;
return EXIT_SUCCESS;
}
std::cout << "test failure" << std::endl;
return EXIT_FAILURE;
}