File: io_test.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (141 lines) | stat: -rw-r--r-- 3,703 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// For more information, see http://www.boost.org

// -- io_test.cpp -----------------------------------------------
//
// Testing the I/O facilities of tuples

#include "boost/tuple/tuple_io.hpp"
#include "boost/tuple/tuple_comparison.hpp"

#include "boost/core/lightweight_test.hpp"

#include <fstream>
#include <iterator>
#include <algorithm>
#include <string>
#include <iomanip>

#if defined BOOST_NO_STRINGSTREAM
#include <strstream>
#else
#include <sstream>
#endif

#define BOOST_CHECK BOOST_TEST

using namespace boost;

#if defined BOOST_NO_STRINGSTREAM
typedef std::ostrstream useThisOStringStream;
typedef std::istrstream useThisIStringStream;
#else
typedef std::ostringstream useThisOStringStream;
typedef std::istringstream useThisIStringStream;
#endif

int main() {
   using boost::tuples::set_close;
   using boost::tuples::set_open;
   using boost::tuples::set_delimiter;
   
  useThisOStringStream os1;

  // Set format [a, b, c] for os1
  os1 << set_open('[');
  os1 << set_close(']');
  os1 << set_delimiter(',');
  os1 << make_tuple(1, 2, 3);
  BOOST_CHECK (os1.str() == std::string("[1,2,3]") );

  {
  useThisOStringStream os2;
  // Set format (a:b:c) for os2; 
  os2 << set_open('(');
  os2 << set_close(')');
  os2 << set_delimiter(':');
#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  os2 << make_tuple("TUPU", "HUPU", "LUPU", 4.5);
  BOOST_CHECK (os2.str() == std::string("(TUPU:HUPU:LUPU:4.5)") );
#endif
  }

  // The format is still [a, b, c] for os1
  os1 << make_tuple(1, 2, 3);
  BOOST_CHECK (os1.str() == std::string("[1,2,3][1,2,3]") );

  // check empty tuple.
  useThisOStringStream os3;
  os3 << make_tuple();
  BOOST_CHECK (os3.str() == std::string("()") );
  os3 << set_open('[');
  os3 << set_close(']');
  os3 << make_tuple();
  BOOST_CHECK (os3.str() == std::string("()[]") );
  
  // check width
  useThisOStringStream os4;
  os4 << std::setw(10) << make_tuple(1, 2, 3);
  BOOST_CHECK (os4.str() == std::string("   (1 2 3)") );

  std::ofstream tmp("temp.tmp");

#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  tmp << make_tuple("One", "Two", 3);
#endif   
  tmp << set_delimiter(':');
  tmp << make_tuple(1000, 2000, 3000) << std::endl;

  tmp.close();
  
  // When teading tuples from a stream, manipulators must be set correctly:
  std::ifstream tmp3("temp.tmp");
  tuple<std::string, std::string, int> j;

#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  tmp3 >> j; 
  BOOST_CHECK (tmp3.good() ); 
#endif
   
  tmp3 >> set_delimiter(':');
  tuple<int, int, int> i;
  tmp3 >> i; 
  BOOST_CHECK (tmp3.good() ); 
   
  tmp3.close(); 


  // reading tuple<int, int, int> in format (a b c); 
  useThisIStringStream is1("(100 200 300)"); 
   
  tuple<int, int, int> ti1; 
  BOOST_CHECK(bool(is1 >> ti1));
  BOOST_CHECK(ti1 == make_tuple(100, 200, 300));

  useThisIStringStream is2("()");
  tuple<> ti2;
  BOOST_CHECK(bool(is2 >> ti2));
  useThisIStringStream is3("[]");
  is3 >> set_open('[');
  is3 >> set_close(']');
  BOOST_CHECK(bool(is3 >> ti2));

  // Make sure that whitespace between elements
  // is skipped.
  useThisIStringStream is4("(100 200 300)"); 
   
  BOOST_CHECK(bool(is4 >> std::noskipws >> ti1));
  BOOST_CHECK(ti1 == make_tuple(100, 200, 300));

  // Note that strings are problematic:
  // writing a tuple on a stream and reading it back doesn't work in
  // general. If this is wanted, some kind of a parseable string class
  // should be used.
  
  return boost::report_errors();
}