blob: 416ff338359e13b8ae46895d3cf5eef70712335b [file] [log] [blame]
Brian Silverman60e3e2a2018-08-04 23:57:12 -07001/*
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
15#include <boost/utility/string_view.hpp>
16
17#include <iomanip>
18#include <sstream>
19#include <algorithm>
20#include <iterator>
21#include <string>
22
23#include <boost/config.hpp>
24#include <boost/core/lightweight_test.hpp>
25
26/* Current implementations seem to be missing codecvt facets to convert chars to char16_t and char32_t even though the types are available.
27*/
28
29static const char* test_strings[] =
30{
31 "begin",
32 "abcd",
33 "end"
34};
35
36//! The context with test data for particular character type
37template< typename CharT >
38struct context
39{
40 typedef CharT char_type;
41 typedef std::basic_string< char_type > string_type;
42 typedef std::basic_ostringstream< char_type > ostream_type;
43
44 string_type begin, abcd, end;
45
46 context()
47 {
48 boost::string_view str = test_strings[0];
49 std::copy(str.begin(), str.end(), std::back_inserter(begin));
50
51 str = test_strings[1];
52 std::copy(str.begin(), str.end(), std::back_inserter(abcd));
53
54 str = test_strings[2];
55 std::copy(str.begin(), str.end(), std::back_inserter(end));
56 }
57};
58
59// Test regular output
60template<class CharT>
61void test_string_view_output()
62{
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;
66
67 context< char_type > ctx;
68
69 ostream_type strm;
70 strm << string_view_type(ctx.abcd);
71 BOOST_TEST(strm.str() == ctx.abcd);
72}
73
74// Test support for padding
75template<class CharT>
76void test_padding()
77{
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;
81
82 context< char_type > ctx;
83
84 // Test for padding
85 {
86 ostream_type strm_ref;
87 strm_ref << ctx.begin << std::setw(8) << string_view_type(ctx.abcd) << ctx.end;
88
89 ostream_type strm_correct;
90 strm_correct << ctx.begin << std::setw(8) << ctx.abcd << ctx.end;
91
92 BOOST_TEST(strm_ref.str() == strm_correct.str());
93 }
94
95 // Test for long padding
96 {
97 ostream_type strm_ref;
98 strm_ref << ctx.begin << std::setw(100) << string_view_type(ctx.abcd) << ctx.end;
99
100 ostream_type strm_correct;
101 strm_correct << ctx.begin << std::setw(100) << ctx.abcd << ctx.end;
102
103 BOOST_TEST(strm_ref.str() == strm_correct.str());
104 }
105
106 // Test that short width does not truncate the string
107 {
108 ostream_type strm_ref;
109 strm_ref << ctx.begin << std::setw(1) << string_view_type(ctx.abcd) << ctx.end;
110
111 ostream_type strm_correct;
112 strm_correct << ctx.begin << std::setw(1) << ctx.abcd << ctx.end;
113
114 BOOST_TEST(strm_ref.str() == strm_correct.str());
115 }
116}
117
118// Test support for padding fill
119template<class CharT>
120void test_padding_fill()
121{
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;
125
126 context< char_type > ctx;
127
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;
130
131 ostream_type strm_correct;
132 strm_correct << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << ctx.abcd << ctx.end;
133
134 BOOST_TEST(strm_ref.str() == strm_correct.str());
135}
136
137// Test support for alignment
138template<class CharT>
139void test_alignment()
140{
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;
144
145 context< char_type > ctx;
146
147 // Left alignment
148 {
149 ostream_type strm_ref;
150 strm_ref << ctx.begin << std::left << std::setw(8) << string_view_type(ctx.abcd) << ctx.end;
151
152 ostream_type strm_correct;
153 strm_correct << ctx.begin << std::left << std::setw(8) << ctx.abcd << ctx.end;
154
155 BOOST_TEST(strm_ref.str() == strm_correct.str());
156 }
157
158 // Right alignment
159 {
160 ostream_type strm_ref;
161 strm_ref << ctx.begin << std::right << std::setw(8) << string_view_type(ctx.abcd) << ctx.end;
162
163 ostream_type strm_correct;
164 strm_correct << ctx.begin << std::right << std::setw(8) << ctx.abcd << ctx.end;
165
166 BOOST_TEST(strm_ref.str() == strm_correct.str());
167 }
168}
169
170template<class CharT>
171void test()
172{
173 test_string_view_output<CharT>();
174 test_padding<CharT>();
175 test_padding_fill<CharT>();
176 test_alignment<CharT>();
177}
178
179int main()
180{
181 test<char>();
182 test<wchar_t>();
183 return boost::report_errors();
184}