Debian Codegraph @

tests/supported_chars_test.cpp

raw
1 #include "fast_float/fast_float.h"
2 #include <iostream>
3 #include <string>
4 #include <system_error>
5
6 template <typename UC> bool test(std::string s, double expected) {
7 std::basic_string<UC> input(s.begin(), s.end());
8 double result;
9 auto answer =
10 fast_float::from_chars(input.data(), input.data() + input.size(), result);
11 if (answer.ec != std::errc()) {
12 std::cerr << "parsing of \"" << s << "\" should succeed\n";
13 return false;
14 }
15 if (result != expected && !(std::isnan(result) && std::isnan(expected))) {
16 std::cerr << "parsing of \"" << s << "\" succeeded, expected " << expected
17 << " got " << result << "\n";
18 return false;
19 }
20 return true;
21 }
22
23 int main() {
24 if (!test<char>("4.2", 4.2)) {
25 std::cout << "test failure for char" << std::endl;
26 return EXIT_FAILURE;
27 }
28
29 if (!test<wchar_t>("4.2", 4.2)) {
30 std::cout << "test failure for wchar_t" << std::endl;
31 return EXIT_FAILURE;
32 }
33
34 #ifdef __cpp_char8_t
35 if (!test<char8_t>("4.2", 4.2)) {
36 std::cout << "test failure for char8_t" << std::endl;
37 return EXIT_FAILURE;
38 }
39 #endif
40
41 if (!test<char16_t>("4.2", 4.2)) {
42 std::cout << "test failure for char16_t" << std::endl;
43 return EXIT_FAILURE;
44 }
45
46 if (!test<char32_t>("4.2", 4.2)) {
47 std::cout << "test failure for char32_t" << std::endl;
48 return EXIT_FAILURE;
49 }
50
51 std::cout << "all ok" << std::endl;
52 return EXIT_SUCCESS;
53 }