]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/tuple/test/io_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / tuple / test / io_test.cpp
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 BOOST_INCLUDE_MAIN // for testing, include rather than link
14 #include "boost/test/test_tools.hpp" // see "Header Implementation Option"
15
16 #include "boost/tuple/tuple_io.hpp"
17 #include "boost/tuple/tuple_comparison.hpp"
18
19 #include <fstream>
20 #include <iterator>
21 #include <algorithm>
22 #include <string>
23 #include <iomanip>
24
25 #if defined BOOST_NO_STRINGSTREAM
26 #include <strstream>
27 #else
28 #include <sstream>
29 #endif
30
31 using namespace boost;
32
33 #if defined BOOST_NO_STRINGSTREAM
34 typedef std::ostrstream useThisOStringStream;
35 typedef std::istrstream useThisIStringStream;
36 #else
37 typedef std::ostringstream useThisOStringStream;
38 typedef std::istringstream useThisIStringStream;
39 #endif
40
41 int test_main(int argc, char * argv[] ) {
42 (void)argc;
43 (void)argv;
44 using boost::tuples::set_close;
45 using boost::tuples::set_open;
46 using boost::tuples::set_delimiter;
47
48 useThisOStringStream os1;
49
50 // Set format [a, b, c] for os1
51 os1 << set_open('[');
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]") );
56
57 {
58 useThisOStringStream os2;
59 // Set format (a:b:c) for os2;
60 os2 << set_open('(');
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)") );
66 #endif
67 }
68
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]") );
72
73 // check empty tuple.
74 useThisOStringStream os3;
75 os3 << make_tuple();
76 BOOST_CHECK (os3.str() == std::string("()") );
77 os3 << set_open('[');
78 os3 << set_close(']');
79 os3 << make_tuple();
80 BOOST_CHECK (os3.str() == std::string("()[]") );
81
82 // check width
83 useThisOStringStream os4;
84 os4 << std::setw(10) << make_tuple(1, 2, 3);
85 BOOST_CHECK (os4.str() == std::string(" (1 2 3)") );
86
87 std::ofstream tmp("temp.tmp");
88
89 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
90 tmp << make_tuple("One", "Two", 3);
91 #endif
92 tmp << set_delimiter(':');
93 tmp << make_tuple(1000, 2000, 3000) << std::endl;
94
95 tmp.close();
96
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;
100
101 #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
102 tmp3 >> j;
103 BOOST_CHECK (tmp3.good() );
104 #endif
105
106 tmp3 >> set_delimiter(':');
107 tuple<int, int, int> i;
108 tmp3 >> i;
109 BOOST_CHECK (tmp3.good() );
110
111 tmp3.close();
112
113
114 // reading tuple<int, int, int> in format (a b c);
115 useThisIStringStream is1("(100 200 300)");
116
117 tuple<int, int, int> ti1;
118 BOOST_CHECK(bool(is1 >> ti1));
119 BOOST_CHECK(ti1 == make_tuple(100, 200, 300));
120
121 useThisIStringStream is2("()");
122 tuple<> ti2;
123 BOOST_CHECK(bool(is2 >> ti2));
124 useThisIStringStream is3("[]");
125 is3 >> set_open('[');
126 is3 >> set_close(']');
127 BOOST_CHECK(bool(is3 >> ti2));
128
129 // Make sure that whitespace between elements
130 // is skipped.
131 useThisIStringStream is4("(100 200 300)");
132
133 BOOST_CHECK(bool(is4 >> std::noskipws >> ti1));
134 BOOST_CHECK(ti1 == make_tuple(100, 200, 300));
135
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
139 // should be used.
140
141 return 0;
142 }
143