Debian Codegraph @

tests/fixedwidthtest.cpp

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