string_ref_test_io.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright Andrey Semashev 2013.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file string_ref_test_io.cpp
  9. * \author Andrey Semashev
  10. * \date 26.05.2013
  11. *
  12. * \brief This header contains tests for stream operations of \c basic_string_ref.
  13. */
  14. #include <boost/utility/string_ref.hpp>
  15. #include <iomanip>
  16. #include <sstream>
  17. #include <algorithm>
  18. #include <iterator>
  19. #include <string>
  20. #include <boost/config.hpp>
  21. #include <boost/core/lightweight_test.hpp>
  22. /* Current implementations seem to be missing codecvt facets to convert chars to char16_t and char32_t even though the types are available.
  23. */
  24. static const char* test_strings[] =
  25. {
  26. "begin",
  27. "abcd",
  28. "end"
  29. };
  30. //! The context with test data for particular character type
  31. template< typename CharT >
  32. struct context
  33. {
  34. typedef CharT char_type;
  35. typedef std::basic_string< char_type > string_type;
  36. typedef std::basic_ostringstream< char_type > ostream_type;
  37. string_type begin, abcd, end;
  38. context()
  39. {
  40. boost::string_ref str = test_strings[0];
  41. std::copy(str.begin(), str.end(), std::back_inserter(begin));
  42. str = test_strings[1];
  43. std::copy(str.begin(), str.end(), std::back_inserter(abcd));
  44. str = test_strings[2];
  45. std::copy(str.begin(), str.end(), std::back_inserter(end));
  46. }
  47. };
  48. // Test regular output
  49. template<class CharT>
  50. void test_string_ref_output()
  51. {
  52. typedef CharT char_type;
  53. typedef std::basic_ostringstream< char_type > ostream_type;
  54. typedef boost::basic_string_ref< char_type > string_ref_type;
  55. context< char_type > ctx;
  56. ostream_type strm;
  57. strm << string_ref_type(ctx.abcd);
  58. BOOST_TEST(strm.str() == ctx.abcd);
  59. }
  60. // Test support for padding
  61. template<class CharT>
  62. void test_padding()
  63. {
  64. typedef CharT char_type;
  65. typedef std::basic_ostringstream< char_type > ostream_type;
  66. typedef boost::basic_string_ref< char_type > string_ref_type;
  67. context< char_type > ctx;
  68. // Test for padding
  69. {
  70. ostream_type strm_ref;
  71. strm_ref << ctx.begin << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end;
  72. ostream_type strm_correct;
  73. strm_correct << ctx.begin << std::setw(8) << ctx.abcd << ctx.end;
  74. BOOST_TEST(strm_ref.str() == strm_correct.str());
  75. }
  76. // Test for long padding
  77. {
  78. ostream_type strm_ref;
  79. strm_ref << ctx.begin << std::setw(100) << string_ref_type(ctx.abcd) << ctx.end;
  80. ostream_type strm_correct;
  81. strm_correct << ctx.begin << std::setw(100) << ctx.abcd << ctx.end;
  82. BOOST_TEST(strm_ref.str() == strm_correct.str());
  83. }
  84. // Test that short width does not truncate the string
  85. {
  86. ostream_type strm_ref;
  87. strm_ref << ctx.begin << std::setw(1) << string_ref_type(ctx.abcd) << ctx.end;
  88. ostream_type strm_correct;
  89. strm_correct << ctx.begin << std::setw(1) << ctx.abcd << ctx.end;
  90. BOOST_TEST(strm_ref.str() == strm_correct.str());
  91. }
  92. }
  93. // Test support for padding fill
  94. template<class CharT>
  95. void test_padding_fill()
  96. {
  97. typedef CharT char_type;
  98. typedef std::basic_ostringstream< char_type > ostream_type;
  99. typedef boost::basic_string_ref< char_type > string_ref_type;
  100. context< char_type > ctx;
  101. ostream_type strm_ref;
  102. strm_ref << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end;
  103. ostream_type strm_correct;
  104. strm_correct << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << ctx.abcd << ctx.end;
  105. BOOST_TEST(strm_ref.str() == strm_correct.str());
  106. }
  107. // Test support for alignment
  108. template<class CharT>
  109. void test_alignment()
  110. {
  111. typedef CharT char_type;
  112. typedef std::basic_ostringstream< char_type > ostream_type;
  113. typedef boost::basic_string_ref< char_type > string_ref_type;
  114. context< char_type > ctx;
  115. // Left alignment
  116. {
  117. ostream_type strm_ref;
  118. strm_ref << ctx.begin << std::left << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end;
  119. ostream_type strm_correct;
  120. strm_correct << ctx.begin << std::left << std::setw(8) << ctx.abcd << ctx.end;
  121. BOOST_TEST(strm_ref.str() == strm_correct.str());
  122. }
  123. // Right alignment
  124. {
  125. ostream_type strm_ref;
  126. strm_ref << ctx.begin << std::right << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end;
  127. ostream_type strm_correct;
  128. strm_correct << ctx.begin << std::right << std::setw(8) << ctx.abcd << ctx.end;
  129. BOOST_TEST(strm_ref.str() == strm_correct.str());
  130. }
  131. }
  132. template<class CharT>
  133. void test()
  134. {
  135. test_string_ref_output<CharT>();
  136. test_padding<CharT>();
  137. test_padding_fill<CharT>();
  138. test_alignment<CharT>();
  139. }
  140. int main()
  141. {
  142. test<char>();
  143. test<wchar_t>();
  144. return boost::report_errors();
  145. }