1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #include "StringTests.h"
29 #include "OgreStringConverter.h"
30 #include "OgreVector3.h"
31 #include "OgreQuaternion.h"
32 #include "OgreMatrix4.h"
33 #include "OgreColourValue.h"
34 
35 using namespace Ogre;
36 
37 // Register the suite
38 CPPUNIT_TEST_SUITE_REGISTRATION( StringTests );
39 
setUp()40 void StringTests::setUp()
41 {
42 	testFileNoPath = "testfile.txt";
43 	testFileRelativePathUnix = "this/is/relative/testfile.txt";
44 	testFileRelativePathWindows = "this\\is\\relative\\testfile.txt";
45 	testFileAbsolutePathUnix = "/this/is/absolute/testfile.txt";
46 	testFileAbsolutePathWindows = "c:\\this\\is\\absolute\\testfile.txt";
47 }
tearDown()48 void StringTests::tearDown()
49 {
50 }
51 
testSplitFileNameNoPath()52 void StringTests::testSplitFileNameNoPath()
53 {
54 	String basename, path;
55 	StringUtil::splitFilename(testFileNoPath, basename, path);
56 
57     CPPUNIT_ASSERT_EQUAL(testFileNoPath, basename);
58     CPPUNIT_ASSERT(path.empty());
59 }
testSplitFileNameRelativePath()60 void StringTests::testSplitFileNameRelativePath()
61 {
62 	String basename, path;
63 	// Unix
64 	StringUtil::splitFilename(testFileRelativePathUnix, basename, path);
65     CPPUNIT_ASSERT_EQUAL(String("testfile.txt"), basename);
66     CPPUNIT_ASSERT_EQUAL(String("this/is/relative/"), path);
67 	// Windows
68 	StringUtil::splitFilename(testFileRelativePathWindows, basename, path);
69     CPPUNIT_ASSERT_EQUAL(String("testfile.txt"), basename);
70     CPPUNIT_ASSERT_EQUAL(String("this/is/relative/"), path);
71 
72 }
testSplitFileNameAbsolutePath()73 void StringTests::testSplitFileNameAbsolutePath()
74 {
75 	String basename, path;
76 	// Unix
77 	StringUtil::splitFilename(testFileAbsolutePathUnix, basename, path);
78     CPPUNIT_ASSERT_EQUAL(String("testfile.txt"), basename);
79     CPPUNIT_ASSERT_EQUAL(String("/this/is/absolute/"), path);
80 	// Windows
81 	StringUtil::splitFilename(testFileAbsolutePathWindows, basename, path);
82     CPPUNIT_ASSERT_EQUAL(String("testfile.txt"), basename);
83 	CPPUNIT_ASSERT_EQUAL(String("c:/this/is/absolute/"), path);
84 }
85 
testMatchCaseSensitive()86 void StringTests::testMatchCaseSensitive()
87 {
88 	// Test positive
89 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, testFileNoPath, true));
90 	// Test negative
91 	String upperCase = testFileNoPath;
92     StringUtil::toUpperCase(upperCase);
93 	CPPUNIT_ASSERT(!StringUtil::match(testFileNoPath, upperCase, true));
94 }
testMatchCaseInSensitive()95 void StringTests::testMatchCaseInSensitive()
96 {
97 	// Test positive
98 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, testFileNoPath, false));
99 	// Test positive
100 	String upperCase = testFileNoPath;
101 	StringUtil::toUpperCase(upperCase);
102 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, upperCase, false));
103 }
testMatchGlobAll()104 void StringTests::testMatchGlobAll()
105 {
106 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, "*", true));
107 }
testMatchGlobStart()108 void StringTests::testMatchGlobStart()
109 {
110 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, "*stfile.txt", true));
111 	CPPUNIT_ASSERT(!StringUtil::match(testFileNoPath, "*astfile.txt", true));
112 }
testMatchGlobEnd()113 void StringTests::testMatchGlobEnd()
114 {
115 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, "testfile.*", true));
116 	CPPUNIT_ASSERT(!StringUtil::match(testFileNoPath, "testfile.d*", true));
117 }
testMatchGlobStartAndEnd()118 void StringTests::testMatchGlobStartAndEnd()
119 {
120 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, "*stfile.*", true));
121 	CPPUNIT_ASSERT(!StringUtil::match(testFileNoPath, "*astfile.d*", true));
122 }
testMatchGlobMiddle()123 void StringTests::testMatchGlobMiddle()
124 {
125 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, "test*.txt", true));
126 	CPPUNIT_ASSERT(!StringUtil::match(testFileNoPath, "last*.txt*", true));
127 }
testMatchSuperGlobtastic()128 void StringTests::testMatchSuperGlobtastic()
129 {
130 	CPPUNIT_ASSERT(StringUtil::match(testFileNoPath, "*e*tf*e.t*t", true));
131 }
testParseReal()132 void StringTests::testParseReal()
133 {
134 	Real r = 23.454;
135 
136 	String s = StringConverter::toString(r);
137 	Real t = StringConverter::parseReal(s);
138 
139 	CPPUNIT_ASSERT_EQUAL(r, t);
140 
141 
142 }
testParseInt()143 void StringTests::testParseInt()
144 {
145 
146 	int r = 223546;
147 
148 	String s = StringConverter::toString(r);
149 	int t = StringConverter::parseInt(s);
150 
151 	CPPUNIT_ASSERT_EQUAL(r, t);
152 }
testParseLong()153 void StringTests::testParseLong()
154 {
155 	long r = -2147483647;
156 
157 	String s = StringConverter::toString(r);
158 	long t = StringConverter::parseLong(s);
159 
160 	CPPUNIT_ASSERT_EQUAL(r, t);
161 
162 }
testParseUnsignedLong()163 void StringTests::testParseUnsignedLong()
164 {
165 	unsigned long r = 4294967295UL;
166 
167 	String s = StringConverter::toString(r);
168 	unsigned long t = StringConverter::parseUnsignedLong(s);
169 
170 	CPPUNIT_ASSERT_EQUAL(r, t);
171 
172 }
testParseVector3()173 void StringTests::testParseVector3()
174 {
175 	Vector3 r(0.12, 3.22, -4.04);
176 
177 	String s = StringConverter::toString(r);
178 	Vector3 t = StringConverter::parseVector3(s);
179 
180 	CPPUNIT_ASSERT_EQUAL(r, t);
181 
182 }
testParseMatrix4()183 void StringTests::testParseMatrix4()
184 {
185 	Matrix4 r(1.12, 0, 0, 34, 0, 0.87, 0, 20, 0, 0, 0.56, 10, 0, 0, 0, 1);
186 
187 	String s = StringConverter::toString(r);
188 	Matrix4 t = StringConverter::parseMatrix4(s);
189 
190 	CPPUNIT_ASSERT_EQUAL(r, t);
191 
192 }
testParseQuaternion()193 void StringTests::testParseQuaternion()
194 {
195 	Quaternion r(1.12, 0.87, 0.67, 1);
196 
197 	String s = StringConverter::toString(r);
198 	Quaternion t = StringConverter::parseQuaternion(s);
199 
200 	CPPUNIT_ASSERT_EQUAL(r, t);
201 
202 }
testParseBool()203 void StringTests::testParseBool()
204 {
205 	bool r = true;
206 	String s = StringConverter::toString(r);
207 	bool t = StringConverter::parseBool(s);
208 	CPPUNIT_ASSERT_EQUAL(r, t);
209 
210 	r = false;
211 	s = StringConverter::toString(r);
212 	t = StringConverter::parseBool(s);
213 	CPPUNIT_ASSERT_EQUAL(r, t);
214 
215 }
testParseColourValue()216 void StringTests::testParseColourValue()
217 {
218 	ColourValue r(0.34, 0.44, 0.77, 1.0);
219 
220 	String s = StringConverter::toString(r);
221 	ColourValue t = StringConverter::parseColourValue(s);
222 	CPPUNIT_ASSERT_EQUAL(r, t);
223 
224 }
225