Jamoma API  0.6.0.a19
TTString.test.cpp
1 /*
2  * Unit tests for the TTString object
3  * Copyright © 2012, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #include "TTString.test.h"
10 
11 #define thisTTClass TTStringTest
12 #define thisTTClassName "string.test"
13 #define thisTTClassTags "test, foundation"
14 
15 
17 {;}
18 
19 
20 TTStringTest::~TTStringTest()
21 {;}
22 
23 
24 void TTStringTestBasic(int& errorCount, int&testAssertionCount)
25 {
26  // TEST: empty string init
27 
28  TTTestLog("\n");
29  TTTestLog("Testing empty string assigment");
30 
31  TTString empty;
32 
33  TTTestAssertion("created static const char* arg with correct size",
34  empty.size() == 0,
35  testAssertionCount,
36  errorCount);
37  TTTestAssertion("created from static const char* arg with correct length",
38  empty.length() == 0,
39  testAssertionCount,
40  errorCount);
41  TTTestAssertion("created from static const char* arg correctly null terminated",
42  empty.at(0) == 0,
43  testAssertionCount,
44  errorCount);
45 
46  // TEST: c-string init
47 
48  TTTestLog("\n");
49  TTTestLog("Testing basic string assigment");
50 
51  TTString foo("foo");
52 
53  TTTestAssertion("created from static const char* arg with correct size",
54  foo.size() == 3,
55  testAssertionCount,
56  errorCount);
57  TTTestAssertion("created from static const char* arg with correct length",
58  foo.length() == 3,
59  testAssertionCount,
60  errorCount);
61  TTTestAssertion("created from static const char* arg with correct chars",
62  foo.at(0) == 'f' && foo.at(1) == 'o' && foo.at(2) == 'o',
63  testAssertionCount,
64  errorCount);
65  TTTestAssertion("created from static const char* arg correctly null terminated",
66  foo.at(3) == 0,
67  testAssertionCount,
68  errorCount);
69 
70  // TEST: = init
71 
72  TTTestLog("\n");
73  TTTestLog("Testing = assigment");
74 
75  TTString jet;
76  jet = "jet";
77 
78  TTTestAssertion("created from = with correct size",
79  jet.size() == 3,
80  testAssertionCount,
81  errorCount);
82  TTTestAssertion("created from = with correct length",
83  jet.length() == 3,
84  testAssertionCount,
85  errorCount);
86  TTTestAssertion("created from = with correct chars",
87  jet.at(0) == 'j' && jet.at(1) == 'e' && jet.at(2) == 't',
88  testAssertionCount,
89  errorCount);
90  TTTestAssertion("created from = correctly null terminated",
91  jet.at(3) == 0,
92  testAssertionCount,
93  errorCount);
94 
95  // TEST: clear
96 
97  TTTestLog("\n");
98  TTTestLog("Testing clear method");
99 
100  TTString nothing = "something";
101  nothing.clear();
102 
103  TTTestAssertion("cleared string with correct size",
104  empty.size() == 0,
105  testAssertionCount,
106  errorCount);
107  TTTestAssertion("cleared string with correct length",
108  empty.length() == 0,
109  testAssertionCount,
110  errorCount);
111  TTTestAssertion("cleared string correctly null terminated",
112  empty.at(0) == 0,
113  testAssertionCount,
114  errorCount);
115 
116  // TEST: individual char access
117 
118  TTTestLog("\n");
119  TTTestLog("Testing [] assigment");
120 
121  foo[0] = 'g';
122  foo[2] = foo[2] + 1;
123  TTTestAssertion("modified some chars using [] notation",
124  foo.at(0) == 'g' && foo.at(1) == 'o' && foo.at(2) == 'p',
125  testAssertionCount,
126  errorCount);
127 
128 
129  // TEST: comparison (depends on the result from above)
130 
131  TTTestLog("\n");
132  TTTestLog("Testing == operator");
133 
134  TTString gop("gop");
135  TTString bar("bar");
136  TTString foobar("foobar");
137 
138  TTTestAssertion("== operator when strings have the same content",
139  foo == gop,
140  testAssertionCount,
141  errorCount);
142  TTTestAssertion("== operator when strings have different content",
143  !(foo == bar),
144  testAssertionCount,
145  errorCount);
146 
147  TTTestAssertion("!= operator when strings have the same content",
148  !(foo != gop),
149  testAssertionCount,
150  errorCount);
151  TTTestAssertion("!= operator when strings have different content",
152  (foo != bar),
153  testAssertionCount,
154  errorCount);
155 
156  TTTestAssertion("== operator with c-string when strings have the same content",
157  foo == "gop",
158  testAssertionCount,
159  errorCount);
160  TTTestAssertion("== operator with c-string when strings have different content",
161  !(foo == "bar"),
162  testAssertionCount,
163  errorCount);
164 
165  TTTestAssertion("!= operator with c-string when strings have the same content",
166  !(foo != "gop"),
167  testAssertionCount,
168  errorCount);
169  TTTestAssertion("!= operator with c-string when strings have different content",
170  (foo != "bar"),
171  testAssertionCount,
172  errorCount);
173 
174  // here the length given to the substring is too long
175  // so the foobar string is bar\00 after the substring
176  // bu the last zero makes foobar different to bar
177  foobar = foobar.substr(3, 4);
178  TTTestAssertion("== operator with string when strings have the same content but one created using substr with an oversize length",
179  bar == foobar,
180  testAssertionCount,
181  errorCount);
182 
183 }
184 
185 
186 void TTStringTestNumeric(int& errorCount, int&testAssertionCount)
187 {
188  TTTestLog("\n");
189  TTTestLog("Testing substring operation");
190 
191  TTString series("0123456789");
192 
193  TTString sub = series.substr(3,3);
194 
195  TTTestAssertion("created from substr with correct size",
196  sub.size() == 3,
197  testAssertionCount,
198  errorCount);
199  TTTestAssertion("created from substr with correct length",
200  sub.length() == 3,
201  testAssertionCount,
202  errorCount);
203  TTTestAssertion("created from substr with correct chars",
204  sub.at(0) == '3' && sub.at(1) == '4' && sub.at(2) == '5',
205  testAssertionCount,
206  errorCount);
207  TTTestAssertion("created from substr correctly null terminated",
208  sub.at(3) == 0,
209  testAssertionCount,
210  errorCount);
211 
212  TTTestLog("\n");
213  TTTestLog("Testing summing operation");
214 
215  TTString sumA;
216  sumA += "/";
217 
218  TTTestAssertion("created from += operator with correct size",
219  sumA.size() == 1,
220  testAssertionCount,
221  errorCount);
222  TTTestAssertion("created from += operator with correct length",
223  sumA.length() == 1,
224  testAssertionCount,
225  errorCount);
226  TTTestAssertion("created from += operator correctly null terminated",
227  sumA.at(1) == 0,
228  testAssertionCount,
229  errorCount);
230 
231  TTString sumB;
232  sumB += '/';
233 
234  TTTestAssertion("created from += operator with correct size",
235  sumB.size() == 1,
236  testAssertionCount,
237  errorCount);
238  TTTestAssertion("created from += operator with correct length",
239  sumB.length() == 1,
240  testAssertionCount,
241  errorCount);
242  TTTestAssertion("created from += operator correctly null terminated",
243  sumB.at(1) == 0,
244  testAssertionCount,
245  errorCount);
246 
247 
248  // TEST: appending numbers
249 
250  TTTestLog("\n");
251  TTTestLog("Testing appending numbers");
252 
253  TTString a;
254  TTString b("Pi is roughly 7/22");
255 
256  a += "Pi is roughly ";
257  a += 7u;
258  a += "/";
259  a += 22;
260  TTTestAssertion("string built-up with a couple of ints in it",
261  a == b,
262  testAssertionCount,
263  errorCount);
264 
265  b = "Pi is roughly 3.140000";
266  a = "Pi is roughly ";
267  a += 3.14f;
268 
269  TTTestAssertion("string built-up with a float in it",
270  a == b,
271  testAssertionCount,
272  errorCount);
273 
274  a = "Pi is roughly ";
275  a += 3.14;
276  TTTestAssertion("string built-up with a double in it",
277  a == b,
278  testAssertionCount,
279  errorCount);
280 
281  TTTestLog("\n");
282  TTTestLog("Testing + operator");
283 
284  TTString z("At the far end of town");
285  TTString y("where the grickle grass grows");
286  TTString x("and the wind smells sour as it blows");
287  TTString w = z + " " + y + " " + x + "...";
288 
289  TTTestAssertion("string built-up with + operator",
290  w == TTString("At the far end of town where the grickle grass grows and the wind smells sour as it blows..."),
291  testAssertionCount,
292  errorCount);
293 
294 }
295 
296 
297 void TTStringTestStream(int& errorCount, int&testAssertionCount)
298 {
299  TTTestLog("\n");
300  TTTestLog("Testing string stream");
301  TTTestLog("(there will be no assertions, look for stdout output)");
302 
303  TTString hi("Hello World!");
304 
305  std::cout << " Passing TTString to stdout: " << hi << " (hooray)" << std::endl;
306 }
307 
308 void TTStringTestParsing(int& errorCount, int&testAssertionCount)
309 {
310  TTString testString1 = "/test";
311  TTString testString2 = "Test?try";
312  TTString testString3 = "Test?try?this";
313  TTString partA, partB;
314  TTInt32 pos, pos2;
315 
316  TTTestAssertion("check a char using [] string operator",
317  testString1[0] == '/' &&
318  testString2[0] != '/',
319  testAssertionCount,
320  errorCount);
321 
322  pos = testString2.find_first_of('?');
323 
324  TTTestAssertion("look for a char using find_first_of method",
325  pos == 4,
326  testAssertionCount,
327  errorCount);
328 
329  partA = testString2.substr(0, pos);
330  partB = testString2.substr(pos+1, testString2.size() - pos);
331 
332  TTTestAssertion("split a string using substring method",
333  partA == "Test" &&
334  partB == "try",
335  testAssertionCount,
336  errorCount);
337 
338  pos2 = testString3.find_last_of('?');
339 
340  TTTestAssertion("look for a char using find_last_of method",
341  pos2 == 8,
342  testAssertionCount,
343  errorCount);
344 }
345 
346 
347 // TODO: Benchmarking
348 
349 
350 TTErr TTStringTest::test(TTValue& returnedTestInfo)
351 {
352  int errorCount = 0;
353  int testAssertionCount = 0;
354 
355  TTStringTestBasic(errorCount, testAssertionCount);
356  TTStringTestNumeric(errorCount, testAssertionCount);
357  TTStringTestStream(errorCount, testAssertionCount);
358  TTStringTestParsing(errorCount, testAssertionCount);
359 
360  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
361 }
TTString substr(size_t pos=0, size_t n=1) const
Returns a string object with its contents initialized to a substring of the current object...
Definition: TTString.h:342
size_t length() const
Find out the length of a string.
Definition: TTString.h:151
std::int32_t TTInt32
32 bit signed integer
Definition: TTBase.h:177
size_t find_last_of(const char aChar)
Return the index of the last instance of a specified char in the string.
Definition: TTString.h:316
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
size_t size() const
Find out the length of a string.
Definition: TTString.h:144
size_t find_first_of(const char aChar, size_t from=0)
Return the index of the first instance of a specified char in the string.
Definition: TTString.h:296
The TTString class is used to represent a string.
Definition: TTString.h:34
TT_OBJECT_CONSTRUCTOR
Constructor macro.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34