File: fortran.cpp

package info (click to toggle)
fast-float 8.1.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 892 kB
  • sloc: cpp: 7,252; ansic: 3,474; python: 366; sh: 37; makefile: 9
file content (75 lines) | stat: -rw-r--r-- 2,733 bytes parent folder | download | duplicates (2)
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
75
/*
 * Exercise the Fortran conversion option.
 */
#include <cstdlib>
#include <iostream>
#include <vector>
#include "fast_float/fast_float.h"

int main_readme() {
  std::string const input = "1d+4";
  double result;
  fast_float::parse_options options{
      fast_float::chars_format::fortran |
      fast_float::chars_format::allow_leading_plus};
  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() {
  std::vector<double> const expected{10000, 1000, 100,  10,   1,
                                     .1,    .01,  .001, .0001};
  std::vector<std::string> const fmt1{"1+4", "1+3", "1+2", "1+1", "1+0",
                                      "1-1", "1-2", "1-3", "1-4"};
  std::vector<std::string> const fmt2{"1d+4", "1d+3", "1d+2", "1d+1", "1d+0",
                                      "1d-1", "1d-2", "1d-3", "1d-4"};
  std::vector<std::string> const fmt3{"+1+4", "+1+3", "+1+2", "+1+1", "+1+0",
                                      "+1-1", "+1-2", "+1-3", "+1-4"};
  fast_float::parse_options const options{
      fast_float::chars_format::fortran |
      fast_float::chars_format::allow_leading_plus};

  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;
}