diff options
author | Treeston <treeston.mmoc@gmail.com> | 2020-09-02 22:04:45 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2022-02-04 23:50:25 +0100 |
commit | da8a66a05e86189511393110be29b5c09fae9b30 (patch) | |
tree | 21616dc5aa94f0193fbfc8f081f7c56240b8f0c0 /tests | |
parent | 0d54a5ecb4490408f4fa0c63b825f60d4b55531c (diff) |
Common/Util: Trinity::StringTo<double> support (PR #25364)
(cherry picked from commit f45aa5cac1579e87cbc599ffb58e10e662066792)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/common/StringConvert.cpp | 25 | ||||
-rw-r--r-- | tests/game/ChatCommand.cpp | 27 |
2 files changed, 46 insertions, 6 deletions
diff --git a/tests/common/StringConvert.cpp b/tests/common/StringConvert.cpp index d25e28f3d19..c1a6f5da7f8 100644 --- a/tests/common/StringConvert.cpp +++ b/tests/common/StringConvert.cpp @@ -119,7 +119,7 @@ TEST_CASE("String to smaller integer types", "[StringConvert]") REQUIRE(Trinity::StringTo<int16>("-1", 0) == -1); } -TEST_CASE("Test.String to boolean", "[StringConvert]") +TEST_CASE("String to boolean", "[StringConvert]") { REQUIRE(Trinity::StringTo<bool>("true") == true); REQUIRE(Trinity::StringTo<bool>("false") == false); @@ -129,3 +129,26 @@ TEST_CASE("Test.String to boolean", "[StringConvert]") REQUIRE(Trinity::StringTo<bool>("1", 10) == true); REQUIRE(Trinity::StringTo<bool>("0", 10) == false); } + +TEST_CASE("String to double", "[StringConvert]") +{ + using namespace Catch::literals; + REQUIRE(Trinity::StringTo<double>("0.5") == 0.5); + REQUIRE(Trinity::StringTo<double>("0.1") == 0.1_a); + REQUIRE(Trinity::StringTo<double>("1.2.3") == std::nullopt); + REQUIRE(Trinity::StringTo<double>("1e+5") == 100000.0); + REQUIRE(Trinity::StringTo<double>("1e+3+5") == std::nullopt); + REQUIRE(Trinity::StringTo<double>("a1.5") == std::nullopt); + REQUIRE(Trinity::StringTo<double>("1.5tail") == std::nullopt); + REQUIRE(Trinity::StringTo<double>("0x0") == 0.0); + REQUIRE(Trinity::StringTo<double>("0x0", 16) == std::nullopt); + REQUIRE(Trinity::StringTo<double>("0", 16) == 0.0); + REQUIRE(Trinity::StringTo<double>("0x1.BC70A3D70A3D7p+6") == 0x1.BC70A3D70A3D7p+6); + REQUIRE(Trinity::StringTo<double>("0x1.BC70A3D70A3D7p+6", 10) == std::nullopt); + REQUIRE(Trinity::StringTo<double>("0x1.BC70A3D70A3D7p+6", 16) == std::nullopt); + REQUIRE(Trinity::StringTo<double>("1.BC70A3D70A3D7p+6", 16) == 0x1.BC70A3D70A3D7p+6); + REQUIRE(Trinity::StringTo<double>("0x1.2.3") == std::nullopt); + REQUIRE(Trinity::StringTo<double>("0x1.AAAp+1-3") == std::nullopt); + REQUIRE(Trinity::StringTo<double>("1.2.3", 16) == std::nullopt); + REQUIRE(Trinity::StringTo<double>("1.AAAp+1-3", 16) == std::nullopt); +} diff --git a/tests/game/ChatCommand.cpp b/tests/game/ChatCommand.cpp index f484b5806c5..2f73b579f84 100644 --- a/tests/game/ChatCommand.cpp +++ b/tests/game/ChatCommand.cpp @@ -48,13 +48,30 @@ TEST_CASE("Command argument parsing", "[ChatCommand]") TestChatCommand("true", [](ChatHandler*, uint32) { return true; }, false); } - SECTION("std::vector<uint8>") + SECTION("Floating point argument") { - TestChatCommand("1 2 3 4 5 6 7 8 9 10", [](ChatHandler*, std::vector<uint8> v) + TestChatCommand("0.5", [](ChatHandler*, float f) { - REQUIRE(v.size() == 10); - for (size_t i = 0; i < 10; ++i) - REQUIRE(v[i] == (i + 1)); + REQUIRE(f == 0.5); + return true; + }); + TestChatCommand("true", [](ChatHandler*, float) { return true; }, false); + } + + SECTION("std::vector<uint16>") + { + TestChatCommand("1 2 3 4 5 6 7 8 9 10", [](ChatHandler*, std::vector<uint16> v) + { + REQUIRE(v == std::vector<uint16>{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + return true; + }); + } + + SECTION("std::array<uint16>") + { + TestChatCommand("1 2 3 4 5 6 7 8 9 10", [](ChatHandler*, std::array<uint16, 10> v) + { + REQUIRE(v == std::array<uint16, 10>{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); return true; }); } |