-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathfortran.cpp
74 lines (66 loc) · 2.59 KB
/
fortran.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Exercise the Fortran conversion option.
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#define FASTFLOAT_ALLOWS_LEADING_PLUS
#include "fast_float/fast_float.h"
int main_readme() {
const std::string input = "1d+4";
double result;
fast_float::parse_options options{fast_float::chars_format::fortran};
auto answer = fast_float::from_chars_advanced(
input.data(), input.data() + input.size(), result, options);
if ((answer.ec != std::errc()) || ((result != 10000))) {
std::cerr << "parsing failure\n" << result << "\n";
return EXIT_FAILURE;
}
std::cout << "parsed the number " << result << std::endl;
return EXIT_SUCCESS;
}
int main() {
const std::vector<double> expected{10000, 1000, 100, 10, 1,
.1, .01, .001, .0001};
const std::vector<std::string> fmt1{"1+4", "1+3", "1+2", "1+1", "1+0",
"1-1", "1-2", "1-3", "1-4"};
const std::vector<std::string> fmt2{"1d+4", "1d+3", "1d+2", "1d+1", "1d+0",
"1d-1", "1d-2", "1d-3", "1d-4"};
const std::vector<std::string> fmt3{"+1+4", "+1+3", "+1+2", "+1+1", "+1+0",
"+1-1", "+1-2", "+1-3", "+1-4"};
const fast_float::parse_options options{fast_float::chars_format::fortran};
for (auto const &f : fmt1) {
auto d{std::distance(&fmt1[0], &f)};
double result;
auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(),
result, options)};
if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
std::cerr << "parsing failure on " << f << std::endl;
return EXIT_FAILURE;
}
}
for (auto const &f : fmt2) {
auto d{std::distance(&fmt2[0], &f)};
double result;
auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(),
result, options)};
if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
std::cerr << "parsing failure on " << f << std::endl;
return EXIT_FAILURE;
}
}
for (auto const &f : fmt3) {
auto d{std::distance(&fmt3[0], &f)};
double result;
auto answer{fast_float::from_chars_advanced(f.data(), f.data() + f.size(),
result, options)};
if (answer.ec != std::errc() || result != expected[std::size_t(d)]) {
std::cerr << "parsing failure on " << f << std::endl;
return EXIT_FAILURE;
}
}
if (main_readme() != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}