Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/test/util/SubStringTest.cc @ 10693

Last change on this file since 10693 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: 5.5 KB
Line 
1#include <gtest/gtest.h>
2#include "util/SubString.h"
3
4namespace orxonox
5{
6    TEST(SubString, Empty)
7    {
8        SubString test;
9        EXPECT_TRUE(test.empty());
10        EXPECT_EQ(0u, test.size());
11    }
12
13    TEST(SubString, CreateSimple)
14    {
15        SubString test("This is a test");
16        EXPECT_FALSE(test.empty());
17        ASSERT_EQ(4u, test.size());
18
19        EXPECT_EQ("This", test[0]);
20        EXPECT_EQ("is", test[1]);
21        EXPECT_EQ("a", test[2]);
22        EXPECT_EQ("test", test[3]);
23
24        EXPECT_EQ("This", test.getString(0));
25        EXPECT_EQ("is", test.getString(1));
26        EXPECT_EQ("a", test.getString(2));
27        EXPECT_EQ("test", test.getString(3));
28
29        EXPECT_EQ("This", test.front());
30        EXPECT_EQ("test", test.back());
31    }
32
33    TEST(SubString, CreateDelimiter)
34    {
35        SubString test1("This|is|a|test");
36        EXPECT_EQ(1u, test1.size());
37
38        SubString test2("This|is|a|test", "|");
39        EXPECT_EQ(4u, test2.size());
40
41        SubString test3("This|is|a|test", "|e");
42        EXPECT_EQ(5u, test3.size());
43    }
44
45    TEST(SubString, CreateDelimiterNeighbours)
46    {
47        SubString test1("This | is | a | test", "|");
48        EXPECT_EQ(4u, test1.size());
49        EXPECT_EQ(" a ", test1[2]);
50
51        SubString test2("This | is | a | test", "|", " ");
52        EXPECT_EQ(4u, test2.size());
53        EXPECT_EQ("a", test2[2]);
54    }
55
56    TEST(SubString, CreateAllowEmptyEntries)
57    {
58        SubString test1("This |  | is |  | a ||| test", "|", " ");
59        EXPECT_EQ(4u, test1.size());
60
61        SubString test2("This |  | is |  | a ||| test", "|", " ", true);
62        EXPECT_EQ(8u, test2.size());
63    }
64
65    TEST(SubString, CreateEscapeChar)
66    {
67        SubString test1("This is a test", SubString::WhiteSpaces, "", false, '\\', true);
68        EXPECT_EQ(4u, test1.size());
69        EXPECT_EQ("is", test1[1]);
70
71        SubString test2("This is\\ a test", SubString::WhiteSpaces, "", false, '\\', true);
72        EXPECT_EQ(3u, test2.size());
73        EXPECT_EQ("is a", test2[1]);
74
75        SubString test3("This is\\ a test", SubString::WhiteSpaces, "", false, '\\', false);
76        EXPECT_EQ(3u, test3.size());
77        EXPECT_EQ("is\\ a", test3[1]);
78    }
79
80    TEST(SubString, CreateSafemodeChar)
81    {
82        SubString test1("This is a test", SubString::WhiteSpaces, "", false, '\\', true, '"', true);
83        EXPECT_EQ(4u, test1.size());
84        EXPECT_EQ("is", test1[1]);
85
86        SubString test2("This \"is a\" test", SubString::WhiteSpaces, "", false, '\\', true, '"', true);
87        EXPECT_EQ(3u, test2.size());
88        EXPECT_EQ("is a", test2[1]);
89
90        SubString test3("This \"is a\" test", SubString::WhiteSpaces, "", false, '\\', true, '"', false);
91        EXPECT_EQ(3u, test3.size());
92        EXPECT_EQ("\"is a\"", test3[1]);
93    }
94
95    TEST(SubString, CreateParenthesisChars)
96    {
97        SubString test1("This is a test", SubString::WhiteSpaces, "", false, '\\', true, '"', true, '{', '}', true);
98        EXPECT_EQ(4u, test1.size());
99        EXPECT_EQ("is", test1[1]);
100
101        SubString test2("This {is a} test", SubString::WhiteSpaces, "", false, '\\', true, '"', true, '{', '}', true);
102        EXPECT_EQ(3u, test2.size());
103        EXPECT_EQ("is a", test2[1]);
104
105        SubString test3("This {is a} test", SubString::WhiteSpaces, "", false, '\\', true, '"', true, '{', '}', false);
106        EXPECT_EQ(3u, test3.size());
107        EXPECT_EQ("{is a}", test3[1]);
108    }
109
110    TEST(SubString, Compare)
111    {
112        SubString test1("This is a test");
113        SubString test2("This is a test");
114        SubString test3("This is not a test");
115
116        EXPECT_TRUE(test1 == test2);
117        EXPECT_TRUE(test1.compare(test2));
118
119        EXPECT_FALSE(test1 == test3);
120        EXPECT_FALSE(test1.compare(test3));
121
122        EXPECT_FALSE(test1.compare(test3, 100));
123        EXPECT_FALSE(test1.compare(test3, 4));
124        EXPECT_FALSE(test1.compare(test3, 3));
125        EXPECT_TRUE(test1.compare(test3, 2));
126        EXPECT_TRUE(test1.compare(test3, 1));
127        EXPECT_TRUE(test1.compare(test3, 0));
128    }
129
130    TEST(SubString, Assign)
131    {
132        SubString test1("This is a test");
133        SubString test2("Hello World");
134
135        EXPECT_EQ(4u, test1.size());
136        EXPECT_EQ(2u, test2.size());
137        EXPECT_FALSE(test1 == test2);
138
139        test2 = test1;
140
141        EXPECT_EQ(4u, test1.size());
142        EXPECT_EQ(4u, test2.size());
143        EXPECT_TRUE(test1 == test2);
144    }
145
146    TEST(SubString, Append)
147    {
148        SubString test1("This is a test");
149        SubString test2("Hello World");
150
151        EXPECT_EQ(4u, test1.size());
152        EXPECT_EQ(2u, test2.size());
153
154        test1 += test2;
155
156        EXPECT_EQ(6u, test1.size());
157        EXPECT_EQ(2u, test2.size());
158    }
159
160    TEST(SubString, Join)
161    {
162        SubString test1("This is a test");
163
164        EXPECT_EQ("This is a test", test1.join());
165        EXPECT_EQ("This|is|a|test", test1.join("|"));
166        EXPECT_EQ("This123is123a123test", test1.join("123"));
167
168        SubString test2("Hello World");
169
170        EXPECT_EQ("Hello World", test2.join());
171
172        test1 += test2;
173
174        EXPECT_EQ("This is a test Hello World", test1.join());
175    }
176
177    TEST(SubString, Subset)
178    {
179        SubString test1("This is a test");
180        EXPECT_EQ(4u, test1.size());
181        EXPECT_EQ("This is a test", test1.join());
182
183        SubString test2 = test1.subSet(1);
184        ASSERT_EQ(3u, test2.size());
185        EXPECT_EQ("is a test", test2.join());
186
187        SubString test3 = test1.subSet(1, 2);
188        ASSERT_EQ(2u, test3.size());
189        EXPECT_EQ("is a", test3.join());
190    }
191}
Note: See TracBrowser for help on using the repository browser.