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
12 #include <iostream>
13 #include <algorithm>
14 #include <complex>
15 #include <boost/numeric/ublas/tensor.hpp>
16 #include <boost/numeric/ublas/tensor/multi_index.hpp>
17
18
19 #include <boost/test/unit_test.hpp>
20
21 #include "utility.hpp"
22
23
24 BOOST_AUTO_TEST_SUITE ( test_multi_index )
25
26
27 using test_types = zip<int,long,float,double,std::complex<float>>::with_t<boost::numeric::ublas::first_order, boost::numeric::ublas::last_order>;
28
29
BOOST_AUTO_TEST_CASE(test_index_classes)30 BOOST_AUTO_TEST_CASE ( test_index_classes )
31 {
32 using namespace boost::numeric::ublas::index;
33
34
35 BOOST_CHECK_EQUAL ( _a.value , 1 ) ;
36 BOOST_CHECK_EQUAL ( _b.value , 2 ) ;
37 BOOST_CHECK_EQUAL ( _c.value , 3 ) ;
38 BOOST_CHECK_EQUAL ( _d.value , 4 ) ;
39 BOOST_CHECK_EQUAL ( _e.value , 5 ) ;
40 BOOST_CHECK_EQUAL ( _f.value , 6 ) ;
41 BOOST_CHECK_EQUAL ( _g.value , 7 ) ;
42 BOOST_CHECK_EQUAL ( _h.value , 8 ) ;
43 BOOST_CHECK_EQUAL ( _i.value , 9 ) ;
44 BOOST_CHECK_EQUAL ( _j.value , 10 ) ;
45 BOOST_CHECK_EQUAL ( _k.value , 11 ) ;
46 BOOST_CHECK_EQUAL ( _l.value , 12 ) ;
47 BOOST_CHECK_EQUAL ( _m.value , 13 ) ;
48 BOOST_CHECK_EQUAL ( _n.value , 14 ) ;
49 BOOST_CHECK_EQUAL ( _o.value , 15 ) ;
50 BOOST_CHECK_EQUAL ( _p.value , 16 ) ;
51 BOOST_CHECK_EQUAL ( _q.value , 17 ) ;
52 BOOST_CHECK_EQUAL ( _r.value , 18 ) ;
53 BOOST_CHECK_EQUAL ( _s.value , 19 ) ;
54 BOOST_CHECK_EQUAL ( _t.value , 20 ) ;
55 BOOST_CHECK_EQUAL ( _u.value , 21 ) ;
56 BOOST_CHECK_EQUAL ( _v.value , 22 ) ;
57 BOOST_CHECK_EQUAL ( _w.value , 23 ) ;
58 BOOST_CHECK_EQUAL ( _x.value , 24 ) ;
59 BOOST_CHECK_EQUAL ( _y.value , 25 ) ;
60 BOOST_CHECK_EQUAL ( _z.value , 26 ) ;
61
62 }
63
BOOST_AUTO_TEST_CASE(test_multi_index_class_construction)64 BOOST_AUTO_TEST_CASE ( test_multi_index_class_construction )
65 {
66 using namespace boost::numeric::ublas;
67 using namespace boost::numeric::ublas::index;
68
69
70 {
71 multi_index<2> ind(_a, _b);
72
73 BOOST_CHECK_EQUAL ( get<0>( ind ), 1 ) ;
74 BOOST_CHECK_EQUAL ( get<1>( ind ), 2 ) ;
75 }
76
77
78 {
79 multi_index<2> ind(_d,_c);
80
81 BOOST_CHECK_EQUAL ( ind[0] , 4 ) ;
82 BOOST_CHECK_EQUAL ( ind[1] , 3 ) ;
83 }
84 }
85
86
BOOST_AUTO_TEST_CASE_TEMPLATE(test_tensor_multi_index_class_generation,value,test_types)87 BOOST_AUTO_TEST_CASE_TEMPLATE( test_tensor_multi_index_class_generation, value, test_types )
88 {
89 using namespace boost::numeric::ublas;
90 using value_type = typename value::first_type;
91 using layout_type = typename value::second_type;
92 using tensor_type = tensor<value_type,layout_type>;
93
94 auto t = std::make_tuple (
95 index::_a, // 0
96 index::_b, // 1
97 index::_c, // 2
98 index::_d, // 3
99 index::_e // 4
100 );
101
102 {
103 auto a = tensor_type(shape{2,3}, value_type{2});
104 auto a_ind = a( std::get<0>(t), std::get<2>(t) );
105
106 BOOST_CHECK_EQUAL ( std::addressof( a_ind.first ), std::addressof( a ) ) ;
107
108 BOOST_CHECK_EQUAL (std::get<0>(a_ind.second)(), index::_a() ) ;
109 BOOST_CHECK_EQUAL (std::get<1>(a_ind.second)(), index::_c() ) ;
110 }
111
112 {
113 auto a = tensor_type(shape{2,3}, value_type{2});
114 auto a_ind = a( std::get<2>(t), std::get<0>(t) );
115
116 BOOST_CHECK_EQUAL ( std::addressof( a_ind.first ), std::addressof( a ) ) ;
117
118 BOOST_CHECK_EQUAL (std::get<0>(a_ind.second)(), index::_c() ) ;
119 BOOST_CHECK_EQUAL (std::get<1>(a_ind.second)(), index::_a() ) ;
120 }
121
122 {
123 auto a = tensor_type(shape{2,3}, value_type{2});
124 auto a_ind = a( std::get<2>(t), std::get<3>(t) );
125
126 BOOST_CHECK_EQUAL (std::addressof( a_ind.first ), std::addressof( a ) ) ;
127
128 BOOST_CHECK_EQUAL (std::get<0>(a_ind.second)(), index::_c() ) ;
129 BOOST_CHECK_EQUAL (std::get<1>(a_ind.second)(), index::_d() ) ;
130 }
131
132 {
133 auto a = tensor_type(shape{2,3,4}, value_type{2});
134 auto a_ind = a( std::get<2>(t), std::get<3>(t), std::get<0>(t) );
135
136 BOOST_CHECK_EQUAL (std::addressof( a_ind.first ), std::addressof( a ) ) ;
137
138 BOOST_CHECK_EQUAL (std::get<0>(a_ind.second)(), index::_c() ) ;
139 BOOST_CHECK_EQUAL (std::get<1>(a_ind.second)(), index::_d() ) ;
140 BOOST_CHECK_EQUAL (std::get<2>(a_ind.second)(), index::_a() ) ;
141 }
142
143 }
144
145 BOOST_AUTO_TEST_SUITE_END()
146
147