| 1 | // |
| 2 | // StringTest.h |
| 3 | // |
| 4 | // Definition of the StringTest class. |
| 5 | // |
| 6 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #ifndef StringTest_INCLUDED |
| 14 | #define StringTest_INCLUDED |
| 15 | |
| 16 | |
| 17 | #include "Poco/Foundation.h" |
| 18 | #include "Poco/CppUnit/TestCase.h" |
| 19 | #include "Poco/NumericString.h" |
| 20 | #include "Poco/MemoryStream.h" |
| 21 | |
| 22 | |
| 23 | class StringTest: public CppUnit::TestCase |
| 24 | { |
| 25 | public: |
| 26 | StringTest(const std::string& name); |
| 27 | ~StringTest(); |
| 28 | |
| 29 | void testTrimLeft(); |
| 30 | void testTrimLeftInPlace(); |
| 31 | void testTrimRight(); |
| 32 | void testTrimRightInPlace(); |
| 33 | void testTrim(); |
| 34 | void testTrimInPlace(); |
| 35 | void testToUpper(); |
| 36 | void testToLower(); |
| 37 | void testIstring(); |
| 38 | void testIcompare(); |
| 39 | void testCILessThan(); |
| 40 | void testTranslate(); |
| 41 | void testTranslateInPlace(); |
| 42 | void testReplace(); |
| 43 | void testReplaceInPlace(); |
| 44 | void testCat(); |
| 45 | void testStartsWith(); |
| 46 | void testEndsWith(); |
| 47 | |
| 48 | void testStringToInt(); |
| 49 | void testStringToFloat(); |
| 50 | void testStringToDouble(); |
| 51 | void testNumericStringPadding(); |
| 52 | void testNumericStringLimit(); |
| 53 | void testStringToFloatError(); |
| 54 | void testNumericLocale(); |
| 55 | void benchmarkStrToFloat(); |
| 56 | void benchmarkStrToInt(); |
| 57 | |
| 58 | void testIntToString(); |
| 59 | void testFloatToString(); |
| 60 | void benchmarkFloatToStr(); |
| 61 | |
| 62 | void testJSONString(); |
| 63 | |
| 64 | void setUp(); |
| 65 | void tearDown(); |
| 66 | |
| 67 | static CppUnit::Test* suite(); |
| 68 | |
| 69 | private: |
| 70 | |
| 71 | template <typename T> |
| 72 | void stringToInt() |
| 73 | { |
| 74 | T result = 0; |
| 75 | if (123 <= std::numeric_limits<T>::max()) |
| 76 | assertTrue (Poco::strToInt("123" , result, 10)); assertTrue (result == 123); |
| 77 | |
| 78 | assertTrue (Poco::strToInt("0" , result, 10)); assertTrue (result == 0); |
| 79 | assertTrue (Poco::strToInt("000" , result, 10)); assertTrue (result == 0); |
| 80 | |
| 81 | if (std::numeric_limits<T>::is_signed && (-123 > std::numeric_limits<T>::min())) |
| 82 | { assertTrue (Poco::strToInt("-123" , result, 10)); assertTrue (result == -123); } |
| 83 | if (0x123 < std::numeric_limits<T>::max()) |
| 84 | { assertTrue (Poco::strToInt("123" , result, 0x10)); assertTrue (result == 0x123); } |
| 85 | if (0x12ab < std::numeric_limits<T>::max()) |
| 86 | { assertTrue (Poco::strToInt("12AB" , result, 0x10)); assertTrue (result == 0x12ab); } |
| 87 | if (123 < std::numeric_limits<T>::max()) |
| 88 | { assertTrue (Poco::strToInt("00" , result, 0x10)); assertTrue (result == 0); } |
| 89 | if (0123 < std::numeric_limits<T>::max()) |
| 90 | { assertTrue (Poco::strToInt("123" , result, 010)); assertTrue (result == 0123); } |
| 91 | if (0123 < std::numeric_limits<T>::max()) |
| 92 | { assertTrue (Poco::strToInt("0123" , result, 010)); assertTrue (result == 0123); } |
| 93 | |
| 94 | assertTrue (Poco::strToInt("0" , result, 010)); assertTrue (result == 0); |
| 95 | assertTrue (Poco::strToInt("000" , result, 010)); assertTrue (result == 0); |
| 96 | } |
| 97 | |
| 98 | template <typename Larger, typename Smaller> |
| 99 | void numericStringLimitSameSign() |
| 100 | { |
| 101 | Larger l = std::numeric_limits<Smaller>::max(); |
| 102 | std::string str = std::to_string(l); |
| 103 | |
| 104 | Smaller s; |
| 105 | assertTrue(Poco::strToInt<Smaller>(str, s, 10)); |
| 106 | assertTrue(s == std::numeric_limits<Smaller>::max()); |
| 107 | ++l; str = std::to_string(l); |
| 108 | assertFalse(Poco::strToInt<Smaller>(str, s, 10)); |
| 109 | ++l; str = std::to_string(l); |
| 110 | assertFalse(Poco::strToInt<Smaller>(str, s, 10)); |
| 111 | ++l; str = std::to_string(l); |
| 112 | assertFalse(Poco::strToInt<Smaller>(str, s, 10)); |
| 113 | ++l; str = std::to_string(l); |
| 114 | assertFalse(Poco::strToInt<Smaller>(str, s, 10)); |
| 115 | ++l; str = std::to_string(l); |
| 116 | assertFalse(Poco::strToInt<Smaller>(str, s, 10)); |
| 117 | ++l; str = std::to_string(l); |
| 118 | assertFalse(Poco::strToInt<Smaller>(str, s, 10)); |
| 119 | } |
| 120 | |
| 121 | template <typename Larger, typename Smaller> |
| 122 | void numericStringLowerLimit() |
| 123 | { |
| 124 | Larger l = std::numeric_limits<Smaller>::min(); |
| 125 | std::string val = std::to_string(l); |
| 126 | Smaller s = -1; |
| 127 | assertFalse(s == std::numeric_limits<Smaller>::min()); |
| 128 | assertTrue(Poco::strToInt<Smaller>(val, s, 10)); |
| 129 | assertTrue (s == std::numeric_limits<Smaller>::min()); |
| 130 | assertTrue(s == std::numeric_limits<Smaller>::min()); |
| 131 | --l; val = std::to_string(l); |
| 132 | assertFalse(Poco::strToInt<Smaller>(val, s, 10)); |
| 133 | --l; val = std::to_string(l); |
| 134 | assertFalse(Poco::strToInt<Smaller>(val, s, 10)); |
| 135 | --l; val = std::to_string(l); |
| 136 | assertFalse(Poco::strToInt<Smaller>(val, s, 10)); |
| 137 | --l; val = std::to_string(l); |
| 138 | assertFalse(Poco::strToInt<Smaller>(val, s, 10)); |
| 139 | --l; val = std::to_string(l); |
| 140 | assertFalse(Poco::strToInt<Smaller>(val, s, 10)); |
| 141 | --l; val = std::to_string(l); |
| 142 | assertFalse(Poco::strToInt<Smaller>(val, s, 10)); |
| 143 | } |
| 144 | |
| 145 | template <typename T> |
| 146 | bool parseStream(const std::string& s, T& value) |
| 147 | { |
| 148 | Poco::MemoryInputStream istr(s.data(), s.size()); |
| 149 | #if !defined(POCO_NO_LOCALE) |
| 150 | istr.imbue(std::locale::classic()); |
| 151 | #endif |
| 152 | istr >> value; |
| 153 | return istr.eof() && !istr.fail(); |
| 154 | } |
| 155 | }; |
| 156 | |
| 157 | |
| 158 | #endif // StringTest_INCLUDED |
| 159 | |