1 // -*-indent-tabs-mode:t;c-basic-offset:4;tab-width:4-*-
2
3 #define BOOST_TEST_MODULE "C++ Unit Tests for Multi rotate"
4 #define BOOST_TEST_DYN_LINK
5 #include<boost/test/unit_test.hpp>
6
7 #include "../array.hpp"
8
9 #include<numeric> // iota
10
11 namespace multi = boost::multi;
12
BOOST_AUTO_TEST_CASE(multi_rotate_3d)13 BOOST_AUTO_TEST_CASE(multi_rotate_3d){
14 multi::array<double, 3> A({3, 4, 5});
15 BOOST_REQUIRE(( sizes(A) == decltype(sizes(A)){3, 4, 5} ));
16
17 auto&& RA = rotated(A);
18 BOOST_REQUIRE(( sizes(RA) == decltype(sizes(RA)){4, 5, 3} ));
19 BOOST_REQUIRE( &A[0][1][2] == &RA[1][2][0] );
20
21 auto&& UA = unrotated(A);
22 BOOST_REQUIRE(( sizes(UA) == decltype(sizes(UA)){5, 3, 4} ));
23 BOOST_REQUIRE( &A[0][1][2] == &UA[2][0][1] );
24
25 auto&& RRA = rotated(RA);
26 BOOST_REQUIRE(( sizes(RRA) == decltype(sizes(RRA)){5, 3, 4} ));
27 BOOST_REQUIRE( &A[0][1][2] == &RRA[2][0][1] );
28 }
29
BOOST_AUTO_TEST_CASE(multi_rotate_4d)30 BOOST_AUTO_TEST_CASE(multi_rotate_4d){
31 multi::array<double, 4> original({14, 14, 7, 4});
32
33 auto&& unrotd = original.unrotated();
34 BOOST_REQUIRE(( sizes(unrotd) == decltype(sizes(unrotd)){4, 14, 14, 7} ));
35 BOOST_REQUIRE( &original[0][1][2][3] == &unrotd[3][0][1][2] );
36
37 auto&& unrotd2 = original.unrotated(2);
38 BOOST_REQUIRE(( sizes(unrotd2) == decltype(sizes(unrotd2)){7, 4, 14, 14} ));
39 BOOST_REQUIRE( &original[0][1][2][3] == &unrotd2[2][3][0][1] );
40 }
41
BOOST_AUTO_TEST_CASE(multi_rotate_4d_op)42 BOOST_AUTO_TEST_CASE(multi_rotate_4d_op){
43 multi::array<double, 4> original({14, 14, 7, 4});
44
45 auto&& unrotd = (original >> 1);
46 BOOST_REQUIRE(( sizes(unrotd) == decltype(sizes(unrotd)){4, 14, 14, 7} ));
47 BOOST_REQUIRE( &original[0][1][2][3] == &unrotd[3][0][1][2] );
48
49 auto&& unrotd2 = (original >> 2);
50 BOOST_REQUIRE(( sizes(unrotd2) == decltype(sizes(unrotd2)){7, 4, 14, 14} ));
51 BOOST_REQUIRE( &original[0][1][2][3] == &unrotd2[2][3][0][1] );
52 }
53
BOOST_AUTO_TEST_CASE(multi_rotate)54 BOOST_AUTO_TEST_CASE(multi_rotate){
55 {
56 std::array<std::array<double, 5>, 4> a = {
57 {
58 { 0, 1, 2, 3, 4},
59 { 5, 6, 7, 8, 9},
60 {10, 11, 12, 13, 14},
61 {15, 16, 17, 18, 19}
62 }
63 };
64 std::array<std::array<double, 5>, 4> b = {};
65
66 multi::array_ref<double, 2> A(&a[0][0], {4, 5});
67 multi::array_ref<double, 2> B(&b[0][0], {4, 5});
68
69 rotated(B) = rotated(A);
70 BOOST_REQUIRE( B[1][1] == 6 );
71 BOOST_REQUIRE( B[2][1] == 11 );
72 BOOST_REQUIRE( B[1][2] == 7 );
73 BOOST_REQUIRE( (B <<1) == (A <<1) );
74 BOOST_REQUIRE( (B<<1)[2][1] == 7 );
75 }
76 {
77 multi::array<double, 2> A = {
78 {00, 01},
79 {10, 11}
80 };
81 BOOST_REQUIRE( A[1][0] == 10 );
82 BOOST_REQUIRE( (A <<1)[0][1] == 10 );
83 BOOST_REQUIRE( & A[1][0] == &(A <<1)[0][1] );
84
85 BOOST_REQUIRE( A.transposed()[0][1] == 10 );
86 BOOST_REQUIRE( transposed(A)[0][1] == 10 );
87 BOOST_REQUIRE( (~A)[0][1] == 10 );
88 BOOST_REQUIRE( &A[1][0] == &A.transposed()[0][1] );
89
90 (A<<1)[0][1] = 100;
91 BOOST_REQUIRE( A[1][0] == 100 );
92 }
93 {
94 multi::array<double, 3> A({11, 13, 17});
95 BOOST_REQUIRE( & A[3][5][7] == & A.transposed()[5][3][7] );
96 BOOST_REQUIRE( & A[3][5][7] == & transposed(A) [5][3][7] );
97 BOOST_REQUIRE( & A[3][5][7] == & (~A) [5][3][7] );
98 BOOST_REQUIRE( & A[3][5][7] == & A[3].transposed()[7][5] );
99 BOOST_REQUIRE( & A[3][5][7] == & (~A[3]) [7][5] );
100
101 BOOST_REQUIRE( & A[3][5] == & (~A)[5][3] );
102
103 BOOST_REQUIRE( & ~~A == & A );
104 BOOST_REQUIRE( & (A <<3) == & A );
105 BOOST_REQUIRE( & A == & (A<<3) );
106 BOOST_REQUIRE( & (A <<1) != & A );
107 BOOST_REQUIRE( & (A >>1 <<1) == & A );
108
109 std::iota(A.data_elements(), A.data_elements() + A.num_elements(), 0.1);
110 BOOST_REQUIRE( ~~A == A );
111 BOOST_REQUIRE( (A >>1 <<1) == A );
112 }
113 {
114 multi::array<double, 2> const A = {
115 {00, 01},
116 {10, 11}
117 };
118 BOOST_REQUIRE( (A<<1)[0][1] == 10 );
119 BOOST_REQUIRE( &(A<<1)[1][0] == &A[0][1] );
120 BOOST_REQUIRE( &(~A)[1][0] == &A[0][1] );
121 }
122 {
123 multi::array<double, 3> const A({3, 5, 7});
124 }
125
126 }
127
BOOST_AUTO_TEST_CASE(multi_transposed)128 BOOST_AUTO_TEST_CASE(multi_transposed){
129 multi::array<double, 2> const M = {
130 { 9., 24., 30., 9.},
131 { 4., 10., 12., 7.},
132 {14., 16., 36., 1.}
133 };
134 multi::array<double, 2> const MT1 = M.transposed();
135 multi::array<double, 2> const MT2 = ~M;
136 BOOST_REQUIRE( MT1 == MT2 );
137 }
138
139