Audacity 3.2.0
Functions
FromCharsTests.cpp File Reference

Tests for the FromChars functions. More...

#include <catch2/catch.hpp>
#include <string>
#include <string_view>
#include <type_traits>
#include "FromChars.h"
Include dependency graph for FromCharsTests.cpp:

Go to the source code of this file.

Functions

template<typename T >
void TestFromChars (std::string_view input, T expectedValue, std::errc errc={})
 
 TEMPLATE_TEST_CASE ("FromChars/signed integers", "", short, int, long, long long)
 
 TEMPLATE_TEST_CASE ("FromChars/unsigned integers", "", unsigned short, unsigned int, unsigned long, unsigned long long)
 
 TEMPLATE_TEST_CASE ("FromChars/floats", "", float, double)
 

Detailed Description

Tests for the FromChars functions.


Audacity: A Digital Audio Editor

Dmitry Vedenko

Definition in file FromCharsTests.cpp.

Function Documentation

◆ TEMPLATE_TEST_CASE() [1/3]

TEMPLATE_TEST_CASE ( "FromChars/floats"  ,
""  ,
float  ,
double   
)

Definition at line 95 of file FromCharsTests.cpp.

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}
void TestFromChars(std::string_view input, T expectedValue, std::errc errc={})

References TestFromChars().

Here is the call graph for this function:

◆ TEMPLATE_TEST_CASE() [2/3]

TEMPLATE_TEST_CASE ( "FromChars/signed integers"  ,
""  ,
short  ,
int  ,
long  ,
long long   
)

Definition at line 40 of file FromCharsTests.cpp.

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}
int min(int a, int b)

References min().

Here is the call graph for this function:

◆ TEMPLATE_TEST_CASE() [3/3]

TEMPLATE_TEST_CASE ( "FromChars/unsigned integers"  ,
""  ,
unsigned short  ,
unsigned int  ,
unsigned long  ,
unsigned long long   
)

Definition at line 69 of file FromCharsTests.cpp.

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}

References min().

Here is the call graph for this function:

◆ TestFromChars()

template<typename T >
void TestFromChars ( std::string_view  input,
expectedValue,
std::errc  errc = {} 
)

Definition at line 20 of file FromCharsTests.cpp.

20 {})
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}
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

Referenced by TEMPLATE_TEST_CASE().

Here is the caller graph for this function: