Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/spirit/fusion/test/io_tests.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.3 KB
Line 
1/*=============================================================================
2    Copyright (C) 1999-2003 Jaakko Järvi
3
4    Use, modification and distribution is subject to the Boost Software
5    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6    http://www.boost.org/LICENSE_1_0.txt)
7==============================================================================*/
8#include <boost/detail/lightweight_test.hpp>
9#include <boost/spirit/fusion/sequence/tuple.hpp>
10#include <boost/spirit/fusion/sequence/make_tuple.hpp>
11#include <boost/spirit/fusion/sequence/equal_to.hpp>
12#include <boost/spirit/fusion/sequence/io.hpp>
13
14#include <fstream>
15#include <iterator>
16#include <algorithm>
17#include <string>
18
19#if defined BOOST_NO_STRINGSTREAM
20# include <strstream>
21#else
22# include <sstream>
23#endif
24
25using boost::fusion::tuple;
26using boost::fusion::make_tuple;
27using boost::fusion::tuple_close;
28using boost::fusion::tuple_open;
29using boost::fusion::tuple_delimiter;
30
31#if defined BOOST_NO_STRINGSTREAM
32  using std::ostrstream;
33  using std::istrstream;
34  typedef ostrstream useThisOStringStream;
35  typedef istrstream useThisIStringStream;
36#else
37  using std::ostringstream;
38  using std::istringstream;
39  typedef ostringstream useThisOStringStream;
40  typedef istringstream useThisIStringStream;
41#endif
42
43using std::endl;
44using std::ofstream;
45using std::ifstream;
46using std::string;
47
48int
49main()
50{
51    using boost::fusion::tuple_close;
52    using boost::fusion::tuple_open;
53    using boost::fusion::tuple_delimiter;
54
55    useThisOStringStream os1;
56
57    // Set format [a, b, c] for os1
58    os1 << tuple_open('[');
59    os1 << tuple_close(']');
60    os1 << tuple_delimiter(',');
61    os1 << make_tuple(1, 2, 3);
62
63    BOOST_TEST (os1.str() == std::string("[1,2,3]") );
64
65    {
66        useThisOStringStream os2;
67        // Set format (a:b:c) for os2;
68        os2 << tuple_open('(');
69        os2 << tuple_close(')');
70        os2 << tuple_delimiter(':');
71
72        // this code now works with VC6/7
73        os2 << make_tuple("TUPU", "HUPU", "LUPU", 4.5);
74        BOOST_TEST (os2.str() == std::string("(TUPU:HUPU:LUPU:4.5)") );
75    }
76
77    // The format is still [a, b, c] for os1
78    os1 << make_tuple(1, 2, 3);
79    BOOST_TEST (os1.str() == std::string("[1,2,3][1,2,3]") );
80
81    std::ofstream tmp("temp.tmp");
82
83#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
84    tmp << make_tuple("One", "Two", 3);
85#endif
86    tmp << tuple_delimiter(':');
87    tmp << make_tuple(1000, 2000, 3000) << endl;
88
89    tmp.close();
90
91    // When teading tuples from a stream, manipulators must be set correctly:
92    ifstream tmp3("temp.tmp");
93    tuple<string, string, int> j;
94
95#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
96    tmp3 >> j;
97    BOOST_TEST (tmp3.good() );
98#endif
99
100    tmp3 >> tuple_delimiter(':');
101    tuple<int, int, int> i;
102    tmp3 >> i;
103    BOOST_TEST (tmp3.good() );
104
105    tmp3.close();
106
107    // reading tuple<int, int, int> in format (a b c);
108    useThisIStringStream is("(100 200 300)");
109
110    tuple<int, int, int> ti;
111    BOOST_TEST(bool((is >> ti) != 0));
112    BOOST_TEST(ti == make_tuple(100, 200, 300));
113
114    // Note that strings are problematic:
115    // writing a tuple on a stream and reading it back doesn't work in
116    // general. If this is wanted, some kind of a parseable string class
117    // should be used.
118
119    return boost::report_errors();
120}
121
Note: See TracBrowser for help on using the repository browser.