io_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // For more information, see http://www.boost.org
  7. // -- io_test.cpp -----------------------------------------------
  8. //
  9. // Testing the I/O facilities of tuples
  10. #define BOOST_INCLUDE_MAIN // for testing, include rather than link
  11. #include "boost/test/test_tools.hpp" // see "Header Implementation Option"
  12. #include "boost/tuple/tuple_io.hpp"
  13. #include "boost/tuple/tuple_comparison.hpp"
  14. #include <fstream>
  15. #include <iterator>
  16. #include <algorithm>
  17. #include <string>
  18. #include <iomanip>
  19. #if defined BOOST_NO_STRINGSTREAM
  20. #include <strstream>
  21. #else
  22. #include <sstream>
  23. #endif
  24. using namespace boost;
  25. #if defined BOOST_NO_STRINGSTREAM
  26. typedef std::ostrstream useThisOStringStream;
  27. typedef std::istrstream useThisIStringStream;
  28. #else
  29. typedef std::ostringstream useThisOStringStream;
  30. typedef std::istringstream useThisIStringStream;
  31. #endif
  32. int test_main(int argc, char * argv[] ) {
  33. (void)argc;
  34. (void)argv;
  35. using boost::tuples::set_close;
  36. using boost::tuples::set_open;
  37. using boost::tuples::set_delimiter;
  38. useThisOStringStream os1;
  39. // Set format [a, b, c] for os1
  40. os1 << set_open('[');
  41. os1 << set_close(']');
  42. os1 << set_delimiter(',');
  43. os1 << make_tuple(1, 2, 3);
  44. BOOST_CHECK (os1.str() == std::string("[1,2,3]") );
  45. {
  46. useThisOStringStream os2;
  47. // Set format (a:b:c) for os2;
  48. os2 << set_open('(');
  49. os2 << set_close(')');
  50. os2 << set_delimiter(':');
  51. #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  52. os2 << make_tuple("TUPU", "HUPU", "LUPU", 4.5);
  53. BOOST_CHECK (os2.str() == std::string("(TUPU:HUPU:LUPU:4.5)") );
  54. #endif
  55. }
  56. // The format is still [a, b, c] for os1
  57. os1 << make_tuple(1, 2, 3);
  58. BOOST_CHECK (os1.str() == std::string("[1,2,3][1,2,3]") );
  59. // check empty tuple.
  60. useThisOStringStream os3;
  61. os3 << make_tuple();
  62. BOOST_CHECK (os3.str() == std::string("()") );
  63. os3 << set_open('[');
  64. os3 << set_close(']');
  65. os3 << make_tuple();
  66. BOOST_CHECK (os3.str() == std::string("()[]") );
  67. // check width
  68. useThisOStringStream os4;
  69. os4 << std::setw(10) << make_tuple(1, 2, 3);
  70. BOOST_CHECK (os4.str() == std::string(" (1 2 3)") );
  71. std::ofstream tmp("temp.tmp");
  72. #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  73. tmp << make_tuple("One", "Two", 3);
  74. #endif
  75. tmp << set_delimiter(':');
  76. tmp << make_tuple(1000, 2000, 3000) << std::endl;
  77. tmp.close();
  78. // When teading tuples from a stream, manipulators must be set correctly:
  79. std::ifstream tmp3("temp.tmp");
  80. tuple<std::string, std::string, int> j;
  81. #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  82. tmp3 >> j;
  83. BOOST_CHECK (tmp3.good() );
  84. #endif
  85. tmp3 >> set_delimiter(':');
  86. tuple<int, int, int> i;
  87. tmp3 >> i;
  88. BOOST_CHECK (tmp3.good() );
  89. tmp3.close();
  90. // reading tuple<int, int, int> in format (a b c);
  91. useThisIStringStream is1("(100 200 300)");
  92. tuple<int, int, int> ti1;
  93. BOOST_CHECK(bool(is1 >> ti1));
  94. BOOST_CHECK(ti1 == make_tuple(100, 200, 300));
  95. useThisIStringStream is2("()");
  96. tuple<> ti2;
  97. BOOST_CHECK(bool(is2 >> ti2));
  98. useThisIStringStream is3("[]");
  99. is3 >> set_open('[');
  100. is3 >> set_close(']');
  101. BOOST_CHECK(bool(is3 >> ti2));
  102. // Make sure that whitespace between elements
  103. // is skipped.
  104. useThisIStringStream is4("(100 200 300)");
  105. BOOST_CHECK(bool(is4 >> std::noskipws >> ti1));
  106. BOOST_CHECK(ti1 == make_tuple(100, 200, 300));
  107. // Note that strings are problematic:
  108. // writing a tuple on a stream and reading it back doesn't work in
  109. // general. If this is wanted, some kind of a parseable string class
  110. // should be used.
  111. return 0;
  112. }