MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_stringutils.cpp File Reference
#include <cstdint>
#include <stdexcept>
#include <catch2/catch_test_macros.hpp>
#include "mxl-internal/JsonUtils.hpp"
#include "mxl-internal/StringUtils.hpp"
Include dependency graph for test_stringutils.cpp:

Go to the source code of this file.

Functions

template<std::integral T>
T jsonStringToInteger (picojson::value const &value, std::string_view name)
template<std::integral T>
T stringToInteger (std::string_view text, std::string_view name)
 TEST_CASE ("jsonStringToInteger : requires a JSON string", "[string]")
 TEST_CASE ("stringToInteger : converts decimal numbers in range", "[string]")
 TEST_CASE ("stringToInteger : rejects what std::stoull accepts", "[string]")

Function Documentation

◆ jsonStringToInteger()

template<std::integral T>
T mxl::lib::jsonStringToInteger ( picojson::value const & value,
std::string_view name )
inline

Convert a JSON string holding a decimal number to the integer type T.

Use this for values such as memory addresses and keys, which are stored as strings because a JSON number is a double and cannot hold every 64-bit integer exactly.

Parameters
valueThe JSON value.
nameThe field name, used in the error message.
Returns
The value as T.
Exceptions
std::invalid_argumentIf the value is not a string, or not a decimal number in the range of T.
See also
stringToInteger()

Definition at line 270 of file JsonUtils.hpp.

271 {
272 if (!value.is<std::string>())
273 {
274 throw std::invalid_argument{fmt::format("'{}' must be a string.", name)};
275 }
276 return stringToInteger<T>(value.get<std::string>(), name);
277 }
T stringToInteger(std::string_view text, std::string_view name)

◆ stringToInteger()

template<std::integral T>
T mxl::lib::stringToInteger ( std::string_view text,
std::string_view name )

Convert a decimal string to the integer type T.

Parameters
textThe string to convert.
nameThe field name, used in the error message.
Returns
The value as T.
Exceptions
std::invalid_argumentIf the string is not a decimal number in the range of T.

Definition at line 24 of file StringUtils.hpp.

25 {
26 auto value = T{};
27 auto const* const first = text.data();
28 auto const* const last = text.data() + text.size();
29 auto const [end, error] = std::from_chars(first, last, value);
30 if (error == std::errc::result_out_of_range)
31 {
32 throw std::invalid_argument{
33 fmt::format("'{}' must be between {} and {}, got '{}'.", name, std::numeric_limits<T>::min(), std::numeric_limits<T>::max(), text)};
34 }
35 if ((error != std::errc{}) || (end != last))
36 {
37 throw std::invalid_argument{fmt::format("'{}' must be a decimal integer, got '{}'.", name, text)};
38 }
39 return value;
40 }

◆ TEST_CASE() [1/3]

TEST_CASE ( "jsonStringToInteger : requires a JSON string" ,
"" [string] )

Definition at line 32 of file test_stringutils.cpp.

32 : requires a JSON string", "[string]")
33{
34 REQUIRE(jsonStringToInteger<std::uint64_t>(picojson::value{"42"}, "v") == 42U);
35 REQUIRE_THROWS_AS(jsonStringToInteger<std::uint64_t>(picojson::value{42.0}, "v"), std::invalid_argument);
36 REQUIRE_THROWS_AS(jsonStringToInteger<std::uint64_t>(picojson::value{}, "v"), std::invalid_argument);
37 REQUIRE_THROWS_AS(jsonStringToInteger<std::uint64_t>(picojson::value{"-1"}, "v"), std::invalid_argument);
38}

◆ TEST_CASE() [2/3]

TEST_CASE ( "stringToInteger : converts decimal numbers in range" ,
"" [string] )

Definition at line 13 of file test_stringutils.cpp.

13 : converts decimal numbers in range", "[string]")
14{
15 REQUIRE(stringToInteger<std::uint64_t>("0", "v") == 0U);
16 REQUIRE(stringToInteger<std::uint64_t>("18446744073709551615", "v") == UINT64_MAX);
17 REQUIRE(stringToInteger<std::uint16_t>("65535", "v") == UINT16_MAX);
18 REQUIRE(stringToInteger<std::int64_t>("-9223372036854775808", "v") == INT64_MIN);
19}

◆ TEST_CASE() [3/3]

TEST_CASE ( "stringToInteger : rejects what std::stoull accepts" ,
"" [string] )

Definition at line 21 of file test_stringutils.cpp.

21 : rejects what std::stoull accepts", "[string]")
22{
23 for (auto const* text : {"", " 1", "1 ", "+1", "-1", "1x", "0x10", "1.0", "1e3", "18446744073709551616"})
24 {
25 CAPTURE(text);
26 REQUIRE_THROWS_AS(stringToInteger<std::uint64_t>(text, "v"), std::invalid_argument);
27 }
28 REQUIRE_THROWS_AS(stringToInteger<std::uint16_t>("65536", "v"), std::invalid_argument);
29 REQUIRE_THROWS_AS(stringToInteger<std::int64_t>("9223372036854775808", "v"), std::invalid_argument);
30}