LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #include <string> | 18 #include <string> |
19 #include <gtest/gtest.h> | 19 #include <gtest/gtest.h> |
20 #include "compiled/String.h" | 20 #include "compiled/String.h" |
21 | 21 |
22 ABP_NS_USING | 22 ABP_NS_USING |
23 | 23 |
24 TEST(TestString, checkConstexprCorrectness) | 24 constexpr void checkConstexprTemplateCtor() |
25 { | 25 { |
26 static constexpr DependentString s(u"Hello world!"); | 26 constexpr DependentString s(u"Test string"); |
27 static constexpr DependentString s2; | 27 } |
28 | 28 |
29 static_assert(s.length() == 12, "Length should be 12"); | 29 constexpr void checkConstexprInvalidCtor() |
| 30 { |
| 31 constexpr DependentString s; |
| 32 static_assert(s.is_invalid(), "String should be invalid"); |
| 33 } |
| 34 |
| 35 constexpr void checkConstexprCtor() |
| 36 { |
| 37 constexpr DependentString s(u"Test string", 11); |
| 38 } |
| 39 |
| 40 constexpr void checkConstexprFuncsAndOps() |
| 41 { |
| 42 constexpr DependentString s = u"Test string"_str; |
| 43 constexpr DependentString s2 = u"Test string"_str; |
| 44 constexpr DependentString s3 = u"Another test string"_str; |
| 45 |
| 46 static_assert(s.length() == s2.length(), "Length should be equal"); |
| 47 static_assert(s.equals(s2), "Strings should be equal"); |
| 48 static_assert(s == s2, "Strings should be equal"); |
| 49 static_assert(s != s3, "Strings should not be equal"); |
30 | 50 |
31 static_assert(!s.empty(), "String should not be empty"); | 51 static_assert(!s.empty(), "String should not be empty"); |
32 static_assert(!s.is_writable(), "String should not be writable"); | 52 static_assert(!s.is_writable(), "String should not be writable"); |
33 static_assert(!s.is_deleted(), "String should not be deleted"); | 53 static_assert(!s.is_deleted(), "String should not be deleted"); |
34 | 54 |
35 static_assert(s.data()[0] == 'H', "First character should be 'H'"); | 55 static_assert(s.data()[0] == 'T', "First character should be 'T'"); |
36 static_assert(s[1] == 'e', "Second character should be 'e'"); | 56 static_assert(s[1] == 'e', "Second character should be 'e'"); |
37 | 57 |
38 static_assert(s2.is_invalid(), "String should be invalid"); | 58 static_assert(s.find(u'e') == 1, "'e' should be at pos 1"); |
| 59 static_assert(s.find(s2) == 0, "String should be found at pos 0"); |
| 60 static_assert(s.rfind(u'e') == 1, "'e' should be at pos 1"); |
39 } | 61 } |
40 | 62 |
41 TEST(TestString, constructInvalidDependentString) | 63 TEST(TestString, constructInvalidDependentString) |
42 { | 64 { |
43 DependentString s; | 65 DependentString s; |
44 EXPECT_TRUE(s.is_invalid()); | 66 EXPECT_TRUE(s.is_invalid()); |
45 | 67 |
46 DependentString s2(s); | 68 DependentString s2(s); |
47 EXPECT_TRUE(s2.is_invalid()); | 69 EXPECT_TRUE(s2.is_invalid()); |
48 } | 70 } |
49 | 71 |
50 TEST(TestString, constructInvalidOwnedString) | 72 TEST(TestString, constructInvalidOwnedString) |
51 { | 73 { |
52 OwnedString s; | 74 OwnedString s; |
53 EXPECT_TRUE(s.is_invalid()); | 75 EXPECT_TRUE(s.is_invalid()); |
54 | 76 |
55 // Valid string | 77 // Valid string |
56 OwnedString s2(2); | 78 OwnedString s2(2); |
57 EXPECT_FALSE(s2.is_invalid()); | 79 EXPECT_FALSE(s2.is_invalid()); |
58 | 80 |
59 // Ensure we still have an invalid string. | 81 // Ensure we still have an invalid string. |
60 OwnedString s3(s); | 82 OwnedString s3(s); |
61 EXPECT_TRUE(s3.is_invalid()); | 83 EXPECT_TRUE(s3.is_invalid()); |
62 | 84 |
63 // Empty valid string lead to valid string. | 85 // Empty valid string lead to valid string. |
64 OwnedString s4(u""_str); | 86 OwnedString s4(ABP_TEXT(""_str)); |
65 EXPECT_FALSE(s4.is_invalid()); | 87 EXPECT_FALSE(s4.is_invalid()); |
66 } | 88 } |
67 | 89 |
68 TEST(TestStringTrimSpaces, zeroLengthString) | 90 TEST(TestStringTrimSpaces, zeroLengthString) |
69 { | 91 { |
70 EXPECT_EQ(u""_str, TrimSpaces(DependentString())); | 92 EXPECT_EQ(ABP_TEXT(""_str), TrimSpaces(DependentString())); |
71 EXPECT_EQ(u""_str, TrimSpaces(OwnedString())); | 93 EXPECT_EQ(ABP_TEXT(""_str), TrimSpaces(OwnedString())); |
72 EXPECT_EQ(u""_str, TrimSpaces(u""_str)); | 94 EXPECT_EQ(ABP_TEXT(""_str), TrimSpaces(ABP_TEXT(""_str))); |
73 } | 95 } |
74 | 96 |
75 TEST(TestStringTrimSpaces, spacesAreRemoved) | 97 TEST(TestStringTrimSpaces, spacesAreRemoved) |
76 { | 98 { |
77 for (uint16_t leftSpaces = 0; leftSpaces < 5; ++leftSpaces) | 99 for (uint16_t leftSpaces = 0; leftSpaces < 5; ++leftSpaces) |
78 { | 100 { |
79 for (uint16_t rightSpaces = 0; rightSpaces < 5; ++rightSpaces) | 101 for (uint16_t rightSpaces = 0; rightSpaces < 5; ++rightSpaces) |
80 { | 102 { |
81 for (uint16_t nonSpaces = 0; nonSpaces < 5; ++nonSpaces) | 103 for (uint16_t nonSpaces = 0; nonSpaces < 5; ++nonSpaces) |
82 { | 104 { |
83 OwnedString str; | 105 OwnedString str; |
84 std::string leftSpacesStdString(leftSpaces, ' '); | 106 std::string leftSpacesStdString(leftSpaces, ' '); |
85 str.append(leftSpacesStdString.c_str(), leftSpacesStdString.length()); | 107 str.append(leftSpacesStdString.c_str(), leftSpacesStdString.length()); |
86 std::string stdString(nonSpaces, 'a'); | 108 std::string stdString(nonSpaces, 'a'); |
87 OwnedString trimmedString; | 109 OwnedString trimmedString; |
88 trimmedString.append(stdString.c_str(), stdString.length()); | 110 trimmedString.append(stdString.c_str(), stdString.length()); |
89 str.append(trimmedString); | 111 str.append(trimmedString); |
90 std::string rightSpacesStdString(rightSpaces, ' '); | 112 std::string rightSpacesStdString(rightSpaces, ' '); |
91 str.append(rightSpacesStdString.c_str(), rightSpacesStdString.length()); | 113 str.append(rightSpacesStdString.c_str(), rightSpacesStdString.length()); |
92 EXPECT_EQ(trimmedString, TrimSpaces(str)); | 114 EXPECT_EQ(trimmedString, TrimSpaces(str)); |
93 } | 115 } |
94 } | 116 } |
95 } | 117 } |
96 } | 118 } |
97 | 119 |
98 TEST(TestStringSplitString, test) | 120 TEST(TestStringSplitString, test) |
99 { | 121 { |
100 { | 122 { |
101 auto str = u"123:abc"_str; | 123 auto str = ABP_TEXT("123:abc"_str); |
102 auto split = SplitString(str, 3); | 124 auto split = SplitString(str, 3); |
103 EXPECT_EQ(u"123"_str, split.first); | 125 EXPECT_EQ(ABP_TEXT("123"_str), split.first); |
104 EXPECT_EQ(u"abc"_str, split.second); | 126 EXPECT_EQ(ABP_TEXT("abc"_str), split.second); |
105 } | 127 } |
106 { | 128 { |
107 auto str = u"123:abc"_str; | 129 auto str = ABP_TEXT("123:abc"_str); |
108 auto split = SplitString(str, 0); | 130 auto split = SplitString(str, 0); |
109 EXPECT_EQ(u""_str, split.first); | 131 EXPECT_EQ(ABP_TEXT(""_str), split.first); |
110 EXPECT_EQ(u"23:abc"_str, split.second); | 132 EXPECT_EQ(ABP_TEXT("23:abc"_str), split.second); |
111 } | 133 } |
112 { | 134 { |
113 auto str = u"123:abc"_str; | 135 auto str = ABP_TEXT("123:abc"_str); |
114 auto split = SplitString(str, 6); | 136 auto split = SplitString(str, 6); |
115 EXPECT_EQ(u"123:ab"_str, split.first); | 137 EXPECT_EQ(ABP_TEXT("123:ab"_str), split.first); |
116 EXPECT_EQ(u""_str, split.second); | 138 EXPECT_EQ(ABP_TEXT(""_str), split.second); |
117 } | 139 } |
118 { | 140 { |
119 auto str = u"123:abc"_str; | 141 auto str = ABP_TEXT("123:abc"_str); |
120 auto split = SplitString(str, 7); | 142 auto split = SplitString(str, 7); |
121 EXPECT_EQ(u"123:abc"_str, split.first); | 143 EXPECT_EQ(ABP_TEXT("123:abc"_str), split.first); |
122 EXPECT_EQ(u""_str, split.second); | 144 EXPECT_EQ(ABP_TEXT(""_str), split.second); |
123 } | 145 } |
124 { | 146 { |
125 auto str = u"123:abc"_str; | 147 auto str = ABP_TEXT("123:abc"_str); |
126 auto split = SplitString(str, 10); | 148 auto split = SplitString(str, 10); |
127 EXPECT_EQ(u"123:abc"_str, split.first); | 149 EXPECT_EQ(ABP_TEXT("123:abc"_str), split.first); |
128 EXPECT_EQ(u""_str, split.second); | 150 EXPECT_EQ(ABP_TEXT(""_str), split.second); |
129 } | 151 } |
130 } | 152 } |
131 | 153 |
132 TEST(TestStringLexicalCast, toIntegers) | 154 TEST(TestStringLexicalCast, toIntegers) |
133 { | 155 { |
134 EXPECT_EQ(0, lexical_cast<int32_t>(u"0"_str)); | 156 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT("0"_str))); |
135 EXPECT_EQ(1, lexical_cast<int32_t>(u"1"_str)); | 157 EXPECT_EQ(1, lexical_cast<int32_t>(ABP_TEXT("1"_str))); |
136 EXPECT_EQ(2, lexical_cast<int32_t>(u"2"_str)); | 158 EXPECT_EQ(2, lexical_cast<int32_t>(ABP_TEXT("2"_str))); |
137 EXPECT_EQ(10, lexical_cast<int32_t>(u"10"_str)); | 159 EXPECT_EQ(10, lexical_cast<int32_t>(ABP_TEXT("10"_str))); |
138 EXPECT_EQ(10, lexical_cast<int32_t>(u"010"_str)); | 160 EXPECT_EQ(10, lexical_cast<int32_t>(ABP_TEXT("010"_str))); |
139 EXPECT_EQ(-1, lexical_cast<int32_t>(u"-1"_str)); | 161 EXPECT_EQ(-1, lexical_cast<int32_t>(ABP_TEXT("-1"_str))); |
140 EXPECT_EQ(-2, lexical_cast<int32_t>(u"-2"_str)); | 162 EXPECT_EQ(-2, lexical_cast<int32_t>(ABP_TEXT("-2"_str))); |
141 EXPECT_EQ(-20, lexical_cast<int32_t>(u"-20"_str)); | 163 EXPECT_EQ(-20, lexical_cast<int32_t>(ABP_TEXT("-20"_str))); |
142 EXPECT_EQ(-20, lexical_cast<int32_t>(u"-020"_str)); | 164 EXPECT_EQ(-20, lexical_cast<int32_t>(ABP_TEXT("-020"_str))); |
143 EXPECT_EQ(0, lexical_cast<int32_t>(u"0-2"_str)); | 165 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT("0-2"_str))); |
144 EXPECT_EQ(-2147483647, lexical_cast<int32_t>(u"-2147483647"_str)); | 166 EXPECT_EQ(-2147483647, lexical_cast<int32_t>(ABP_TEXT("-2147483647"_str))); |
145 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(u"-2147483648"_str)); | 167 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(ABP_TEXT("-2147483648"_str))); |
146 EXPECT_EQ( 0, lexical_cast<int32_t>(u"-2147483649"_str)); | 168 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("-2147483649"_str))); |
147 EXPECT_EQ( 0, lexical_cast<int32_t>(u"-2157483649"_str)); | 169 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("-2157483649"_str))); |
148 EXPECT_EQ( 0, lexical_cast<int32_t>(u"-3147483649"_str)); | 170 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("-3147483649"_str))); |
149 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(u"-02147483648"_str)); | 171 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(ABP_TEXT("-02147483648"_str))); |
150 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(u"-000002147483648"_str)); | 172 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(ABP_TEXT("-000002147483648"_str))
); |
151 EXPECT_EQ( 0, lexical_cast<int32_t>(u"-21474836480"_str)); | 173 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("-21474836480"_str))); |
152 EXPECT_EQ(2147483647, lexical_cast<int32_t>(u"2147483647"_str)); | 174 EXPECT_EQ(2147483647, lexical_cast<int32_t>(ABP_TEXT("2147483647"_str))); |
153 EXPECT_EQ(2147483647, lexical_cast<int32_t>(u"000002147483647"_str)); | 175 EXPECT_EQ(2147483647, lexical_cast<int32_t>(ABP_TEXT("000002147483647"_str))); |
154 EXPECT_EQ(2147483647, lexical_cast<int32_t>(u"02147483647"_str)); | 176 EXPECT_EQ(2147483647, lexical_cast<int32_t>(ABP_TEXT("02147483647"_str))); |
155 EXPECT_EQ( 0, lexical_cast<int32_t>(u"21474836470"_str)); | 177 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("21474836470"_str))); |
156 EXPECT_EQ( 0, lexical_cast<int32_t>(u"2147483648"_str)); | 178 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("2147483648"_str))); |
157 EXPECT_EQ( 0, lexical_cast<int32_t>(u"2157483648"_str)); | 179 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("2157483648"_str))); |
158 EXPECT_EQ( 0, lexical_cast<int32_t>(u"3147483648"_str)); | 180 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("3147483648"_str))); |
159 EXPECT_EQ(0u, lexical_cast<uint32_t>(u"0"_str)); | 181 EXPECT_EQ(0u, lexical_cast<uint32_t>(ABP_TEXT("0"_str))); |
160 EXPECT_EQ(2u, lexical_cast<uint32_t>(u"2"_str)); | 182 EXPECT_EQ(2u, lexical_cast<uint32_t>(ABP_TEXT("2"_str))); |
161 EXPECT_EQ(123u, lexical_cast<uint32_t>(u"123"_str)); | 183 EXPECT_EQ(123u, lexical_cast<uint32_t>(ABP_TEXT("123"_str))); |
162 EXPECT_EQ(123u, lexical_cast<uint32_t>(u"0123"_str)); | 184 EXPECT_EQ(123u, lexical_cast<uint32_t>(ABP_TEXT("0123"_str))); |
163 EXPECT_EQ(123u, lexical_cast<uint32_t>(u"0000123"_str)); | 185 EXPECT_EQ(123u, lexical_cast<uint32_t>(ABP_TEXT("0000123"_str))); |
164 EXPECT_EQ(4294967294u, lexical_cast<uint32_t>(u"4294967294"_str)); | 186 EXPECT_EQ(4294967294u, lexical_cast<uint32_t>(ABP_TEXT("4294967294"_str))); |
165 EXPECT_EQ(4294967295u, lexical_cast<uint32_t>(u"4294967295"_str)); | 187 EXPECT_EQ(4294967295u, lexical_cast<uint32_t>(ABP_TEXT("4294967295"_str))); |
166 EXPECT_EQ( 0u, lexical_cast<uint32_t>(u"4294967296"_str)); | 188 EXPECT_EQ( 0u, lexical_cast<uint32_t>(ABP_TEXT("4294967296"_str))); |
167 EXPECT_EQ( 0u, lexical_cast<uint32_t>(u"4594967295"_str)); | 189 EXPECT_EQ( 0u, lexical_cast<uint32_t>(ABP_TEXT("4594967295"_str))); |
168 EXPECT_EQ( 0u, lexical_cast<uint32_t>(u"5294967295"_str)); | 190 EXPECT_EQ( 0u, lexical_cast<uint32_t>(ABP_TEXT("5294967295"_str))); |
169 EXPECT_EQ( 0u, lexical_cast<uint32_t>(u"42949672950"_str)); | 191 EXPECT_EQ( 0u, lexical_cast<uint32_t>(ABP_TEXT("42949672950"_str))); |
170 EXPECT_EQ(4294967295u, lexical_cast<uint32_t>(u"04294967295"_str)); | 192 EXPECT_EQ(4294967295u, lexical_cast<uint32_t>(ABP_TEXT("04294967295"_str))); |
171 | 193 |
172 EXPECT_EQ(0, lexical_cast<int32_t>(u" 123"_str)); | 194 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT(" 123"_str))); |
173 EXPECT_EQ(0u, lexical_cast<uint32_t>(u" 123"_str)); | 195 EXPECT_EQ(0u, lexical_cast<uint32_t>(ABP_TEXT(" 123"_str))); |
174 EXPECT_EQ(0, lexical_cast<int32_t>(u"123abc"_str)); | 196 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT("123abc"_str))); |
175 EXPECT_EQ(0u, lexical_cast<uint32_t>(u"123abc"_str)); | 197 EXPECT_EQ(0u, lexical_cast<uint32_t>(ABP_TEXT("123abc"_str))); |
176 EXPECT_EQ(0, lexical_cast<int32_t>(u"1 23"_str)); | 198 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT("1 23"_str))); |
177 EXPECT_EQ(0u, lexical_cast<uint32_t>(u"1 23"_str)); | 199 EXPECT_EQ(0u, lexical_cast<uint32_t>(ABP_TEXT("1 23"_str))); |
178 } | 200 } |
179 | 201 |
180 TEST(TestStringLexicalCast, toBoolean) | 202 TEST(TestStringLexicalCast, toBoolean) |
181 { | 203 { |
182 EXPECT_TRUE(lexical_cast<bool>(u"true"_str)); | 204 EXPECT_TRUE(lexical_cast<bool>(ABP_TEXT("true"_str))); |
183 EXPECT_FALSE(lexical_cast<bool>(u"true123"_str)); | 205 EXPECT_FALSE(lexical_cast<bool>(ABP_TEXT("true123"_str))); |
184 EXPECT_FALSE(lexical_cast<bool>(u"false"_str)); | 206 EXPECT_FALSE(lexical_cast<bool>(ABP_TEXT("false"_str))); |
185 EXPECT_FALSE(lexical_cast<bool>(u"some-string"_str)); | 207 EXPECT_FALSE(lexical_cast<bool>(ABP_TEXT("some-string"_str))); |
186 EXPECT_FALSE(lexical_cast<bool>(u""_str)); | 208 EXPECT_FALSE(lexical_cast<bool>(ABP_TEXT(""_str))); |
187 EXPECT_FALSE(lexical_cast<bool>(DependentString())); | 209 EXPECT_FALSE(lexical_cast<bool>(DependentString())); |
188 } | 210 } |
189 | 211 |
190 TEST(TestStringLexicalCast, toOwnedString) | 212 TEST(TestStringLexicalCast, toOwnedString) |
191 { | 213 { |
192 EXPECT_EQ(u"some-string"_str, lexical_cast<OwnedString>(u"some-string"_str)); | 214 EXPECT_EQ(ABP_TEXT("some-string"_str), lexical_cast<OwnedString>(ABP_TEXT("som
e-string"_str))); |
193 EXPECT_EQ(u""_str, lexical_cast<OwnedString>(u""_str)); | 215 EXPECT_EQ(ABP_TEXT(""_str), lexical_cast<OwnedString>(ABP_TEXT(""_str))); |
194 } | 216 } |
LEFT | RIGHT |