vector_n.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/fusion/container/vector/vector10.hpp>
  8. #include <boost/fusion/adapted/mpl.hpp>
  9. #include <boost/fusion/sequence/intrinsic/size.hpp>
  10. #include <boost/fusion/sequence/intrinsic/at.hpp>
  11. #include <boost/fusion/sequence/intrinsic/value_at.hpp>
  12. #include <boost/fusion/container/vector/vector20.hpp>
  13. #include <boost/fusion/container/vector/vector30.hpp>
  14. #include <boost/fusion/container/vector/vector40.hpp>
  15. #include <boost/fusion/container/vector/vector50.hpp>
  16. #include <boost/type_traits/is_same.hpp>
  17. #include <boost/type_traits/is_empty.hpp>
  18. #include <boost/static_assert.hpp>
  19. #include <iostream>
  20. #include <boost/fusion/algorithm/transformation/push_back.hpp>
  21. #include <boost/mpl/vector_c.hpp>
  22. int
  23. main()
  24. {
  25. using namespace boost::fusion;
  26. using namespace boost;
  27. {
  28. vector0<> vec;
  29. (void) vec;
  30. std::cout << "(): " << sizeof(vec) << std::endl;
  31. std::cout << (boost::is_empty<vector0<> >::value ? "is empty" : "is not empty") << std::endl;
  32. }
  33. {
  34. typedef vector1<int> type;
  35. type vec;
  36. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<type>::value == 1);
  37. BOOST_TEST(at_c<0>(vec) == 0);
  38. BOOST_STATIC_ASSERT((boost::is_same<int, boost::fusion::result_of::value_at_c<type, 0>::type>::value));
  39. // prove that it is mutable
  40. at_c<0>(vec) = 987;
  41. BOOST_TEST(at_c<0>(vec) == 987);
  42. }
  43. {
  44. typedef vector1<int> type;
  45. type vec(123);
  46. BOOST_TEST(at_c<0>(vec) == 123);
  47. std::cout << "(int): " << sizeof(vec) << std::endl;
  48. }
  49. { // testing const vector
  50. vector1<short> const vec(999);
  51. BOOST_TEST(at_c<0>(vec) == 999);
  52. #ifdef FUSION_TEST_COMPILE_FAIL
  53. at_c<0>(vec) = 321;
  54. #endif
  55. }
  56. {
  57. vector1<int> t1(123L); // try conversion from long to int
  58. vector1<double> t2(t1); // try copy
  59. (void)t2;
  60. }
  61. {
  62. typedef vector2<int, char> type;
  63. type vec;
  64. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<type>::value == 2);
  65. BOOST_TEST(at_c<0>(vec) == 0);
  66. BOOST_TEST(at_c<1>(vec) == char());
  67. BOOST_STATIC_ASSERT((boost::is_same<int, boost::fusion::result_of::value_at_c<type, 0>::type>::value));
  68. BOOST_STATIC_ASSERT((boost::is_same<char, boost::fusion::result_of::value_at_c<type, 1>::type>::value));
  69. }
  70. {
  71. typedef vector2<int, char> type;
  72. type vec(123, 'x');
  73. BOOST_TEST(at_c<0>(vec) == 123);
  74. BOOST_TEST(at_c<1>(vec) == 'x');
  75. std::cout << "(int, char): " << sizeof(vec) << std::endl;
  76. }
  77. {
  78. vector2<int, short> t1(123, 456);
  79. vector2<double, float> t2(t1);
  80. (void)t2;
  81. }
  82. {
  83. typedef vector3<int, char, double> type;
  84. type vec;
  85. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<type>::value == 3);
  86. BOOST_TEST(at_c<0>(vec) == 0);
  87. BOOST_TEST(at_c<1>(vec) == char());
  88. BOOST_TEST(at_c<2>(vec) == double());
  89. BOOST_STATIC_ASSERT((boost::is_same<int, boost::fusion::result_of::value_at_c<type, 0>::type>::value));
  90. BOOST_STATIC_ASSERT((boost::is_same<char, boost::fusion::result_of::value_at_c<type, 1>::type>::value));
  91. BOOST_STATIC_ASSERT((boost::is_same<double, boost::fusion::result_of::value_at_c<type, 2>::type>::value));
  92. }
  93. {
  94. typedef vector3<int, char, double> type;
  95. type vec(123, 'x', 123.456);
  96. BOOST_TEST(at_c<0>(vec) == 123);
  97. BOOST_TEST(at_c<1>(vec) == 'x');
  98. BOOST_TEST(at_c<2>(vec) >= 123.455 && at_c<2>(vec) <= 123.457);
  99. std::cout << "(int, char, double): " << sizeof(vec) << std::endl;
  100. }
  101. {
  102. typedef vector4<int, char, double, bool> type;
  103. type vec(123, 'x', 123.456, true);
  104. std::cout << "(int, char, double, bool): " << sizeof(vec) << std::endl;
  105. }
  106. {
  107. typedef vector4<int, char, bool, double> type;
  108. type vec(123, 'x', true, 123.456);
  109. std::cout << "(int, char, bool, double): " << sizeof(vec) << std::endl;
  110. }
  111. {
  112. typedef vector7<bool, char, short, int, long, float, double> type;
  113. type vec(false, 'x', 3, 4, 5, 6.f, 7.0);
  114. BOOST_TEST(at_c<0>(vec) == false);
  115. BOOST_TEST(at_c<1>(vec) == 'x');
  116. BOOST_TEST(at_c<2>(vec) == 3);
  117. BOOST_TEST(at_c<3>(vec) == 4);
  118. BOOST_TEST(at_c<4>(vec) == 5);
  119. BOOST_TEST(at_c<5>(vec) >= 5.9 && at_c<5>(vec) <= 6.1);
  120. BOOST_TEST(at_c<6>(vec) >= 6.9 && at_c<6>(vec) <= 7.1);
  121. BOOST_STATIC_ASSERT((boost::is_same<bool, boost::fusion::result_of::value_at_c<type, 0>::type>::value));
  122. BOOST_STATIC_ASSERT((boost::is_same<char, boost::fusion::result_of::value_at_c<type, 1>::type>::value));
  123. BOOST_STATIC_ASSERT((boost::is_same<short, boost::fusion::result_of::value_at_c<type, 2>::type>::value));
  124. BOOST_STATIC_ASSERT((boost::is_same<int, boost::fusion::result_of::value_at_c<type, 3>::type>::value));
  125. BOOST_STATIC_ASSERT((boost::is_same<long, boost::fusion::result_of::value_at_c<type, 4>::type>::value));
  126. BOOST_STATIC_ASSERT((boost::is_same<float, boost::fusion::result_of::value_at_c<type, 5>::type>::value));
  127. BOOST_STATIC_ASSERT((boost::is_same<double, boost::fusion::result_of::value_at_c<type, 6>::type>::value));
  128. std::cout << "(bool, char, short, int, long, float, double): " << sizeof(vec) << std::endl;
  129. }
  130. {
  131. typedef vector10<int, int, int, int, int, int, int, int, int, int> type;
  132. type vec; // compile check only
  133. std::cout << "vector10 of int: " << sizeof(vec) << std::endl;
  134. }
  135. {
  136. typedef vector20<
  137. int, int, int, int, int, int, int, int, int, int
  138. , int, int, int, int, int, int, int, int, int, int> type;
  139. type vec; // compile check only
  140. std::cout << "vector20 of int: " << sizeof(vec) << std::endl;
  141. }
  142. {
  143. typedef vector30<
  144. int, int, int, int, int, int, int, int, int, int
  145. , int, int, int, int, int, int, int, int, int, int
  146. , int, int, int, int, int, int, int, int, int, int> type;
  147. type vec; // compile check only
  148. std::cout << "vector30 of int: " << sizeof(vec) << std::endl;
  149. }
  150. {
  151. typedef vector40<
  152. int, int, int, int, int, int, int, int, int, int
  153. , int, int, int, int, int, int, int, int, int, int
  154. , int, int, int, int, int, int, int, int, int, int
  155. , int, int, int, int, int, int, int, int, int, int> type;
  156. type vec; // compile check only
  157. std::cout << "vector40 of int: " << sizeof(vec) << std::endl;
  158. }
  159. {
  160. typedef vector50<
  161. int, int, int, int, int, int, int, int, int, int
  162. , int, int, int, int, int, int, int, int, int, int
  163. , int, int, int, int, int, int, int, int, int, int
  164. , int, int, int, int, int, int, int, int, int, int
  165. , int, int, int, int, int, int, int, int, int, int> type;
  166. type vec; // compile check only
  167. std::cout << "vector50 of int: " << sizeof(vec) << std::endl;
  168. }
  169. {
  170. // testing copy and assign from a view
  171. vector0<> empty;
  172. fusion::vector2<int, long> v(fusion::push_back(fusion::push_back(empty, 123), 456));
  173. BOOST_TEST(at_c<0>(v) == 123);
  174. BOOST_TEST(at_c<1>(v) == 456);
  175. v = fusion::push_back(fusion::push_back(empty, 123), 456); // test assign
  176. BOOST_TEST(at_c<0>(v) == 123);
  177. BOOST_TEST(at_c<1>(v) == 456);
  178. }
  179. {
  180. // testing copy and assign from a vector_c
  181. mpl::vector_c<int, 123, 456> vec_c;
  182. fusion::vector2<int, long> v(vec_c);
  183. BOOST_TEST(at_c<0>(v) == 123);
  184. BOOST_TEST(at_c<1>(v) == 456);
  185. v = mpl::vector_c<int, 123, 456>(); // test assign
  186. BOOST_TEST(at_c<0>(v) == 123);
  187. BOOST_TEST(at_c<1>(v) == 456);
  188. }
  189. return boost::report_errors();
  190. }