test_stream.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 Steven Watanabe
  4. //
  5. // Distributed under the Boost Software License Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // $Id$
  10. #include <boost/type_erasure/any.hpp>
  11. #include <boost/type_erasure/tuple.hpp>
  12. #include <boost/type_erasure/builtin.hpp>
  13. #include <boost/type_erasure/operators.hpp>
  14. #include <boost/type_erasure/any_cast.hpp>
  15. #include <boost/type_erasure/tuple.hpp>
  16. #include <boost/mpl/vector.hpp>
  17. #include <sstream>
  18. #define BOOST_TEST_MAIN
  19. #include <boost/test/unit_test.hpp>
  20. using namespace boost::type_erasure;
  21. BOOST_AUTO_TEST_CASE(test_output_int)
  22. {
  23. typedef ostreamable<_a, int> test_concept;
  24. std::ostringstream ss;
  25. any<test_concept, _a&> x(ss);
  26. x << 17;
  27. BOOST_CHECK_EQUAL(ss.str(), "17");
  28. }
  29. BOOST_AUTO_TEST_CASE(test_output_int_wide)
  30. {
  31. typedef ostreamable<_a, int> test_concept;
  32. std::wostringstream ss;
  33. any<test_concept, _a&> x(ss);
  34. x << 17;
  35. BOOST_CHECK(ss.str() == L"17");
  36. }
  37. BOOST_AUTO_TEST_CASE(test_output_int_any)
  38. {
  39. typedef boost::mpl::vector<ostreamable<>, copy_constructible<> > test_concept;
  40. std::ostringstream ss;
  41. any<test_concept> x(10);
  42. ss << x;
  43. BOOST_CHECK_EQUAL(ss.str(), "10");
  44. }
  45. BOOST_AUTO_TEST_CASE(test_output_int_any_wide)
  46. {
  47. typedef boost::mpl::vector<ostreamable<std::wostream>, copy_constructible<> > test_concept;
  48. std::wostringstream ss;
  49. any<test_concept> x(10);
  50. ss << x;
  51. BOOST_CHECK(ss.str() == L"10");
  52. }
  53. BOOST_AUTO_TEST_CASE(test_output_both_any)
  54. {
  55. typedef boost::mpl::vector<ostreamable<_a>, copy_constructible<> > test_concept;
  56. std::ostringstream ss;
  57. int val = 19;
  58. tuple<test_concept, _a&, _self> t(ss, val);
  59. get<0>(t) << get<1>(t);
  60. BOOST_CHECK_EQUAL(ss.str(), "19");
  61. }
  62. BOOST_AUTO_TEST_CASE(test_output_both_any_wide)
  63. {
  64. typedef boost::mpl::vector<ostreamable<_a>, copy_constructible<> > test_concept;
  65. std::wostringstream ss;
  66. int val = 19;
  67. tuple<test_concept, _a&, _self> t(ss, val);
  68. get<0>(t) << get<1>(t);
  69. BOOST_CHECK(ss.str() == L"19");
  70. }
  71. BOOST_AUTO_TEST_CASE(test_output_overload_all)
  72. {
  73. typedef boost::mpl::vector<
  74. ostreamable<_a>,
  75. ostreamable<_a, int>,
  76. ostreamable<_b>,
  77. ostreamable<_b, int>,
  78. ostreamable<>,
  79. ostreamable<std::wostream>,
  80. copy_constructible<>
  81. > test_concept;
  82. {
  83. std::ostringstream ss;
  84. std::wostringstream wss;
  85. int val = 2;
  86. tuple<test_concept, _a&, _b&, _self> t(ss, wss, val);
  87. get<0>(t) << get<2>(t);
  88. get<1>(t) << get<2>(t);
  89. BOOST_CHECK_EQUAL(ss.str(), "2");
  90. BOOST_CHECK(wss.str() == L"2");
  91. }
  92. {
  93. std::ostringstream ss;
  94. std::wostringstream wss;
  95. int val = 2;
  96. tuple<test_concept, _a&, _b&, _self> t(ss, wss, val);
  97. get<0>(t) << 3;
  98. get<1>(t) << 3;
  99. BOOST_CHECK_EQUAL(ss.str(), "3");
  100. BOOST_CHECK(wss.str() == L"3");
  101. }
  102. {
  103. std::ostringstream ss;
  104. std::wostringstream wss;
  105. int val = 5;
  106. tuple<test_concept, _a&, _b&, _self> t(ss, wss, val);
  107. ss << get<2>(t);
  108. wss << get<2>(t);
  109. BOOST_CHECK_EQUAL(ss.str(), "5");
  110. BOOST_CHECK(wss.str() == L"5");
  111. }
  112. {
  113. std::ostringstream ss;
  114. std::wostringstream wss;
  115. int val = 5;
  116. tuple<test_concept, _a&, _b&, _self> t(ss, wss, val);
  117. // we can't do anything with these, but it should
  118. // still compile.
  119. any<test_concept, const _a&> os(get<0>(t));
  120. any<test_concept, const _b&> wos(get<1>(t));
  121. }
  122. }
  123. BOOST_AUTO_TEST_CASE(test_input_int)
  124. {
  125. typedef istreamable<_a, int> test_concept;
  126. std::istringstream ss("17");
  127. int i;
  128. any<test_concept, _a&> x(ss);
  129. x >> i;
  130. BOOST_CHECK_EQUAL(i, 17);
  131. }
  132. BOOST_AUTO_TEST_CASE(test_input_int_wide)
  133. {
  134. typedef istreamable<_a, int> test_concept;
  135. std::wistringstream ss(L"17");
  136. int i;
  137. any<test_concept, _a&> x(ss);
  138. x >> i;
  139. BOOST_CHECK_EQUAL(i, 17);
  140. }
  141. BOOST_AUTO_TEST_CASE(test_input_int_any)
  142. {
  143. typedef istreamable<> test_concept;
  144. std::istringstream ss("10");
  145. int i;
  146. any<test_concept, _self&> x(i);
  147. ss >> x;
  148. BOOST_CHECK_EQUAL(i, 10);
  149. }
  150. BOOST_AUTO_TEST_CASE(test_input_int_any_wide)
  151. {
  152. typedef istreamable<std::wistream> test_concept;
  153. std::wistringstream ss(L"10");
  154. int i;
  155. any<test_concept, _self&> x(i);
  156. ss >> x;
  157. BOOST_CHECK_EQUAL(i, 10);
  158. }
  159. BOOST_AUTO_TEST_CASE(test_input_both_any)
  160. {
  161. typedef istreamable<_a> test_concept;
  162. std::istringstream ss("19");
  163. int i;
  164. tuple<test_concept, _a&, _self&> t(ss, i);
  165. get<0>(t) >> get<1>(t);
  166. BOOST_CHECK_EQUAL(i, 19);
  167. }
  168. BOOST_AUTO_TEST_CASE(test_input_both_any_wide)
  169. {
  170. typedef istreamable<_a> test_concept;
  171. std::wistringstream ss(L"19");
  172. int i;
  173. tuple<test_concept, _a&, _self&> t(ss, i);
  174. get<0>(t) >> get<1>(t);
  175. BOOST_CHECK_EQUAL(i, 19);
  176. }
  177. BOOST_AUTO_TEST_CASE(test_input_overload_all)
  178. {
  179. typedef boost::mpl::vector<
  180. istreamable<_a>,
  181. istreamable<_a, int>,
  182. istreamable<_b>,
  183. istreamable<_b, int>,
  184. istreamable<>,
  185. istreamable<std::wistream>
  186. > test_concept;
  187. {
  188. std::istringstream ss("2");
  189. std::wistringstream wss(L"3");
  190. int i = 0;
  191. tuple<test_concept, _a&, _b&, _self&> t(ss, wss, i);
  192. get<0>(t) >> get<2>(t);
  193. BOOST_CHECK_EQUAL(i, 2);
  194. get<1>(t) >> get<2>(t);
  195. BOOST_CHECK_EQUAL(i, 3);
  196. }
  197. {
  198. std::istringstream ss("5");
  199. std::wistringstream wss(L"7");
  200. int i = 0;
  201. tuple<test_concept, _a&, _b&, _self&> t(ss, wss, i);
  202. get<0>(t) >> i;
  203. BOOST_CHECK_EQUAL(i, 5);
  204. get<1>(t) >> i;
  205. BOOST_CHECK_EQUAL(i, 7);
  206. }
  207. {
  208. std::istringstream ss("11");
  209. std::wistringstream wss(L"13");
  210. int i = 0;
  211. tuple<test_concept, _a&, _b&, _self&> t(ss, wss, i);
  212. ss >> get<2>(t);
  213. BOOST_CHECK_EQUAL(i, 11);
  214. wss >> get<2>(t);
  215. BOOST_CHECK_EQUAL(i, 13);
  216. }
  217. {
  218. std::istringstream ss;
  219. std::wistringstream wss;
  220. int val = 5;
  221. tuple<test_concept, _a&, _b&, _self&> t(ss, wss, val);
  222. // we can't do anything with these, but it should
  223. // still compile.
  224. any<test_concept, const _a&> is(get<0>(t));
  225. any<test_concept, const _b&> wis(get<1>(t));
  226. }
  227. }