1 // Copyright (C) 1999, 2000 Jaakko Järvi (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"
24 #if defined BOOST_NO_STRINGSTREAM
31 using namespace boost
;
33 #if defined BOOST_NO_STRINGSTREAM
34 typedef ostrstream useThisOStringStream
;
35 typedef istrstream useThisIStringStream
;
37 typedef ostringstream useThisOStringStream
;
38 typedef 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]") );
73 ofstream
tmp("temp.tmp");
75 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
76 tmp
<< make_tuple("One", "Two", 3);
78 tmp
<< set_delimiter(':');
79 tmp
<< make_tuple(1000, 2000, 3000) << endl
;
83 // When teading tuples from a stream, manipulators must be set correctly:
84 ifstream
tmp3("temp.tmp");
85 tuple
<string
, string
, int> j
;
87 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
89 BOOST_CHECK (tmp3
.good() );
92 tmp3
>> set_delimiter(':');
93 tuple
<int, int, int> i
;
95 BOOST_CHECK (tmp3
.good() );
100 // reading tuple<int, int, int> in format (a b c);
101 useThisIStringStream
is("(100 200 300)");
103 tuple
<int, int, int> ti
;
104 BOOST_CHECK(bool(is
>> ti
));
105 BOOST_CHECK(ti
== make_tuple(100, 200, 300));
108 // Note that strings are problematic:
109 // writing a tuple on a stream and reading it back doesn't work in
110 // general. If this is wanted, some kind of a parseable string class