File: long_test.cpp

package info (click to toggle)
fast-float 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 896 kB
  • sloc: cpp: 7,252; ansic: 3,474; python: 366; sh: 37; makefile: 9
file content (63 lines) | stat: -rw-r--r-- 1,806 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
#include "fast_float/fast_float.h"

#include <cctype>
#include <iostream>
#include <stdexcept>
#include <string>
#include <system_error>
#include <vector>

inline void Assert(bool Assertion) {
  if (!Assertion) {
    throw std::runtime_error("bug");
  }
}

template <typename T> bool test() {
  std::string input = "0.156250000000000000000000000000000000000000  "
                      "3.14159265358979323846264338327950288419716939937510 "
                      "2.71828182845904523536028747135266249775724709369995";
  std::vector<T> answers = {T(0.15625), T(3.141592653589793),
                            T(2.718281828459045)};
  char const *begin = input.data();
  char const *end = input.data() + input.size();
  for (size_t i = 0; i < answers.size(); i++) {
    T result_value;
    while ((begin < end) && (std::isspace(*begin))) {
      begin++;
    }
    auto result = fast_float::from_chars(begin, end, result_value);
    if (result.ec != std::errc() &&
        result.ec != std::errc::result_out_of_range) {
      printf("parsing %.*s\n", int(end - begin), begin);
      std::cerr << " I could not parse " << std::endl;
      return false;
    }
    if (result_value != answers[i]) {
      printf("parsing %.*s\n", int(end - begin), begin);
      std::cerr << " Mismatch " << std::endl;
      std::cerr << " Expected " << answers[i] << std::endl;
      std::cerr << " Got      " << result_value << std::endl;

      return false;
    }
    begin = result.ptr;
  }
  if (begin != end) {
    std::cerr << " bad ending " << std::endl;
    return false;
  }
  return true;
}

int main() {

  std::cout << "32 bits checks" << std::endl;
  Assert(test<float>());

  std::cout << "64 bits checks" << std::endl;
  Assert(test<double>());

  std::cout << "All ok" << std::endl;
  return EXIT_SUCCESS;
}