1 | // Copyright (C) 1999, 2000 Jaakko Järvi (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 | |
---|
24 | #if defined BOOST_NO_STRINGSTREAM |
---|
25 | #include <strstream> |
---|
26 | #else |
---|
27 | #include <sstream> |
---|
28 | #endif |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | using namespace boost; |
---|
32 | |
---|
33 | #if defined BOOST_NO_STRINGSTREAM |
---|
34 | typedef ostrstream useThisOStringStream; |
---|
35 | typedef istrstream useThisIStringStream; |
---|
36 | #else |
---|
37 | typedef ostringstream useThisOStringStream; |
---|
38 | typedef 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 | ofstream tmp("temp.tmp"); |
---|
74 | |
---|
75 | #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) |
---|
76 | tmp << make_tuple("One", "Two", 3); |
---|
77 | #endif |
---|
78 | tmp << set_delimiter(':'); |
---|
79 | tmp << make_tuple(1000, 2000, 3000) << endl; |
---|
80 | |
---|
81 | tmp.close(); |
---|
82 | |
---|
83 | // When teading tuples from a stream, manipulators must be set correctly: |
---|
84 | ifstream tmp3("temp.tmp"); |
---|
85 | tuple<string, string, int> j; |
---|
86 | |
---|
87 | #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) |
---|
88 | tmp3 >> j; |
---|
89 | BOOST_CHECK (tmp3.good() ); |
---|
90 | #endif |
---|
91 | |
---|
92 | tmp3 >> set_delimiter(':'); |
---|
93 | tuple<int, int, int> i; |
---|
94 | tmp3 >> i; |
---|
95 | BOOST_CHECK (tmp3.good() ); |
---|
96 | |
---|
97 | tmp3.close(); |
---|
98 | |
---|
99 | |
---|
100 | // reading tuple<int, int, int> in format (a b c); |
---|
101 | useThisIStringStream is("(100 200 300)"); |
---|
102 | |
---|
103 | tuple<int, int, int> ti; |
---|
104 | BOOST_CHECK(bool(is >> ti)); |
---|
105 | BOOST_CHECK(ti == make_tuple(100, 200, 300)); |
---|
106 | |
---|
107 | |
---|
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 |
---|
111 | // should be used. |
---|
112 | |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|