diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ae51bf8aef..599a104f2a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -81,6 +81,7 @@ set(INTERNAL_HEADERS ${SOURCES_DIR}/internal/catch_istream.hpp ${SOURCES_DIR}/internal/catch_unique_name.hpp ${SOURCES_DIR}/internal/catch_sharding.hpp + ${SOURCES_DIR}/internal/catch_parse_numbers.hpp ${SOURCES_DIR}/generators/catch_generator_exception.hpp ${SOURCES_DIR}/generators/catch_generators.hpp ${SOURCES_DIR}/generators/catch_generators_adapters.hpp @@ -184,6 +185,7 @@ set(IMPL_SOURCES ${SOURCES_DIR}/internal/catch_fatal_condition_handler.cpp ${SOURCES_DIR}/internal/catch_floating_point_helpers.cpp ${SOURCES_DIR}/internal/catch_istream.cpp + ${SOURCES_DIR}/internal/catch_parse_numbers.cpp ${SOURCES_DIR}/interfaces/catch_interfaces_generatortracker.cpp ${SOURCES_DIR}/interfaces/catch_interfaces_reporter.cpp ${SOURCES_DIR}/internal/catch_list.cpp diff --git a/src/catch2/catch_all.hpp b/src/catch2/catch_all.hpp index bd83f5a7c5..3d32ce7f36 100644 --- a/src/catch2/catch_all.hpp +++ b/src/catch2/catch_all.hpp @@ -78,6 +78,7 @@ #include #include #include +#include #include #include #include diff --git a/src/catch2/internal/catch_parse_numbers.cpp b/src/catch2/internal/catch_parse_numbers.cpp new file mode 100644 index 0000000000..05649046d1 --- /dev/null +++ b/src/catch2/internal/catch_parse_numbers.cpp @@ -0,0 +1,49 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#include + +#include +#include + +#include + +namespace Catch { + + Optional parseUInt(std::string const& input, int base) { + auto trimmed = trim( input ); + // std::stoull is annoying and accepts numbers starting with '-', + // it just negates them into unsigned int + if ( trimmed.empty() || trimmed[0] == '-' ) { + return {}; + } + + CATCH_TRY { + size_t pos = 0; + const auto ret = std::stoull( trimmed, &pos, base ); + + // We did not consume the whole input, so there is an issue + // This can be bunch of different stuff, like multiple numbers + // in the input, or invalid digits/characters and so on. Either + // way, we do not want to return the partially parsed result. + if ( pos != trimmed.size() ) { + return {}; + } + // Too large + if ( ret > std::numeric_limits::max() ) { + return {}; + } + return static_cast(ret); + } CATCH_CATCH_ANON( std::exception const& ) { + // There was a larger issue with the input, e.g. the parsed + // number would be too large to fit within ull. + return {}; + } + } + +} // namespace Catch diff --git a/src/catch2/internal/catch_parse_numbers.hpp b/src/catch2/internal/catch_parse_numbers.hpp new file mode 100644 index 0000000000..578488167e --- /dev/null +++ b/src/catch2/internal/catch_parse_numbers.hpp @@ -0,0 +1,22 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#include + +#include + +namespace Catch { + + /** + * Parses unsigned int from the input, using provided base + * + * Effectively a wrapper around std::stoul but with better error checking + * e.g. "-1" is rejected, instead of being parsed as UINT_MAX. + */ + Optional parseUInt(std::string const& input, int base = 10); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7f5af9949a..0dca91e840 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -85,6 +85,7 @@ set(TEST_SOURCES ${SELF_TEST_DIR}/IntrospectiveTests/FloatingPoint.tests.cpp ${SELF_TEST_DIR}/IntrospectiveTests/GeneratorsImpl.tests.cpp ${SELF_TEST_DIR}/IntrospectiveTests/InternalBenchmark.tests.cpp + ${SELF_TEST_DIR}/IntrospectiveTests/Parse.tests.cpp ${SELF_TEST_DIR}/IntrospectiveTests/PartTracker.tests.cpp ${SELF_TEST_DIR}/IntrospectiveTests/RandomNumberGeneration.tests.cpp ${SELF_TEST_DIR}/IntrospectiveTests/Reporters.tests.cpp diff --git a/tests/SelfTest/Baselines/automake.sw.approved.txt b/tests/SelfTest/Baselines/automake.sw.approved.txt index b8b5956601..d3a1b63fc3 100644 --- a/tests/SelfTest/Baselines/automake.sw.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.approved.txt @@ -189,6 +189,7 @@ Nor would this :test-result: FAIL Output from all sections is reported :test-result: PASS Overloaded comma or address-of operators are not used :test-result: PASS Parse test names and tags +:test-result: PASS Parse uints :test-result: PASS Parsed tags are matched case insensitive :test-result: PASS Parsing sharding-related cli flags :test-result: PASS Parsing tags with non-alphabetical characters is pass-through diff --git a/tests/SelfTest/Baselines/automake.sw.multi.approved.txt b/tests/SelfTest/Baselines/automake.sw.multi.approved.txt index 2afd308bc4..8604cde2da 100644 --- a/tests/SelfTest/Baselines/automake.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.multi.approved.txt @@ -187,6 +187,7 @@ :test-result: FAIL Output from all sections is reported :test-result: PASS Overloaded comma or address-of operators are not used :test-result: PASS Parse test names and tags +:test-result: PASS Parse uints :test-result: PASS Parsed tags are matched case insensitive :test-result: PASS Parsing sharding-related cli flags :test-result: PASS Parsing tags with non-alphabetical characters is pass-through diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index 6d23d01de2..768562ad87 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -1270,6 +1270,17 @@ CmdLine.tests.cpp:: passed: !(spec.matches(*fakeTestCase("hidden an CmdLine.tests.cpp:: passed: !(spec.matches(*fakeTestCase("only foo", "[foo]"))) for: !false CmdLine.tests.cpp:: passed: !(spec.matches(*fakeTestCase("only hidden", "[.]"))) for: !false CmdLine.tests.cpp:: passed: spec.matches(*fakeTestCase("neither foo nor hidden", "[bar]")) for: true +Parse.tests.cpp:: passed: parseUInt( "0" ) == Optional{ 0 } for: {?} == {?} +Parse.tests.cpp:: passed: parseUInt( "100" ) == Optional{ 100 } for: {?} == {?} +Parse.tests.cpp:: passed: parseUInt( "4294967295" ) == Optional{ 4294967295 } for: {?} == {?} +Parse.tests.cpp:: passed: parseUInt( "0x", 16 ) == Optional{ 255 } for: {?} == {?} +Parse.tests.cpp:: passed: !(parseUInt( "" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "!!KJHF*#" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "-1" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "4294967296" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "42949672964294967296429496729642949672964294967296" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "2 4" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "0x", 10 )) for: !{?} TestSpecParser.tests.cpp:: passed: spec.hasFilters() for: true TestSpecParser.tests.cpp:: passed: spec.getInvalidSpecs().empty() for: true TestSpecParser.tests.cpp:: passed: spec.matches( testCase ) for: true diff --git a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt index a3500a352d..d8d0413026 100644 --- a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt @@ -1268,6 +1268,17 @@ CmdLine.tests.cpp:: passed: !(spec.matches(*fakeTestCase("hidden an CmdLine.tests.cpp:: passed: !(spec.matches(*fakeTestCase("only foo", "[foo]"))) for: !false CmdLine.tests.cpp:: passed: !(spec.matches(*fakeTestCase("only hidden", "[.]"))) for: !false CmdLine.tests.cpp:: passed: spec.matches(*fakeTestCase("neither foo nor hidden", "[bar]")) for: true +Parse.tests.cpp:: passed: parseUInt( "0" ) == Optional{ 0 } for: {?} == {?} +Parse.tests.cpp:: passed: parseUInt( "100" ) == Optional{ 100 } for: {?} == {?} +Parse.tests.cpp:: passed: parseUInt( "4294967295" ) == Optional{ 4294967295 } for: {?} == {?} +Parse.tests.cpp:: passed: parseUInt( "0x", 16 ) == Optional{ 255 } for: {?} == {?} +Parse.tests.cpp:: passed: !(parseUInt( "" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "!!KJHF*#" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "-1" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "4294967296" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "42949672964294967296429496729642949672964294967296" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "2 4" )) for: !{?} +Parse.tests.cpp:: passed: !(parseUInt( "0x", 10 )) for: !{?} TestSpecParser.tests.cpp:: passed: spec.hasFilters() for: true TestSpecParser.tests.cpp:: passed: spec.getInvalidSpecs().empty() for: true TestSpecParser.tests.cpp:: passed: spec.matches( testCase ) for: true diff --git a/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index 6962e50826..9515b2c6df 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1394,6 +1394,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 394 | 318 passed | 69 failed | 7 failed as expected -assertions: 2284 | 2129 passed | 128 failed | 27 failed as expected +test cases: 395 | 319 passed | 69 failed | 7 failed as expected +assertions: 2295 | 2140 passed | 128 failed | 27 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index 63dd231e89..8bd2dc0c1b 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -9055,6 +9055,75 @@ CmdLine.tests.cpp:: PASSED: with expansion: true +------------------------------------------------------------------------------- +Parse uints + proper inputs +------------------------------------------------------------------------------- +Parse.tests.cpp: +............................................................................... + +Parse.tests.cpp:: PASSED: + REQUIRE( parseUInt( "0" ) == Optional{ 0 } ) +with expansion: + {?} == {?} + +Parse.tests.cpp:: PASSED: + REQUIRE( parseUInt( "100" ) == Optional{ 100 } ) +with expansion: + {?} == {?} + +Parse.tests.cpp:: PASSED: + REQUIRE( parseUInt( "4294967295" ) == Optional{ 4294967295 } ) +with expansion: + {?} == {?} + +Parse.tests.cpp:: PASSED: + REQUIRE( parseUInt( "0x", 16 ) == Optional{ 255 } ) +with expansion: + {?} == {?} + +------------------------------------------------------------------------------- +Parse uints + Bad inputs +------------------------------------------------------------------------------- +Parse.tests.cpp: +............................................................................... + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "!!KJHF*#" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "-1" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "4294967296" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "42949672964294967296429496729642949672964294967296" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "2 4" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "0x", 10 ) ) +with expansion: + !{?} + ------------------------------------------------------------------------------- Parsed tags are matched case insensitive ------------------------------------------------------------------------------- @@ -18450,6 +18519,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 394 | 304 passed | 83 failed | 7 failed as expected -assertions: 2299 | 2129 passed | 143 failed | 27 failed as expected +test cases: 395 | 305 passed | 83 failed | 7 failed as expected +assertions: 2310 | 2140 passed | 143 failed | 27 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.multi.approved.txt b/tests/SelfTest/Baselines/console.sw.multi.approved.txt index 9cba323d9b..0f5655605f 100644 --- a/tests/SelfTest/Baselines/console.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.multi.approved.txt @@ -9053,6 +9053,75 @@ CmdLine.tests.cpp:: PASSED: with expansion: true +------------------------------------------------------------------------------- +Parse uints + proper inputs +------------------------------------------------------------------------------- +Parse.tests.cpp: +............................................................................... + +Parse.tests.cpp:: PASSED: + REQUIRE( parseUInt( "0" ) == Optional{ 0 } ) +with expansion: + {?} == {?} + +Parse.tests.cpp:: PASSED: + REQUIRE( parseUInt( "100" ) == Optional{ 100 } ) +with expansion: + {?} == {?} + +Parse.tests.cpp:: PASSED: + REQUIRE( parseUInt( "4294967295" ) == Optional{ 4294967295 } ) +with expansion: + {?} == {?} + +Parse.tests.cpp:: PASSED: + REQUIRE( parseUInt( "0x", 16 ) == Optional{ 255 } ) +with expansion: + {?} == {?} + +------------------------------------------------------------------------------- +Parse uints + Bad inputs +------------------------------------------------------------------------------- +Parse.tests.cpp: +............................................................................... + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "!!KJHF*#" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "-1" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "4294967296" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "42949672964294967296429496729642949672964294967296" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "2 4" ) ) +with expansion: + !{?} + +Parse.tests.cpp:: PASSED: + REQUIRE_FALSE( parseUInt( "0x", 10 ) ) +with expansion: + !{?} + ------------------------------------------------------------------------------- Parsed tags are matched case insensitive ------------------------------------------------------------------------------- @@ -18442,6 +18511,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 394 | 304 passed | 83 failed | 7 failed as expected -assertions: 2299 | 2129 passed | 143 failed | 27 failed as expected +test cases: 395 | 305 passed | 83 failed | 7 failed as expected +assertions: 2310 | 2140 passed | 143 failed | 27 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index f7411501da..eed28e7bb3 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -1083,6 +1083,8 @@ Message.tests.cpp: + + diff --git a/tests/SelfTest/Baselines/junit.sw.multi.approved.txt b/tests/SelfTest/Baselines/junit.sw.multi.approved.txt index aa0860a3c9..54a5a13cc5 100644 --- a/tests/SelfTest/Baselines/junit.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.multi.approved.txt @@ -1,6 +1,6 @@ - + @@ -1082,6 +1082,8 @@ Message.tests.cpp: + + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt index a9319a6280..be43f8d8d3 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt @@ -157,6 +157,10 @@ + + + + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt index 97b2d98817..75157eca76 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt @@ -156,6 +156,10 @@ + + + + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index 2c7adae54d..cae94b24ec 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -2383,6 +2383,28 @@ ok {test-number} - !(spec.matches(*fakeTestCase("only foo", "[foo]"))) for: !fal ok {test-number} - !(spec.matches(*fakeTestCase("only hidden", "[.]"))) for: !false # Parse test names and tags ok {test-number} - spec.matches(*fakeTestCase("neither foo nor hidden", "[bar]")) for: true +# Parse uints +ok {test-number} - parseUInt( "0" ) == Optional{ 0 } for: {?} == {?} +# Parse uints +ok {test-number} - parseUInt( "100" ) == Optional{ 100 } for: {?} == {?} +# Parse uints +ok {test-number} - parseUInt( "4294967295" ) == Optional{ 4294967295 } for: {?} == {?} +# Parse uints +ok {test-number} - parseUInt( "0x", 16 ) == Optional{ 255 } for: {?} == {?} +# Parse uints +ok {test-number} - !(parseUInt( "" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "!!KJHF*#" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "-1" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "4294967296" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "42949672964294967296429496729642949672964294967296" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "2 4" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "0x", 10 )) for: !{?} # Parsed tags are matched case insensitive ok {test-number} - spec.hasFilters() for: true # Parsed tags are matched case insensitive @@ -4601,5 +4623,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2299 +1..2310 diff --git a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt index d13b493c90..15942bb6a9 100644 --- a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt @@ -2381,6 +2381,28 @@ ok {test-number} - !(spec.matches(*fakeTestCase("only foo", "[foo]"))) for: !fal ok {test-number} - !(spec.matches(*fakeTestCase("only hidden", "[.]"))) for: !false # Parse test names and tags ok {test-number} - spec.matches(*fakeTestCase("neither foo nor hidden", "[bar]")) for: true +# Parse uints +ok {test-number} - parseUInt( "0" ) == Optional{ 0 } for: {?} == {?} +# Parse uints +ok {test-number} - parseUInt( "100" ) == Optional{ 100 } for: {?} == {?} +# Parse uints +ok {test-number} - parseUInt( "4294967295" ) == Optional{ 4294967295 } for: {?} == {?} +# Parse uints +ok {test-number} - parseUInt( "0x", 16 ) == Optional{ 255 } for: {?} == {?} +# Parse uints +ok {test-number} - !(parseUInt( "" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "!!KJHF*#" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "-1" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "4294967296" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "42949672964294967296429496729642949672964294967296" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "2 4" )) for: !{?} +# Parse uints +ok {test-number} - !(parseUInt( "0x", 10 )) for: !{?} # Parsed tags are matched case insensitive ok {test-number} - spec.hasFilters() for: true # Parsed tags are matched case insensitive @@ -4593,5 +4615,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2299 +1..2310 diff --git a/tests/SelfTest/Baselines/teamcity.sw.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.approved.txt index 526f00a90f..d707dd2928 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.approved.txt @@ -483,6 +483,8 @@ Message.tests.cpp:|nexplicit failure with message:|n "Message from ##teamcity[testFinished name='Overloaded comma or address-of operators are not used' duration="{duration}"] ##teamcity[testStarted name='Parse test names and tags'] ##teamcity[testFinished name='Parse test names and tags' duration="{duration}"] +##teamcity[testStarted name='Parse uints'] +##teamcity[testFinished name='Parse uints' duration="{duration}"] ##teamcity[testStarted name='Parsed tags are matched case insensitive'] ##teamcity[testFinished name='Parsed tags are matched case insensitive' duration="{duration}"] ##teamcity[testStarted name='Parsing sharding-related cli flags'] diff --git a/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt index 2b7914464b..9f29b20132 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt @@ -483,6 +483,8 @@ Message.tests.cpp:|nexplicit failure with message:|n "Message from ##teamcity[testFinished name='Overloaded comma or address-of operators are not used' duration="{duration}"] ##teamcity[testStarted name='Parse test names and tags'] ##teamcity[testFinished name='Parse test names and tags' duration="{duration}"] +##teamcity[testStarted name='Parse uints'] +##teamcity[testFinished name='Parse uints' duration="{duration}"] ##teamcity[testStarted name='Parsed tags are matched case insensitive'] ##teamcity[testFinished name='Parsed tags are matched case insensitive' duration="{duration}"] ##teamcity[testStarted name='Parsing sharding-related cli flags'] diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index 0167e59cf2..4358597c5a 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -11005,6 +11005,103 @@ C + +
+ + + parseUInt( "0" ) == Optional<unsigned int>{ 0 } + + + {?} == {?} + + + + + parseUInt( "100" ) == Optional<unsigned int>{ 100 } + + + {?} == {?} + + + + + parseUInt( "4294967295" ) == Optional<unsigned int>{ 4294967295 } + + + {?} == {?} + + + + + parseUInt( "0x", 16 ) == Optional<unsigned int>{ 255 } + + + {?} == {?} + + + +
+
+ + + !(parseUInt( "" )) + + + !{?} + + + + + !(parseUInt( "!!KJHF*#" )) + + + !{?} + + + + + !(parseUInt( "-1" )) + + + !{?} + + + + + !(parseUInt( "4294967296" )) + + + !{?} + + + + + !(parseUInt( "42949672964294967296429496729642949672964294967296" )) + + + !{?} + + + + + !(parseUInt( "2 4" )) + + + !{?} + + + + + !(parseUInt( "0x", 10 )) + + + !{?} + + + +
+ +
@@ -21702,6 +21799,6 @@ loose text artifact - - + + diff --git a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt index 7a88f7de7f..9af9db43d7 100644 --- a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt @@ -11005,6 +11005,103 @@ C
+ +
+ + + parseUInt( "0" ) == Optional<unsigned int>{ 0 } + + + {?} == {?} + + + + + parseUInt( "100" ) == Optional<unsigned int>{ 100 } + + + {?} == {?} + + + + + parseUInt( "4294967295" ) == Optional<unsigned int>{ 4294967295 } + + + {?} == {?} + + + + + parseUInt( "0x", 16 ) == Optional<unsigned int>{ 255 } + + + {?} == {?} + + + +
+
+ + + !(parseUInt( "" )) + + + !{?} + + + + + !(parseUInt( "!!KJHF*#" )) + + + !{?} + + + + + !(parseUInt( "-1" )) + + + !{?} + + + + + !(parseUInt( "4294967296" )) + + + !{?} + + + + + !(parseUInt( "42949672964294967296429496729642949672964294967296" )) + + + !{?} + + + + + !(parseUInt( "2 4" )) + + + !{?} + + + + + !(parseUInt( "0x", 10 )) + + + !{?} + + + +
+ +
@@ -21701,6 +21798,6 @@ There is no extra whitespace here - - + + diff --git a/tests/SelfTest/IntrospectiveTests/Parse.tests.cpp b/tests/SelfTest/IntrospectiveTests/Parse.tests.cpp new file mode 100644 index 0000000000..99d977b374 --- /dev/null +++ b/tests/SelfTest/IntrospectiveTests/Parse.tests.cpp @@ -0,0 +1,38 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#include + +#include + +TEST_CASE("Parse uints", "[parse-numbers]") { + using Catch::parseUInt; + using Catch::Optional; + + SECTION("proper inputs") { + REQUIRE( parseUInt( "0" ) == Optional{ 0 } ); + REQUIRE( parseUInt( "100" ) == Optional{ 100 } ); + REQUIRE( parseUInt( "4294967295" ) == + Optional{ 4294967295 } ); + REQUIRE( parseUInt( "0xFF", 16 ) == Optional{ 255 } ); + } + SECTION( "Bad inputs" ) { + // empty + REQUIRE_FALSE( parseUInt( "" ) ); + // random noise + REQUIRE_FALSE( parseUInt( "!!KJHF*#" ) ); + // negative + REQUIRE_FALSE( parseUInt( "-1" ) ); + // too large + REQUIRE_FALSE( parseUInt( "4294967296" ) ); + REQUIRE_FALSE( parseUInt( "42949672964294967296429496729642949672964294967296" ) ); + REQUIRE_FALSE( parseUInt( "2 4" ) ); + // hex with base 10 + REQUIRE_FALSE( parseUInt( "0xFF", 10 ) ); + } +}