diff options
Diffstat (limited to 'test/base')
-rw-r--r-- | test/base/CMakeLists.txt | 1 | ||||
-rw-r--r-- | test/base/StringTest.cpp | 26 | ||||
-rw-r--r-- | test/base/StringToNumberConverterTest.cpp | 166 | ||||
-rw-r--r-- | test/base/StringUtilTest.cpp | 112 |
4 files changed, 112 insertions, 193 deletions
diff --git a/test/base/CMakeLists.txt b/test/base/CMakeLists.txt index 95313ccb..13a0a2cb 100644 --- a/test/base/CMakeLists.txt +++ b/test/base/CMakeLists.txt @@ -4,7 +4,6 @@ add_executable(CruBaseTest PropertyTreeTest.cpp SelfResolvableTest.cpp StringTest.cpp - StringToNumberConverterTest.cpp StringUtilTest.cpp SubProcessTest.cpp ) diff --git a/test/base/StringTest.cpp b/test/base/StringTest.cpp index 9ed351e5..65fe0a99 100644 --- a/test/base/StringTest.cpp +++ b/test/base/StringTest.cpp @@ -84,29 +84,3 @@ TEST_CASE("String FromUtf8", "[string]") { REQUIRE(cru::String::FromUtf8(utf8_text) == utf16_text); } - -TEST_CASE("StringView ParseToDouble", "[string]") { - using cru::StringToNumberFlags; - using cru::StringView; - REQUIRE(StringView(u"3.14159").ParseToDouble() == 3.14159); - REQUIRE( - StringView(u" 3.14159") - .ParseToDouble(nullptr, StringToNumberFlags::kAllowLeadingSpaces) == - 3.14159); - REQUIRE(StringView(u" 3.14159 ") - .ParseToDouble(nullptr, - StringToNumberFlags::kAllowLeadingSpaces | - StringToNumberFlags::kAllowTrailingSpaces) == - 3.14159); -} - -TEST_CASE("String ParseToDoubleList", "[string]") { - using cru::StringView; - - auto list = StringView(u" 1.23 2.34 3.45 ").ParseToDoubleList(); - - REQUIRE(list.size() == 3); - REQUIRE(list[0] == 1.23); - REQUIRE(list[1] == 2.34); - REQUIRE(list[2] == 3.45); -} diff --git a/test/base/StringToNumberConverterTest.cpp b/test/base/StringToNumberConverterTest.cpp deleted file mode 100644 index 82062bdb..00000000 --- a/test/base/StringToNumberConverterTest.cpp +++ /dev/null @@ -1,166 +0,0 @@ -#include "cru/base/Exception.h" -#include "cru/base/StringToNumberConverter.h" - -#include <catch2/catch_test_macros.hpp> - -TEST_CASE("StringToIntegerConverterImpl Base0", "[string]") { - using namespace cru; - StringToIntegerConverter converter({}, 0); - Index processed_characters_count; - - REQUIRE(converter.Parse("12345678", &processed_characters_count) == - StringToIntegerResult(false, 12345678)); - REQUIRE(processed_characters_count == 8); - - REQUIRE(converter.Parse("0", &processed_characters_count) == - StringToIntegerResult(false, 0)); - REQUIRE(processed_characters_count == 1); - - REQUIRE(converter.Parse("012", &processed_characters_count) == - StringToIntegerResult(false, 012)); - REQUIRE(processed_characters_count == 3); - - REQUIRE(converter.Parse("0x12", &processed_characters_count) == - StringToIntegerResult(false, 0x12)); - REQUIRE(processed_characters_count == 4); - - REQUIRE(converter.Parse("0X12", &processed_characters_count) == - StringToIntegerResult(false, 0x12)); - REQUIRE(processed_characters_count == 4); - - REQUIRE(converter.Parse("0b101", &processed_characters_count) == - StringToIntegerResult(false, 0b101)); - REQUIRE(processed_characters_count == 5); - - REQUIRE(converter.Parse("0B101", &processed_characters_count) == - StringToIntegerResult(false, 0b101)); - REQUIRE(processed_characters_count == 5); - - REQUIRE(converter.Parse("-123", &processed_characters_count) == - StringToIntegerResult(true, 123)); - REQUIRE(processed_characters_count == 4); - - REQUIRE(converter.Parse("-0x10", &processed_characters_count) == - StringToIntegerResult(true, 0x10)); - REQUIRE(processed_characters_count == 5); -} - -TEST_CASE("StringToIntegerConverterImpl Base0ForError", "[string]") { - using namespace cru; - StringToIntegerConverter converter({}, 0); - Index processed_characters_count; - - REQUIRE(converter.Parse("a", &processed_characters_count) == - StringToIntegerResult(false, 0)); - REQUIRE(processed_characters_count == 0); - - REQUIRE(converter.Parse("0a", &processed_characters_count) == - StringToIntegerResult(false, 0)); - REQUIRE(processed_characters_count == 0); - - REQUIRE(converter.Parse("0x", &processed_characters_count) == - StringToIntegerResult(false, 0)); - REQUIRE(processed_characters_count == 0); - - REQUIRE(converter.Parse("0xx", &processed_characters_count) == - StringToIntegerResult(false, 0)); - REQUIRE(processed_characters_count == 0); - - REQUIRE(converter.Parse(" 0", &processed_characters_count) == - StringToIntegerResult(false, 0)); - REQUIRE(processed_characters_count == 0); - - REQUIRE(converter.Parse("0 ", &processed_characters_count) == - StringToIntegerResult(false, 0)); - REQUIRE(processed_characters_count == 0); -} - -TEST_CASE("StringToIntegerConverterImpl ThrowOnErrorFlag", "[string]") { - using namespace cru; - StringToIntegerConverter converter(StringToNumberFlags::kThrowOnError, 0); - Index processed_characters_count; - REQUIRE_THROWS_AS(converter.Parse("?", &processed_characters_count), - Exception); -} - -TEST_CASE("StringToIntegerConverterImpl AllowLeadingZeroFlag", "[string]") { - using namespace cru; - StringToIntegerConverter converter( - StringToNumberFlags::kAllowLeadingSpaces, 0); - Index processed_characters_count; - REQUIRE(converter.Parse(" 123", &processed_characters_count) == - StringToIntegerResult(false, 123)); - REQUIRE(processed_characters_count == 6); -} - -TEST_CASE("StringToIntegerConverterImpl AllowTrailingZeroFlag", "[string]") { - using namespace cru; - StringToIntegerConverter converter( - StringToNumberFlags::kAllowTrailingSpaces, 0); - Index processed_characters_count; - REQUIRE(converter.Parse("123 ", &processed_characters_count) == - StringToIntegerResult(false, 123)); - REQUIRE(processed_characters_count == 6); -} - -TEST_CASE("StringToIntegerConverterImpl AllowTrailingJunk", "[string]") { - using namespace cru; - StringToIntegerConverter converter( - StringToNumberFlags::kAllowTrailingJunk, 0); - Index processed_characters_count; - REQUIRE(converter.Parse("123 12", &processed_characters_count) == - StringToIntegerResult(false, 123)); - REQUIRE(processed_characters_count == 3); -} - -TEST_CASE("StringToIntegerConverterImpl AllowLeadingZeroForInteger", - "[string]") { - using namespace cru; - StringToIntegerConverter converter( - StringToNumberFlags::kAllowLeadingZeroForInteger, 0); - Index processed_characters_count; - REQUIRE(converter.Parse("0x0012", &processed_characters_count) == - StringToIntegerResult(false, 0x12)); - REQUIRE(processed_characters_count == 6); - - REQUIRE(converter.Parse("000011", &processed_characters_count) == - StringToIntegerResult(false, 000011)); - REQUIRE(processed_characters_count == 6); - - REQUIRE(converter.Parse("0b0011", &processed_characters_count) == - StringToIntegerResult(false, 0b0011)); - REQUIRE(processed_characters_count == 6); -} - -TEST_CASE("StringToIntegerConverterImpl CompositeFlags", "[string]") { - using namespace cru; - StringToIntegerConverter converter( - StringToNumberFlags::kAllowLeadingSpaces | - StringToNumberFlags::kAllowTrailingJunk | - StringToNumberFlags::kAllowLeadingZeroForInteger, - 0); - Index processed_characters_count; - - REQUIRE(converter.Parse(" 0x00123!!!", &processed_characters_count) == - StringToIntegerResult(false, 0x00123)); - REQUIRE(processed_characters_count == 10); -} - -TEST_CASE("StringToIntegerConverterImpl OtherBase", "[string]") { - using namespace cru; - StringToIntegerConverter converter({}, 7); - Index processed_characters_count; - - REQUIRE(converter.Parse("12", &processed_characters_count) == - StringToIntegerResult(false, 9)); - REQUIRE(processed_characters_count == 2); - - REQUIRE(converter.Parse("-12", &processed_characters_count) == - StringToIntegerResult(true, 9)); - REQUIRE(processed_characters_count == 3); - - converter.base = 11; - REQUIRE(converter.Parse("1a", &processed_characters_count) == - StringToIntegerResult(false, 21)); - REQUIRE(processed_characters_count == 2); -} diff --git a/test/base/StringUtilTest.cpp b/test/base/StringUtilTest.cpp index 3ce4cff1..5951531d 100644 --- a/test/base/StringUtilTest.cpp +++ b/test/base/StringUtilTest.cpp @@ -101,6 +101,118 @@ TEST_CASE("StringUtil Utf16CodePointIterator", "[string]") { REQUIRE(code_points == expected_code_points); } +TEST_CASE("ParseToNumber Work", "[string]") { + using namespace cru::string; + + auto r1 = ParseToNumber<int>("123"); + REQUIRE(r1.valid); + REQUIRE(r1.value == 123); + REQUIRE(r1.processed_char_count == 3); + + auto r2 = ParseToNumber<int>("123.123"); + REQUIRE(!r2.valid); + + auto r3 = ParseToNumber<float>("123.123"); + REQUIRE(r3.valid); + REQUIRE(r3.value == 123.123f); + REQUIRE(r3.processed_char_count == 7); + + auto r4 = ParseToNumber<float>("a123"); + REQUIRE(!r4.valid); +} + +TEST_CASE("ParseToNumber AllowLeadingZeroFlag", "[string]") { + using namespace cru::string; + + auto r1 = ParseToNumber<int>(" 123"); + REQUIRE(!r1.valid); + + auto r2 = ParseToNumber<int>(" 123", ParseToNumberFlags::AllowLeadingSpaces); + REQUIRE(r2.valid); + REQUIRE(r2.value == 123); + REQUIRE(r2.processed_char_count == 5); + + auto r3 = ParseToNumber<float>(" 123.123"); + REQUIRE(!r3.valid); + + auto r4 = + ParseToNumber<float>(" 123.123", ParseToNumberFlags::AllowLeadingSpaces); + REQUIRE(r4.valid); + REQUIRE(r4.value == 123.123f); + REQUIRE(r4.processed_char_count == 9); +} + +TEST_CASE("StringToIntegerConverterImpl AllowTrailingSpacesFlag", "[string]") { + using namespace cru::string; + + auto r1 = ParseToNumber<int>("123 "); + REQUIRE(!r1.valid); + + auto r2 = + ParseToNumber<int>("123 ", ParseToNumberFlags::AllowTrailingSpaces); + REQUIRE(r2.valid); + REQUIRE(r2.value == 123); + REQUIRE(r2.processed_char_count == 3); + + auto r3 = ParseToNumber<float>("123.123 "); + REQUIRE(!r3.valid); + + auto r4 = ParseToNumber<float>("123.123 ", + ParseToNumberFlags::AllowTrailingSpaces); + REQUIRE(r4.valid); + REQUIRE(r4.value == 123.123f); + REQUIRE(r4.processed_char_count == 7); +} + +TEST_CASE("StringToIntegerConverterImpl AllowTrailingJunk", "[string]") { + using namespace cru::string; + + auto r1 = ParseToNumber<int>("123ab"); + REQUIRE(!r1.valid); + + auto r2 = ParseToNumber<int>("123ab", ParseToNumberFlags::AllowTrailingJunk); + REQUIRE(r2.valid); + REQUIRE(r2.value == 123); + REQUIRE(r2.processed_char_count == 3); + + auto r3 = ParseToNumber<float>("123.123ab"); + REQUIRE(!r3.valid); + + auto r4 = + ParseToNumber<float>("123.123ab", ParseToNumberFlags::AllowTrailingJunk); + REQUIRE(r4.valid); + REQUIRE(r4.value == 123.123f); + REQUIRE(r4.processed_char_count == 7); +} + +TEST_CASE("StringToIntegerConverterImpl CompositeFlags", "[string]") { + using namespace cru::string; + + auto r1 = + ParseToNumber<int>(" 123ab", ParseToNumberFlags::AllowLeadingSpaces | + ParseToNumberFlags::AllowTrailingJunk); + REQUIRE(r1.valid); + REQUIRE(r1.value == 123); + REQUIRE(r1.processed_char_count == 5); + + auto r2 = ParseToNumber<float>(" 123.123ab", + ParseToNumberFlags::AllowLeadingSpaces | + ParseToNumberFlags::AllowTrailingJunk); + REQUIRE(r2.valid); + REQUIRE(r2.value == 123.123f); + REQUIRE(r2.processed_char_count == 9); +} + +TEST_CASE("String ParseToNumberList", "[string]") { + using namespace cru::string; + + auto r1 = ParseToNumberList<int>("123 456 789"); + REQUIRE(r1 == std::vector<int>{123, 456, 789}); + + auto r2 = ParseToNumberList<float>("1.1 2.2 3.3"); + REQUIRE(r2 == std::vector<float>{1.1f, 2.2f, 3.3f}); +} + // TEST(WinString, IndexUtf8ToUtf16) { // using cru::platform::win::IndexUtf8ToUtf16; // std::string_view utf8_string = "aπ你🤣!"; |