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

source code of boost/libs/tuple/test/io_test.cpp