diff --git a/common/StringUtil.cpp b/common/StringUtil.cpp index 8a73ba9b4..71bb961e1 100644 --- a/common/StringUtil.cpp +++ b/common/StringUtil.cpp @@ -193,7 +193,10 @@ uint32 AppendAnyLenString(char** ret, uint32* bufsize, uint32* strlen, const cha return *strlen; } -uint32 hextoi(char* num) { +uint32 hextoi(const char* num) { + if (num == nullptr) + return 0; + int len = strlen(num); if (len < 3) return 0; @@ -217,7 +220,10 @@ uint32 hextoi(char* num) { return ret; } -uint64 hextoi64(char* num) { +uint64 hextoi64(const char* num) { + if (num == nullptr) + return 0; + int len = strlen(num); if (len < 3) return 0; @@ -241,7 +247,10 @@ uint64 hextoi64(char* num) { return ret; } -bool atobool(char* iBool) { +bool atobool(const char* iBool) { + + if (iBool == nullptr) + return false; if (!strcasecmp(iBool, "true")) return true; if (!strcasecmp(iBool, "false")) diff --git a/common/StringUtil.h b/common/StringUtil.h index 81ba6e27e..46ff43183 100644 --- a/common/StringUtil.h +++ b/common/StringUtil.h @@ -33,9 +33,9 @@ void MakeLowerString(const char *source, char *target); int MakeAnyLenString(char** ret, const char* format, ...); uint32 AppendAnyLenString(char** ret, uint32* bufsize, uint32* strlen, const char* format, ...); -uint32 hextoi(char* num); -uint64 hextoi64(char* num); -bool atobool(char* iBool); +uint32 hextoi(const char* num); +uint64 hextoi64(const char* num); +bool atobool(const char* iBool); char* strn0cpy(char* dest, const char* source, uint32 size); // return value =true if entire string(source) fit, false if it was truncated diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2cffeae88..80f6a2f1d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,6 +11,8 @@ SET(tests_headers fixed_memory_variable_test.h ipc_mutex_test.h memory_mapped_file_test.h + atobool_test.h + hextoi_32_64_test.h ) ADD_EXECUTABLE(tests ${tests_sources} ${tests_headers}) diff --git a/tests/atobool_test.h b/tests/atobool_test.h new file mode 100644 index 000000000..ce02df02d --- /dev/null +++ b/tests/atobool_test.h @@ -0,0 +1,103 @@ +/* EQEMu: Everquest Server Emulator + Copyright (C) 2001-2013 EQEMu Development Team (http://eqemulator.net) + + 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; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY except by those people which sell it, which + are required to give you total support for your newly bought product; + 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, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __EQEMU_TESTS_ATOBOOL_H +#define __EQEMU_TESTS_ATOBOOL_H + +#include "cppunit/cpptest.h" +#include "../common/StringUtil.h" + +class atoboolTest : public Test::Suite { + typedef void(atoboolTest::*TestFunction)(void); +public: + atoboolTest() { + TEST_ADD(atoboolTest::TrueTest); + TEST_ADD(atoboolTest::FalseTest); + TEST_ADD(atoboolTest::YesTest); + TEST_ADD(atoboolTest::NoTest); + TEST_ADD(atoboolTest::OnTest); + TEST_ADD(atoboolTest::OffTest); + TEST_ADD(atoboolTest::EnableTest); + TEST_ADD(atoboolTest::DisableTest); + TEST_ADD(atoboolTest::EnabledTest); + TEST_ADD(atoboolTest::DisabledTest); + TEST_ADD(atoboolTest::YTest); + TEST_ADD(atoboolTest::NTest); + TEST_ADD(atoboolTest::nullptrTest); + } + + ~atoboolTest() { + } + + private: + + void TrueTest() { + TEST_ASSERT(atobool("true")); + } + + void FalseTest() { + TEST_ASSERT(!atobool("false")); + } + + void YesTest() { + TEST_ASSERT(atobool("yes")); + } + + void NoTest() { + TEST_ASSERT(!atobool("no")); + } + + void OnTest() { + TEST_ASSERT(atobool("on")); + } + + void OffTest() { + TEST_ASSERT(!atobool("off")); + } + + void EnableTest() { + TEST_ASSERT(atobool("enable")); + } + + void DisableTest() { + TEST_ASSERT(!atobool("disable")); + } + + void EnabledTest() { + TEST_ASSERT(atobool("enabled")); + } + + void DisabledTest() { + TEST_ASSERT(!atobool("disabled")); + } + + void YTest() { + TEST_ASSERT(atobool("y")); + } + + void NTest() { + TEST_ASSERT(!atobool("n")); + } + + void nullptrTest() { + TEST_ASSERT(!atobool(nullptr)); + } + +}; + +#endif diff --git a/tests/hextoi_32_64_test.h b/tests/hextoi_32_64_test.h new file mode 100644 index 000000000..eda5e7fc2 --- /dev/null +++ b/tests/hextoi_32_64_test.h @@ -0,0 +1,213 @@ +/* EQEMu: Everquest Server Emulator + Copyright (C) 2001-2013 EQEMu Development Team (http://eqemulator.net) + + 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; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY except by those people which sell it, which + are required to give you total support for your newly bought product; + 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, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __EQEMU_TESTS_HEXTOI_32_64_H +#define __EQEMU_TESTS_HEXTOI_32_64_H + +#include "cppunit/cpptest.h" +#include "../common/StringUtil.h" + +class hextoi_32_64_Test : public Test::Suite { + typedef void(hextoi_32_64_Test::*TestFunction)(void); +public: + hextoi_32_64_Test() { + TEST_ADD(hextoi_32_64_Test::nullptr32Test); + TEST_ADD(hextoi_32_64_Test::ShortString32Test); + TEST_ADD(hextoi_32_64_Test::SingleDigitUpper32Test); + TEST_ADD(hextoi_32_64_Test::SingleDigitLower32Test); + TEST_ADD(hextoi_32_64_Test::DoubleDigitUpper32Test); + TEST_ADD(hextoi_32_64_Test::DoubleDigitLower32Test); + + TEST_ADD(hextoi_32_64_Test::nullptr64Test); + TEST_ADD(hextoi_32_64_Test::ShortString64Test); + TEST_ADD(hextoi_32_64_Test::SingleDigitUpper64Test); + TEST_ADD(hextoi_32_64_Test::SingleDigitLower64Test); + TEST_ADD(hextoi_32_64_Test::DoubleDigitUpper64Test); + TEST_ADD(hextoi_32_64_Test::DoubleDigitLower64Test); + } + + ~hextoi_32_64_Test() { + } + + private: + + void nullptr32Test() { + TEST_ASSERT(hextoi(nullptr) == 0); + } + + void ShortString32Test() { + // if the string is too short then it should + // spit out a zero. + // strings should be formatted: 0x** or 0X** + TEST_ASSERT(hextoi("") == 0); + TEST_ASSERT(hextoi("0") == 0); + TEST_ASSERT(hextoi("01") == 0); + } + + void SingleDigitUpper32Test() { + TEST_ASSERT(hextoi("0x0") == 0); + TEST_ASSERT(hextoi("0x1") == 1); + TEST_ASSERT(hextoi("0x2") == 2); + TEST_ASSERT(hextoi("0x3") == 3); + TEST_ASSERT(hextoi("0x4") == 4); + TEST_ASSERT(hextoi("0x5") == 5); + TEST_ASSERT(hextoi("0x6") == 6); + TEST_ASSERT(hextoi("0x7") == 7); + TEST_ASSERT(hextoi("0x8") == 8); + TEST_ASSERT(hextoi("0x9") == 9); + TEST_ASSERT(hextoi("0xA") == 10); + TEST_ASSERT(hextoi("0xB") == 11); + TEST_ASSERT(hextoi("0xC") == 12); + TEST_ASSERT(hextoi("0xD") == 13); + TEST_ASSERT(hextoi("0xE") == 14); + TEST_ASSERT(hextoi("0xF") == 15); + } + + void SingleDigitLower32Test() { + TEST_ASSERT(hextoi("0x0") == 0); + TEST_ASSERT(hextoi("0x1") == 1); + TEST_ASSERT(hextoi("0x2") == 2); + TEST_ASSERT(hextoi("0x3") == 3); + TEST_ASSERT(hextoi("0x4") == 4); + TEST_ASSERT(hextoi("0x5") == 5); + TEST_ASSERT(hextoi("0x6") == 6); + TEST_ASSERT(hextoi("0x7") == 7); + TEST_ASSERT(hextoi("0x8") == 8); + TEST_ASSERT(hextoi("0x9") == 9); + TEST_ASSERT(hextoi("0xa") == 10); + TEST_ASSERT(hextoi("0xb") == 11); + TEST_ASSERT(hextoi("0xc") == 12); + TEST_ASSERT(hextoi("0xd") == 13); + TEST_ASSERT(hextoi("0xe") == 14); + TEST_ASSERT(hextoi("0xf") == 15); + } + + // A bit excessive to do an exhaustive test like this + // but it usefully tests multi digit hex. + void DoubleDigitUpper32Test() { + + std::string prepend = "0x"; + std::string hexToTest; + + std::string hexElements = "0123456789ABCDEF"; + uint32 value = 0; + for (std::string::iterator firstDigitIter = hexElements.begin(); firstDigitIter != hexElements.end(); ++firstDigitIter) { + for (std::string::iterator secondDigitIter = hexElements.begin(); secondDigitIter != hexElements.end(); ++secondDigitIter) { + std::string hexToTest = prepend + *firstDigitIter + *secondDigitIter; + TEST_ASSERT(hextoi(hexToTest.c_str()) == value); + value++; + } + } + } + + // A bit excessive to do an exhaustive test like this + // but it usefully tests multi digit hex. + void DoubleDigitLower32Test() { + std::string prepend = "0x"; + std::string hexToTest; + + std::string hexElements = "0123456789abcdef"; + uint32 value = 0; + for (std::string::iterator firstDigitIter = hexElements.begin(); firstDigitIter != hexElements.end(); ++firstDigitIter) { + for (std::string::iterator secondDigitIter = hexElements.begin(); secondDigitIter != hexElements.end(); ++secondDigitIter) { + std::string hexToTest = prepend + *firstDigitIter + *secondDigitIter; + TEST_ASSERT(hextoi(hexToTest.c_str()) == value); + value++; + } + } + } + + + void nullptr64Test() { + TEST_ASSERT(hextoi64(nullptr) == 0); + } + + void ShortString64Test() { + // if the string is too short then it should + // spit out a zero. + // strings should be formatted: 0x** or 0X** + TEST_ASSERT(hextoi64("") == 0); + TEST_ASSERT(hextoi64("0") == 0); + TEST_ASSERT(hextoi64("01") == 0); + } + + void SingleDigitUpper64Test() { + std::string prepend = "0x"; + std::string hexToTest; + + std::string hexElements = "0123456789ABCDEF"; + uint64 value = 0; + for (std::string::iterator firstDigitIter = hexElements.begin(); firstDigitIter != hexElements.end(); ++firstDigitIter) { + std::string hexToTest = prepend + *firstDigitIter; + TEST_ASSERT(hextoi64(hexToTest.c_str()) == value); + value++; + } + } + + void SingleDigitLower64Test() { + + std::string prepend = "0x"; + std::string hexToTest; + + std::string hexElements = "0123456789abcdef"; + uint64 value = 0; + for (std::string::iterator firstDigitIter = hexElements.begin(); firstDigitIter != hexElements.end(); ++firstDigitIter) { + std::string hexToTest = prepend + *firstDigitIter; + TEST_ASSERT(hextoi64(hexToTest.c_str()) == value); + value++; + } + } + + // A bit excessive to do an exhaustive test like this + // but it usefully tests multi digit hex. + void DoubleDigitUpper64Test() { + + std::string prepend = "0x"; + std::string hexToTest; + + std::string hexElements = "0123456789ABCDEF"; + uint64 value = 0; + for (std::string::iterator firstDigitIter = hexElements.begin(); firstDigitIter != hexElements.end(); ++firstDigitIter) { + for (std::string::iterator secondDigitIter = hexElements.begin(); secondDigitIter != hexElements.end(); ++secondDigitIter) { + std::string hexToTest = prepend + *firstDigitIter + *secondDigitIter; + TEST_ASSERT(hextoi64(hexToTest.c_str()) == value); + value++; + } + } + } + + // A bit excessive to do an exhaustive test like this + // but it usefully tests multi digit hex. + void DoubleDigitLower64Test() { + std::string prepend = "0x"; + std::string hexToTest; + + std::string hexElements = "0123456789abcdef"; + uint64 value = 0; + for (std::string::iterator firstDigitIter = hexElements.begin(); firstDigitIter != hexElements.end(); ++firstDigitIter) { + for (std::string::iterator secondDigitIter = hexElements.begin(); secondDigitIter != hexElements.end(); ++secondDigitIter) { + std::string hexToTest = prepend + *firstDigitIter + *secondDigitIter; + TEST_ASSERT(hextoi64(hexToTest.c_str()) == value); + value++; + } + } + } + +}; + +#endif diff --git a/tests/main.cpp b/tests/main.cpp index d12aa143c..fd0862edb 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -24,6 +24,8 @@ #include "ipc_mutex_test.h" #include "fixed_memory_test.h" #include "fixed_memory_variable_test.h" +#include "atobool_test.h" +#include "hextoi_32_64_test.h" int main() { try { @@ -34,6 +36,8 @@ int main() { tests.add(new IPCMutexTest()); tests.add(new FixedMemoryHashTest()); tests.add(new FixedMemoryVariableHashTest()); + tests.add(new atoboolTest()); + tests.add(new hextoi_32_64_Test()); tests.run(*output, true); } catch(...) { return -1;