11#include <catch2/catch.hpp>
20void TestFromChars(std::string_view input, T expectedValue, std::errc errc = {})
25 FromChars(input.data(), input.data() + input.length(), value);
27 REQUIRE(errc == result.ec);
29 if (errc == std::errc {})
31 if constexpr (std::is_floating_point_v<std::decay_t<T>>)
32 REQUIRE(Approx(expectedValue) == value);
34 REQUIRE(expectedValue == value);
36 REQUIRE(result.ptr == input.data() + input.length());
41 "FromChars/signed integers",
"",
short,
int,
long,
long long)
43 TestFromChars<TestType>(
"0", 0, {});
44 TestFromChars<TestType>(
"1", 1, {});
45 TestFromChars<TestType>(
"-1", -1, {});
46 TestFromChars<TestType>(
"127", 127, {});
47 TestFromChars<TestType>(
"-12", -12, {});
49 TestFromChars<TestType>(
"", {}, std::errc::invalid_argument);
50 TestFromChars<TestType>(
"a", {}, std::errc::invalid_argument);
52 TestFromChars<TestType>(
56 TestFromChars<TestType>(
57 std::to_string(std::numeric_limits<TestType>::max()),
58 std::numeric_limits<TestType>::max(), {});
60 TestFromChars<TestType>(
64 TestFromChars<TestType>(
65 std::to_string(std::numeric_limits<TestType>::max()) +
"0",
66 std::numeric_limits<TestType>::max(), std::errc::result_out_of_range);
70 "FromChars/unsigned integers",
"",
unsigned short,
unsigned int,
71 unsigned long,
unsigned long long)
73 TestFromChars<TestType>(
"0", 0, {});
74 TestFromChars<TestType>(
"1", 1, {});
75 TestFromChars<TestType>(
"-1", -1, std::errc::invalid_argument);
76 TestFromChars<TestType>(
"127", 127, {});
77 TestFromChars<TestType>(
"-12", -12, std::errc::invalid_argument);
79 TestFromChars<TestType>(
"", {}, std::errc::invalid_argument);
80 TestFromChars<TestType>(
"a", {}, std::errc::invalid_argument);
82 TestFromChars<TestType>(
86 TestFromChars<TestType>(
87 std::to_string(std::numeric_limits<TestType>::max()),
88 std::numeric_limits<TestType>::max(), {});
90 TestFromChars<TestType>(
91 std::to_string(std::numeric_limits<TestType>::max()) +
"0",
92 std::numeric_limits<TestType>::max(), std::errc::result_out_of_range);
97 TestFromChars<TestType>(
"0", 0, {});
98 TestFromChars<TestType>(
"0.0", 0, {});
99 TestFromChars<TestType>(
".0", 0, {});
100 TestFromChars<TestType>(
"1", 1, {});
101 TestFromChars<TestType>(
"-1", -1, {});
102 TestFromChars<TestType>(
"127", 127, {});
103 TestFromChars<TestType>(
"-12", -12, {});
104 TestFromChars<TestType>(
"-12.5", -12.5, {});
105 TestFromChars<TestType>(
"3.1415", 3.1415, {});
106 TestFromChars<TestType>(
"3.14159265359", 3.14159265359, {});
108 for (
auto magnitude = std::numeric_limits<TestType>::min_exponent10;
109 magnitude <= std::numeric_limits<TestType>::max_exponent10; ++magnitude)
112 "1e" + std::to_string(magnitude), std::pow(10, magnitude), {});
115 "-1e" + std::to_string(magnitude), -std::pow(10, magnitude), {});
118 TestFromChars<TestType>(
"", {}, std::errc::invalid_argument);
119 TestFromChars<TestType>(
"a", {}, std::errc::invalid_argument);
121 TestFromChars<TestType>(
122 "1e1000", std::numeric_limits<TestType>::infinity(), {});
FromCharsResult FromChars(const char *buffer, const char *last, float &value) noexcept
Parse a string into a single precision floating point value, always uses the dot as decimal.
Declare functions to convert numeric types to string representation.
void TestFromChars(std::string_view input, T expectedValue, std::errc errc={})
TEMPLATE_TEST_CASE("FromChars/signed integers", "", short, int, long, long long)