MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_stringutils.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#include <cstdint>
5#include <stdexcept>
6#include <catch2/catch_test_macros.hpp>
9
12
13TEST_CASE("stringToInteger : converts decimal numbers in range", "[string]")
14{
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}
20
21TEST_CASE("stringToInteger : 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}
31
32TEST_CASE("jsonStringToInteger : 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}
T stringToInteger(std::string_view text, std::string_view name)
T jsonStringToInteger(picojson::value const &value, std::string_view name)
T stringToInteger(std::string_view text, std::string_view name)
T jsonStringToInteger(picojson::value const &value, std::string_view name)
TEST_CASE("stringToInteger : converts decimal numbers in range", "[string]")
REQUIRE(timeDelta< 33 '366 '667LL)