aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/llstreamtools_tut.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--linden/indra/test/llstreamtools_tut.cpp928
1 files changed, 928 insertions, 0 deletions
diff --git a/linden/indra/test/llstreamtools_tut.cpp b/linden/indra/test/llstreamtools_tut.cpp
new file mode 100644
index 0000000..aea2e4f
--- /dev/null
+++ b/linden/indra/test/llstreamtools_tut.cpp
@@ -0,0 +1,928 @@
1/**
2 * @file llstreamtools_tut.cpp
3 * @author Adroit
4 * @date February 2007
5 * @brief llstreamtools test cases.
6 *
7 * Copyright (c) 2007-2007, Linden Research, Inc.
8 *
9 * Second Life Viewer Source Code
10 * The source code in this file ("Source Code") is provided by Linden Lab
11 * to you under the terms of the GNU General Public License, version 2.0
12 * ("GPL"), unless you have obtained a separate licensing agreement
13 * ("Other License"), formally executed by you and Linden Lab. Terms of
14 * the GPL can be found in doc/GPL-license.txt in this distribution, or
15 * online at http://secondlife.com/developers/opensource/gplv2
16 *
17 * There are special exceptions to the terms and conditions of the GPL as
18 * it is applied to this Source Code. View the full text of the exception
19 * in the file doc/FLOSS-exception.txt in this software distribution, or
20 * online at http://secondlife.com/developers/opensource/flossexception
21 *
22 * By copying, modifying or distributing this software, you acknowledge
23 * that you have read and understood your obligations described above,
24 * and agree to abide by those obligations.
25 *
26 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
27 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
28 * COMPLETENESS OR PERFORMANCE.
29 */
30
31#include <tut/tut.h>
32#include <fstream>
33#include "llstreamtools.h"
34#include "lltut.h"
35
36
37namespace tut
38{
39 struct streamtools_data
40 {
41 };
42 typedef test_group<streamtools_data> streamtools_test;
43 typedef streamtools_test::object streamtools_object;
44 tut::streamtools_test streamtools_testcase("streamtools");
45
46 //test cases for skip_whitespace()
47 template<> template<>
48 void streamtools_object::test<1>()
49 {
50 char arr[255];
51 std::string str;
52 std::string expected_result;
53 std::string actual_result;
54 std::istringstream is;
55
56 is.str(str = "");
57 ensure("skip_whitespace: empty string", (false == skip_whitespace(is)));
58
59 is.clear();
60 is.str(str = " SecondLife is a 3D World");
61 skip_whitespace(is);
62 is.get(arr, 255, '\0');
63 expected_result = "SecondLife is a 3D World";
64 ensure_equals("skip_whitespace: space", arr, expected_result);
65
66 is.clear();
67 is.str(str = "\t \tSecondLife is a 3D World");
68 skip_whitespace(is);
69 is.get(arr, 255, '\0');
70 expected_result = "SecondLife is a 3D World";
71 ensure_equals("skip_whitespace: space and tabs", arr, expected_result);
72
73 is.clear();
74 is.str(str = "\t \tSecondLife is a 3D World ");
75 skip_whitespace(is);
76 is.get(arr, 255, '\0');
77 expected_result = "SecondLife is a 3D World ";
78 ensure_equals("skip_whitespace: space at end", arr, expected_result);
79
80 is.clear();
81 is.str(str = "\t \r\nSecondLife is a 3D World");
82 skip_whitespace(is);
83 is.get(arr, 255, '\0');
84 expected_result = "\r\nSecondLife is a 3D World";
85 ensure_equals("skip_whitespace: space at end", arr, expected_result);
86 }
87
88 //testcases for skip_emptyspaces()
89 template<> template<>
90 void streamtools_object::test<2>()
91 {
92 char arr[255];
93 std::string str;
94 std::string expected_result;
95 std::string actual_result;
96 std::istringstream is;
97 bool ret;
98
99 is.clear();
100 is.str(str = " \tSecondLife is a 3D World.\n");
101 skip_emptyspace(is);
102 is.get(arr, 255, '\0');
103 expected_result = "SecondLife is a 3D World.\n";
104 ensure_equals("skip_emptyspace: space and tabs", arr, expected_result);
105
106 is.clear();
107 is.str(str = " \t\r\n \r SecondLife is a 3D World.\n");
108 skip_emptyspace(is);
109 is.get(arr, 255, '\0');
110 expected_result = "SecondLife is a 3D World.\n";
111 ensure_equals("skip_emptyspace: space, tabs, carriage return, newline", arr, expected_result);
112
113 is.clear();
114 is.str(str = "");
115 ret = skip_emptyspace(is);
116 is.get(arr, 255, '\0');
117 ensure("skip_emptyspace: empty string", ret == false);
118
119 is.clear();
120 is.str(str = " \r\n \t ");
121 ret = skip_emptyspace(is);
122 is.get(arr, 255, '\0');
123 ensure("skip_emptyspace: space newline empty", ret == false);
124 }
125
126 //testcases for skip_comments_and_emptyspace()
127 template<> template<>
128 void streamtools_object::test<3>()
129 {
130 char arr[255];
131 std::string str;
132 std::string expected_result;
133 std::string actual_result;
134 std::istringstream is;
135 bool ret;
136
137 is.clear();
138 is.str(str = " \t\r\n \r SecondLife is a 3D World.\n");
139 skip_comments_and_emptyspace(is);
140 is.get(arr, 255, '\0');
141 expected_result = "SecondLife is a 3D World.\n";
142 ensure_equals("skip_comments_and_emptyspace: space, tabs, carriage return, newline", arr, expected_result);
143
144 is.clear();
145 is.str(str = "# \r\n SecondLife is a 3D World.");
146 skip_comments_and_emptyspace(is);
147 is.get(arr, 255, '\0');
148 expected_result = "SecondLife is a 3D World.";
149 ensure_equals("skip_comments_and_emptyspace: skip comment - 1", arr, expected_result);
150
151 is.clear();
152 is.str(str = "# \r\n # SecondLife is a 3D World. ##");
153 skip_comments_and_emptyspace(is);
154 is.get(arr, 255, '\0');
155 expected_result = "";
156 ensure_equals("skip_comments_and_emptyspace: skip comment - 2", arr, expected_result);
157
158 is.clear();
159 is.str(str = " \r\n SecondLife is a 3D World. ##");
160 skip_comments_and_emptyspace(is);
161 is.get(arr, 255, '\0');
162 expected_result = "SecondLife is a 3D World. ##";
163 ensure_equals("skip_comments_and_emptyspace: skip comment - 3", arr, expected_result);
164
165 is.clear();
166 is.str(str = "");
167 ret = skip_comments_and_emptyspace(is);
168 is.get(arr, 255, '\0');
169 ensure("skip_comments_and_emptyspace: empty string", ret == false);
170
171 is.clear();
172 is.str(str = " \r\n \t # SecondLife is a 3D World");
173 ret = skip_comments_and_emptyspace(is);
174 is.get(arr, 255, '\0');
175 ensure("skip_comments_and_emptyspace: space newline comment empty", ret == false);
176 }
177
178 //testcases for skip_line()
179 template<> template<>
180 void streamtools_object::test<4>()
181 {
182 char arr[255];
183 std::string str;
184 std::string expected_result;
185 std::string actual_result;
186 std::istringstream is;
187 bool ret;
188
189 is.clear();
190 is.str(str = "SecondLife is a 3D World.\n\n It provides an opportunity to the site \nuser to perform real life activities in virtual world.");
191 skip_line(is);
192 is.get(arr, 255, '\0');
193 expected_result = "\n It provides an opportunity to the site \nuser to perform real life activities in virtual world.";
194 ensure_equals("skip_line: 1 newline", arr, expected_result);
195
196 is.clear();
197 is.str(expected_result);
198 skip_line(is);
199 is.get(arr, 255, '\0');
200 expected_result = " It provides an opportunity to the site \nuser to perform real life activities in virtual world.";
201 ensure_equals("skip_line: 2 newline", arr, expected_result);
202
203 is.clear();
204 is.str(expected_result);
205 skip_line(is);
206 is.get(arr, 255, '\0');
207 expected_result = "user to perform real life activities in virtual world.";
208 ensure_equals("skip_line: 3 newline", arr, expected_result);
209
210 is.clear();
211 is.str(str = "");
212 ret = skip_line(is);
213 ensure("skip_line: empty string", ret == false);
214 }
215
216
217 // testcases for skip_to_next_word()
218 template<> template<>
219 void streamtools_object::test<5>()
220 {
221 char arr[255];
222 std::string str;
223 std::string expected_result;
224 std::string actual_result;
225 std::istringstream is;
226 bool ret;
227
228 is.clear();
229 is.str(str = "SecondLife is a 3D_World.\n\n It-provides an opportunity to the site \nuser to perform real life activities in virtual world.");
230 skip_to_next_word(is); // get past SecondLife
231 is.get(arr, 255, '\0');
232 expected_result = "is a 3D_World.\n\n It-provides an opportunity to the site \nuser to perform real life activities in virtual world.";
233 ensure_equals("skip_to_next_word: 1", arr, expected_result);
234
235 is.clear();
236 is.str(expected_result);
237 skip_to_next_word(is); // get past is
238 skip_to_next_word(is); // get past a
239 skip_to_next_word(is); // get past 3D_World.\n\n
240 is.get(arr, 255, '\0');
241 expected_result = "It-provides an opportunity to the site \nuser to perform real life activities in virtual world.";
242 ensure_equals("skip_to_next_word: get past .\n\n 2", arr, expected_result);
243
244 is.clear();
245 is.str(expected_result);
246 skip_to_next_word(is); // get past It-
247 expected_result = "provides an opportunity to the site \nuser to perform real life activities in virtual world.";
248 is.get(arr, 255, '\0');
249 ensure_equals("skip_to_next_word: get past -", arr, expected_result);
250
251 is.clear();
252 is.str(str = "");
253 ret = skip_to_next_word(is);
254 ensure("skip_line: empty string", ret == false);
255
256 is.clear();
257 is.str(str = " \r\n\r\n");
258 ret = skip_to_next_word(is);
259 ensure("skip_line: space new lines", ret == false);
260 }
261
262
263 //testcases for skip_to_end_of_next_keyword()
264 template<> template<>
265 void streamtools_object::test<6>()
266 {
267 char arr[255];
268 std::string str;
269 std::string expected_result;
270 std::string actual_result;
271 std::istringstream is;
272 bool ret;
273
274 is.clear();
275 is.str(str = "FIRSTKEY followed by second delimiter\nSECONDKEY\t SecondValue followed by third delimiter \nSECONDKEY\nFOURTHKEY FOURTHVALUEis a 3DWorld.");
276 ret = skip_to_end_of_next_keyword("FIRSTKEY", is);
277 is.get(arr, 255, '\0');
278 expected_result = " followed by second delimiter\nSECONDKEY\t SecondValue followed by third delimiter \nSECONDKEY\nFOURTHKEY FOURTHVALUEis a 3DWorld.";
279 ensure_equals("skip_to_end_of_next_keyword: 1", arr, expected_result);
280
281 is.clear();
282 is.str(expected_result);
283 ret = skip_to_end_of_next_keyword("SECONDKEY", is);
284 is.get(arr, 255, '\0');
285 expected_result = "\t SecondValue followed by third delimiter \nSECONDKEY\nFOURTHKEY FOURTHVALUEis a 3DWorld.";
286 ensure_equals("skip_to_end_of_next_keyword: 2", arr, expected_result);
287
288 is.clear();
289 is.str(expected_result);
290 ret = skip_to_end_of_next_keyword("SECONDKEY", is);
291 is.get(arr, 255, '\0');
292 expected_result = "\nFOURTHKEY FOURTHVALUEis a 3DWorld.";
293 ensure_equals("skip_to_end_of_next_keyword: 3", arr, expected_result);
294
295 is.clear();
296 is.str(expected_result);
297 ret = skip_to_end_of_next_keyword("FOURTHKEY", is);
298 is.get(arr, 255, '\0');
299 expected_result = " FOURTHVALUEis a 3DWorld.";
300 ensure_equals("skip_to_end_of_next_keyword: 4", arr, expected_result);
301
302 is.clear();
303 is.str(str = "{should be skipped as newline/space/tab does not follow but this one should be picked\n { Does it?\n");
304 ret = skip_to_end_of_next_keyword("{", is);
305 is.get(arr, 255, '\0');
306 expected_result = " Does it?\n";
307 ensure_equals("skip_to_end_of_next_keyword: multiple delim matches on same line", arr, expected_result);
308
309 is.clear();
310 is.str(str = "Delim { could not be found at start");
311 ret = skip_to_end_of_next_keyword("{", is);
312 ensure("skip_to_end_of_next_keyword: delim should not be present", ret == false);
313
314 is.clear();
315 is.str(str = "Empty Delim");
316 ret = skip_to_end_of_next_keyword("", is);
317 ensure("skip_to_end_of_next_keyword: empty delim should not be valid", ret == false);
318
319 is.clear();
320 is.str(str = "");
321 ret = skip_to_end_of_next_keyword("}", is);
322 ensure("skip_to_end_of_next_keyword: empty string", ret == false);
323 }
324
325 //testcases for get_word(std::string& output_string, std::istream& input_stream)
326 template<> template<>
327 void streamtools_object::test<7>()
328 {
329 std::string str;
330 std::string expected_result;
331 std::string actual_result;
332 std::istringstream is;
333 bool ret;
334
335 is.clear();
336 is.str(str = " First Second \t \r \n Third Fourth-ShouldThisBePartOfFourth Fifth\n");
337 actual_result = "";
338 ret = get_word(actual_result, is);
339 expected_result = "First";
340 ensure_equals("get_word: 1", actual_result, expected_result);
341
342 actual_result = "";
343 ret = get_word(actual_result, is);
344 expected_result = "Second";
345 ensure_equals("get_word: 2", actual_result, expected_result);
346
347 actual_result = "";
348 ret = get_word(actual_result, is);
349 expected_result = "Third";
350 ensure_equals("get_word: 3", actual_result, expected_result);
351
352 // the current implementation of get_word seems inconsistent with
353 // skip_to_next_word. skip_to_next_word treats any character other
354 // than alhpa-numeric and '_' as a delimter, while get_word()
355 // treats only isspace() (i.e. space, form-feed('\f'), newline ('\n'),
356 // carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v')
357 // as delimiters
358 actual_result = "";
359 ret = get_word(actual_result, is); // will copy Fourth-ShouldThisBePartOfFourth
360 expected_result = "Fourth-ShouldThisBePartOfFourth"; // as per current impl.
361 ensure_equals("get_word: 4", actual_result, expected_result);
362
363 actual_result = "";
364 ret = get_word(actual_result, is); // will copy Fifth
365 expected_result = "Fifth"; // as per current impl.
366 ensure_equals("get_word: 5", actual_result, expected_result);
367
368 is.clear();
369 is.str(str = " \t \r \n ");
370 actual_result = "";
371 ret = get_word(actual_result, is);
372 ensure("get_word: empty all spaces, newline tabs", ret == false);
373
374 is.clear();
375 is.str(str = "");
376 actual_result = "";
377 ret = get_word(actual_result, is);
378 ensure("get_word: empty string", ret == false);
379 }
380
381 // testcase for get_word and skip_to_next_word compatibility
382 template<> template<>
383 void streamtools_object::test<8>()
384 {
385 std::string str;
386 std::string expected_result;
387 std::string actual_result;
388 std::istringstream is;
389 bool ret;
390
391 is.clear();
392 is.str(str = " First Second \t \r \n Third Fourth-ShouldThisBePartOfFourth Fifth\n");
393 actual_result = "";
394 ret = get_word(actual_result, is); // First
395 actual_result = "";
396 ret = get_word(actual_result, is); // Second
397 actual_result = "";
398 ret = get_word(actual_result, is); // Third
399
400 // the current implementation of get_word seems inconsistent with
401 // skip_to_next_word. skip_to_next_word treats any character other
402 // than alhpa-numeric and '_' as a delimter, while get_word()
403 // treats only isspace() (i.e. space, form-feed('\f'), newline ('\n'),
404 // carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v')
405 // as delimiters
406 actual_result = "";
407 ret = get_word(actual_result, is); // will copy Fourth-ShouldThisBePartOfFourth
408
409 actual_result = "";
410 ret = get_word(actual_result, is); // will copy Fifth
411
412 is.clear();
413 is.str(str = " First Second \t \r \n Third Fourth_ShouldThisBePartOfFourth Fifth\n");
414 ret = skip_to_next_word(is); // should now point to First
415 ret = skip_to_next_word(is); // should now point to Second
416 ret = skip_to_next_word(is); // should now point to Third
417 ret = skip_to_next_word(is); // should now point to Fourth
418 ret = skip_to_next_word(is); // should now point to ShouldThisBePartOfFourth
419 expected_result = "";
420 // will copy ShouldThisBePartOfFourth, the fifth word,
421 // while using get_word above five times result in getting "Fifth"
422 ret = get_word(expected_result, is);
423 ensure_equals("get_word: skip_to_next_word compatibility", actual_result, expected_result);
424 }
425
426 //testcases for get_word(std::string& output_string, std::istream& input_stream, int n)
427 template<> template<>
428 void streamtools_object::test<9>()
429 {
430 std::string str;
431 std::string expected_result;
432 std::string actual_result;
433 std::istringstream is;
434 bool ret;
435
436 is.clear();
437 is.str(str = " First Second \t \r \n Third Fourth-ShouldThisBePartOfFourth Fifth\n");
438 actual_result = "";
439 ret = get_word(actual_result, is, 255);
440 expected_result = "First";
441 ensure_equals("get_word: 1", actual_result, expected_result);
442
443 actual_result = "";
444 ret = get_word(actual_result, is, 4);
445 expected_result = "Seco"; // should be cut short
446 ensure_equals("get_word: 2", actual_result, expected_result);
447
448 actual_result = "";
449 ret = get_word(actual_result, is, 255);
450 expected_result = "nd"; // get remainder of Second
451 ensure_equals("get_word: 3", actual_result, expected_result);
452
453 actual_result = "";
454 ret = get_word(actual_result, is, 0); // 0 size string
455 expected_result = ""; // get remainder of Second
456 ensure_equals("get_word: 0 sized output", actual_result, expected_result);
457
458 actual_result = "";
459 ret = get_word(actual_result, is, 255);
460 expected_result = "Third";
461 ensure_equals("get_word: 4", actual_result, expected_result);
462
463 is.clear();
464 is.str(str = " \t \r \n ");
465 actual_result = "";
466 ret = get_word(actual_result, is, 255);
467 ensure("get_word: empty all spaces, newline tabs", ret == false);
468
469 is.clear();
470 is.str(str = "");
471 actual_result = "";
472 ret = get_word(actual_result, is, 255);
473 ensure("get_word: empty string", ret == false);
474 }
475
476 //test cases for get_line(std::string& output_string, std::istream& input_stream)
477 template<> template<>
478 void streamtools_object::test<10>()
479 {
480 std::string str;
481 std::string expected_result;
482 std::string actual_result;
483 std::istringstream is;
484 bool ret;
485
486 is.clear();
487 is.str(str = "First Second \t \r\n Third Fourth-ShouldThisBePartOfFourth IsThisFifth\n");
488 actual_result = "";
489 ret = get_line(actual_result, is);
490 expected_result = "First Second \t \n";
491 ensure_equals("get_line: 1", actual_result, expected_result);
492
493 actual_result = "";
494 ret = get_line(actual_result, is);
495 expected_result = " Third Fourth-ShouldThisBePartOfFourth IsThisFifth\n";
496 ensure_equals("get_line: 2", actual_result, expected_result);
497
498 is.clear();
499 is.str(str = "\nFirst Line.\n\nSecond Line.\n");
500 actual_result = "";
501 ret = get_line(actual_result, is);
502 expected_result = "\n";
503 ensure_equals("get_line: First char as newline", actual_result, expected_result);
504
505 actual_result = "";
506 ret = get_line(actual_result, is);
507 expected_result = "First Line.\n";
508 ensure_equals("get_line: 3", actual_result, expected_result);
509
510 actual_result = "";
511 ret = get_line(actual_result, is);
512 expected_result = "\n";
513 ensure_equals("get_line: 4", actual_result, expected_result);
514
515 actual_result = "";
516 ret = get_line(actual_result, is);
517 expected_result = "Second Line.\n";
518 ensure_equals("get_line: 5", actual_result, expected_result);
519 }
520
521 //test cases for get_line(std::string& output_string, std::istream& input_stream)
522 template<> template<>
523 void streamtools_object::test<11>()
524 {
525 std::string str;
526 std::string expected_result;
527 std::string actual_result;
528 std::istringstream is;
529 bool ret;
530
531 is.clear();
532 is.str(str = "One Line only with no newline");
533 actual_result = "";
534 ret = get_line(actual_result, is);
535 expected_result = "One Line only with no newline";
536 ensure_equals("get_line: No newline", actual_result, expected_result);
537 ensure_equals("return value is good state of stream", ret, is.good());
538 }
539
540 //test cases for get_line(std::string& output_string, std::istream& input_stream)
541 template<> template<>
542 void streamtools_object::test<12>()
543 {
544 skip_fail("get_line() incorrectly handles lone carriage return.");
545 std::string str;
546 std::string expected_result;
547 std::string actual_result;
548 std::istringstream is;
549 bool ret;
550
551 // need to be check if this test case is wrong or the implementation is wrong.
552 is.clear();
553 is.str(str = "Should not skip \r unless they are followed with newline .\r\n");
554 actual_result = "";
555 ret = get_line(actual_result, is);
556 expected_result = "Should not skip \r unless they are followed with newline .\n";
557 ensure_equals("get_line: carriage return skipped even though not followed by newline", actual_result, expected_result);
558 }
559
560 //test cases for get_line(std::string& output_string, std::istream& input_stream)
561 template<> template<>
562 void streamtools_object::test<13>()
563 {
564 std::string str;
565 std::string expected_result;
566 std::string actual_result;
567 std::istringstream is;
568 bool ret;
569
570 is.clear();
571 is.str(str = "\n");
572 actual_result = "";
573 ret = get_line(actual_result, is);
574 expected_result = "\n";
575 ensure_equals("get_line: Just newline", actual_result, expected_result);
576 }
577
578
579 //testcases for get_line(std::string& output_string, std::istream& input_stream, int n)
580 template<> template<>
581 void streamtools_object::test<14>()
582 {
583 std::string str;
584 std::string expected_result;
585 std::string actual_result;
586 std::istringstream is;
587 bool ret;
588
589 is.clear();
590 is.str(str = "First Line.\nSecond Line.\n");
591 actual_result = "";
592 ret = get_line(actual_result, is, 255);
593 expected_result = "First Line.\n";
594 ensure_equals("get_line: Basic Operation", actual_result, expected_result);
595
596 actual_result = "";
597 ret = get_line(actual_result, is, sizeof("Second")-1);
598 expected_result = "Second\n";
599 ensure_equals("get_line: Insufficient length 1", actual_result, expected_result);
600
601 actual_result = "";
602 ret = get_line(actual_result, is, 255);
603 expected_result = " Line.\n";
604 ensure_equals("get_line: Remainder after earlier insufficient length", actual_result, expected_result);
605
606 is.clear();
607 is.str(str = "One Line only with no newline with limited length");
608 actual_result = "";
609 ret = get_line(actual_result, is, sizeof("One Line only with no newline with limited length")-1);
610 expected_result = "One Line only with no newline with limited length\n";
611 ensure_equals("get_line: No newline with limited length", actual_result, expected_result);
612
613 is.clear();
614 is.str(str = "One Line only with no newline");
615 actual_result = "";
616 ret = get_line(actual_result, is, 255);
617 expected_result = "One Line only with no newline";
618 ensure_equals("get_line: No newline", actual_result, expected_result);
619 }
620
621 //testcases for get_line(std::string& output_string, std::istream& input_stream, int n)
622 template<> template<>
623 void streamtools_object::test<15>()
624 {
625 std::string str;
626 std::string expected_result;
627 std::string actual_result;
628 std::istringstream is;
629 bool ret;
630
631 is.clear();
632 is.str(str = "One Line only with no newline");
633 actual_result = "";
634 ret = get_line(actual_result, is, 255);
635 expected_result = "One Line only with no newline";
636 ensure_equals("get_line: No newline", actual_result, expected_result);
637 ensure_equals("return value is good state of stream", ret, is.good());
638 }
639
640 //testcases for remove_last_char()
641 template<> template<>
642 void streamtools_object::test<16>()
643 {
644 std::string str;
645 std::string expected_result;
646 bool ret;
647
648 str = "SecondLife is a 3D World";
649
650 ret = remove_last_char('d',str);
651 expected_result = "SecondLife is a 3D Worl";
652 ensure_equals("remove_last_char: should remove last char", str, expected_result);
653
654 str = "SecondLife is a 3D World";
655 ret = remove_last_char('W',str);
656 expected_result = "SecondLife is a 3D World";
657 ensure_equals("remove_last_char: should not remove as it is not last char", str, expected_result);
658 ensure("remove_last_char: should return false", ret == false);
659
660 str = "SecondLife is a 3D World\n";
661 ret = remove_last_char('\n',str);
662 expected_result = "SecondLife is a 3D World";
663 ensure_equals("remove_last_char: should remove last newline", str, expected_result);
664 ensure("remove_last_char: should remove newline and return true", ret == true);
665 }
666
667
668 //testcases for unescaped_string()
669 template<> template<>
670 void streamtools_object::test<17>()
671 {
672 std::string str;
673 std::string expected_result;
674
675 str = "SecondLife is a 3D world \\n";
676 unescape_string(str);
677 expected_result = "SecondLife is a 3D world \n";
678 ensure_equals("unescape_string: newline", str, expected_result);
679
680 str = "SecondLife is a 3D world \\\\t \\n";
681 unescape_string(str);
682 expected_result = "SecondLife is a 3D world \\t \n";
683 ensure_equals("unescape_string: backslash and newline", str, expected_result);
684
685 str = "SecondLife is a 3D world \\ ";
686 unescape_string(str);
687 expected_result = "SecondLife is a 3D world \\ ";
688 ensure_equals("unescape_string: insufficient to unescape", str, expected_result);
689
690 str = "SecondLife is a 3D world \\n \\n \\n \\\\\\n";
691 unescape_string(str);
692 expected_result = "SecondLife is a 3D world \n \n \n \\\n";
693 ensure_equals("unescape_string: multipel newline and backslash", str, expected_result);
694
695 str = "SecondLife is a 3D world \\t";
696 unescape_string(str);
697 expected_result = "SecondLife is a 3D world \\t";
698 ensure_equals("unescape_string: leaves tab as is", str, expected_result);
699
700 str = "\\n";
701 unescape_string(str);
702 expected_result = "\n";
703 ensure_equals("unescape_string: only a newline", str, expected_result);
704 }
705
706 //testcases for escape_string()
707 template<> template<>
708 void streamtools_object::test<18>()
709 {
710 std::string str;
711 std::string expected_result;
712
713 str = "SecondLife is a 3D world \n";
714 escape_string(str);
715 expected_result = "SecondLife is a 3D world \\n";
716 ensure_equals("escape_string: newline", str, expected_result);
717
718 str = "SecondLife is a 3D world \\t \n";
719 escape_string(str);
720 expected_result = "SecondLife is a 3D world \\\\t \\n";
721 ensure_equals("escape_string: backslash and newline", str, expected_result);
722
723 str = "SecondLife is a 3D world \n \n \n \\\n";
724 escape_string(str);
725 expected_result = "SecondLife is a 3D world \\n \\n \\n \\\\\\n";
726 ensure_equals("escape_string: multipel newline and backslash", str, expected_result);
727
728 str = "SecondLife is a 3D world \t";
729 escape_string(str);
730 expected_result = "SecondLife is a 3D world \t";
731 ensure_equals("unescape_string: leaves tab as is", str, expected_result);
732
733 str = "\n";
734 escape_string(str);
735 expected_result = "\\n";
736 ensure_equals("unescape_string: only a newline", str, expected_result);
737
738 // serialization/deserialization escape->unescape
739 str = "SecondLife is a 3D world \n \n \n \\\n";
740 escape_string(str);
741 unescape_string(str);
742 expected_result = "SecondLife is a 3D world \n \n \n \\\n";
743 ensure_equals("escape_string: should preserve with escape/unescape", str, expected_result);
744
745 // serialization/deserialization unescape->escape
746 str = "SecondLife is a 3D world \\n \\n \\n \\\\";
747 unescape_string(str);
748 escape_string(str);
749 expected_result = "SecondLife is a 3D world \\n \\n \\n \\\\";
750 ensure_equals("escape_string: should preserve with unescape/escape", str, expected_result);
751 }
752
753 // testcases for replace_newlines_with_whitespace()
754 template<> template<>
755 void streamtools_object::test<19>()
756 {
757 std::string str;
758 std::string expected_result;
759
760 str = "SecondLife is a 3D \n\nworld\n";
761 replace_newlines_with_whitespace(str);
762 expected_result = "SecondLife is a 3D world ";
763 ensure_equals("replace_newlines_with_whitespace: replace all newline", str, expected_result);
764
765 str = "\nSecondLife is a 3D world\n";
766 replace_newlines_with_whitespace(str);
767 expected_result = " SecondLife is a 3D world ";
768 ensure_equals("replace_newlines_with_whitespace: begin and newline", str, expected_result);
769
770 str = "SecondLife is a 3D world\r\t";
771 replace_newlines_with_whitespace(str);
772 expected_result = "SecondLife is a 3D world\r\t";
773 ensure_equals("replace_newlines_with_whitespace: should only replace newline", str, expected_result);
774
775 str = "";
776 replace_newlines_with_whitespace(str);
777 expected_result = "";
778 ensure_equals("replace_newlines_with_whitespace: empty string", str, expected_result);
779 }
780
781 //testcases for remove_double_quotes()
782 template<> template<>
783 void streamtools_object::test<20>()
784 {
785 std::string str;
786 std::string expected_result;
787
788 str = "SecondLife is a \"\"3D world";
789 remove_double_quotes(str);
790 expected_result = "SecondLife is a 3D world";
791 ensure_equals("remove_double_quotes: replace empty doube quotes", str, expected_result);
792
793 str = "SecondLife is a \"3D world";
794 remove_double_quotes(str);
795 expected_result = "SecondLife is a 3D world";
796 ensure_equals("remove_double_quotes: keep as is it matching quote not found", str, expected_result);
797 }
798
799 // testcases for get_brace_count()
800 template<> template<>
801 void streamtools_object::test<21>()
802 {
803 skip_fail("get_brace_count() has bugs.");
804
805 std::string str;
806 std::string expected_result;
807 int count;
808
809 str = " { ";
810 count = get_brace_count(str);
811 ensure("get_brace_count: 1 for {", count == 1);
812
813 str = "\t}\t\t \n";
814 count = get_brace_count(str);
815 ensure("get_brace_count: 1 for {", count == -1);
816
817 str = "\t\t\t \n";
818 count = get_brace_count(str);
819 ensure("get_brace_count: 0 for no braces", count == 0);
820
821 str = "{ Remaining line not empty\n";
822 count = get_brace_count(str);
823 ensure("get_brace_count: 0 for remainign line not empty", count == 0);
824
825 /* shouldn't this return 1? */
826 str = "{ /*Remaining line in comment*/\n";
827 count = get_brace_count(str);
828 ensure("get_brace_count: 1 for { with remaining line in comment", count == 1);
829
830 /* shouldn't this return -1? */
831 str = " } //Remaining line in comment \n";
832 count = get_brace_count(str);
833 ensure("get_brace_count: -1 for } with remaining line in comment", count == -1);
834 }
835
836 //testcases for get_keyword_and_value()
837 template<> template<>
838 void streamtools_object::test<22>()
839 {
840 std::string s = "SecondLife is a 3D World";
841 std::string keyword;
842 std::string value;
843 get_keyword_and_value(keyword, value, s);
844 ensure("get_keyword_and_value: Unable to get Keyword and Value", ((keyword == "SecondLife") && (value == "is a 3D World")));
845
846 s = "SecondLife";
847 get_keyword_and_value(keyword, value, s);
848 ensure("get_keyword_and_value: value should be empty", ((keyword == "SecondLife") && (value == "")));
849
850 s = "SecondLife \t is cool! \n";
851 get_keyword_and_value(keyword, value, s);
852 ensure("get_keyword_and_value: remove space before value but not after", ((keyword == "SecondLife") && (value == "is cool! ")));
853 }
854
855 //testcases for get_keyword_and_value()
856 template<> template<>
857 void streamtools_object::test<23>()
858 {
859 skip_fail("get_keyword_and_value() has bugs.");
860
861 std::string s;
862 std::string keyword = "SOME PRIOR KEYWORD";
863 std::string value = "SOME PRIOR VALUE";
864
865 s = "SecondLife\n";
866 get_keyword_and_value(keyword, value, s);
867 ensure("get_keyword_and_value: terminated with newline. value should be empty", ((keyword == "SecondLife") && (value == "")));
868 }
869
870 //testcases for get_keyword_and_value()
871 template<> template<>
872 void streamtools_object::test<24>()
873 {
874 skip_fail("get_keyword_and_value() has bugs.");
875
876 std::string s;
877 std::string keyword = "SOME PRIOR KEYWORD";
878 std::string value = "SOME PRIOR VALUE";
879
880 s = "";
881 get_keyword_and_value(keyword, value, s);
882 ensure("get_keyword_and_value: empty string. keyword value should empty", ((keyword == "") && (value == "")));
883 }
884
885 //testcase for fullread()
886 template<> template<>
887 void streamtools_object::test<25>()
888 {
889 std::string str = "First Line.\nSecond Line\n";
890 std::istringstream is(str);
891 char buf[255] = {0};
892
893 fullread(is, buf, 255);
894 ensure_memory_matches("fullread: read with newlines", (void*) buf, str.size()-1, (void*) str.c_str(), str.size()-1);
895
896 is.clear();
897 is.str(str = "First Line.\nSecond Line\n");
898 memset(buf, 0, 255);
899
900 char expected_string[] = "First Line.\nSecond";
901 int len = sizeof(expected_string)-1;
902 fullread(is, buf, len);
903 ensure_memory_matches("fullread: read with newlines", (void*) buf, len, (void*) &expected_string, len);
904 }
905
906
907// testcases for operator >>
908
909 template<> template<>
910 void streamtools_object::test<26>()
911 {
912 char arr[255];
913 std::string str;
914 std::string toCheck = "SecondLife" ;
915 std::string expected_result;
916 std::istringstream stream("SecondLife is a 3D World");
917 stream >> toCheck.c_str();
918 stream.get(arr, 255, '\0');
919 expected_result = " is a 3D World";
920 ensure_equals("istream << operator", arr, expected_result);
921
922 stream.clear();
923 stream.str(str = "SecondLife is a 3D World");
924 toCheck = "is";
925 stream >> toCheck.c_str();
926 ensure("istream << operator should have failed", stream.good() == false);
927 }
928}