|
1 | 1 | #include <cstdlib> |
2 | 2 | #include <iostream> |
3 | 3 | #include <vector> |
4 | | -#include <iomanip> |
5 | 4 | #include <cstring> |
6 | 5 | #include "fast_float/fast_float.h" |
7 | 6 | #include <cstdint> |
8 | 7 | #include <stdfloat> |
9 | 8 |
|
10 | | -int main() |
11 | | -{ |
| 9 | +int main() { |
12 | 10 | // Write some testcases for the parsing of floating point numbers in the float32_t type. |
13 | 11 | // We use the from_chars function defined in this library. |
14 | | - |
| 12 | +#if __STDCPP_FLOAT32_T__ |
15 | 13 | const std::vector<std::float32_t> float32_test_expected{123.456f, -78.9f, 0.0001f, 3.40282e+038f}; |
16 | | - const std::vector<std::string> float32_test{"123.456", "-78.9", "0.0001", "3.40282e+038"}; |
| 14 | + const std::vector<std::string_view> float32_test{"123.456", "-78.9", "0.0001", "3.40282e+038"}; |
17 | 15 |
|
18 | 16 | for (std::size_t i = 0; i < float32_test.size(); ++i) { |
19 | 17 | const auto& f = float32_test[i]; |
20 | 18 | std::float32_t result; |
21 | 19 | auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); |
22 | 20 |
|
23 | | - if (answer.ec != std::errc() || result != float32_test_expected[i]) { |
24 | | - std::cerr << "Test failed for input: " << std::quoted(f) << std::endl; |
| 21 | + if (answer.ec != std::errc()) { |
| 22 | + std::cerr << "Failed to parse: \"" << f << "\"" << std::endl; |
| 23 | + return EXIT_FAILURE; |
| 24 | + } |
| 25 | + if(result != float32_test_expected[i]) { |
| 26 | + std::cerr << "Test failed for input: \"" << f << "\" expected " << float32_test_expected[i] << " got " << result << std::endl; |
25 | 27 | return EXIT_FAILURE; |
26 | 28 | } |
27 | 29 | } |
| 30 | +#else |
| 31 | + std::cout << "No std::float32_t type available." << std::endl; |
| 32 | +#endif |
28 | 33 |
|
| 34 | +#if __STDCPP_FLOAT64_T__ |
29 | 35 | // Test cases for std::float64_t |
30 | 36 | const std::vector<std::float64_t> float64_test_expected{1.23e4, -5.67e-8, 1.7976931348623157e+308, -1.7976931348623157e+308}; |
31 | | - const std::vector<std::string> float64_test{"1.23e4", "-5.67e-8", "1.7976931348623157e+308", "-1.7976931348623157e+308"}; |
| 37 | + const std::vector<std::string_view> float64_test{"1.23e4", "-5.67e-8", "1.7976931348623157e+308", "-1.7976931348623157e+308"}; |
32 | 38 |
|
33 | 39 | for (std::size_t i = 0; i < float64_test.size(); ++i) { |
34 | 40 | const auto& f = float64_test[i]; |
35 | 41 | std::float64_t result; |
36 | 42 | auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); |
37 | 43 |
|
38 | | - if (answer.ec != std::errc() || result != float64_test_expected[i]) { |
39 | | - std::cerr << "Test failed for input: " << std::quoted(f) << std::endl; |
| 44 | + if (answer.ec != std::errc()) { |
| 45 | + std::cerr << "Failed to parse: \"" << f << "\"" << std::endl; |
| 46 | + return EXIT_FAILURE; |
| 47 | + } |
| 48 | + if(result != float32_test_expected[i]) { |
| 49 | + std::cerr << "Test failed for input: \"" << f << "\" expected " << float32_test_expected[i] << " got " << result << std::endl; |
40 | 50 | return EXIT_FAILURE; |
41 | 51 | } |
42 | 52 | } |
43 | | - |
| 53 | +#else |
| 54 | + std::cout << "No std::float64_t type available." << std::endl; |
| 55 | +#endif |
44 | 56 | std::cout << "All tests passed successfully." << std::endl; |
45 | 57 | return EXIT_SUCCESS; |
46 | 58 |
|
|
0 commit comments