From 5c5c496b605886b286d1b99e0f9e28ec02117ad5 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Fri, 17 Oct 2025 14:06:48 +0800 Subject: Use std::from_chars. --- test/base/StringUtilTest.cpp | 112 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'test/base/StringUtilTest.cpp') 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("123"); + REQUIRE(r1.valid); + REQUIRE(r1.value == 123); + REQUIRE(r1.processed_char_count == 3); + + auto r2 = ParseToNumber("123.123"); + REQUIRE(!r2.valid); + + auto r3 = ParseToNumber("123.123"); + REQUIRE(r3.valid); + REQUIRE(r3.value == 123.123f); + REQUIRE(r3.processed_char_count == 7); + + auto r4 = ParseToNumber("a123"); + REQUIRE(!r4.valid); +} + +TEST_CASE("ParseToNumber AllowLeadingZeroFlag", "[string]") { + using namespace cru::string; + + auto r1 = ParseToNumber(" 123"); + REQUIRE(!r1.valid); + + auto r2 = ParseToNumber(" 123", ParseToNumberFlags::AllowLeadingSpaces); + REQUIRE(r2.valid); + REQUIRE(r2.value == 123); + REQUIRE(r2.processed_char_count == 5); + + auto r3 = ParseToNumber(" 123.123"); + REQUIRE(!r3.valid); + + auto r4 = + ParseToNumber(" 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("123 "); + REQUIRE(!r1.valid); + + auto r2 = + ParseToNumber("123 ", ParseToNumberFlags::AllowTrailingSpaces); + REQUIRE(r2.valid); + REQUIRE(r2.value == 123); + REQUIRE(r2.processed_char_count == 3); + + auto r3 = ParseToNumber("123.123 "); + REQUIRE(!r3.valid); + + auto r4 = ParseToNumber("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("123ab"); + REQUIRE(!r1.valid); + + auto r2 = ParseToNumber("123ab", ParseToNumberFlags::AllowTrailingJunk); + REQUIRE(r2.valid); + REQUIRE(r2.value == 123); + REQUIRE(r2.processed_char_count == 3); + + auto r3 = ParseToNumber("123.123ab"); + REQUIRE(!r3.valid); + + auto r4 = + ParseToNumber("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(" 123ab", ParseToNumberFlags::AllowLeadingSpaces | + ParseToNumberFlags::AllowTrailingJunk); + REQUIRE(r1.valid); + REQUIRE(r1.value == 123); + REQUIRE(r1.processed_char_count == 5); + + auto r2 = ParseToNumber(" 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("123 456 789"); + REQUIRE(r1 == std::vector{123, 456, 789}); + + auto r2 = ParseToNumberList("1.1 2.2 3.3"); + REQUIRE(r2 == std::vector{1.1f, 2.2f, 3.3f}); +} + // TEST(WinString, IndexUtf8ToUtf16) { // using cru::platform::win::IndexUtf8ToUtf16; // std::string_view utf8_string = "aπ你🤣!"; -- cgit v1.2.3