test_multi_index.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2018-2019 Cem Bassoy
  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. //
  7. // The authors gratefully acknowledge the support of
  8. // Fraunhofer and Google in producing this work
  9. // which started as a Google Summer of Code project.
  10. //
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <complex>
  14. #include <boost/numeric/ublas/tensor.hpp>
  15. #include <boost/numeric/ublas/tensor/multi_index.hpp>
  16. #include <boost/test/unit_test.hpp>
  17. #include "utility.hpp"
  18. BOOST_AUTO_TEST_SUITE ( test_multi_index )
  19. using test_types = zip<int,long,float,double,std::complex<float>>::with_t<boost::numeric::ublas::first_order, boost::numeric::ublas::last_order>;
  20. BOOST_AUTO_TEST_CASE ( test_index_classes )
  21. {
  22. using namespace boost::numeric::ublas::index;
  23. BOOST_CHECK_EQUAL ( _a.value , 1 ) ;
  24. BOOST_CHECK_EQUAL ( _b.value , 2 ) ;
  25. BOOST_CHECK_EQUAL ( _c.value , 3 ) ;
  26. BOOST_CHECK_EQUAL ( _d.value , 4 ) ;
  27. BOOST_CHECK_EQUAL ( _e.value , 5 ) ;
  28. BOOST_CHECK_EQUAL ( _f.value , 6 ) ;
  29. BOOST_CHECK_EQUAL ( _g.value , 7 ) ;
  30. BOOST_CHECK_EQUAL ( _h.value , 8 ) ;
  31. BOOST_CHECK_EQUAL ( _i.value , 9 ) ;
  32. BOOST_CHECK_EQUAL ( _j.value , 10 ) ;
  33. BOOST_CHECK_EQUAL ( _k.value , 11 ) ;
  34. BOOST_CHECK_EQUAL ( _l.value , 12 ) ;
  35. BOOST_CHECK_EQUAL ( _m.value , 13 ) ;
  36. BOOST_CHECK_EQUAL ( _n.value , 14 ) ;
  37. BOOST_CHECK_EQUAL ( _o.value , 15 ) ;
  38. BOOST_CHECK_EQUAL ( _p.value , 16 ) ;
  39. BOOST_CHECK_EQUAL ( _q.value , 17 ) ;
  40. BOOST_CHECK_EQUAL ( _r.value , 18 ) ;
  41. BOOST_CHECK_EQUAL ( _s.value , 19 ) ;
  42. BOOST_CHECK_EQUAL ( _t.value , 20 ) ;
  43. BOOST_CHECK_EQUAL ( _u.value , 21 ) ;
  44. BOOST_CHECK_EQUAL ( _v.value , 22 ) ;
  45. BOOST_CHECK_EQUAL ( _w.value , 23 ) ;
  46. BOOST_CHECK_EQUAL ( _x.value , 24 ) ;
  47. BOOST_CHECK_EQUAL ( _y.value , 25 ) ;
  48. BOOST_CHECK_EQUAL ( _z.value , 26 ) ;
  49. }
  50. BOOST_AUTO_TEST_CASE ( test_multi_index_class_construction )
  51. {
  52. using namespace boost::numeric::ublas;
  53. using namespace boost::numeric::ublas::index;
  54. {
  55. multi_index<2> ind(_a, _b);
  56. BOOST_CHECK_EQUAL ( get<0>( ind ), 1 ) ;
  57. BOOST_CHECK_EQUAL ( get<1>( ind ), 2 ) ;
  58. }
  59. {
  60. multi_index<2> ind(_d,_c);
  61. BOOST_CHECK_EQUAL ( ind[0] , 4 ) ;
  62. BOOST_CHECK_EQUAL ( ind[1] , 3 ) ;
  63. }
  64. }
  65. BOOST_AUTO_TEST_CASE_TEMPLATE( test_tensor_multi_index_class_generation, value, test_types )
  66. {
  67. using namespace boost::numeric::ublas;
  68. using value_type = typename value::first_type;
  69. using layout_type = typename value::second_type;
  70. using tensor_type = tensor<value_type,layout_type>;
  71. auto t = std::make_tuple (
  72. index::_a, // 0
  73. index::_b, // 1
  74. index::_c, // 2
  75. index::_d, // 3
  76. index::_e // 4
  77. );
  78. {
  79. auto a = tensor_type(shape{2,3}, value_type{2});
  80. auto a_ind = a( std::get<0>(t), std::get<2>(t) );
  81. BOOST_CHECK_EQUAL ( std::addressof( a_ind.first ), std::addressof( a ) ) ;
  82. BOOST_CHECK_EQUAL (std::get<0>(a_ind.second)(), index::_a() ) ;
  83. BOOST_CHECK_EQUAL (std::get<1>(a_ind.second)(), index::_c() ) ;
  84. }
  85. {
  86. auto a = tensor_type(shape{2,3}, value_type{2});
  87. auto a_ind = a( std::get<2>(t), std::get<0>(t) );
  88. BOOST_CHECK_EQUAL ( std::addressof( a_ind.first ), std::addressof( a ) ) ;
  89. BOOST_CHECK_EQUAL (std::get<0>(a_ind.second)(), index::_c() ) ;
  90. BOOST_CHECK_EQUAL (std::get<1>(a_ind.second)(), index::_a() ) ;
  91. }
  92. {
  93. auto a = tensor_type(shape{2,3}, value_type{2});
  94. auto a_ind = a( std::get<2>(t), std::get<3>(t) );
  95. BOOST_CHECK_EQUAL (std::addressof( a_ind.first ), std::addressof( a ) ) ;
  96. BOOST_CHECK_EQUAL (std::get<0>(a_ind.second)(), index::_c() ) ;
  97. BOOST_CHECK_EQUAL (std::get<1>(a_ind.second)(), index::_d() ) ;
  98. }
  99. {
  100. auto a = tensor_type(shape{2,3,4}, value_type{2});
  101. auto a_ind = a( std::get<2>(t), std::get<3>(t), std::get<0>(t) );
  102. BOOST_CHECK_EQUAL (std::addressof( a_ind.first ), std::addressof( a ) ) ;
  103. BOOST_CHECK_EQUAL (std::get<0>(a_ind.second)(), index::_c() ) ;
  104. BOOST_CHECK_EQUAL (std::get<1>(a_ind.second)(), index::_d() ) ;
  105. BOOST_CHECK_EQUAL (std::get<2>(a_ind.second)(), index::_a() ) ;
  106. }
  107. }
  108. BOOST_AUTO_TEST_SUITE_END()