Left: | ||
Right: |
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_assert(s.length() == 12, "Length should be 12"); | 27 } |
28 | 28 |
29 EXPECT_FALSE(s.is_writable()); | 29 constexpr void checkConstexprInvalidCtor() |
sergei
2018/03/14 16:57:44
I wonder whether it should be static_assert too.
René Jeschke
2018/03/14 17:01:00
Yeah, was thinking about this, too. I wasn't sure
sergei
2018/03/14 17:03:09
I think it's fine, JIC technically it's possible.
René Jeschke
2018/03/14 17:12:22
Done.
| |
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"); | |
50 | |
51 static_assert(!s.empty(), "String should not be empty"); | |
52 static_assert(!s.is_writable(), "String should not be writable"); | |
53 static_assert(!s.is_deleted(), "String should not be deleted"); | |
54 | |
55 static_assert(s.data()[0] == 'T', "First character should be 'T'"); | |
56 static_assert(s[1] == 'e', "Second character should be 'e'"); | |
57 | |
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"); | |
30 } | 61 } |
31 | 62 |
32 TEST(TestString, constructInvalidDependentString) | 63 TEST(TestString, constructInvalidDependentString) |
33 { | 64 { |
34 DependentString s; | 65 DependentString s; |
35 EXPECT_TRUE(s.is_invalid()); | 66 EXPECT_TRUE(s.is_invalid()); |
36 | 67 |
37 DependentString s2(s); | 68 DependentString s2(s); |
38 EXPECT_TRUE(s2.is_invalid()); | 69 EXPECT_TRUE(s2.is_invalid()); |
39 } | 70 } |
40 | 71 |
41 TEST(TestString, constructInvalidOwnedString) | 72 TEST(TestString, constructInvalidOwnedString) |
42 { | 73 { |
43 OwnedString s; | 74 OwnedString s; |
44 EXPECT_TRUE(s.is_invalid()); | 75 EXPECT_TRUE(s.is_invalid()); |
45 | 76 |
46 // Valid string | 77 // Valid string |
47 OwnedString s2(2); | 78 OwnedString s2(2); |
48 EXPECT_FALSE(s2.is_invalid()); | 79 EXPECT_FALSE(s2.is_invalid()); |
49 | 80 |
50 // Ensure we still have an invalid string. | 81 // Ensure we still have an invalid string. |
51 OwnedString s3(s); | 82 OwnedString s3(s); |
52 EXPECT_TRUE(s3.is_invalid()); | 83 EXPECT_TRUE(s3.is_invalid()); |
53 | 84 |
54 // Empty valid string lead to valid string. | 85 // Empty valid string lead to valid string. |
55 OwnedString s4(u""_str); | 86 OwnedString s4(ABP_TEXT(""_str)); |
56 EXPECT_FALSE(s4.is_invalid()); | 87 EXPECT_FALSE(s4.is_invalid()); |
57 } | 88 } |
58 | 89 |
59 TEST(TestStringTrimSpaces, zeroLengthString) | 90 TEST(TestStringTrimSpaces, zeroLengthString) |
60 { | 91 { |
61 EXPECT_EQ(u""_str, TrimSpaces(DependentString())); | 92 EXPECT_EQ(ABP_TEXT(""_str), TrimSpaces(DependentString())); |
62 EXPECT_EQ(u""_str, TrimSpaces(OwnedString())); | 93 EXPECT_EQ(ABP_TEXT(""_str), TrimSpaces(OwnedString())); |
63 EXPECT_EQ(u""_str, TrimSpaces(u""_str)); | 94 EXPECT_EQ(ABP_TEXT(""_str), TrimSpaces(ABP_TEXT(""_str))); |
64 } | 95 } |
65 | 96 |
66 TEST(TestStringTrimSpaces, spacesAreRemoved) | 97 TEST(TestStringTrimSpaces, spacesAreRemoved) |
67 { | 98 { |
68 for (uint16_t leftSpaces = 0; leftSpaces < 5; ++leftSpaces) | 99 for (uint16_t leftSpaces = 0; leftSpaces < 5; ++leftSpaces) |
69 { | 100 { |
70 for (uint16_t rightSpaces = 0; rightSpaces < 5; ++rightSpaces) | 101 for (uint16_t rightSpaces = 0; rightSpaces < 5; ++rightSpaces) |
71 { | 102 { |
72 for (uint16_t nonSpaces = 0; nonSpaces < 5; ++nonSpaces) | 103 for (uint16_t nonSpaces = 0; nonSpaces < 5; ++nonSpaces) |
73 { | 104 { |
74 OwnedString str; | 105 OwnedString str; |
75 std::string leftSpacesStdString(leftSpaces, ' '); | 106 std::string leftSpacesStdString(leftSpaces, ' '); |
76 str.append(leftSpacesStdString.c_str(), leftSpacesStdString.length()); | 107 str.append(leftSpacesStdString.c_str(), leftSpacesStdString.length()); |
77 std::string stdString(nonSpaces, 'a'); | 108 std::string stdString(nonSpaces, 'a'); |
78 OwnedString trimmedString; | 109 OwnedString trimmedString; |
79 trimmedString.append(stdString.c_str(), stdString.length()); | 110 trimmedString.append(stdString.c_str(), stdString.length()); |
80 str.append(trimmedString); | 111 str.append(trimmedString); |
81 std::string rightSpacesStdString(rightSpaces, ' '); | 112 std::string rightSpacesStdString(rightSpaces, ' '); |
82 str.append(rightSpacesStdString.c_str(), rightSpacesStdString.length()); | 113 str.append(rightSpacesStdString.c_str(), rightSpacesStdString.length()); |
83 EXPECT_EQ(trimmedString, TrimSpaces(str)); | 114 EXPECT_EQ(trimmedString, TrimSpaces(str)); |
84 } | 115 } |
85 } | 116 } |
86 } | 117 } |
87 } | 118 } |
88 | 119 |
89 TEST(TestStringSplitString, test) | 120 TEST(TestStringSplitString, test) |
90 { | 121 { |
91 { | 122 { |
92 auto str = u"123:abc"_str; | 123 auto str = ABP_TEXT("123:abc"_str); |
93 auto split = SplitString(str, 3); | 124 auto split = SplitString(str, 3); |
94 EXPECT_EQ(u"123"_str, split.first); | 125 EXPECT_EQ(ABP_TEXT("123"_str), split.first); |
95 EXPECT_EQ(u"abc"_str, split.second); | 126 EXPECT_EQ(ABP_TEXT("abc"_str), split.second); |
96 } | 127 } |
97 { | 128 { |
98 auto str = u"123:abc"_str; | 129 auto str = ABP_TEXT("123:abc"_str); |
99 auto split = SplitString(str, 0); | 130 auto split = SplitString(str, 0); |
100 EXPECT_EQ(u""_str, split.first); | 131 EXPECT_EQ(ABP_TEXT(""_str), split.first); |
101 EXPECT_EQ(u"23:abc"_str, split.second); | 132 EXPECT_EQ(ABP_TEXT("23:abc"_str), split.second); |
102 } | 133 } |
103 { | 134 { |
104 auto str = u"123:abc"_str; | 135 auto str = ABP_TEXT("123:abc"_str); |
105 auto split = SplitString(str, 6); | 136 auto split = SplitString(str, 6); |
106 EXPECT_EQ(u"123:ab"_str, split.first); | 137 EXPECT_EQ(ABP_TEXT("123:ab"_str), split.first); |
107 EXPECT_EQ(u""_str, split.second); | 138 EXPECT_EQ(ABP_TEXT(""_str), split.second); |
108 } | 139 } |
109 { | 140 { |
110 auto str = u"123:abc"_str; | 141 auto str = ABP_TEXT("123:abc"_str); |
111 auto split = SplitString(str, 7); | 142 auto split = SplitString(str, 7); |
112 EXPECT_EQ(u"123:abc"_str, split.first); | 143 EXPECT_EQ(ABP_TEXT("123:abc"_str), split.first); |
113 EXPECT_EQ(u""_str, split.second); | 144 EXPECT_EQ(ABP_TEXT(""_str), split.second); |
114 } | 145 } |
115 { | 146 { |
116 auto str = u"123:abc"_str; | 147 auto str = ABP_TEXT("123:abc"_str); |
117 auto split = SplitString(str, 10); | 148 auto split = SplitString(str, 10); |
118 EXPECT_EQ(u"123:abc"_str, split.first); | 149 EXPECT_EQ(ABP_TEXT("123:abc"_str), split.first); |
119 EXPECT_EQ(u""_str, split.second); | 150 EXPECT_EQ(ABP_TEXT(""_str), split.second); |
120 } | 151 } |
121 } | 152 } |
122 | 153 |
123 TEST(TestStringLexicalCast, toIntegers) | 154 TEST(TestStringLexicalCast, toIntegers) |
124 { | 155 { |
125 EXPECT_EQ(0, lexical_cast<int32_t>(u"0"_str)); | 156 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT("0"_str))); |
126 EXPECT_EQ(1, lexical_cast<int32_t>(u"1"_str)); | 157 EXPECT_EQ(1, lexical_cast<int32_t>(ABP_TEXT("1"_str))); |
127 EXPECT_EQ(2, lexical_cast<int32_t>(u"2"_str)); | 158 EXPECT_EQ(2, lexical_cast<int32_t>(ABP_TEXT("2"_str))); |
128 EXPECT_EQ(10, lexical_cast<int32_t>(u"10"_str)); | 159 EXPECT_EQ(10, lexical_cast<int32_t>(ABP_TEXT("10"_str))); |
129 EXPECT_EQ(10, lexical_cast<int32_t>(u"010"_str)); | 160 EXPECT_EQ(10, lexical_cast<int32_t>(ABP_TEXT("010"_str))); |
130 EXPECT_EQ(-1, lexical_cast<int32_t>(u"-1"_str)); | 161 EXPECT_EQ(-1, lexical_cast<int32_t>(ABP_TEXT("-1"_str))); |
131 EXPECT_EQ(-2, lexical_cast<int32_t>(u"-2"_str)); | 162 EXPECT_EQ(-2, lexical_cast<int32_t>(ABP_TEXT("-2"_str))); |
132 EXPECT_EQ(-20, lexical_cast<int32_t>(u"-20"_str)); | 163 EXPECT_EQ(-20, lexical_cast<int32_t>(ABP_TEXT("-20"_str))); |
133 EXPECT_EQ(-20, lexical_cast<int32_t>(u"-020"_str)); | 164 EXPECT_EQ(-20, lexical_cast<int32_t>(ABP_TEXT("-020"_str))); |
134 EXPECT_EQ(0, lexical_cast<int32_t>(u"0-2"_str)); | 165 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT("0-2"_str))); |
135 EXPECT_EQ(-2147483647, lexical_cast<int32_t>(u"-2147483647"_str)); | 166 EXPECT_EQ(-2147483647, lexical_cast<int32_t>(ABP_TEXT("-2147483647"_str))); |
136 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(u"-2147483648"_str)); | 167 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(ABP_TEXT("-2147483648"_str))); |
137 EXPECT_EQ( 0, lexical_cast<int32_t>(u"-2147483649"_str)); | 168 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("-2147483649"_str))); |
138 EXPECT_EQ( 0, lexical_cast<int32_t>(u"-2157483649"_str)); | 169 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("-2157483649"_str))); |
139 EXPECT_EQ( 0, lexical_cast<int32_t>(u"-3147483649"_str)); | 170 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("-3147483649"_str))); |
140 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(u"-02147483648"_str)); | 171 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(ABP_TEXT("-02147483648"_str))); |
141 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(u"-000002147483648"_str)); | 172 EXPECT_EQ(-2147483648, lexical_cast<int32_t>(ABP_TEXT("-000002147483648"_str)) ); |
142 EXPECT_EQ( 0, lexical_cast<int32_t>(u"-21474836480"_str)); | 173 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("-21474836480"_str))); |
143 EXPECT_EQ(2147483647, lexical_cast<int32_t>(u"2147483647"_str)); | 174 EXPECT_EQ(2147483647, lexical_cast<int32_t>(ABP_TEXT("2147483647"_str))); |
144 EXPECT_EQ(2147483647, lexical_cast<int32_t>(u"000002147483647"_str)); | 175 EXPECT_EQ(2147483647, lexical_cast<int32_t>(ABP_TEXT("000002147483647"_str))); |
145 EXPECT_EQ(2147483647, lexical_cast<int32_t>(u"02147483647"_str)); | 176 EXPECT_EQ(2147483647, lexical_cast<int32_t>(ABP_TEXT("02147483647"_str))); |
146 EXPECT_EQ( 0, lexical_cast<int32_t>(u"21474836470"_str)); | 177 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("21474836470"_str))); |
147 EXPECT_EQ( 0, lexical_cast<int32_t>(u"2147483648"_str)); | 178 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("2147483648"_str))); |
148 EXPECT_EQ( 0, lexical_cast<int32_t>(u"2157483648"_str)); | 179 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("2157483648"_str))); |
149 EXPECT_EQ( 0, lexical_cast<int32_t>(u"3147483648"_str)); | 180 EXPECT_EQ( 0, lexical_cast<int32_t>(ABP_TEXT("3147483648"_str))); |
150 EXPECT_EQ(0u, lexical_cast<uint32_t>(u"0"_str)); | 181 EXPECT_EQ(0u, lexical_cast<uint32_t>(ABP_TEXT("0"_str))); |
151 EXPECT_EQ(2u, lexical_cast<uint32_t>(u"2"_str)); | 182 EXPECT_EQ(2u, lexical_cast<uint32_t>(ABP_TEXT("2"_str))); |
152 EXPECT_EQ(123u, lexical_cast<uint32_t>(u"123"_str)); | 183 EXPECT_EQ(123u, lexical_cast<uint32_t>(ABP_TEXT("123"_str))); |
153 EXPECT_EQ(123u, lexical_cast<uint32_t>(u"0123"_str)); | 184 EXPECT_EQ(123u, lexical_cast<uint32_t>(ABP_TEXT("0123"_str))); |
154 EXPECT_EQ(123u, lexical_cast<uint32_t>(u"0000123"_str)); | 185 EXPECT_EQ(123u, lexical_cast<uint32_t>(ABP_TEXT("0000123"_str))); |
155 EXPECT_EQ(4294967294u, lexical_cast<uint32_t>(u"4294967294"_str)); | 186 EXPECT_EQ(4294967294u, lexical_cast<uint32_t>(ABP_TEXT("4294967294"_str))); |
156 EXPECT_EQ(4294967295u, lexical_cast<uint32_t>(u"4294967295"_str)); | 187 EXPECT_EQ(4294967295u, lexical_cast<uint32_t>(ABP_TEXT("4294967295"_str))); |
157 EXPECT_EQ( 0u, lexical_cast<uint32_t>(u"4294967296"_str)); | 188 EXPECT_EQ( 0u, lexical_cast<uint32_t>(ABP_TEXT("4294967296"_str))); |
158 EXPECT_EQ( 0u, lexical_cast<uint32_t>(u"4594967295"_str)); | 189 EXPECT_EQ( 0u, lexical_cast<uint32_t>(ABP_TEXT("4594967295"_str))); |
159 EXPECT_EQ( 0u, lexical_cast<uint32_t>(u"5294967295"_str)); | 190 EXPECT_EQ( 0u, lexical_cast<uint32_t>(ABP_TEXT("5294967295"_str))); |
160 EXPECT_EQ( 0u, lexical_cast<uint32_t>(u"42949672950"_str)); | 191 EXPECT_EQ( 0u, lexical_cast<uint32_t>(ABP_TEXT("42949672950"_str))); |
161 EXPECT_EQ(4294967295u, lexical_cast<uint32_t>(u"04294967295"_str)); | 192 EXPECT_EQ(4294967295u, lexical_cast<uint32_t>(ABP_TEXT("04294967295"_str))); |
162 | 193 |
163 EXPECT_EQ(0, lexical_cast<int32_t>(u" 123"_str)); | 194 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT(" 123"_str))); |
164 EXPECT_EQ(0u, lexical_cast<uint32_t>(u" 123"_str)); | 195 EXPECT_EQ(0u, lexical_cast<uint32_t>(ABP_TEXT(" 123"_str))); |
165 EXPECT_EQ(0, lexical_cast<int32_t>(u"123abc"_str)); | 196 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT("123abc"_str))); |
166 EXPECT_EQ(0u, lexical_cast<uint32_t>(u"123abc"_str)); | 197 EXPECT_EQ(0u, lexical_cast<uint32_t>(ABP_TEXT("123abc"_str))); |
167 EXPECT_EQ(0, lexical_cast<int32_t>(u"1 23"_str)); | 198 EXPECT_EQ(0, lexical_cast<int32_t>(ABP_TEXT("1 23"_str))); |
168 EXPECT_EQ(0u, lexical_cast<uint32_t>(u"1 23"_str)); | 199 EXPECT_EQ(0u, lexical_cast<uint32_t>(ABP_TEXT("1 23"_str))); |
169 } | 200 } |
170 | 201 |
171 TEST(TestStringLexicalCast, toBoolean) | 202 TEST(TestStringLexicalCast, toBoolean) |
172 { | 203 { |
173 EXPECT_TRUE(lexical_cast<bool>(u"true"_str)); | 204 EXPECT_TRUE(lexical_cast<bool>(ABP_TEXT("true"_str))); |
174 EXPECT_FALSE(lexical_cast<bool>(u"true123"_str)); | 205 EXPECT_FALSE(lexical_cast<bool>(ABP_TEXT("true123"_str))); |
175 EXPECT_FALSE(lexical_cast<bool>(u"false"_str)); | 206 EXPECT_FALSE(lexical_cast<bool>(ABP_TEXT("false"_str))); |
176 EXPECT_FALSE(lexical_cast<bool>(u"some-string"_str)); | 207 EXPECT_FALSE(lexical_cast<bool>(ABP_TEXT("some-string"_str))); |
177 EXPECT_FALSE(lexical_cast<bool>(u""_str)); | 208 EXPECT_FALSE(lexical_cast<bool>(ABP_TEXT(""_str))); |
178 EXPECT_FALSE(lexical_cast<bool>(DependentString())); | 209 EXPECT_FALSE(lexical_cast<bool>(DependentString())); |
179 } | 210 } |
180 | 211 |
181 TEST(TestStringLexicalCast, toOwnedString) | 212 TEST(TestStringLexicalCast, toOwnedString) |
182 { | 213 { |
183 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))); |
184 EXPECT_EQ(u""_str, lexical_cast<OwnedString>(u""_str)); | 215 EXPECT_EQ(ABP_TEXT(""_str), lexical_cast<OwnedString>(ABP_TEXT(""_str))); |
185 } | 216 } |
LEFT | RIGHT |