PageRenderTime 86ms CodeModel.GetById 10ms app.highlight 66ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/test/llstreamtools_tut.cpp

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