Debian Codegraph @

tests/fortran.cpp

raw
1 /*
2 * Exercise the Fortran conversion option.
3 */
4 #include <cstdlib>
5 #include <iostream>
6 #include <vector>
7 #include "fast_float/fast_float.h"
8
9 int main_readme() {
10 std::string const input = "1d+4";
11 double result;
12 fast_float::parse_options options{
13 fast_float::chars_format::fortran |
14 fast_float::chars_format::allow_leading_plus};
15 auto answer = fast_float::from_chars_advanced(
16 input.data(), input.data() + input.size(), result, options);
17 if ((answer.ec != std::errc()) || ((result != 10000))) {
18 std::cerr << "parsing failure\n" << result << "\n";
19 return EXIT_FAILURE;
20 }
21 std::cout << "parsed the number " << result << std::endl;
22 return EXIT_SUCCESS;
23 }
24
25 int main() {
26 std::vector<double> const expected{10000, 1000, 100, 10, 1,
27 .1, .01, .001, .0001};
28 std::vector<std::string> const fmt1{"1+4", "1+3", "1+2", "1+1", "1+0",
29 "1-1", "1-2", "1-3", "1-4"};
30 std::vector<std::string> const fmt2{"1d+4", "1d+3", "1d+2", "1d+1", "1d+0",
31 "1d-1", "1d-2", "1d-3", "1d-4"};
32 std::vector<std::string> const fmt3{"+1+4", "+1+3", "+1+2", "+1+1", "+1+0",
33 "+1-1", "+1-2", "+1-3", "+1-4"};
34 fast_float::parse_options const options{
35 fast_float::chars_format::fortran |
36 fast_float::chars_format::allow_leading_plus};
37
38 for (auto const &f : fmt1) {
39 auto d{std::distance(&fmt1[0], &f)};
40 double result;
41 auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(),
42 result, options)};
43 if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
44 std::cerr << "parsing failure on " << f << std::endl;
45 return EXIT_FAILURE;
46 }
47 }
48
49 for (auto const &f : fmt2) {
50 auto d{std::distance(&fmt2[0], &f)};
51 double result;
52 auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(),
53 result, options)};
54 if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
55 std::cerr << "parsing failure on " << f << std::endl;
56 return EXIT_FAILURE;
57 }
58 }
59
60 for (auto const &f : fmt3) {
61 auto d{std::distance(&fmt3[0], &f)};
62 double result;
63 auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(),
64 result, options)};
65 if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
66 std::cerr << "parsing failure on " << f << std::endl;
67 return EXIT_FAILURE;
68 }
69 }
70 if (main_readme() != EXIT_SUCCESS) {
71 return EXIT_FAILURE;
72 }
73
74 return EXIT_SUCCESS;
75 }