newline_test.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2004-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. #include <string>
  7. #include <boost/iostreams/compose.hpp>
  8. #include <boost/iostreams/copy.hpp>
  9. #include <boost/iostreams/device/back_inserter.hpp>
  10. #include <boost/iostreams/device/null.hpp>
  11. #include <boost/iostreams/filter/newline.hpp>
  12. #include <boost/iostreams/filter/test.hpp>
  13. #include <boost/iostreams/filtering_stream.hpp>
  14. #include <boost/test/test_tools.hpp>
  15. #include <boost/test/unit_test.hpp>
  16. #include <boost/utility/base_from_member.hpp>
  17. namespace io = boost::iostreams;
  18. using boost::unit_test::test_suite;
  19. const std::string posix =
  20. "When I was one-and-twenty\n"
  21. "I heard a wise man say,\n"
  22. "'Give crowns and pounds and guineas\n"
  23. "But not your heart away;\n"
  24. "\n"
  25. "Give pearls away and rubies\n"
  26. "But keep your fancy free.'\n"
  27. "But I was one-and-twenty,\n"
  28. "No use to talk to me.\n"
  29. "\n"
  30. "When I was one-and-twenty\n"
  31. "I heard him say again,\n"
  32. "'The heart out of the bosom\n"
  33. "Was never given in vain;\n"
  34. "\n"
  35. "'Tis paid with sighs a plenty\n"
  36. "And sold for endless rue.'\n"
  37. "And I am two-and-twenty,\n"
  38. "And oh, 'tis true, 'tis true.\n";
  39. const std::string dos =
  40. "When I was one-and-twenty\r\n"
  41. "I heard a wise man say,\r\n"
  42. "'Give crowns and pounds and guineas\r\n"
  43. "But not your heart away;\r\n"
  44. "\r\n"
  45. "Give pearls away and rubies\r\n"
  46. "But keep your fancy free.'\r\n"
  47. "But I was one-and-twenty,\r\n"
  48. "No use to talk to me.\r\n"
  49. "\r\n"
  50. "When I was one-and-twenty\r\n"
  51. "I heard him say again,\r\n"
  52. "'The heart out of the bosom\r\n"
  53. "Was never given in vain;\r\n"
  54. "\r\n"
  55. "'Tis paid with sighs a plenty\r\n"
  56. "And sold for endless rue.'\r\n"
  57. "And I am two-and-twenty,\r\n"
  58. "And oh, 'tis true, 'tis true.\r\n";
  59. const std::string mac =
  60. "When I was one-and-twenty\r"
  61. "I heard a wise man say,\r"
  62. "'Give crowns and pounds and guineas\r"
  63. "But not your heart away;\r"
  64. "\r"
  65. "Give pearls away and rubies\r"
  66. "But keep your fancy free.'\r"
  67. "But I was one-and-twenty,\r"
  68. "No use to talk to me.\r"
  69. "\r"
  70. "When I was one-and-twenty\r"
  71. "I heard him say again,\r"
  72. "'The heart out of the bosom\r"
  73. "Was never given in vain;\r"
  74. "\r"
  75. "'Tis paid with sighs a plenty\r"
  76. "And sold for endless rue.'\r"
  77. "And I am two-and-twenty,\r"
  78. "And oh, 'tis true, 'tis true.\r";
  79. const std::string no_final_newline =
  80. "When I was one-and-twenty\n"
  81. "I heard a wise man say,\n"
  82. "'Give crowns and pounds and guineas\n"
  83. "But not your heart away;\n"
  84. "\n"
  85. "Give pearls away and rubies\n"
  86. "But keep your fancy free.'\n"
  87. "But I was one-and-twenty,\n"
  88. "No use to talk to me.\n"
  89. "\n"
  90. "When I was one-and-twenty\n"
  91. "I heard him say again,\n"
  92. "'The heart out of the bosom\n"
  93. "Was never given in vain;\n"
  94. "\n"
  95. "'Tis paid with sighs a plenty\n"
  96. "And sold for endless rue.'\n"
  97. "And I am two-and-twenty,\n"
  98. "And oh, 'tis true, 'tis true.";
  99. const std::string mixed =
  100. "When I was one-and-twenty\n"
  101. "I heard a wise man say,\r\n"
  102. "'Give crowns and pounds and guineas\r"
  103. "But not your heart away;\n"
  104. "\r\n"
  105. "Give pearls away and rubies\r"
  106. "But keep your fancy free.'\n"
  107. "But I was one-and-twenty,\r\n"
  108. "No use to talk to me.\r"
  109. "\r"
  110. "When I was one-and-twenty\r\n"
  111. "I heard him say again,\r"
  112. "'The heart out of the bosom\n"
  113. "Was never given in vain;\r\n"
  114. "\r"
  115. "'Tis paid with sighs a plenty\n"
  116. "And sold for endless rue.'\r\n"
  117. "And I am two-and-twenty,\r"
  118. "And oh, 'tis true, 'tis true.\n";
  119. struct string_source : boost::base_from_member<std::string>, io::array_source {
  120. typedef io::array_source base_type;
  121. typedef boost::base_from_member<std::string> pbase_type;
  122. string_source(const std::string& src)
  123. : pbase_type(src), base_type(member.data(), member.size())
  124. { }
  125. string_source(const string_source& src)
  126. : pbase_type(src.member), base_type(member.data(), member.size())
  127. { }
  128. };
  129. void read_newline_filter()
  130. {
  131. using namespace io;
  132. // Test converting to posix format.
  133. BOOST_CHECK(test_input_filter(newline_filter(newline::posix), posix, posix));
  134. BOOST_CHECK(test_input_filter(newline_filter(newline::posix), dos, posix));
  135. BOOST_CHECK(test_input_filter(newline_filter(newline::posix), mac, posix));
  136. BOOST_CHECK(test_input_filter(newline_filter(newline::posix), mixed, posix));
  137. // Test converting to dos format.
  138. BOOST_CHECK(test_input_filter(newline_filter(newline::dos), posix, dos));
  139. BOOST_CHECK(test_input_filter(newline_filter(newline::dos), dos, dos));
  140. BOOST_CHECK(test_input_filter(newline_filter(newline::dos), mac, dos));
  141. BOOST_CHECK(test_input_filter(newline_filter(newline::dos), mixed, dos));
  142. // Test converting to mac format.
  143. BOOST_CHECK(test_input_filter(newline_filter(newline::mac), posix, mac));
  144. BOOST_CHECK(test_input_filter(newline_filter(newline::mac), dos, mac));
  145. BOOST_CHECK(test_input_filter(newline_filter(newline::mac), mac, mac));
  146. BOOST_CHECK(test_input_filter(newline_filter(newline::mac), mixed, mac));
  147. }
  148. // Verify that a filter works as expected with both a non-blocking sink
  149. // and a normal output stream.
  150. //
  151. // test_output_filter only tests for a non-blocking sink.
  152. // TODO: Other tests should probably test with an output stream.
  153. template<typename Filter>
  154. bool my_test_output_filter(Filter filter,
  155. const std::string& input,
  156. const std::string& output)
  157. {
  158. const std::streamsize default_increment = 5;
  159. for ( int inc = default_increment;
  160. inc < default_increment * 40;
  161. inc += default_increment )
  162. {
  163. io::array_source src(input.data(), input.data() + input.size());
  164. std::ostringstream stream;
  165. io::copy(src, compose(filter, stream));
  166. if (stream.str() != output )
  167. return false;
  168. }
  169. return test_output_filter(filter, input, output);
  170. }
  171. void write_newline_filter()
  172. {
  173. using namespace io;
  174. // Test converting to posix format.
  175. BOOST_CHECK(my_test_output_filter(newline_filter(newline::posix), posix, posix));
  176. BOOST_CHECK(my_test_output_filter(newline_filter(newline::posix), dos, posix));
  177. BOOST_CHECK(my_test_output_filter(newline_filter(newline::posix), mac, posix));
  178. BOOST_CHECK(my_test_output_filter(newline_filter(newline::posix), mixed, posix));
  179. // Test converting to dos format.
  180. BOOST_CHECK(my_test_output_filter(newline_filter(newline::dos), posix, dos));
  181. BOOST_CHECK(my_test_output_filter(newline_filter(newline::dos), dos, dos));
  182. BOOST_CHECK(my_test_output_filter(newline_filter(newline::dos), mac, dos));
  183. BOOST_CHECK(my_test_output_filter(newline_filter(newline::dos), mixed, dos));
  184. // Test converting to mac format.
  185. BOOST_CHECK(my_test_output_filter(newline_filter(newline::mac), posix, mac));
  186. BOOST_CHECK(my_test_output_filter(newline_filter(newline::mac), dos, mac));
  187. BOOST_CHECK(my_test_output_filter(newline_filter(newline::mac), mac, mac));
  188. BOOST_CHECK(my_test_output_filter(newline_filter(newline::mac), mixed, mac));
  189. }
  190. void test_input_against_flags(int flags, const std::string& input, bool read)
  191. {
  192. if (read) {
  193. io::copy(
  194. io::compose(
  195. io::newline_checker(flags),
  196. string_source(input)
  197. ),
  198. io::null_sink()
  199. );
  200. } else {
  201. io::copy(
  202. string_source(input),
  203. io::compose(io::newline_checker(flags), io::null_sink())
  204. );
  205. }
  206. }
  207. void read_newline_checker()
  208. {
  209. io::filtering_istream in;
  210. io::newline_checker* checker = 0;
  211. // Verify properties of ::posix.
  212. in.push(io::newline_checker(io::newline::posix));
  213. in.push(string_source(::posix));
  214. BOOST_CHECK_NO_THROW(io::copy(in, io::null_sink()));
  215. checker = BOOST_IOSTREAMS_COMPONENT(in, 0, io::newline_checker);
  216. BOOST_CHECK(checker->is_posix());
  217. BOOST_CHECK(!checker->is_dos());
  218. BOOST_CHECK(!checker->is_mac());
  219. BOOST_CHECK(!checker->is_mixed());
  220. BOOST_CHECK(checker->has_final_newline());
  221. in.pop(); // pop checker.
  222. // Verify properties of ::dos.
  223. in.push(io::newline_checker(io::newline::dos));
  224. in.push(string_source(::dos));
  225. try {
  226. io::copy(in, io::null_sink());
  227. } catch (io::newline_error&) {
  228. BOOST_CHECK_MESSAGE(
  229. false, "failed checking for dos line endings"
  230. );
  231. }
  232. checker = BOOST_IOSTREAMS_COMPONENT(in, 0, io::newline_checker);
  233. BOOST_CHECK(!checker->is_posix());
  234. BOOST_CHECK(checker->is_dos());
  235. BOOST_CHECK(!checker->is_mac());
  236. BOOST_CHECK(!checker->is_mixed());
  237. BOOST_CHECK(checker->has_final_newline());
  238. in.pop(); // pop checker.
  239. // Verify properties of ::mac.
  240. in.push(io::newline_checker(io::newline::mac));
  241. in.push(string_source(::mac));
  242. BOOST_CHECK_NO_THROW(io::copy(in, io::null_sink()));
  243. checker = BOOST_IOSTREAMS_COMPONENT(in, 0, io::newline_checker);
  244. BOOST_CHECK(!checker->is_posix());
  245. BOOST_CHECK(!checker->is_dos());
  246. BOOST_CHECK(checker->is_mac());
  247. BOOST_CHECK(!checker->is_mixed());
  248. BOOST_CHECK(checker->has_final_newline());
  249. in.pop(); // pop checker.
  250. // Verify properties of no_final_newline.
  251. in.push(io::newline_checker(io::newline::posix));
  252. in.push(string_source(::no_final_newline));
  253. BOOST_CHECK_NO_THROW(io::copy(in, io::null_sink()));
  254. checker = BOOST_IOSTREAMS_COMPONENT(in, 0, io::newline_checker);
  255. BOOST_CHECK(checker->is_posix());
  256. BOOST_CHECK(!checker->is_dos());
  257. BOOST_CHECK(!checker->is_mac());
  258. BOOST_CHECK(!checker->is_mixed());
  259. BOOST_CHECK(!checker->has_final_newline());
  260. in.pop(); // pop checker.
  261. // Verify properties of mixed.
  262. in.push(io::newline_checker());
  263. in.push(string_source(::mixed));
  264. BOOST_CHECK_NO_THROW(io::copy(in, io::null_sink()));
  265. checker = BOOST_IOSTREAMS_COMPONENT(in, 0, io::newline_checker);
  266. BOOST_CHECK(!checker->is_posix());
  267. BOOST_CHECK(!checker->is_dos());
  268. BOOST_CHECK(!checker->is_mac());
  269. BOOST_CHECK(checker->is_mixed_posix());
  270. BOOST_CHECK(checker->is_mixed_dos());
  271. BOOST_CHECK(checker->is_mixed_mac());
  272. BOOST_CHECK(checker->is_mixed());
  273. BOOST_CHECK(checker->has_final_newline());
  274. in.pop(); // pop checker.
  275. // Verify exceptions when input does not satisfy target conditions.
  276. BOOST_CHECK_THROW(
  277. test_input_against_flags(io::newline::dos, ::posix, true),
  278. io::newline_error
  279. );
  280. BOOST_CHECK_THROW(
  281. test_input_against_flags(io::newline::mac, ::posix, true),
  282. io::newline_error
  283. );
  284. BOOST_CHECK_THROW(
  285. test_input_against_flags(io::newline::posix, ::dos, true),
  286. io::newline_error
  287. );
  288. BOOST_CHECK_THROW(
  289. test_input_against_flags(io::newline::mac, ::dos, true),
  290. io::newline_error
  291. );
  292. BOOST_CHECK_THROW(
  293. test_input_against_flags(io::newline::posix, ::mac, true),
  294. io::newline_error
  295. );
  296. BOOST_CHECK_THROW(
  297. test_input_against_flags(io::newline::dos, ::mac, true),
  298. io::newline_error
  299. );
  300. BOOST_CHECK_THROW(
  301. test_input_against_flags(io::newline::final_newline, ::no_final_newline, true),
  302. io::newline_error
  303. );
  304. BOOST_CHECK_THROW(
  305. test_input_against_flags(io::newline::posix, ::mixed, true),
  306. io::newline_error
  307. );
  308. BOOST_CHECK_THROW(
  309. test_input_against_flags(io::newline::dos, ::mixed, true),
  310. io::newline_error
  311. );
  312. BOOST_CHECK_THROW(
  313. test_input_against_flags(io::newline::mac, ::mixed, true),
  314. io::newline_error
  315. );
  316. }
  317. void write_newline_checker()
  318. {
  319. io::filtering_ostream out;
  320. io::newline_checker* checker = 0;
  321. // Verify properties of ::posix.
  322. out.push(io::newline_checker(io::newline::posix));
  323. out.push(io::null_sink());
  324. BOOST_CHECK_NO_THROW(io::copy(string_source(::posix), out));
  325. checker = BOOST_IOSTREAMS_COMPONENT(out, 0, io::newline_checker);
  326. BOOST_CHECK(checker->is_posix());
  327. BOOST_CHECK(!checker->is_dos());
  328. BOOST_CHECK(!checker->is_mac());
  329. BOOST_CHECK(!checker->is_mixed());
  330. BOOST_CHECK(checker->has_final_newline());
  331. out.pop(); // pop checker.
  332. // Verify properties of ::dos.
  333. out.push(io::newline_checker(io::newline::dos));
  334. out.push(io::null_sink());
  335. BOOST_CHECK_NO_THROW(io::copy(string_source(::dos), out));
  336. checker = BOOST_IOSTREAMS_COMPONENT(out, 0, io::newline_checker);
  337. BOOST_CHECK(!checker->is_posix());
  338. BOOST_CHECK(checker->is_dos());
  339. BOOST_CHECK(!checker->is_mac());
  340. BOOST_CHECK(!checker->is_mixed());
  341. BOOST_CHECK(checker->has_final_newline());
  342. out.pop(); // pop checker.
  343. // Verify properties of ::mac.
  344. out.push(io::newline_checker(io::newline::mac));
  345. out.push(io::null_sink());
  346. BOOST_CHECK_NO_THROW(io::copy(string_source(::mac), out));
  347. checker = BOOST_IOSTREAMS_COMPONENT(out, 0, io::newline_checker);
  348. BOOST_CHECK(!checker->is_posix());
  349. BOOST_CHECK(!checker->is_dos());
  350. BOOST_CHECK(checker->is_mac());
  351. BOOST_CHECK(!checker->is_mixed());
  352. BOOST_CHECK(checker->has_final_newline());
  353. out.pop(); // pop checker.
  354. // Verify properties of no_final_newline.
  355. out.push(io::newline_checker(io::newline::posix));
  356. out.push(io::null_sink());
  357. BOOST_CHECK_NO_THROW(io::copy(string_source(::no_final_newline), out));
  358. checker = BOOST_IOSTREAMS_COMPONENT(out, 0, io::newline_checker);
  359. BOOST_CHECK(checker->is_posix());
  360. BOOST_CHECK(!checker->is_dos());
  361. BOOST_CHECK(!checker->is_mac());
  362. BOOST_CHECK(!checker->is_mixed());
  363. BOOST_CHECK(!checker->has_final_newline());
  364. out.pop(); // pop checker.
  365. // Verify properties of mixed.
  366. out.push(io::newline_checker());
  367. out.push(io::null_sink());
  368. BOOST_CHECK_NO_THROW(io::copy(string_source(::mixed), out));
  369. checker = BOOST_IOSTREAMS_COMPONENT(out, 0, io::newline_checker);
  370. BOOST_CHECK(!checker->is_posix());
  371. BOOST_CHECK(!checker->is_dos());
  372. BOOST_CHECK(!checker->is_mac());
  373. BOOST_CHECK(checker->is_mixed_posix());
  374. BOOST_CHECK(checker->is_mixed_dos());
  375. BOOST_CHECK(checker->is_mixed_mac());
  376. BOOST_CHECK(checker->is_mixed());
  377. BOOST_CHECK(checker->has_final_newline());
  378. out.pop(); // pop checker.
  379. // Verify exceptions when input does not satisfy target conditions.
  380. BOOST_CHECK_THROW(
  381. test_input_against_flags(io::newline::dos, ::posix, false),
  382. io::newline_error
  383. );
  384. BOOST_CHECK_THROW(
  385. test_input_against_flags(io::newline::mac, ::posix, false),
  386. io::newline_error
  387. );
  388. BOOST_CHECK_THROW(
  389. test_input_against_flags(io::newline::posix, ::dos, false),
  390. io::newline_error
  391. );
  392. BOOST_CHECK_THROW(
  393. test_input_against_flags(io::newline::mac, ::dos, false),
  394. io::newline_error
  395. );
  396. BOOST_CHECK_THROW(
  397. test_input_against_flags(io::newline::posix, ::mac, false),
  398. io::newline_error
  399. );
  400. BOOST_CHECK_THROW(
  401. test_input_against_flags(io::newline::dos, ::mac, false),
  402. io::newline_error
  403. );
  404. BOOST_CHECK_THROW(
  405. test_input_against_flags(io::newline::final_newline, ::no_final_newline, false),
  406. io::newline_error
  407. );
  408. BOOST_CHECK_THROW(
  409. test_input_against_flags(io::newline::posix, ::mixed, false),
  410. io::newline_error
  411. );
  412. BOOST_CHECK_THROW(
  413. test_input_against_flags(io::newline::dos, ::mixed, false),
  414. io::newline_error
  415. );
  416. BOOST_CHECK_THROW(
  417. test_input_against_flags(io::newline::mac, ::mixed, false),
  418. io::newline_error
  419. );
  420. }
  421. test_suite* init_unit_test_suite(int, char* [])
  422. {
  423. test_suite* test = BOOST_TEST_SUITE("newline_filter test");
  424. test->add(BOOST_TEST_CASE(&read_newline_filter));
  425. test->add(BOOST_TEST_CASE(&write_newline_filter));
  426. test->add(BOOST_TEST_CASE(&read_newline_checker));
  427. test->add(BOOST_TEST_CASE(&write_newline_checker));
  428. return test;
  429. }