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