1 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
2 //
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)
6 
7 // For more information, see http://www.boost.org
8 
9 // -- io_test.cpp -----------------------------------------------
10 //
11 // Testing the I/O facilities of tuples
12 
13 #include "boost/tuple/tuple_io.hpp"
14 #include "boost/tuple/tuple_comparison.hpp"
15 
16 #include "boost/core/lightweight_test.hpp"
17 
18 #include <fstream>
19 #include <iterator>
20 #include <algorithm>
21 #include <string>
22 #include <iomanip>
23 
24 #if defined BOOST_NO_STRINGSTREAM
25 #include <strstream>
26 #else
27 #include <sstream>
28 #endif
29 
30 #define BOOST_CHECK BOOST_TEST
31 
32 using namespace boost;
33 
34 #if defined BOOST_NO_STRINGSTREAM
35 typedef std::ostrstream useThisOStringStream;
36 typedef std::istrstream useThisIStringStream;
37 #else
38 typedef std::ostringstream useThisOStringStream;
39 typedef std::istringstream useThisIStringStream;
40 #endif
41 
main()42 int main() {
43    using boost::tuples::set_close;
44    using boost::tuples::set_open;
45    using boost::tuples::set_delimiter;
46 
47   useThisOStringStream os1;
48 
49   // Set format [a, b, c] for os1
50   os1 << set_open('[');
51   os1 << set_close(']');
52   os1 << set_delimiter(',');
53   os1 << make_tuple(1, 2, 3);
54   BOOST_CHECK (os1.str() == std::string("[1,2,3]") );
55 
56   {
57   useThisOStringStream os2;
58   // Set format (a:b:c) for os2;
59   os2 << set_open('(');
60   os2 << set_close(')');
61   os2 << set_delimiter(':');
62 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
63   os2 << make_tuple("TUPU", "HUPU", "LUPU", 4.5);
64   BOOST_CHECK (os2.str() == std::string("(TUPU:HUPU:LUPU:4.5)") );
65 #endif
66   }
67 
68   // The format is still [a, b, c] for os1
69   os1 << make_tuple(1, 2, 3);
70   BOOST_CHECK (os1.str() == std::string("[1,2,3][1,2,3]") );
71 
72   // check empty tuple.
73   useThisOStringStream os3;
74   os3 << make_tuple();
75   BOOST_CHECK (os3.str() == std::string("()") );
76   os3 << set_open('[');
77   os3 << set_close(']');
78   os3 << make_tuple();
79   BOOST_CHECK (os3.str() == std::string("()[]") );
80 
81   // check width
82   useThisOStringStream os4;
83   os4 << std::setw(10) << make_tuple(1, 2, 3);
84   BOOST_CHECK (os4.str() == std::string("   (1 2 3)") );
85 
86   std::ofstream tmp("temp.tmp");
87 
88 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
89   tmp << make_tuple("One", "Two", 3);
90 #endif
91   tmp << set_delimiter(':');
92   tmp << make_tuple(1000, 2000, 3000) << std::endl;
93 
94   tmp.close();
95 
96   // When teading tuples from a stream, manipulators must be set correctly:
97   std::ifstream tmp3("temp.tmp");
98   tuple<std::string, std::string, int> j;
99 
100 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
101   tmp3 >> j;
102   BOOST_CHECK (tmp3.good() );
103 #endif
104 
105   tmp3 >> set_delimiter(':');
106   tuple<int, int, int> i;
107   tmp3 >> i;
108   BOOST_CHECK (tmp3.good() );
109 
110   tmp3.close();
111 
112 
113   // reading tuple<int, int, int> in format (a b c);
114   useThisIStringStream is1("(100 200 300)");
115 
116   tuple<int, int, int> ti1;
117   BOOST_CHECK(bool(is1 >> ti1));
118   BOOST_CHECK(ti1 == make_tuple(100, 200, 300));
119 
120   useThisIStringStream is2("()");
121   tuple<> ti2;
122   BOOST_CHECK(bool(is2 >> ti2));
123   useThisIStringStream is3("[]");
124   is3 >> set_open('[');
125   is3 >> set_close(']');
126   BOOST_CHECK(bool(is3 >> ti2));
127 
128   // Make sure that whitespace between elements
129   // is skipped.
130   useThisIStringStream is4("(100 200 300)");
131 
132   BOOST_CHECK(bool(is4 >> std::noskipws >> ti1));
133   BOOST_CHECK(ti1 == make_tuple(100, 200, 300));
134 
135   // Note that strings are problematic:
136   // writing a tuple on a stream and reading it back doesn't work in
137   // general. If this is wanted, some kind of a parseable string class
138   // should be used.
139 
140   return boost::report_errors();
141 }
142