Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/testing/test/util/StringUtilsTest.cc @ 9542

Last change on this file since 9542 was 9114, checked in by landauf, 13 years ago

added tests for several classes and functions in util

  • Property svn:eol-style set to native
File size: 14.5 KB
RevLine 
[9114]1#include <gtest/gtest.h>
2#include "util/StringUtils.h"
3
4namespace orxonox
5{
6    TEST(StringUtils, BLANKSTRING)
7    {
8        EXPECT_EQ("", BLANKSTRING);
9    }
10
11    TEST(StringUtils, getUniqueNumberString)
12    {
13        std::set<std::string> numberStrings;
14
15        const size_t iterations = 1000;
16
17        for (size_t i = 0; i < iterations; ++i)
18            numberStrings.insert(getUniqueNumberString());
19
20        // must have exactly <iterations> entries in the set, since all strings should be unique
21        EXPECT_EQ(iterations, numberStrings.size());
22    }
23
24//    _UtilExport std::string  getStripped(const std::string& str);
25    TEST(StringUtils, getStripped)
26    {
27        EXPECT_EQ("", getStripped(""));
28        EXPECT_EQ("test", getStripped("test"));
29        EXPECT_EQ("test", getStripped("te st"));
30        EXPECT_EQ("test", getStripped("   test"));
31        EXPECT_EQ("test", getStripped("test  "));
32        EXPECT_EQ("test", getStripped("   test   "));
33        EXPECT_EQ("test", getStripped("   t  e  s  t   "));
34        EXPECT_EQ("test", getStripped(\t te \n\n\n  st\n"));
35    }
36
37//    _UtilExport std::string  removeTrailingWhitespaces(const std::string& str);
38    TEST(StringUtils, removeTrailingWhitespaces)
39    {
40        EXPECT_EQ("", removeTrailingWhitespaces(""));
41        EXPECT_EQ("test", removeTrailingWhitespaces("test"));
42        EXPECT_EQ("test", removeTrailingWhitespaces("   test"));
43        EXPECT_EQ("test", removeTrailingWhitespaces("test"));
44        EXPECT_EQ("test", removeTrailingWhitespaces("   test   "));
45        EXPECT_EQ("test", removeTrailingWhitespaces(" \t  test  \n "));
46        EXPECT_EQ("te  st", removeTrailingWhitespaces("te  st"));
47        EXPECT_EQ("te  st", removeTrailingWhitespaces("   te  st   "));
48        EXPECT_EQ("Hel lo\nWo rld", removeTrailingWhitespaces(" \t Hel lo\nWo rld \n"));
49    }
50
51//    _UtilExport void         vectorize(const std::string& str, char delimiter, std::vector<std::string>* output);
52    TEST(StringUtils, vectorize)
53    {
54        std::vector<std::string> output;
55        vectorize("this is a test", ' ', &output);
56        ASSERT_EQ(4u, output.size());
57        EXPECT_EQ("this", output[0]);
58        EXPECT_EQ("is", output[1]);
59        EXPECT_EQ("a", output[2]);
60        EXPECT_EQ("test", output[3]);
61
62        vectorize("this is a test", 't', &output);
63        ASSERT_EQ(4u, output.size());
64        EXPECT_EQ("", output[0]);
65        EXPECT_EQ("his is a ", output[1]);
66        EXPECT_EQ("es", output[2]);
67        EXPECT_EQ("", output[3]);
68    }
69
70//    _UtilExport size_t       getNextQuote(const std::string& str, size_t start = 0);
71    TEST(StringUtils, getNextQuote)
72    {
73        EXPECT_EQ(std::string::npos, getNextQuote(""));
74        EXPECT_EQ(std::string::npos, getNextQuote("text"));
75        EXPECT_EQ(0u, getNextQuote("\"text"));
76        EXPECT_EQ(4u, getNextQuote("text\""));
77        EXPECT_EQ(std::string::npos, getNextQuote("\\\"text"));
78        EXPECT_EQ(std::string::npos, getNextQuote("text\\\""));
79        EXPECT_EQ(3u, getNextQuote("012\"456\"89", 0));
80        EXPECT_EQ(7u, getNextQuote("012\"456\"89", 5));
81    }
82
83//    _UtilExport bool         isBetweenQuotes(const std::string& str, size_t pos);
84    TEST(StringUtils, isBetweenQuotes)
85    {
86        EXPECT_FALSE(isBetweenQuotes("", 0));
87
88        EXPECT_FALSE(isBetweenQuotes("abc", 0));
89        EXPECT_FALSE(isBetweenQuotes("abc", 1));
90        EXPECT_FALSE(isBetweenQuotes("abc", 2));
91        EXPECT_FALSE(isBetweenQuotes("abc", 3));
92
93        EXPECT_FALSE(isBetweenQuotes("\"abc", 0));
94        EXPECT_FALSE(isBetweenQuotes("\"abc", 1));
95        EXPECT_FALSE(isBetweenQuotes("\"abc", 2));
96        EXPECT_FALSE(isBetweenQuotes("\"abc", 3));
97
98        EXPECT_FALSE(isBetweenQuotes("abc\"", 0));
99        EXPECT_FALSE(isBetweenQuotes("abc\"", 1));
100        EXPECT_FALSE(isBetweenQuotes("abc\"", 2));
101        EXPECT_FALSE(isBetweenQuotes("abc\"", 3));
102
103        EXPECT_FALSE(isBetweenQuotes("\"abc\"", 0));
104        EXPECT_TRUE(isBetweenQuotes("\"abc\"", 1));
105        EXPECT_TRUE(isBetweenQuotes("\"abc\"", 2));
106        EXPECT_TRUE(isBetweenQuotes("\"abc\"", 3));
107        EXPECT_FALSE(isBetweenQuotes("\"abc\"", 4));
108
109        //                            012 3456 7890
110        EXPECT_FALSE(isBetweenQuotes("abc\"abc\"abc", 0));
111        EXPECT_FALSE(isBetweenQuotes("abc\"abc\"abc", 1));
112        EXPECT_FALSE(isBetweenQuotes("abc\"abc\"abc", 2));
113        EXPECT_FALSE(isBetweenQuotes("abc\"abc\"abc", 3));
114        EXPECT_TRUE(isBetweenQuotes("abc\"abc\"abc", 4));
115        EXPECT_TRUE(isBetweenQuotes("abc\"abc\"abc", 5));
116        EXPECT_TRUE(isBetweenQuotes("abc\"abc\"abc", 6));
117        EXPECT_FALSE(isBetweenQuotes("abc\"abc\"abc", 7));
118        EXPECT_FALSE(isBetweenQuotes("abc\"abc\"abc", 8));
119        EXPECT_FALSE(isBetweenQuotes("abc\"abc\"abc", 9));
120        EXPECT_FALSE(isBetweenQuotes("abc\"abc\"abc", 10));
121    }
122
123//    _UtilExport bool         hasStringBetweenQuotes(const std::string& str);
124    TEST(StringUtils, hasStringBetweenQuotes)
125    {
126        EXPECT_FALSE(hasStringBetweenQuotes(""));
127        EXPECT_FALSE(hasStringBetweenQuotes("test"));
128        EXPECT_FALSE(hasStringBetweenQuotes("\"test"));
129        EXPECT_FALSE(hasStringBetweenQuotes("te\"st"));
130        EXPECT_FALSE(hasStringBetweenQuotes("test\""));
131        EXPECT_TRUE(hasStringBetweenQuotes("\"test\""));
132        EXPECT_TRUE(hasStringBetweenQuotes("te\"st\""));
133        EXPECT_TRUE(hasStringBetweenQuotes("\"te\"st"));
134        EXPECT_FALSE(hasStringBetweenQuotes("te\"\"st"));
135        EXPECT_FALSE(hasStringBetweenQuotes("\"\"test"));
136        EXPECT_FALSE(hasStringBetweenQuotes("test\"\""));
137    }
138
139//    _UtilExport std::string  getStringBetweenQuotes(const std::string& str);
140    TEST(StringUtils, getStringBetweenQuotes)
141    {
142        EXPECT_EQ("", getStringBetweenQuotes(""));
143        EXPECT_EQ("", getStringBetweenQuotes("test"));
144        EXPECT_EQ("", getStringBetweenQuotes("\"test"));
145        EXPECT_EQ("", getStringBetweenQuotes("te\"st"));
146        EXPECT_EQ("", getStringBetweenQuotes("test\""));
147        EXPECT_EQ("test", getStringBetweenQuotes("\"test\""));
148        EXPECT_EQ("st", getStringBetweenQuotes("te\"st\""));
149        EXPECT_EQ("te", getStringBetweenQuotes("\"te\"st"));
150        EXPECT_EQ("", getStringBetweenQuotes("te\"\"st"));
151        EXPECT_EQ("", getStringBetweenQuotes("\"\"test"));
152        EXPECT_EQ("", getStringBetweenQuotes("test\"\""));
153    }
154
155//    _UtilExport std::string  stripEnclosingQuotes(const std::string& str);
156    TEST(StringUtils, stripEnclosingQuotes)
157    {
158        EXPECT_EQ("", stripEnclosingQuotes(""));
159        EXPECT_EQ("test", stripEnclosingQuotes("test"));
160        EXPECT_EQ(" test", stripEnclosingQuotes(" test"));
161        EXPECT_EQ("   test   ", stripEnclosingQuotes("   test   "));
162        EXPECT_EQ("\"test", stripEnclosingQuotes("\"test"));
163        EXPECT_EQ("test\"", stripEnclosingQuotes("test\""));
164        EXPECT_EQ(" \"test", stripEnclosingQuotes(" \"test"));
165        EXPECT_EQ("test\" ", stripEnclosingQuotes("test\" "));
166        EXPECT_EQ("test", stripEnclosingQuotes("\"test\""));
167        EXPECT_EQ("test", stripEnclosingQuotes(\"test\"  "));
168        EXPECT_EQ("a  \"test\"  ", stripEnclosingQuotes("a  \"test\"  "));
169    }
170
171//    _UtilExport std::string  stripEnclosingBraces(const std::string& str);
172    TEST(StringUtils, stripEnclosingBraces)
173    {
174        EXPECT_EQ("", stripEnclosingBraces(""));
175        EXPECT_EQ("test", stripEnclosingBraces("test"));
176        EXPECT_EQ("{test", stripEnclosingBraces("{test"));
177        EXPECT_EQ("test}", stripEnclosingBraces("test}"));
178        EXPECT_EQ("t{es}t", stripEnclosingBraces("t{es}t"));
179        EXPECT_EQ(" {test}", stripEnclosingBraces(" {test}"));
180        EXPECT_EQ("}test}", stripEnclosingBraces("}test}"));
181        EXPECT_EQ("{test{", stripEnclosingBraces("{test{"));
182        EXPECT_EQ("test", stripEnclosingBraces("{test}"));
183    }
184
185//    _UtilExport bool         isEmpty(const std::string& str);
186    TEST(StringUtils, isEmpty)
187    {
188        EXPECT_TRUE(isEmpty(""));
189        EXPECT_TRUE(isEmpty(" "));
190        EXPECT_TRUE(isEmpty("       "));
191        EXPECT_TRUE(isEmpty("\t"));
192        EXPECT_TRUE(isEmpty("\n"));
193        EXPECT_TRUE(isEmpty(" \t\n\t\n  \t"));
194        EXPECT_FALSE(isEmpty("a"));
195        EXPECT_FALSE(isEmpty("test"));
196        EXPECT_FALSE(isEmpty("    a   "));
197    }
198
199//    _UtilExport bool         isComment(const std::string& str);
200    TEST(StringUtils, isComment)
201    {
202        EXPECT_FALSE(isComment(""));
203        EXPECT_FALSE(isComment("test"));
204        EXPECT_TRUE(isComment("#test"));
205        EXPECT_TRUE(isComment("%test"));
206        EXPECT_TRUE(isComment(";test"));
207        EXPECT_TRUE(isComment("//test"));
208        EXPECT_FALSE(isComment("/test"));
209
210        EXPECT_TRUE(isComment("        #test"));
211        EXPECT_TRUE(isComment("      %test"));
212        EXPECT_TRUE(isComment("    ;test"));
213        EXPECT_TRUE(isComment("  //test"));
214    }
215
216//    _UtilExport std::string  addSlashes(const std::string& str);
217    TEST(StringUtils, addSlashes)
218    {
219        EXPECT_EQ("", addSlashes(""));
220        EXPECT_EQ("test", addSlashes("test"));
221        EXPECT_EQ("test\\ntest", addSlashes("test\ntest"));
222        EXPECT_EQ("\\n\\t\\r\\f\\a\\b\\v\\\"\\\\", addSlashes("\n\t\r\f\a\b\v\"\\"));
223    }
224
225//    _UtilExport std::string  removeSlashes(const std::string& str);
226    TEST(StringUtils, removeSlashes)
227    {
228        EXPECT_EQ("", removeSlashes(""));
229        EXPECT_EQ("test", removeSlashes("test"));
230        EXPECT_EQ("test\ntest", removeSlashes("test\\ntest"));
231        EXPECT_EQ("\n\t\r\f\a\b\v\"\\", removeSlashes("\\n\\t\\r\\f\\a\\b\\v\\\"\\\\"));
232    }
233
234//    _UtilExport std::string  getLowercase(const std::string& str);
235    TEST(StringUtils, getLowercase)
236    {
237        EXPECT_EQ("", getLowercase(""));
238        EXPECT_EQ("test", getLowercase("test"));
239        EXPECT_EQ("test", getLowercase("Test"));
240        EXPECT_EQ("test", getLowercase("TeSt"));
241        EXPECT_EQ("test", getLowercase("TEST"));
242        EXPECT_EQ("a test b", getLowercase("a TEST b"));
243    }
244
245//    _UtilExport std::string  getUppercase(const std::string& str);
246    TEST(StringUtils, getUppercase)
247    {
248        EXPECT_EQ("", getUppercase(""));
249        EXPECT_EQ("TEST", getUppercase("test"));
250        EXPECT_EQ("TEST", getUppercase("Test"));
251        EXPECT_EQ("TEST", getUppercase("TeSt"));
252        EXPECT_EQ("TEST", getUppercase("TEST"));
253        EXPECT_EQ("A TEST B", getUppercase("A test B"));
254    }
255
256//    _UtilExport int          nocaseCmp(const std::string& s1, const std::string& s2);
257    TEST(StringUtils, nocaseCmp)
258    {
259        EXPECT_EQ(0, nocaseCmp("", ""));
260        EXPECT_EQ(0, nocaseCmp("a", "a"));
261        EXPECT_EQ(0, nocaseCmp("a", "A"));
262        EXPECT_EQ(0, nocaseCmp("A", "a"));
263        EXPECT_EQ(0, nocaseCmp("A", "A"));
264        EXPECT_EQ(0, nocaseCmp("test", "test"));
265        EXPECT_EQ(0, nocaseCmp("test", "TEST"));
266        EXPECT_EQ(0, nocaseCmp("tESt", "test"));
267        EXPECT_EQ(0, nocaseCmp("TesT", "teST"));
268
269        EXPECT_EQ(strcmp("a", "b"), nocaseCmp("a", "b"));
270        EXPECT_EQ(strcmp("b", "a"), nocaseCmp("b", "a"));
271        EXPECT_EQ(strcmp("hello", "world"), nocaseCmp("hello", "world"));
272    }
273
274//    _UtilExport int          nocaseCmp(const std::string& s1, const std::string& s2, size_t len);
275    TEST(StringUtils, nocaseCmpWithLen)
276    {
277        EXPECT_EQ(0, nocaseCmp("test", "TEST", 0));
278        EXPECT_EQ(0, nocaseCmp("test", "TEST", 1));
279        EXPECT_EQ(0, nocaseCmp("test", "TEST", 2));
280        EXPECT_EQ(0, nocaseCmp("test", "TEST", 3));
281        EXPECT_EQ(0, nocaseCmp("test", "TEST", 4));
282        EXPECT_EQ(0, nocaseCmp("test", "TEST", 5));
283    }
284
285//    _UtilExport bool         hasComment(const std::string& str);
286    TEST(StringUtils, hasComment)
287    {
288        EXPECT_FALSE(hasComment(""));
289        EXPECT_FALSE(hasComment("test"));
290        EXPECT_FALSE(hasComment(" asdf / asdf "));
291        EXPECT_TRUE(hasComment("#test"));
292        EXPECT_TRUE(hasComment(" %test"));
293        EXPECT_TRUE(hasComment("test ;test"));
294        EXPECT_TRUE(hasComment("test // test // test"));
295    }
296
297//    _UtilExport std::string  getComment(const std::string& str);
298    TEST(StringUtils, getComment)
299    {
300        EXPECT_EQ("", getComment(""));
301        EXPECT_EQ("", getComment("test"));
302        EXPECT_EQ("", getComment(" asdf / asdf "));
303        EXPECT_EQ("#test", getComment("#test"));
304        EXPECT_EQ(" %test", getComment(" %test"));
305        EXPECT_EQ(" ;test", getComment("test ;test"));
306        EXPECT_EQ(" // test // test", getComment("test // test // test"));
307    }
308
309//    _UtilExport size_t       getNextCommentPosition(const std::string& str, size_t start = 0);
310    TEST(StringUtils, getNextCommentPosition)
311    {
312        EXPECT_EQ(std::string::npos, getNextCommentPosition(""));
313        EXPECT_EQ(std::string::npos, getNextCommentPosition("test"));
314        EXPECT_EQ(std::string::npos, getNextCommentPosition(" asdf / asdf "));
315        EXPECT_EQ(0u, getNextCommentPosition("#test"));
316        EXPECT_EQ(0u, getNextCommentPosition(" %test"));
317        EXPECT_EQ(4u, getNextCommentPosition("test ;test"));
318        EXPECT_EQ(4u, getNextCommentPosition("test // test // test"));
319
320        EXPECT_EQ(std::string::npos, getNextCommentPosition("", 10));
321        EXPECT_EQ(std::string::npos, getNextCommentPosition("test", 10));
322        EXPECT_EQ(std::string::npos, getNextCommentPosition(" asdf / asdf ", 3));
323        EXPECT_EQ(std::string::npos, getNextCommentPosition("#test", 1));
324        EXPECT_EQ(1u, getNextCommentPosition(" %test", 1));
325        EXPECT_EQ(5u, getNextCommentPosition("test ;test", 5));
326        EXPECT_EQ(12u, getNextCommentPosition("test // test // test", 6));
327    }
328
329//    _UtilExport size_t       replaceCharacters(std::string& str, char target, char replacement);
330    TEST(StringUtils, replaceCharacters)
331    {
332        std::string test1 = "test";
333        EXPECT_EQ(1u, replaceCharacters(test1, 'e', '3'));
334        EXPECT_EQ("t3st", test1);
335
336        std::string test2 = "test";
337        EXPECT_EQ(2u, replaceCharacters(test2, 't', '1'));
338        EXPECT_EQ("1es1", test2);
339    }
340
341//    _UtilExport unsigned int getLevenshteinDistance(const std::string& str1, const std::string& str2);
342    TEST(StringUtils, getLevenshteinDistance)
343    {
344        EXPECT_EQ(0u, getLevenshteinDistance("test", "test"));
345        EXPECT_EQ(1u, getLevenshteinDistance("test", "tes"));
346        EXPECT_EQ(1u, getLevenshteinDistance("test", "testt"));
347        EXPECT_EQ(1u, getLevenshteinDistance("test", "t3st"));
348        EXPECT_EQ(1u, getLevenshteinDistance("test", "te1st"));
349        EXPECT_EQ(2u, getLevenshteinDistance("test", "1es1"));
350        EXPECT_EQ(3u, getLevenshteinDistance("test", "1es11"));
351        EXPECT_EQ(4u, getLevenshteinDistance("test", "blub"));
352        EXPECT_EQ(5u, getLevenshteinDistance("test", "abcde"));
353        EXPECT_EQ(6u, getLevenshteinDistance("test", "abcdef"));
354    }
355}
Note: See TracBrowser for help on using the repository browser.