1// Boost.TypeErasure library
2//
3// Copyright 2011 Steven Watanabe
4//
5// Distributed under the Boost Software License Version 1.0. (See
6// accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// $Id$
10
11#include <boost/type_erasure/any.hpp>
12#include <boost/type_erasure/tuple.hpp>
13#include <boost/type_erasure/builtin.hpp>
14#include <boost/type_erasure/operators.hpp>
15#include <boost/type_erasure/any_cast.hpp>
16#include <boost/type_erasure/tuple.hpp>
17#include <boost/mpl/vector.hpp>
18#include <sstream>
19
20#define BOOST_TEST_MAIN
21#include <boost/test/unit_test.hpp>
22
23using namespace boost::type_erasure;
24
25BOOST_AUTO_TEST_CASE(test_output_int)
26{
27 typedef ostreamable<_a, int> test_concept;
28 std::ostringstream ss;
29 any<test_concept, _a&> x(ss);
30 x << 17;
31 BOOST_CHECK_EQUAL(ss.str(), "17");
32}
33
34BOOST_AUTO_TEST_CASE(test_output_int_wide)
35{
36 typedef ostreamable<_a, int> test_concept;
37 std::wostringstream ss;
38 any<test_concept, _a&> x(ss);
39 x << 17;
40 BOOST_CHECK(ss.str() == L"17");
41}
42
43BOOST_AUTO_TEST_CASE(test_output_int_any)
44{
45 typedef boost::mpl::vector<ostreamable<>, copy_constructible<> > test_concept;
46 std::ostringstream ss;
47 any<test_concept> x(10);
48 ss << x;
49 BOOST_CHECK_EQUAL(ss.str(), "10");
50}
51
52BOOST_AUTO_TEST_CASE(test_output_int_any_wide)
53{
54 typedef boost::mpl::vector<ostreamable<std::wostream>, copy_constructible<> > test_concept;
55 std::wostringstream ss;
56 any<test_concept> x(10);
57 ss << x;
58 BOOST_CHECK(ss.str() == L"10");
59}
60
61BOOST_AUTO_TEST_CASE(test_output_both_any)
62{
63 typedef boost::mpl::vector<ostreamable<_a>, copy_constructible<> > test_concept;
64 std::ostringstream ss;
65 int val = 19;
66 tuple<test_concept, _a&, _self> t(ss, val);
67 get<0>(t) << get<1>(t);
68 BOOST_CHECK_EQUAL(ss.str(), "19");
69}
70
71BOOST_AUTO_TEST_CASE(test_output_both_any_wide)
72{
73 typedef boost::mpl::vector<ostreamable<_a>, copy_constructible<> > test_concept;
74 std::wostringstream ss;
75 int val = 19;
76 tuple<test_concept, _a&, _self> t(ss, val);
77 get<0>(t) << get<1>(t);
78 BOOST_CHECK(ss.str() == L"19");
79}
80
81BOOST_AUTO_TEST_CASE(test_output_overload_all)
82{
83 typedef boost::mpl::vector<
84 ostreamable<_a>,
85 ostreamable<_a, int>,
86 ostreamable<_b>,
87 ostreamable<_b, int>,
88 ostreamable<>,
89 ostreamable<std::wostream>,
90 copy_constructible<>
91 > test_concept;
92 {
93 std::ostringstream ss;
94 std::wostringstream wss;
95 int val = 2;
96 tuple<test_concept, _a&, _b&, _self> t(ss, wss, val);
97 get<0>(t) << get<2>(t);
98 get<1>(t) << get<2>(t);
99 BOOST_CHECK_EQUAL(ss.str(), "2");
100 BOOST_CHECK(wss.str() == L"2");
101 }
102 {
103 std::ostringstream ss;
104 std::wostringstream wss;
105 int val = 2;
106 tuple<test_concept, _a&, _b&, _self> t(ss, wss, val);
107 get<0>(t) << 3;
108 get<1>(t) << 3;
109 BOOST_CHECK_EQUAL(ss.str(), "3");
110 BOOST_CHECK(wss.str() == L"3");
111 }
112 {
113 std::ostringstream ss;
114 std::wostringstream wss;
115 int val = 5;
116 tuple<test_concept, _a&, _b&, _self> t(ss, wss, val);
117 ss << get<2>(t);
118 wss << get<2>(t);
119 BOOST_CHECK_EQUAL(ss.str(), "5");
120 BOOST_CHECK(wss.str() == L"5");
121 }
122 {
123 std::ostringstream ss;
124 std::wostringstream wss;
125 int val = 5;
126 tuple<test_concept, _a&, _b&, _self> t(ss, wss, val);
127 // we can't do anything with these, but it should
128 // still compile.
129 any<test_concept, const _a&> os(get<0>(t));
130 any<test_concept, const _b&> wos(get<1>(t));
131 }
132}
133
134
135BOOST_AUTO_TEST_CASE(test_input_int)
136{
137 typedef istreamable<_a, int> test_concept;
138 std::istringstream ss("17");
139 int i;
140 any<test_concept, _a&> x(ss);
141 x >> i;
142 BOOST_CHECK_EQUAL(i, 17);
143}
144
145BOOST_AUTO_TEST_CASE(test_input_int_wide)
146{
147 typedef istreamable<_a, int> test_concept;
148 std::wistringstream ss(L"17");
149 int i;
150 any<test_concept, _a&> x(ss);
151 x >> i;
152 BOOST_CHECK_EQUAL(i, 17);
153}
154
155BOOST_AUTO_TEST_CASE(test_input_int_any)
156{
157 typedef istreamable<> test_concept;
158 std::istringstream ss("10");
159 int i;
160 any<test_concept, _self&> x(i);
161 ss >> x;
162 BOOST_CHECK_EQUAL(i, 10);
163}
164
165BOOST_AUTO_TEST_CASE(test_input_int_any_wide)
166{
167 typedef istreamable<std::wistream> test_concept;
168 std::wistringstream ss(L"10");
169 int i;
170 any<test_concept, _self&> x(i);
171 ss >> x;
172 BOOST_CHECK_EQUAL(i, 10);
173}
174
175BOOST_AUTO_TEST_CASE(test_input_both_any)
176{
177 typedef istreamable<_a> test_concept;
178 std::istringstream ss("19");
179 int i;
180 tuple<test_concept, _a&, _self&> t(ss, i);
181 get<0>(t) >> get<1>(t);
182 BOOST_CHECK_EQUAL(i, 19);
183}
184
185BOOST_AUTO_TEST_CASE(test_input_both_any_wide)
186{
187 typedef istreamable<_a> test_concept;
188 std::wistringstream ss(L"19");
189 int i;
190 tuple<test_concept, _a&, _self&> t(ss, i);
191 get<0>(t) >> get<1>(t);
192 BOOST_CHECK_EQUAL(i, 19);
193}
194
195BOOST_AUTO_TEST_CASE(test_input_overload_all)
196{
197 typedef boost::mpl::vector<
198 istreamable<_a>,
199 istreamable<_a, int>,
200 istreamable<_b>,
201 istreamable<_b, int>,
202 istreamable<>,
203 istreamable<std::wistream>
204 > test_concept;
205 {
206 std::istringstream ss("2");
207 std::wistringstream wss(L"3");
208 int i = 0;
209 tuple<test_concept, _a&, _b&, _self&> t(ss, wss, i);
210 get<0>(t) >> get<2>(t);
211 BOOST_CHECK_EQUAL(i, 2);
212 get<1>(t) >> get<2>(t);
213 BOOST_CHECK_EQUAL(i, 3);
214 }
215 {
216 std::istringstream ss("5");
217 std::wistringstream wss(L"7");
218 int i = 0;
219 tuple<test_concept, _a&, _b&, _self&> t(ss, wss, i);
220 get<0>(t) >> i;
221 BOOST_CHECK_EQUAL(i, 5);
222 get<1>(t) >> i;
223 BOOST_CHECK_EQUAL(i, 7);
224 }
225 {
226 std::istringstream ss("11");
227 std::wistringstream wss(L"13");
228 int i = 0;
229 tuple<test_concept, _a&, _b&, _self&> t(ss, wss, i);
230 ss >> get<2>(t);
231 BOOST_CHECK_EQUAL(i, 11);
232 wss >> get<2>(t);
233 BOOST_CHECK_EQUAL(i, 13);
234 }
235 {
236 std::istringstream ss;
237 std::wistringstream wss;
238 int val = 5;
239 tuple<test_concept, _a&, _b&, _self&> t(ss, wss, val);
240 // we can't do anything with these, but it should
241 // still compile.
242 any<test_concept, const _a&> is(get<0>(t));
243 any<test_concept, const _b&> wis(get<1>(t));
244 }
245}
246

source code of boost/libs/type_erasure/test/test_stream.cpp