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)
8 * \file string_ref_test_io.cpp
9 * \author Andrey Semashev
12 * \brief This header contains tests for stream operations of \c basic_string_ref.
15 #include <boost/utility/string_view.hpp>
23 #include <boost/config.hpp>
24 #include <boost/core/lightweight_test.hpp>
26 /* Current implementations seem to be missing codecvt facets to convert chars to char16_t and char32_t even though the types are available.
29 static const char* test_strings
[] =
36 //! The context with test data for particular character type
37 template< typename CharT
>
40 typedef CharT char_type
;
41 typedef std::basic_string
< char_type
> string_type
;
42 typedef std::basic_ostringstream
< char_type
> ostream_type
;
44 string_type begin
, abcd
, end
;
48 boost::string_view str
= test_strings
[0];
49 std::copy(str
.begin(), str
.end(), std::back_inserter(begin
));
51 str
= test_strings
[1];
52 std::copy(str
.begin(), str
.end(), std::back_inserter(abcd
));
54 str
= test_strings
[2];
55 std::copy(str
.begin(), str
.end(), std::back_inserter(end
));
59 // Test regular output
61 void test_string_view_output()
63 typedef CharT char_type
;
64 typedef std::basic_ostringstream
< char_type
> ostream_type
;
65 typedef boost::basic_string_view
< char_type
> string_view_type
;
67 context
< char_type
> ctx
;
70 strm
<< string_view_type(ctx
.abcd
);
71 BOOST_TEST(strm
.str() == ctx
.abcd
);
74 // Test support for padding
78 typedef CharT char_type
;
79 typedef std::basic_ostringstream
< char_type
> ostream_type
;
80 typedef boost::basic_string_view
< char_type
> string_view_type
;
82 context
< char_type
> ctx
;
86 ostream_type strm_ref
;
87 strm_ref
<< ctx
.begin
<< std::setw(8) << string_view_type(ctx
.abcd
) << ctx
.end
;
89 ostream_type strm_correct
;
90 strm_correct
<< ctx
.begin
<< std::setw(8) << ctx
.abcd
<< ctx
.end
;
92 BOOST_TEST(strm_ref
.str() == strm_correct
.str());
95 // Test for long padding
97 ostream_type strm_ref
;
98 strm_ref
<< ctx
.begin
<< std::setw(100) << string_view_type(ctx
.abcd
) << ctx
.end
;
100 ostream_type strm_correct
;
101 strm_correct
<< ctx
.begin
<< std::setw(100) << ctx
.abcd
<< ctx
.end
;
103 BOOST_TEST(strm_ref
.str() == strm_correct
.str());
106 // Test that short width does not truncate the string
108 ostream_type strm_ref
;
109 strm_ref
<< ctx
.begin
<< std::setw(1) << string_view_type(ctx
.abcd
) << ctx
.end
;
111 ostream_type strm_correct
;
112 strm_correct
<< ctx
.begin
<< std::setw(1) << ctx
.abcd
<< ctx
.end
;
114 BOOST_TEST(strm_ref
.str() == strm_correct
.str());
118 // Test support for padding fill
119 template<class CharT
>
120 void test_padding_fill()
122 typedef CharT char_type
;
123 typedef std::basic_ostringstream
< char_type
> ostream_type
;
124 typedef boost::basic_string_view
< char_type
> string_view_type
;
126 context
< char_type
> ctx
;
128 ostream_type strm_ref
;
129 strm_ref
<< ctx
.begin
<< std::setfill(static_cast< char_type
>('x')) << std::setw(8) << string_view_type(ctx
.abcd
) << ctx
.end
;
131 ostream_type strm_correct
;
132 strm_correct
<< ctx
.begin
<< std::setfill(static_cast< char_type
>('x')) << std::setw(8) << ctx
.abcd
<< ctx
.end
;
134 BOOST_TEST(strm_ref
.str() == strm_correct
.str());
137 // Test support for alignment
138 template<class CharT
>
139 void test_alignment()
141 typedef CharT char_type
;
142 typedef std::basic_ostringstream
< char_type
> ostream_type
;
143 typedef boost::basic_string_view
< char_type
> string_view_type
;
145 context
< char_type
> ctx
;
149 ostream_type strm_ref
;
150 strm_ref
<< ctx
.begin
<< std::left
<< std::setw(8) << string_view_type(ctx
.abcd
) << ctx
.end
;
152 ostream_type strm_correct
;
153 strm_correct
<< ctx
.begin
<< std::left
<< std::setw(8) << ctx
.abcd
<< ctx
.end
;
155 BOOST_TEST(strm_ref
.str() == strm_correct
.str());
160 ostream_type strm_ref
;
161 strm_ref
<< ctx
.begin
<< std::right
<< std::setw(8) << string_view_type(ctx
.abcd
) << ctx
.end
;
163 ostream_type strm_correct
;
164 strm_correct
<< ctx
.begin
<< std::right
<< std::setw(8) << ctx
.abcd
<< ctx
.end
;
166 BOOST_TEST(strm_ref
.str() == strm_correct
.str());
170 template<class CharT
>
173 test_string_view_output
<CharT
>();
174 test_padding
<CharT
>();
175 test_padding_fill
<CharT
>();
176 test_alignment
<CharT
>();
183 return boost::report_errors();