From 2aaebc193c231878133b70e466bd564645db5e4e Mon Sep 17 00:00:00 2001 From: Treeston Date: Sat, 29 Aug 2020 22:03:22 +0200 Subject: UnitTests: Cleanup + StringConvert/ChatCommand tests (PR #25353) (cherry picked from commit 8ce3635d39dbdd101ddf180a66c483162df99c04) --- tests/common/StringConvert.cpp | 131 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tests/common/StringConvert.cpp (limited to 'tests/common/StringConvert.cpp') diff --git a/tests/common/StringConvert.cpp b/tests/common/StringConvert.cpp new file mode 100644 index 00000000000..d25e28f3d19 --- /dev/null +++ b/tests/common/StringConvert.cpp @@ -0,0 +1,131 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "tc_catch2.h" + +#include "StringConvert.h" + +TEST_CASE("String to uint32", "[StringConvert]") +{ + REQUIRE(Trinity::StringTo("42") == 42); + REQUIRE(Trinity::StringTo("42", 10) == 42); + REQUIRE(Trinity::StringTo(" 42") == std::nullopt); + REQUIRE(Trinity::StringTo("tail42") == std::nullopt); + REQUIRE(Trinity::StringTo("42tail") == std::nullopt); + + REQUIRE(Trinity::StringTo("ff", 16) == 0xff); + REQUIRE(Trinity::StringTo("0xff") == std::nullopt); + REQUIRE(Trinity::StringTo("0xff", 0) == 0xff); + + REQUIRE(Trinity::StringTo("101010", 2) == 0b101010); + REQUIRE(Trinity::StringTo("0b101010") == std::nullopt); + REQUIRE(Trinity::StringTo("0b101010", 0) == 0b101010); + + REQUIRE(Trinity::StringTo("5000000000") == std::nullopt); + REQUIRE(Trinity::StringTo("100000000", 16) == std::nullopt); + REQUIRE(Trinity::StringTo("0x100000000", 0) == std::nullopt); + REQUIRE(Trinity::StringTo("0xffffffff", 0) == 0xffffffff); +} + +TEST_CASE("String to uint64", "[StringConvert]") +{ + REQUIRE(Trinity::StringTo("42") == 42); + REQUIRE(Trinity::StringTo("42", 10) == 42); + REQUIRE(Trinity::StringTo(" 42") == std::nullopt); + REQUIRE(Trinity::StringTo("tail42") == std::nullopt); + REQUIRE(Trinity::StringTo("42tail") == std::nullopt); + REQUIRE(Trinity::StringTo("-1", 0) == std::nullopt); + + REQUIRE(Trinity::StringTo("ff", 16) == 0xff); + REQUIRE(Trinity::StringTo("0xff") == std::nullopt); + REQUIRE(Trinity::StringTo("0xff", 0) == 0xff); + + REQUIRE(Trinity::StringTo("101010", 2) == 0b101010); + REQUIRE(Trinity::StringTo("0b101010") == std::nullopt); + REQUIRE(Trinity::StringTo("0b101010", 0) == 0b101010); + + REQUIRE(Trinity::StringTo("5000000000") == 5000000000ULL); + REQUIRE(Trinity::StringTo("100000000", 16) == 0x100000000ULL); + + REQUIRE(Trinity::StringTo("20000000000000000000") == std::nullopt); + REQUIRE(Trinity::StringTo("10000000000000000", 16) == std::nullopt); + REQUIRE(Trinity::StringTo("0x10000000000000000", 0) == std::nullopt); + REQUIRE(Trinity::StringTo("0xFFFFFFFFFFFFFFFF", 0) == 0xffffffffffffffffULL); +} + +TEST_CASE("String to int32", "[StringConvert]") +{ + REQUIRE(Trinity::StringTo("-42") == -42); + REQUIRE(Trinity::StringTo("42") == 42); + REQUIRE(Trinity::StringTo("+42") == std::nullopt); + REQUIRE(Trinity::StringTo("--42") == std::nullopt); + REQUIRE(Trinity::StringTo("~42") == std::nullopt); + REQUIRE(Trinity::StringTo("42-") == std::nullopt); + + REQUIRE(Trinity::StringTo("-ffff", 16) == -0xffff); + REQUIRE(Trinity::StringTo("ffffffff", 16) == std::nullopt); + REQUIRE(Trinity::StringTo("7fffffff", 16) == 0x7fffffff); +} + +TEST_CASE("String to int64", "[StringConvert]") +{ + REQUIRE(Trinity::StringTo("-42") == -42); + REQUIRE(Trinity::StringTo("42") == 42); + REQUIRE(Trinity::StringTo("+42") == std::nullopt); + REQUIRE(Trinity::StringTo("--42") == std::nullopt); + REQUIRE(Trinity::StringTo("~42") == std::nullopt); + REQUIRE(Trinity::StringTo("42-") == std::nullopt); + + REQUIRE(Trinity::StringTo("-ffff", 16) == -0xffff); + REQUIRE(Trinity::StringTo("ffffffff", 16) == 0xffffffff); + REQUIRE(Trinity::StringTo("7fffffff", 16) == 0x7fffffff); + + REQUIRE(Trinity::StringTo("ffffffffffffffff", 16) == std::nullopt); + REQUIRE(Trinity::StringTo("7fffffffffffffff", 16) == 0x7fffffffffffffffLL); + + REQUIRE(Trinity::StringTo("-8500000000000000", 16) == std::nullopt); +} + +TEST_CASE("String to smaller integer types", "[StringConvert]") +{ + REQUIRE(Trinity::StringTo("0xff", 0) == 0xff); + REQUIRE(Trinity::StringTo("0x1ff", 0) == std::nullopt); + + REQUIRE(Trinity::StringTo("0xff", 0) == std::nullopt); + REQUIRE(Trinity::StringTo("0x7f", 0) == 0x7f); + REQUIRE(Trinity::StringTo("-7f", 16) == -0x7f); + + REQUIRE(Trinity::StringTo("0x1ff", 0) == 0x1ff); + REQUIRE(Trinity::StringTo("0x1ffff", 0) == std::nullopt); + REQUIRE(Trinity::StringTo("-1", 0) == std::nullopt); + + REQUIRE(Trinity::StringTo("0x1ff", 0) == 0x1ff); + REQUIRE(Trinity::StringTo("0xffff", 0) == std::nullopt); + REQUIRE(Trinity::StringTo("0x7fff", 0) == 0x7fff); + REQUIRE(Trinity::StringTo("-1", 0) == -1); +} + +TEST_CASE("Test.String to boolean", "[StringConvert]") +{ + REQUIRE(Trinity::StringTo("true") == true); + REQUIRE(Trinity::StringTo("false") == false); + REQUIRE(Trinity::StringTo("ture") == std::nullopt); + REQUIRE(Trinity::StringTo("true", 10) == std::nullopt); + REQUIRE(Trinity::StringTo("1") == true); + REQUIRE(Trinity::StringTo("1", 10) == true); + REQUIRE(Trinity::StringTo("0", 10) == false); +} -- cgit v1.2.3