• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // string_utils_unittests:
7 //   Unit tests for the string utils.
8 //
9 
10 #include "string_utils.h"
11 
12 #include <gmock/gmock.h>
13 #include <gtest/gtest.h>
14 
15 using namespace angle;
16 
17 namespace
18 {
19 
20 // Basic SplitString tests
TEST(StringUtilsTest,SplitString_Basics)21 TEST(StringUtilsTest, SplitString_Basics)
22 {
23     std::vector<std::string> r;
24 
25     r = SplitString(std::string(), ",:;", KEEP_WHITESPACE, SPLIT_WANT_ALL);
26     EXPECT_TRUE(r.empty());
27 
28     // Empty separator list
29     r = SplitString("hello, world", "", KEEP_WHITESPACE, SPLIT_WANT_ALL);
30     ASSERT_EQ(1u, r.size());
31     EXPECT_EQ("hello, world", r[0]);
32 
33     // Should split on any of the separators.
34     r = SplitString("::,,;;", ",:;", KEEP_WHITESPACE, SPLIT_WANT_ALL);
35     ASSERT_EQ(7u, r.size());
36     for (auto str : r)
37         ASSERT_TRUE(str.empty());
38 
39     r = SplitString("red, green; blue:", ",:;", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
40     ASSERT_EQ(3u, r.size());
41     EXPECT_EQ("red", r[0]);
42     EXPECT_EQ("green", r[1]);
43     EXPECT_EQ("blue", r[2]);
44 
45     // Want to split a string along whitespace sequences.
46     r = SplitString("  red green   \tblue\n", " \t\n", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
47     ASSERT_EQ(3u, r.size());
48     EXPECT_EQ("red", r[0]);
49     EXPECT_EQ("green", r[1]);
50     EXPECT_EQ("blue", r[2]);
51 
52     // Weird case of splitting on spaces but not trimming.
53     r = SplitString(" red ", " ", TRIM_WHITESPACE, SPLIT_WANT_ALL);
54     ASSERT_EQ(3u, r.size());
55     EXPECT_EQ("", r[0]);  // Before the first space.
56     EXPECT_EQ("red", r[1]);
57     EXPECT_EQ("", r[2]);  // After the last space.
58 }
59 
60 // Check different whitespace and result types for SplitString
TEST(StringUtilsTest,SplitString_WhitespaceAndResultType)61 TEST(StringUtilsTest, SplitString_WhitespaceAndResultType)
62 {
63     std::vector<std::string> r;
64 
65     // Empty input handling.
66     r = SplitString(std::string(), ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
67     EXPECT_TRUE(r.empty());
68     r = SplitString(std::string(), ",", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
69     EXPECT_TRUE(r.empty());
70 
71     // Input string is space and we're trimming.
72     r = SplitString(" ", ",", TRIM_WHITESPACE, SPLIT_WANT_ALL);
73     ASSERT_EQ(1u, r.size());
74     EXPECT_EQ("", r[0]);
75     r = SplitString(" ", ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
76     EXPECT_TRUE(r.empty());
77 
78     // Test all 4 combinations of flags on ", ,".
79     r = SplitString(", ,", ",", KEEP_WHITESPACE, SPLIT_WANT_ALL);
80     ASSERT_EQ(3u, r.size());
81     EXPECT_EQ("", r[0]);
82     EXPECT_EQ(" ", r[1]);
83     EXPECT_EQ("", r[2]);
84     r = SplitString(", ,", ",", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY);
85     ASSERT_EQ(1u, r.size());
86     ASSERT_EQ(" ", r[0]);
87     r = SplitString(", ,", ",", TRIM_WHITESPACE, SPLIT_WANT_ALL);
88     ASSERT_EQ(3u, r.size());
89     EXPECT_EQ("", r[0]);
90     EXPECT_EQ("", r[1]);
91     EXPECT_EQ("", r[2]);
92     r = SplitString(", ,", ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
93     ASSERT_TRUE(r.empty());
94 }
95 
96 // Tests for SplitStringAlongWhitespace
TEST(StringUtilsTest,SplitStringAlongWhitespace)97 TEST(StringUtilsTest, SplitStringAlongWhitespace)
98 {
99     {
100         // No whitespace.
101         std::vector<std::string> r;
102         SplitStringAlongWhitespace("abcd", &r);
103         ASSERT_THAT(r, testing::ElementsAre("abcd"));
104     }
105 
106     {
107         // Just whitespace.
108         std::vector<std::string> r;
109         SplitStringAlongWhitespace(" \t", &r);
110         ASSERT_THAT(r, testing::ElementsAre());
111     }
112 
113     {
114         // Consecutive whitespace of same type.
115         std::vector<std::string> r;
116         SplitStringAlongWhitespace("a  b", &r);
117         ASSERT_THAT(r, testing::ElementsAre("a", "b"));
118     }
119 
120     {
121         // Consecutive whitespace of different types.
122         std::vector<std::string> r;
123         SplitStringAlongWhitespace("ab \tcd", &r);
124         ASSERT_THAT(r, testing::ElementsAre("ab", "cd"));
125     }
126 
127     {
128         // Non-empty output std::vector.
129         std::vector<std::string> r;
130         r.push_back("z");
131         SplitStringAlongWhitespace("abc", &r);
132         ASSERT_THAT(r, testing::ElementsAre("z", "abc"));
133     }
134 }
135 
136 // Tests for TrimString
TEST(StringUtilsTest,TrimString)137 TEST(StringUtilsTest, TrimString)
138 {
139     // Basic tests
140     EXPECT_EQ("a", TrimString("a", kWhitespaceASCII));
141     EXPECT_EQ("a", TrimString(" a", kWhitespaceASCII));
142     EXPECT_EQ("a", TrimString("a ", kWhitespaceASCII));
143     EXPECT_EQ("a", TrimString(" a ", kWhitespaceASCII));
144 
145     // Tests with empty strings
146     EXPECT_EQ("", TrimString("", kWhitespaceASCII));
147     EXPECT_EQ("", TrimString(" \n\r\t", kWhitespaceASCII));
148     EXPECT_EQ(" foo ", TrimString(" foo ", ""));
149 
150     // Tests it doesn't removes characters in the middle
151     EXPECT_EQ("foo bar", TrimString(" foo bar ", kWhitespaceASCII));
152 
153     // Test with non-whitespace trimChars
154     EXPECT_EQ(" ", TrimString("foo bar", "abcdefghijklmnopqrstuvwxyz"));
155 }
156 
157 // Basic functionality tests for HexStringToUInt
TEST(StringUtilsTest,HexStringToUIntBasic)158 TEST(StringUtilsTest, HexStringToUIntBasic)
159 {
160     unsigned int uintValue;
161 
162     std::string emptyString;
163     ASSERT_FALSE(HexStringToUInt(emptyString, &uintValue));
164 
165     std::string testStringA("0xBADF00D");
166     ASSERT_TRUE(HexStringToUInt(testStringA, &uintValue));
167     EXPECT_EQ(0xBADF00Du, uintValue);
168 
169     std::string testStringB("0xBADFOOD");
170     EXPECT_FALSE(HexStringToUInt(testStringB, &uintValue));
171 
172     std::string testStringC("BADF00D");
173     EXPECT_TRUE(HexStringToUInt(testStringC, &uintValue));
174     EXPECT_EQ(0xBADF00Du, uintValue);
175 
176     std::string testStringD("0x BADF00D");
177     EXPECT_FALSE(HexStringToUInt(testStringD, &uintValue));
178 }
179 
180 // Basic functionality for NamesMatchWithWildcard.
TEST(StringUtilsTest,NamesMatchWithWildcard)181 TEST(StringUtilsTest, NamesMatchWithWildcard)
182 {
183     EXPECT_TRUE(NamesMatchWithWildcard("ASDF", "ASDF"));
184     EXPECT_TRUE(NamesMatchWithWildcard("A*", "ASDF"));
185     EXPECT_TRUE(NamesMatchWithWildcard("AS*", "ASDF"));
186     EXPECT_TRUE(NamesMatchWithWildcard("ASD*", "ASDF"));
187     EXPECT_TRUE(NamesMatchWithWildcard("ASDF*", "ASDF"));
188 
189     EXPECT_TRUE(NamesMatchWithWildcard("*F", "ASDF"));
190     EXPECT_TRUE(NamesMatchWithWildcard("*DF", "ASDF"));
191     EXPECT_TRUE(NamesMatchWithWildcard("*SDF", "ASDF"));
192     EXPECT_TRUE(NamesMatchWithWildcard("*ASDF", "ASDF"));
193 
194     EXPECT_TRUE(NamesMatchWithWildcard("AS**", "ASDF"));
195     EXPECT_TRUE(NamesMatchWithWildcard("AS***", "ASDF"));
196     EXPECT_TRUE(NamesMatchWithWildcard("**DF", "ASDF"));
197     EXPECT_TRUE(NamesMatchWithWildcard("***DF", "ASDF"));
198 
199     EXPECT_TRUE(NamesMatchWithWildcard("A*F", "ASDF"));
200     EXPECT_TRUE(NamesMatchWithWildcard("A**F", "ASDF"));
201     EXPECT_TRUE(NamesMatchWithWildcard("*SD*", "ASDF"));
202     EXPECT_TRUE(NamesMatchWithWildcard("*S*D*", "ASDF"));
203 
204     EXPECT_TRUE(NamesMatchWithWildcard("ASD*", "ASDF*"));
205 }
206 
207 // Note: ReadFileToString is harder to test
208 
209 class BeginsWithTest : public testing::Test
210 {
211   public:
BeginsWithTest()212     BeginsWithTest() : mMode(TestMode::CHAR_ARRAY) {}
213 
214     enum class TestMode
215     {
216         CHAR_ARRAY,
217         STRING_AND_CHAR_ARRAY,
218         STRING
219     };
220 
setMode(TestMode mode)221     void setMode(TestMode mode) { mMode = mode; }
222 
runBeginsWith(const char * str,const char * prefix)223     bool runBeginsWith(const char *str, const char *prefix)
224     {
225         if (mMode == TestMode::CHAR_ARRAY)
226         {
227             return BeginsWith(str, prefix);
228         }
229         if (mMode == TestMode::STRING_AND_CHAR_ARRAY)
230         {
231             return BeginsWith(std::string(str), prefix);
232         }
233         return BeginsWith(std::string(str), std::string(prefix));
234     }
235 
runTest()236     void runTest()
237     {
238         ASSERT_FALSE(runBeginsWith("foo", "bar"));
239         ASSERT_FALSE(runBeginsWith("", "foo"));
240         ASSERT_FALSE(runBeginsWith("foo", "foobar"));
241 
242         ASSERT_TRUE(runBeginsWith("foobar", "foo"));
243         ASSERT_TRUE(runBeginsWith("foobar", ""));
244         ASSERT_TRUE(runBeginsWith("foo", "foo"));
245         ASSERT_TRUE(runBeginsWith("", ""));
246     }
247 
248   private:
249     TestMode mMode;
250 };
251 
252 // Test that BeginsWith works correctly for const char * arguments.
TEST_F(BeginsWithTest,CharArrays)253 TEST_F(BeginsWithTest, CharArrays)
254 {
255     setMode(TestMode::CHAR_ARRAY);
256     runTest();
257 }
258 
259 // Test that BeginsWith works correctly for std::string and const char * arguments.
TEST_F(BeginsWithTest,StringAndCharArray)260 TEST_F(BeginsWithTest, StringAndCharArray)
261 {
262     setMode(TestMode::STRING_AND_CHAR_ARRAY);
263     runTest();
264 }
265 
266 // Test that BeginsWith works correctly for std::string arguments.
TEST_F(BeginsWithTest,Strings)267 TEST_F(BeginsWithTest, Strings)
268 {
269     setMode(TestMode::STRING);
270     runTest();
271 }
272 
273 class EndsWithTest : public testing::Test
274 {
275   public:
EndsWithTest()276     EndsWithTest() : mMode(TestMode::CHAR_ARRAY) {}
277 
278     enum class TestMode
279     {
280         CHAR_ARRAY,
281         STRING_AND_CHAR_ARRAY,
282         STRING
283     };
284 
setMode(TestMode mode)285     void setMode(TestMode mode) { mMode = mode; }
286 
runEndsWith(const char * str,const char * suffix)287     bool runEndsWith(const char *str, const char *suffix)
288     {
289         if (mMode == TestMode::CHAR_ARRAY)
290         {
291             return EndsWith(str, suffix);
292         }
293         if (mMode == TestMode::STRING_AND_CHAR_ARRAY)
294         {
295             return EndsWith(std::string(str), suffix);
296         }
297         return EndsWith(std::string(str), std::string(suffix));
298     }
299 
runTest()300     void runTest()
301     {
302         ASSERT_FALSE(EndsWith("foo", "bar"));
303         ASSERT_FALSE(EndsWith("", "bar"));
304         ASSERT_FALSE(EndsWith("foo", "foobar"));
305 
306         ASSERT_TRUE(EndsWith("foobar", "bar"));
307         ASSERT_TRUE(EndsWith("foobar", ""));
308         ASSERT_TRUE(EndsWith("bar", "bar"));
309         ASSERT_TRUE(EndsWith("", ""));
310     }
311 
312   private:
313     TestMode mMode;
314 };
315 
316 // Test that EndsWith works correctly for const char * arguments.
TEST_F(EndsWithTest,CharArrays)317 TEST_F(EndsWithTest, CharArrays)
318 {
319     setMode(TestMode::CHAR_ARRAY);
320     runTest();
321 }
322 
323 // Test that EndsWith works correctly for std::string and const char * arguments.
TEST_F(EndsWithTest,StringAndCharArray)324 TEST_F(EndsWithTest, StringAndCharArray)
325 {
326     setMode(TestMode::STRING_AND_CHAR_ARRAY);
327     runTest();
328 }
329 
330 // Test that EndsWith works correctly for std::string arguments.
TEST_F(EndsWithTest,Strings)331 TEST_F(EndsWithTest, Strings)
332 {
333     setMode(TestMode::STRING);
334     runTest();
335 }
336 
337 }  // anonymous namespace
338