1 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // For more information, see http://www.boost.org
9 // -- io_test.cpp -----------------------------------------------
11 // Testing the I/O facilities of tuples
13 #define BOOST_INCLUDE_MAIN // for testing, include rather than link
14 #include "boost/test/test_tools.hpp" // see "Header Implementation Option"
16 #include "boost/tuple/tuple_io.hpp"
17 #include "boost/tuple/tuple_comparison.hpp"
25 #if defined BOOST_NO_STRINGSTREAM
31 using namespace boost
;
33 #if defined BOOST_NO_STRINGSTREAM
34 typedef std::ostrstream useThisOStringStream
;
35 typedef std::istrstream useThisIStringStream
;
37 typedef std::ostringstream useThisOStringStream
;
38 typedef std::istringstream useThisIStringStream
;
41 int test_main(int argc
, char * argv
[] ) {
44 using boost::tuples::set_close
;
45 using boost::tuples::set_open
;
46 using boost::tuples::set_delimiter
;
48 useThisOStringStream os1
;
50 // Set format [a, b, c] for os1
52 os1
<< set_close(']');
53 os1
<< set_delimiter(',');
54 os1
<< make_tuple(1, 2, 3);
55 BOOST_CHECK (os1
.str() == std::string("[1,2,3]") );
58 useThisOStringStream os2
;
59 // Set format (a:b:c) for os2;
61 os2
<< set_close(')');
62 os2
<< set_delimiter(':');
63 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
64 os2
<< make_tuple("TUPU", "HUPU", "LUPU", 4.5);
65 BOOST_CHECK (os2
.str() == std::string("(TUPU:HUPU:LUPU:4.5)") );
69 // The format is still [a, b, c] for os1
70 os1
<< make_tuple(1, 2, 3);
71 BOOST_CHECK (os1
.str() == std::string("[1,2,3][1,2,3]") );
74 useThisOStringStream os3
;
76 BOOST_CHECK (os3
.str() == std::string("()") );
78 os3
<< set_close(']');
80 BOOST_CHECK (os3
.str() == std::string("()[]") );
83 useThisOStringStream os4
;
84 os4
<< std::setw(10) << make_tuple(1, 2, 3);
85 BOOST_CHECK (os4
.str() == std::string(" (1 2 3)") );
87 std::ofstream
tmp("temp.tmp");
89 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
90 tmp
<< make_tuple("One", "Two", 3);
92 tmp
<< set_delimiter(':');
93 tmp
<< make_tuple(1000, 2000, 3000) << std::endl
;
97 // When teading tuples from a stream, manipulators must be set correctly:
98 std::ifstream
tmp3("temp.tmp");
99 tuple
<std::string
, std::string
, int> j
;
101 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
103 BOOST_CHECK (tmp3
.good() );
106 tmp3
>> set_delimiter(':');
107 tuple
<int, int, int> i
;
109 BOOST_CHECK (tmp3
.good() );
114 // reading tuple<int, int, int> in format (a b c);
115 useThisIStringStream
is1("(100 200 300)");
117 tuple
<int, int, int> ti1
;
118 BOOST_CHECK(bool(is1
>> ti1
));
119 BOOST_CHECK(ti1
== make_tuple(100, 200, 300));
121 useThisIStringStream
is2("()");
123 BOOST_CHECK(bool(is2
>> ti2
));
124 useThisIStringStream
is3("[]");
125 is3
>> set_open('[');
126 is3
>> set_close(']');
127 BOOST_CHECK(bool(is3
>> ti2
));
129 // Make sure that whitespace between elements
131 useThisIStringStream
is4("(100 200 300)");
133 BOOST_CHECK(bool(is4
>> std::noskipws
>> ti1
));
134 BOOST_CHECK(ti1
== make_tuple(100, 200, 300));
136 // Note that strings are problematic:
137 // writing a tuple on a stream and reading it back doesn't work in
138 // general. If this is wanted, some kind of a parseable string class