quoted_manip_test.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // libs/io/test/quote_manip_test.cpp ----------------------------------------------- //
  2. // Copyright Beman Dawes 2010
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // Library home page: http://www.boost.org/libs/io
  6. // ---------------------------------------------------------------------------------- //
  7. #include <boost/io/detail/quoted_manip.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <iostream>
  10. #include <sstream>
  11. using boost::io::quoted;
  12. using std::string;
  13. using std::wstring;
  14. int main()
  15. {
  16. std::wstringstream wss;
  17. string r; // test results
  18. const string s0("foo");
  19. {
  20. std::stringstream ss;
  21. ss << quoted(s0);
  22. ss >> r;
  23. BOOST_TEST(r == "\"foo\"");
  24. }
  25. {
  26. std::stringstream ss;
  27. ss << quoted(s0);
  28. ss >> quoted(r);
  29. BOOST_TEST(r == "foo");
  30. }
  31. const string s0s("foo bar");
  32. {
  33. std::stringstream ss;
  34. ss << quoted(s0s);
  35. ss >> r;
  36. BOOST_TEST(r == "\"foo");
  37. }
  38. {
  39. std::stringstream ss;
  40. ss << quoted(s0s);
  41. ss >> quoted(r);
  42. BOOST_TEST(r == "foo bar");
  43. }
  44. const string s1("foo\\bar, \" *");
  45. {
  46. std::stringstream ss;
  47. ss << quoted(s1);
  48. ss >> r;
  49. BOOST_TEST(r == "\"foo\\\\bar,");
  50. }
  51. {
  52. std::stringstream ss;
  53. ss << quoted("foo\\bar, \" *");
  54. ss >> r;
  55. BOOST_TEST(r == "\"foo\\\\bar,");
  56. }
  57. {
  58. std::stringstream ss;
  59. ss << quoted(s1);
  60. ss >> quoted(r);
  61. BOOST_TEST(r == s1);
  62. }
  63. {
  64. std::stringstream ss;
  65. ss << quoted(s1.c_str());
  66. ss >> quoted(r);
  67. BOOST_TEST(r == s1);
  68. }
  69. string s2("'Jack & Jill'");
  70. {
  71. std::stringstream ss;
  72. ss << quoted(s2, '&', '\'');
  73. ss >> quoted(r, '&', '\'');
  74. BOOST_TEST(r == s2);
  75. }
  76. wstring ws1(L"foo$bar, \" *");
  77. wstring wr; // test results
  78. {
  79. std::wstringstream wss;
  80. wss << quoted(ws1, L'$');
  81. wss >> quoted(wr, L'$');
  82. BOOST_TEST(wr == ws1);
  83. }
  84. const string s3("const string");
  85. {
  86. std::stringstream ss;
  87. ss << quoted(s3);
  88. ss >> quoted(r);
  89. BOOST_TEST(r == s3);
  90. }
  91. {
  92. // missing end delimiter test
  93. std::stringstream ss;
  94. ss << "\"abc"; // load ss with faulty quoting
  95. ss >> quoted(r); // this loops if istream error/eof not detected
  96. BOOST_TEST(r == "abc");
  97. }
  98. {
  99. // no initial delmiter test
  100. std::stringstream ss;
  101. ss << "abc";
  102. ss >> quoted(r);
  103. BOOST_TEST(r == "abc");
  104. }
  105. {
  106. // no initial delmiter, space in ss
  107. std::stringstream ss;
  108. ss << "abc def";
  109. ss >> quoted(r);
  110. BOOST_TEST(r == "abc");
  111. }
  112. // these should fail to compile because the arguments are const:
  113. // ss >> quoted(s1);
  114. // ss >> quoted("foo");
  115. return boost::report_errors();
  116. }