Audacity 3.2.0
FromCharsTests.cpp
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file FromCharsTests.cpp
6 @brief Tests for the FromChars functions
7
8 Dmitry Vedenko
9 **********************************************************************/
10
11#include <catch2/catch.hpp>
12
13#include <string>
14#include <string_view>
15#include <type_traits>
16
17#include "FromChars.h"
18
19template <typename T>
20void TestFromChars(std::string_view input, T expectedValue, std::errc errc = {})
21{
22 T value;
23
24 const auto result =
25 FromChars(input.data(), input.data() + input.length(), value);
26
27 REQUIRE(errc == result.ec);
28
29 if (errc == std::errc {})
30 {
31 if constexpr (std::is_floating_point_v<std::decay_t<T>>)
32 REQUIRE(Approx(expectedValue) == value);
33 else
34 REQUIRE(expectedValue == value);
35
36 REQUIRE(result.ptr == input.data() + input.length());
37 }
38}
39
41 "FromChars/signed integers", "", short, int, long, long long)
42{
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, {});
48
49 TestFromChars<TestType>("", {}, std::errc::invalid_argument);
50 TestFromChars<TestType>("a", {}, std::errc::invalid_argument);
51
52 TestFromChars<TestType>(
53 std::to_string(std::numeric_limits<TestType>::min()),
55
56 TestFromChars<TestType>(
57 std::to_string(std::numeric_limits<TestType>::max()),
58 std::numeric_limits<TestType>::max(), {});
59
60 TestFromChars<TestType>(
61 std::to_string(std::numeric_limits<TestType>::min()) + "0",
62 std::numeric_limits<TestType>::min(), std::errc::result_out_of_range);
63
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);
67}
68
70 "FromChars/unsigned integers", "", unsigned short, unsigned int,
71 unsigned long, unsigned long long)
72{
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);
78
79 TestFromChars<TestType>("", {}, std::errc::invalid_argument);
80 TestFromChars<TestType>("a", {}, std::errc::invalid_argument);
81
82 TestFromChars<TestType>(
83 std::to_string(std::numeric_limits<TestType>::min()),
85
86 TestFromChars<TestType>(
87 std::to_string(std::numeric_limits<TestType>::max()),
88 std::numeric_limits<TestType>::max(), {});
89
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);
93}
94
95TEMPLATE_TEST_CASE("FromChars/floats", "", float, double)
96{
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, {});
107
108 for (auto magnitude = std::numeric_limits<TestType>::min_exponent10;
109 magnitude <= std::numeric_limits<TestType>::max_exponent10; ++magnitude)
110 {
112 "1e" + std::to_string(magnitude), std::pow(10, magnitude), {});
113
115 "-1e" + std::to_string(magnitude), -std::pow(10, magnitude), {});
116 }
117
118 TestFromChars<TestType>("", {}, std::errc::invalid_argument);
119 TestFromChars<TestType>("a", {}, std::errc::invalid_argument);
120
121 TestFromChars<TestType>(
122 "1e1000", std::numeric_limits<TestType>::infinity(), {});
123}
int min(int a, int b)
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.
Definition: FromChars.cpp:153
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)