File: supported_chars_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 (53 lines) | stat: -rw-r--r-- 1,412 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
#include "fast_float/fast_float.h"
#include <iostream>
#include <string>
#include <system_error>

template <typename UC> bool test(std::string s, double expected) {
  std::basic_string<UC> input(s.begin(), s.end());
  double result;
  auto answer =
      fast_float::from_chars(input.data(), input.data() + input.size(), result);
  if (answer.ec != std::errc()) {
    std::cerr << "parsing of \"" << s << "\" should succeed\n";
    return false;
  }
  if (result != expected && !(std::isnan(result) && std::isnan(expected))) {
    std::cerr << "parsing of \"" << s << "\" succeeded, expected " << expected
              << " got " << result << "\n";
    return false;
  }
  return true;
}

int main() {
  if (!test<char>("4.2", 4.2)) {
    std::cout << "test failure for char" << std::endl;
    return EXIT_FAILURE;
  }

  if (!test<wchar_t>("4.2", 4.2)) {
    std::cout << "test failure for wchar_t" << std::endl;
    return EXIT_FAILURE;
  }

#ifdef __cpp_char8_t
  if (!test<char8_t>("4.2", 4.2)) {
    std::cout << "test failure for char8_t" << std::endl;
    return EXIT_FAILURE;
  }
#endif

  if (!test<char16_t>("4.2", 4.2)) {
    std::cout << "test failure for char16_t" << std::endl;
    return EXIT_FAILURE;
  }

  if (!test<char32_t>("4.2", 4.2)) {
    std::cout << "test failure for char32_t" << std::endl;
    return EXIT_FAILURE;
  }

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