2008-06-28 13:57:20 +00:00
|
|
|
// Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
|
2003-02-04 11:54:01 +00:00
|
|
|
//
|
2004-07-27 03:43:34 +00:00
|
|
|
// 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)
|
2003-02-04 11:54:01 +00:00
|
|
|
|
|
|
|
// For more information, see http://www.boost.org
|
|
|
|
|
2001-08-10 11:48:57 +00:00
|
|
|
// -- io_test.cpp -----------------------------------------------
|
|
|
|
//
|
|
|
|
// Testing the I/O facilities of tuples
|
|
|
|
|
2023-09-28 21:49:27 +03:00
|
|
|
#define _CRT_SECURE_NO_WARNINGS // std::tmpnam
|
|
|
|
|
2001-08-10 11:48:57 +00:00
|
|
|
#include "boost/tuple/tuple_io.hpp"
|
|
|
|
#include "boost/tuple/tuple_comparison.hpp"
|
|
|
|
|
2018-01-03 18:41:14 +02:00
|
|
|
#include "boost/core/lightweight_test.hpp"
|
|
|
|
|
2001-08-10 11:48:57 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
2011-04-19 20:10:43 +00:00
|
|
|
#include <iomanip>
|
2023-09-28 21:49:27 +03:00
|
|
|
#include <cstdio>
|
2001-08-10 11:48:57 +00:00
|
|
|
|
|
|
|
#if defined BOOST_NO_STRINGSTREAM
|
|
|
|
#include <strstream>
|
|
|
|
#else
|
|
|
|
#include <sstream>
|
|
|
|
#endif
|
|
|
|
|
2018-01-03 18:41:14 +02:00
|
|
|
#define BOOST_CHECK BOOST_TEST
|
|
|
|
|
2001-08-10 11:48:57 +00:00
|
|
|
using namespace boost;
|
|
|
|
|
|
|
|
#if defined BOOST_NO_STRINGSTREAM
|
2009-06-13 18:30:31 +00:00
|
|
|
typedef std::ostrstream useThisOStringStream;
|
|
|
|
typedef std::istrstream useThisIStringStream;
|
2001-08-10 11:48:57 +00:00
|
|
|
#else
|
2009-06-13 18:30:31 +00:00
|
|
|
typedef std::ostringstream useThisOStringStream;
|
|
|
|
typedef std::istringstream useThisIStringStream;
|
2001-08-10 11:48:57 +00:00
|
|
|
#endif
|
|
|
|
|
2018-01-03 18:41:14 +02:00
|
|
|
int main() {
|
2001-09-14 07:55:25 +00:00
|
|
|
using boost::tuples::set_close;
|
|
|
|
using boost::tuples::set_open;
|
|
|
|
using boost::tuples::set_delimiter;
|
|
|
|
|
2001-08-10 11:48:57 +00:00
|
|
|
useThisOStringStream os1;
|
|
|
|
|
|
|
|
// Set format [a, b, c] for os1
|
|
|
|
os1 << set_open('[');
|
|
|
|
os1 << set_close(']');
|
|
|
|
os1 << set_delimiter(',');
|
|
|
|
os1 << make_tuple(1, 2, 3);
|
2005-02-03 13:55:45 +00:00
|
|
|
BOOST_CHECK (os1.str() == std::string("[1,2,3]") );
|
2001-08-10 11:48:57 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
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);
|
2005-02-03 13:55:45 +00:00
|
|
|
BOOST_CHECK (os2.str() == std::string("(TUPU:HUPU:LUPU:4.5)") );
|
2001-08-10 11:48:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// The format is still [a, b, c] for os1
|
|
|
|
os1 << make_tuple(1, 2, 3);
|
2005-02-03 13:55:45 +00:00
|
|
|
BOOST_CHECK (os1.str() == std::string("[1,2,3][1,2,3]") );
|
2001-08-10 11:48:57 +00:00
|
|
|
|
2009-06-13 18:30:31 +00:00
|
|
|
// 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("()[]") );
|
2011-04-19 20:10:43 +00:00
|
|
|
|
|
|
|
// check width
|
|
|
|
useThisOStringStream os4;
|
|
|
|
os4 << std::setw(10) << make_tuple(1, 2, 3);
|
|
|
|
BOOST_CHECK (os4.str() == std::string(" (1 2 3)") );
|
2009-06-13 18:30:31 +00:00
|
|
|
|
2023-09-28 21:49:27 +03:00
|
|
|
std::string fn = std::tmpnam( 0 );
|
|
|
|
|
|
|
|
std::ofstream tmp( fn.c_str() );
|
2001-08-10 11:48:57 +00:00
|
|
|
|
|
|
|
#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
|
|
|
tmp << make_tuple("One", "Two", 3);
|
|
|
|
#endif
|
|
|
|
tmp << set_delimiter(':');
|
2009-06-13 18:30:31 +00:00
|
|
|
tmp << make_tuple(1000, 2000, 3000) << std::endl;
|
2001-08-10 11:48:57 +00:00
|
|
|
|
|
|
|
tmp.close();
|
|
|
|
|
|
|
|
// When teading tuples from a stream, manipulators must be set correctly:
|
2023-09-28 21:49:27 +03:00
|
|
|
std::ifstream tmp3( fn.c_str() );
|
2009-06-13 18:30:31 +00:00
|
|
|
tuple<std::string, std::string, int> j;
|
2001-08-10 11:48:57 +00:00
|
|
|
|
|
|
|
#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
|
|
|
tmp3 >> j;
|
2005-02-03 13:55:45 +00:00
|
|
|
BOOST_CHECK (tmp3.good() );
|
2001-08-10 11:48:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
tmp3 >> set_delimiter(':');
|
|
|
|
tuple<int, int, int> i;
|
|
|
|
tmp3 >> i;
|
2005-02-03 13:55:45 +00:00
|
|
|
BOOST_CHECK (tmp3.good() );
|
2001-08-10 11:48:57 +00:00
|
|
|
|
|
|
|
tmp3.close();
|
|
|
|
|
2023-09-28 21:49:27 +03:00
|
|
|
std::remove( fn.c_str() );
|
2001-08-10 11:48:57 +00:00
|
|
|
|
|
|
|
// reading tuple<int, int, int> in format (a b c);
|
2009-06-13 18:30:31 +00:00
|
|
|
useThisIStringStream is1("(100 200 300)");
|
2001-08-10 11:48:57 +00:00
|
|
|
|
2009-06-13 18:30:31 +00:00
|
|
|
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));
|
2001-08-10 11:48:57 +00:00
|
|
|
|
2011-01-02 20:25:11 +00:00
|
|
|
// 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));
|
|
|
|
|
2001-08-10 11:48:57 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-01-03 18:41:14 +02:00
|
|
|
return boost::report_errors();
|
2001-08-10 11:48:57 +00:00
|
|
|
}
|