4129 |
19 Jan 22 |
peter |
// $Id$ |
4129 |
19 Jan 22 |
peter |
2 |
|
4129 |
19 Jan 22 |
peter |
3 |
/* |
4378 |
11 Oct 23 |
peter |
Copyright (C) 2022, 2023 Peter Johansson |
4129 |
19 Jan 22 |
peter |
5 |
|
4129 |
19 Jan 22 |
peter |
This file is part of the yat library, https://dev.thep.lu.se/yat |
4129 |
19 Jan 22 |
peter |
7 |
|
4129 |
19 Jan 22 |
peter |
The yat library is free software; you can redistribute it and/or |
4129 |
19 Jan 22 |
peter |
modify it under the terms of the GNU General Public License as |
4129 |
19 Jan 22 |
peter |
published by the Free Software Foundation; either version 3 of the |
4129 |
19 Jan 22 |
peter |
License, or (at your option) any later version. |
4129 |
19 Jan 22 |
peter |
12 |
|
4129 |
19 Jan 22 |
peter |
The yat library is distributed in the hope that it will be useful, |
4129 |
19 Jan 22 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4129 |
19 Jan 22 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4129 |
19 Jan 22 |
peter |
General Public License for more details. |
4129 |
19 Jan 22 |
peter |
17 |
|
4129 |
19 Jan 22 |
peter |
You should have received a copy of the GNU General Public License |
4129 |
19 Jan 22 |
peter |
along with yat. If not, see <https://www.gnu.org/licenses/>. |
4129 |
19 Jan 22 |
peter |
20 |
*/ |
4129 |
19 Jan 22 |
peter |
21 |
|
4129 |
19 Jan 22 |
peter |
22 |
#include <config.h> |
4129 |
19 Jan 22 |
peter |
23 |
|
4129 |
19 Jan 22 |
peter |
24 |
#include "Suite.h" |
4129 |
19 Jan 22 |
peter |
25 |
|
4132 |
21 Jan 22 |
peter |
26 |
#include "yat/utility/concept_check.h" |
4129 |
19 Jan 22 |
peter |
27 |
#include "yat/utility/Matrix.h" |
4129 |
19 Jan 22 |
peter |
28 |
#include "yat/utility/MatrixConstView.h" |
4129 |
19 Jan 22 |
peter |
29 |
#include "yat/utility/MatrixView.h" |
4129 |
19 Jan 22 |
peter |
30 |
|
4132 |
21 Jan 22 |
peter |
31 |
#include <boost/concept_check.hpp> |
4132 |
21 Jan 22 |
peter |
32 |
|
4129 |
19 Jan 22 |
peter |
33 |
using namespace theplu::yat; |
4129 |
19 Jan 22 |
peter |
34 |
|
4129 |
19 Jan 22 |
peter |
35 |
void test_matrix_view(test::Suite& suite); |
4129 |
19 Jan 22 |
peter |
36 |
void test_matrix_const_view(test::Suite& suite); |
4378 |
11 Oct 23 |
peter |
37 |
void test_move_constructor(test::Suite& suite); |
4378 |
11 Oct 23 |
peter |
38 |
void test_move_assignment(test::Suite& suite); |
4129 |
19 Jan 22 |
peter |
39 |
|
4129 |
19 Jan 22 |
peter |
40 |
int main(int argc, char* argv[]) |
4129 |
19 Jan 22 |
peter |
41 |
{ |
4129 |
19 Jan 22 |
peter |
42 |
test::Suite suite(argc, argv); |
4129 |
19 Jan 22 |
peter |
43 |
test_matrix_view(suite); |
4129 |
19 Jan 22 |
peter |
44 |
test_matrix_const_view(suite); |
4378 |
11 Oct 23 |
peter |
45 |
test_move_constructor(suite); |
4378 |
11 Oct 23 |
peter |
46 |
test_move_assignment(suite); |
4129 |
19 Jan 22 |
peter |
47 |
return suite.return_value(); |
4129 |
19 Jan 22 |
peter |
48 |
} |
4129 |
19 Jan 22 |
peter |
49 |
|
4129 |
19 Jan 22 |
peter |
50 |
|
4129 |
19 Jan 22 |
peter |
51 |
template<class VIEW> |
4129 |
19 Jan 22 |
peter |
52 |
void test_constructors(test::Suite& suite, utility::Matrix m) |
4129 |
19 Jan 22 |
peter |
53 |
{ |
4129 |
19 Jan 22 |
peter |
54 |
VIEW view; |
4129 |
19 Jan 22 |
peter |
55 |
VIEW view2(view); |
4129 |
19 Jan 22 |
peter |
56 |
suite.out() << "matrix:\n" << m << "\n"; |
4129 |
19 Jan 22 |
peter |
57 |
VIEW view3(m); |
4129 |
19 Jan 22 |
peter |
58 |
if (view3 != m) { |
4129 |
19 Jan 22 |
peter |
59 |
suite.add(false); |
4129 |
19 Jan 22 |
peter |
60 |
suite.err() << "view3 != m\n"; |
4129 |
19 Jan 22 |
peter |
61 |
} |
4129 |
19 Jan 22 |
peter |
62 |
VIEW view4(view3); |
4129 |
19 Jan 22 |
peter |
63 |
if (view4 != view3) { |
4129 |
19 Jan 22 |
peter |
64 |
suite.add(false); |
4129 |
19 Jan 22 |
peter |
65 |
suite.err() << "view4 != view3\n"; |
4129 |
19 Jan 22 |
peter |
66 |
} |
4129 |
19 Jan 22 |
peter |
67 |
m(0,0) = 3.1415; |
4129 |
19 Jan 22 |
peter |
68 |
if (view4 != m) { |
4129 |
19 Jan 22 |
peter |
69 |
suite.add(false); |
4129 |
19 Jan 22 |
peter |
70 |
suite.err() << "view4 != m\n"; |
4129 |
19 Jan 22 |
peter |
71 |
} |
4129 |
19 Jan 22 |
peter |
72 |
|
4129 |
19 Jan 22 |
peter |
73 |
VIEW view5(m, 2, 3, 3, 4); |
4129 |
19 Jan 22 |
peter |
74 |
if (view5.rows() != 3) { |
4129 |
19 Jan 22 |
peter |
75 |
suite.add(false); |
4129 |
19 Jan 22 |
peter |
76 |
suite.err() << "view5: incorrect rows: " << view5.rows() << "\n"; |
4129 |
19 Jan 22 |
peter |
77 |
} |
4129 |
19 Jan 22 |
peter |
78 |
if (view5.columns() != 4) { |
4129 |
19 Jan 22 |
peter |
79 |
suite.add(false); |
4129 |
19 Jan 22 |
peter |
80 |
suite.err() << "view5: incorrect columns: " << view5.columns() << "\n"; |
4129 |
19 Jan 22 |
peter |
81 |
} |
4129 |
19 Jan 22 |
peter |
82 |
for (size_t row = 0; row<view5.rows(); ++row) |
4129 |
19 Jan 22 |
peter |
83 |
for (size_t col = 0; col<view5.columns(); ++col) { |
4129 |
19 Jan 22 |
peter |
84 |
if (view5(row, col) != m(2+row, 3+col)) { |
4129 |
19 Jan 22 |
peter |
85 |
suite.add(false); |
4129 |
19 Jan 22 |
peter |
86 |
suite.err() << "incorrect view5:" << view5 << "\n"; |
4129 |
19 Jan 22 |
peter |
87 |
} |
4129 |
19 Jan 22 |
peter |
88 |
} |
4129 |
19 Jan 22 |
peter |
89 |
|
4129 |
19 Jan 22 |
peter |
// construct a matrix copy of a view and test that it's not linked |
4129 |
19 Jan 22 |
peter |
91 |
utility::Matrix m2(view5); |
4129 |
19 Jan 22 |
peter |
92 |
if (m2 != view5) { |
4129 |
19 Jan 22 |
peter |
93 |
suite.add(false); |
4129 |
19 Jan 22 |
peter |
94 |
suite.err() << "m2 != view5:\nm2:\n" << m2 << "\nview5:\n" << view5 |
4129 |
19 Jan 22 |
peter |
95 |
<< "\n"; |
4129 |
19 Jan 22 |
peter |
96 |
} |
4129 |
19 Jan 22 |
peter |
97 |
m2(0,0) = -1; |
4129 |
19 Jan 22 |
peter |
98 |
if (view5(0, 0) == -1) { |
4129 |
19 Jan 22 |
peter |
99 |
suite.err() << "view5(0,0) = -1\n"; |
4129 |
19 Jan 22 |
peter |
100 |
} |
4129 |
19 Jan 22 |
peter |
101 |
} |
4129 |
19 Jan 22 |
peter |
102 |
|
4129 |
19 Jan 22 |
peter |
103 |
|
4129 |
19 Jan 22 |
peter |
104 |
template<class VIEW> |
4129 |
19 Jan 22 |
peter |
105 |
void test_assignment(test::Suite& suite, utility::Matrix m) |
4129 |
19 Jan 22 |
peter |
106 |
{ |
4129 |
19 Jan 22 |
peter |
107 |
utility::Matrix m0(m.rows(), m.columns(), 0.0); |
4129 |
19 Jan 22 |
peter |
108 |
VIEW view(m0); |
4129 |
19 Jan 22 |
peter |
109 |
view = m; |
4129 |
19 Jan 22 |
peter |
110 |
if (!suite.add(suite.equal_matrix(m, view))) { |
4129 |
19 Jan 22 |
peter |
111 |
suite.err() << "view != m\n"; |
4129 |
19 Jan 22 |
peter |
112 |
} |
4129 |
19 Jan 22 |
peter |
113 |
if (!suite.add(suite.equal_matrix(m, m0))) { |
4129 |
19 Jan 22 |
peter |
114 |
suite.err() << "m != m0\n"; |
4129 |
19 Jan 22 |
peter |
115 |
} |
4129 |
19 Jan 22 |
peter |
116 |
|
4129 |
19 Jan 22 |
peter |
117 |
utility::Matrix m2(2,2,1.0); |
4129 |
19 Jan 22 |
peter |
118 |
VIEW view1(m, 1, 1, m2.rows(), m2.columns()); |
4129 |
19 Jan 22 |
peter |
119 |
VIEW view2(m, 1, 1, m2.rows(), m2.columns()); |
4129 |
19 Jan 22 |
peter |
120 |
view2 = m2; |
4129 |
19 Jan 22 |
peter |
121 |
if (!suite.add(suite.equal_matrix(view1, view2))) { |
4129 |
19 Jan 22 |
peter |
122 |
suite.err() << "view1 != view2 after = m2\n"; |
4129 |
19 Jan 22 |
peter |
123 |
} |
4129 |
19 Jan 22 |
peter |
124 |
view2 = m2 + m2; |
4129 |
19 Jan 22 |
peter |
125 |
if (!suite.add(suite.equal_matrix(view1, view2))) { |
4129 |
19 Jan 22 |
peter |
126 |
suite.err() << "view1 != view2 after = m2 + m2\n"; |
4129 |
19 Jan 22 |
peter |
127 |
} |
4129 |
19 Jan 22 |
peter |
128 |
view2 += m2*m2; |
4129 |
19 Jan 22 |
peter |
129 |
if (!suite.add(suite.equal_matrix(view1, view2))) { |
4129 |
19 Jan 22 |
peter |
130 |
suite.err() << "view1 != view2 after += m2*m2\n"; |
4129 |
19 Jan 22 |
peter |
131 |
} |
4129 |
19 Jan 22 |
peter |
132 |
view2 -= m2*m2; |
4129 |
19 Jan 22 |
peter |
133 |
if (!suite.add(suite.equal_matrix(view1, view2))) { |
4129 |
19 Jan 22 |
peter |
134 |
suite.err() << "view1 != view2 after -= m2*m2\n"; |
4129 |
19 Jan 22 |
peter |
135 |
} |
4129 |
19 Jan 22 |
peter |
136 |
view2 *= 1.2; |
4129 |
19 Jan 22 |
peter |
137 |
if (!suite.add(suite.equal_matrix(view1, view2))) { |
4129 |
19 Jan 22 |
peter |
138 |
suite.err() << "view1 != view2 after *= 1.2\n"; |
4129 |
19 Jan 22 |
peter |
139 |
} |
4129 |
19 Jan 22 |
peter |
140 |
|
4129 |
19 Jan 22 |
peter |
// create views that overlap with view |
4129 |
19 Jan 22 |
peter |
142 |
VIEW view3(m, 1, 5, 2, 2); |
4129 |
19 Jan 22 |
peter |
143 |
utility::Matrix copy3(view3); |
4129 |
19 Jan 22 |
peter |
144 |
VIEW view4(m, 2, 6, 2, 2); |
4129 |
19 Jan 22 |
peter |
145 |
suite.out() << "m:\n" << m << "\n"; |
4129 |
19 Jan 22 |
peter |
146 |
suite.out() << "copy3:\n" << copy3 << "\n"; |
4129 |
19 Jan 22 |
peter |
147 |
suite.out() << "view3:\n" << view3 << "\n"; |
4129 |
19 Jan 22 |
peter |
148 |
suite.out() << "view4:\n" << view4 << "\n"; |
4129 |
19 Jan 22 |
peter |
149 |
view4 = view3; |
4129 |
19 Jan 22 |
peter |
150 |
suite.add(suite.equal_matrix(view4, copy3)); |
4129 |
19 Jan 22 |
peter |
151 |
} |
4129 |
19 Jan 22 |
peter |
152 |
|
4129 |
19 Jan 22 |
peter |
153 |
|
4131 |
21 Jan 22 |
peter |
154 |
template<class VIEW> |
4131 |
21 Jan 22 |
peter |
155 |
void test_iterators(test::Suite& suite, utility::Matrix m) |
4131 |
21 Jan 22 |
peter |
156 |
{ |
4131 |
21 Jan 22 |
peter |
157 |
suite.out () << __func__ << "\n"; |
4131 |
21 Jan 22 |
peter |
158 |
size_t n1 = 2; |
4131 |
21 Jan 22 |
peter |
159 |
size_t n2 = 4; |
4131 |
21 Jan 22 |
peter |
160 |
VIEW view(m, 1, 3, n1, n2); |
4131 |
21 Jan 22 |
peter |
161 |
|
4131 |
21 Jan 22 |
peter |
162 |
suite.test_random_access_iterator(view.begin()); |
4131 |
21 Jan 22 |
peter |
163 |
suite.test_random_access_iterator(view.begin_row(0)); |
4131 |
21 Jan 22 |
peter |
164 |
suite.test_random_access_iterator(view.begin_column(0)); |
4131 |
21 Jan 22 |
peter |
165 |
|
4131 |
21 Jan 22 |
peter |
166 |
suite.out() << "testing ::row_iterator\n"; |
4131 |
21 Jan 22 |
peter |
167 |
size_t n = view.end_row(0) - view.begin_row(0); |
4131 |
21 Jan 22 |
peter |
168 |
if (n != n2) { |
4131 |
21 Jan 22 |
peter |
169 |
suite.add(false); |
4131 |
21 Jan 22 |
peter |
170 |
suite.err() << "end - begin: " << n << "; expected " << n2 << "\n"; |
4131 |
21 Jan 22 |
peter |
171 |
} |
4131 |
21 Jan 22 |
peter |
172 |
if (view.begin_row(0)[1] != view(0,1)) { |
4131 |
21 Jan 22 |
peter |
173 |
suite.add(false); |
4131 |
21 Jan 22 |
peter |
174 |
suite.err() << "error: view.begin_row(0)[1]: " << view.begin_row(0)[1] |
4131 |
21 Jan 22 |
peter |
175 |
<< "; expected: " << view(0,1) << "\n"; |
4131 |
21 Jan 22 |
peter |
176 |
} |
4131 |
21 Jan 22 |
peter |
177 |
|
4131 |
21 Jan 22 |
peter |
178 |
suite.out() << "testing ::column_iterator\n"; |
4131 |
21 Jan 22 |
peter |
179 |
n = view.end_column(0) - view.begin_column(0); |
4131 |
21 Jan 22 |
peter |
180 |
if (n != n1) { |
4131 |
21 Jan 22 |
peter |
181 |
suite.add(false); |
4131 |
21 Jan 22 |
peter |
182 |
suite.err() << "end - begin: " << n << "; expected " << n1 << "\n"; |
4131 |
21 Jan 22 |
peter |
183 |
} |
4131 |
21 Jan 22 |
peter |
184 |
if (view.begin_column(0)[1] != view(1,0)) { |
4131 |
21 Jan 22 |
peter |
185 |
suite.add(false); |
4131 |
21 Jan 22 |
peter |
186 |
suite.err() << "error: view.begin_column(0)[1]: " << view.begin_column(0)[1] |
4131 |
21 Jan 22 |
peter |
187 |
<< "; expected: " << view(1,0) << "\n"; |
4131 |
21 Jan 22 |
peter |
188 |
} |
4131 |
21 Jan 22 |
peter |
189 |
|
4131 |
21 Jan 22 |
peter |
190 |
suite.out() << "testing ::iterator\n"; |
4131 |
21 Jan 22 |
peter |
191 |
n = view.end() - view.begin(); |
4131 |
21 Jan 22 |
peter |
192 |
if (n != n1*n2) { |
4131 |
21 Jan 22 |
peter |
193 |
suite.add(false); |
4131 |
21 Jan 22 |
peter |
194 |
suite.err() << "end - begin: " << n << "; expected " << n1*n2 << "\n"; |
4131 |
21 Jan 22 |
peter |
195 |
} |
4131 |
21 Jan 22 |
peter |
196 |
if (view.begin()[n2] != view(1,0)) { |
4131 |
21 Jan 22 |
peter |
197 |
suite.add(false); |
4131 |
21 Jan 22 |
peter |
198 |
suite.err() << "error: view.begin(n2)[1]: " << view.begin()[n2] |
4131 |
21 Jan 22 |
peter |
199 |
<< "; expected: " << view(1,0) << "\n"; |
4131 |
21 Jan 22 |
peter |
200 |
} |
4131 |
21 Jan 22 |
peter |
201 |
} |
4131 |
21 Jan 22 |
peter |
202 |
|
4131 |
21 Jan 22 |
peter |
203 |
|
4137 |
21 Jan 22 |
peter |
204 |
utility::MatrixView make_view(utility::Matrix& m) |
4137 |
21 Jan 22 |
peter |
205 |
{ |
4137 |
21 Jan 22 |
peter |
206 |
return utility::MatrixView(m); |
4137 |
21 Jan 22 |
peter |
207 |
} |
4137 |
21 Jan 22 |
peter |
208 |
|
4137 |
21 Jan 22 |
peter |
209 |
|
4129 |
19 Jan 22 |
peter |
210 |
void test_matrix_view(test::Suite& suite) |
4129 |
19 Jan 22 |
peter |
211 |
{ |
4132 |
21 Jan 22 |
peter |
212 |
BOOST_CONCEPT_ASSERT((utility::Mutable_Container2D<utility::MatrixView>)); |
4129 |
19 Jan 22 |
peter |
213 |
suite.out() << "=== " << __func__ << "===\n"; |
4129 |
19 Jan 22 |
peter |
214 |
using utility::MatrixView; |
4129 |
19 Jan 22 |
peter |
// test constructors |
4129 |
19 Jan 22 |
peter |
216 |
utility::Matrix m(5, 10); |
4129 |
19 Jan 22 |
peter |
217 |
for (size_t i=0; i<m.rows(); ++i) |
4129 |
19 Jan 22 |
peter |
218 |
for (size_t j=0; j<m.columns(); ++j) |
4129 |
19 Jan 22 |
peter |
219 |
m(i,j) = 10*i + j; |
4129 |
19 Jan 22 |
peter |
220 |
|
4129 |
19 Jan 22 |
peter |
221 |
test_constructors<MatrixView>(suite, m); |
4129 |
19 Jan 22 |
peter |
222 |
test_assignment<MatrixView>(suite, m); |
4131 |
21 Jan 22 |
peter |
223 |
test_iterators<MatrixView>(suite, m); |
4137 |
21 Jan 22 |
peter |
224 |
MatrixView view = make_view(m); |
4141 |
01 Feb 22 |
peter |
225 |
|
4129 |
19 Jan 22 |
peter |
226 |
} |
4129 |
19 Jan 22 |
peter |
227 |
|
4129 |
19 Jan 22 |
peter |
228 |
|
4137 |
21 Jan 22 |
peter |
229 |
utility::MatrixConstView make_const_view(const utility::Matrix& m) |
4137 |
21 Jan 22 |
peter |
230 |
{ |
4137 |
21 Jan 22 |
peter |
231 |
return utility::MatrixConstView(m); |
4137 |
21 Jan 22 |
peter |
232 |
} |
4137 |
21 Jan 22 |
peter |
233 |
|
4137 |
21 Jan 22 |
peter |
234 |
|
4129 |
19 Jan 22 |
peter |
235 |
void test_matrix_const_view(test::Suite& suite) |
4129 |
19 Jan 22 |
peter |
236 |
{ |
4132 |
21 Jan 22 |
peter |
237 |
BOOST_CONCEPT_ASSERT((utility::Container2D<utility::MatrixConstView>)); |
4129 |
19 Jan 22 |
peter |
238 |
suite.out() << "=== " << __func__ << "===\n"; |
4129 |
19 Jan 22 |
peter |
239 |
using utility::MatrixConstView; |
4129 |
19 Jan 22 |
peter |
// test constructors |
4129 |
19 Jan 22 |
peter |
241 |
utility::Matrix m(5, 10); |
4129 |
19 Jan 22 |
peter |
242 |
for (size_t i=0; i<m.rows(); ++i) |
4129 |
19 Jan 22 |
peter |
243 |
for (size_t j=0; j<m.columns(); ++j) |
4129 |
19 Jan 22 |
peter |
244 |
m(i,j) = 10*i + j; |
4129 |
19 Jan 22 |
peter |
245 |
test_constructors<MatrixConstView>(suite, m); |
4131 |
21 Jan 22 |
peter |
246 |
test_iterators<MatrixConstView>(suite, m); |
4137 |
21 Jan 22 |
peter |
247 |
|
4137 |
21 Jan 22 |
peter |
248 |
MatrixConstView view = make_const_view(m); |
4141 |
01 Feb 22 |
peter |
249 |
|
4141 |
01 Feb 22 |
peter |
250 |
std::vector<double> vec(100,0); |
4141 |
01 Feb 22 |
peter |
251 |
for (size_t i=0; i<vec.size(); ++i) |
4141 |
01 Feb 22 |
peter |
252 |
vec[i] = i; |
4141 |
01 Feb 22 |
peter |
253 |
utility::Matrix correct(4, 5); |
4141 |
01 Feb 22 |
peter |
254 |
for (size_t i=0; i<correct.rows(); ++i) |
4141 |
01 Feb 22 |
peter |
255 |
for (size_t j=0; j<correct.columns(); ++j) |
4141 |
01 Feb 22 |
peter |
256 |
correct(i,j) = 5*i + j; |
4141 |
01 Feb 22 |
peter |
257 |
MatrixConstView view2(&vec.front(), 4, 5); |
4141 |
01 Feb 22 |
peter |
258 |
if (!suite.equal_matrix(view2, correct)) { |
4141 |
01 Feb 22 |
peter |
259 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
260 |
suite.err() << "error: view2 not equal correct\n"; |
4141 |
01 Feb 22 |
peter |
261 |
suite.err() << "view2:\n" << view2 << "\ncorrect:\n" << correct << "\n"; |
4141 |
01 Feb 22 |
peter |
262 |
} |
4141 |
01 Feb 22 |
peter |
263 |
|
4141 |
01 Feb 22 |
peter |
264 |
size_t tda = 10; |
4141 |
01 Feb 22 |
peter |
265 |
MatrixConstView view3(&vec.front(), 4, 5, tda); |
4141 |
01 Feb 22 |
peter |
266 |
for (size_t i=0; i<correct.rows(); ++i) |
4141 |
01 Feb 22 |
peter |
267 |
for (size_t j=0; j<correct.columns(); ++j) |
4141 |
01 Feb 22 |
peter |
268 |
correct(i,j) = tda*i + j; |
4141 |
01 Feb 22 |
peter |
269 |
if (!suite.equal_matrix(view3, correct)) { |
4141 |
01 Feb 22 |
peter |
270 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
271 |
suite.err() << "error: view3 not equal correct\n"; |
4141 |
01 Feb 22 |
peter |
272 |
suite.err() << "view3:\n" << view3 << "\ncorrect:\n" << correct << "\n"; |
4141 |
01 Feb 22 |
peter |
273 |
} |
4141 |
01 Feb 22 |
peter |
274 |
|
4141 |
01 Feb 22 |
peter |
275 |
MatrixConstView view4(correct.gsl_matrix_p()); |
4141 |
01 Feb 22 |
peter |
276 |
if (!suite.equal_matrix(view4, correct)) { |
4141 |
01 Feb 22 |
peter |
277 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
278 |
suite.err() << "error: view4 not equal correct\n"; |
4141 |
01 Feb 22 |
peter |
279 |
suite.err() << "view4:\n" << view4 << "\ncorrect:\n" << correct << "\n"; |
4141 |
01 Feb 22 |
peter |
280 |
} |
4129 |
19 Jan 22 |
peter |
281 |
} |
4378 |
11 Oct 23 |
peter |
282 |
|
4378 |
11 Oct 23 |
peter |
283 |
|
4378 |
11 Oct 23 |
peter |
284 |
void test_move_constructor1(test::Suite& suite, utility::Matrix copy) |
4378 |
11 Oct 23 |
peter |
285 |
{ |
4378 |
11 Oct 23 |
peter |
// test move from matrix to matrix |
4378 |
11 Oct 23 |
peter |
287 |
{ |
4378 |
11 Oct 23 |
peter |
288 |
utility::Matrix copy2(copy); |
4378 |
11 Oct 23 |
peter |
289 |
utility::Matrix matrix(std::move(copy2)); |
4378 |
11 Oct 23 |
peter |
290 |
suite.add(suite.equal_matrix(copy, matrix)); |
4378 |
11 Oct 23 |
peter |
291 |
} |
4378 |
11 Oct 23 |
peter |
292 |
} |
4378 |
11 Oct 23 |
peter |
293 |
|
4378 |
11 Oct 23 |
peter |
294 |
|
4378 |
11 Oct 23 |
peter |
295 |
void test_move_constructor2(test::Suite& suite, utility::MatrixMutable&& m) |
4378 |
11 Oct 23 |
peter |
296 |
{ |
4378 |
11 Oct 23 |
peter |
297 |
utility::Matrix copy(m); |
4378 |
11 Oct 23 |
peter |
298 |
|
4378 |
11 Oct 23 |
peter |
// create a matrix with move constructor |
4378 |
11 Oct 23 |
peter |
300 |
utility::Matrix matrix(std::move(m)); |
4378 |
11 Oct 23 |
peter |
301 |
suite.add(suite.equal_matrix(copy, matrix)); |
4378 |
11 Oct 23 |
peter |
302 |
suite.add(suite.equal_matrix(copy, matrix)); |
4378 |
11 Oct 23 |
peter |
303 |
} |
4378 |
11 Oct 23 |
peter |
304 |
|
4378 |
11 Oct 23 |
peter |
305 |
|
4378 |
11 Oct 23 |
peter |
306 |
void test_move_constructor(test::Suite& suite) |
4378 |
11 Oct 23 |
peter |
307 |
{ |
4378 |
11 Oct 23 |
peter |
308 |
suite.out() << "testing move constructors\n"; |
4378 |
11 Oct 23 |
peter |
309 |
utility::Matrix matrix(4,3); |
4378 |
11 Oct 23 |
peter |
310 |
test_move_constructor1(suite, matrix); |
4378 |
11 Oct 23 |
peter |
311 |
|
4378 |
11 Oct 23 |
peter |
312 |
utility::MatrixView copy(matrix); |
4378 |
11 Oct 23 |
peter |
313 |
test_move_constructor2(suite, std::move(copy)); |
4378 |
11 Oct 23 |
peter |
314 |
utility::MatrixView view(matrix); |
4378 |
11 Oct 23 |
peter |
315 |
test_move_constructor2(suite, std::move(view)); |
4378 |
11 Oct 23 |
peter |
316 |
|
4378 |
11 Oct 23 |
peter |
// check that matrix has not been affected by view above |
4378 |
11 Oct 23 |
peter |
318 |
if (matrix.rows() != 4) { |
4378 |
11 Oct 23 |
peter |
319 |
suite.add(false); |
4378 |
11 Oct 23 |
peter |
320 |
suite.err() << "test_move_constructor: ::rows() returns" |
4378 |
11 Oct 23 |
peter |
321 |
<< matrix.rows() << "\n"; |
4378 |
11 Oct 23 |
peter |
322 |
} |
4378 |
11 Oct 23 |
peter |
323 |
if (matrix.columns() != 3) { |
4378 |
11 Oct 23 |
peter |
324 |
suite.add(false); |
4378 |
11 Oct 23 |
peter |
325 |
suite.err() << "test_move_constructor: ::columns() returns" |
4378 |
11 Oct 23 |
peter |
326 |
<< matrix.columns() << "\n"; |
4378 |
11 Oct 23 |
peter |
327 |
} |
4378 |
11 Oct 23 |
peter |
328 |
} |
4378 |
11 Oct 23 |
peter |
329 |
|
4378 |
11 Oct 23 |
peter |
330 |
|
4378 |
11 Oct 23 |
peter |
331 |
void test_move_assignment1(utility::Matrix& matrix, |
4378 |
11 Oct 23 |
peter |
332 |
utility::MatrixMutable&& mm) |
4378 |
11 Oct 23 |
peter |
333 |
{ |
4378 |
11 Oct 23 |
peter |
334 |
matrix = std::move(mm); |
4378 |
11 Oct 23 |
peter |
335 |
} |
4378 |
11 Oct 23 |
peter |
336 |
|
4378 |
11 Oct 23 |
peter |
337 |
|
4378 |
11 Oct 23 |
peter |
338 |
void test_move_assignment2(utility::MatrixView& view, |
4378 |
11 Oct 23 |
peter |
339 |
utility::MatrixMutable&& mm) |
4378 |
11 Oct 23 |
peter |
340 |
{ |
4378 |
11 Oct 23 |
peter |
341 |
assert(view.rows() == mm.rows()); |
4378 |
11 Oct 23 |
peter |
342 |
assert(view.columns() == mm.columns()); |
4378 |
11 Oct 23 |
peter |
343 |
view = std::move(mm); |
4378 |
11 Oct 23 |
peter |
344 |
} |
4378 |
11 Oct 23 |
peter |
345 |
|
4378 |
11 Oct 23 |
peter |
346 |
|
4378 |
11 Oct 23 |
peter |
347 |
void test_move_assignment3(utility::MatrixMutable& lhs, |
4378 |
11 Oct 23 |
peter |
348 |
utility::MatrixMutable&& rhs) |
4378 |
11 Oct 23 |
peter |
349 |
{ |
4378 |
11 Oct 23 |
peter |
350 |
assert(lhs.rows() == rhs.rows()); |
4378 |
11 Oct 23 |
peter |
351 |
assert(lhs.columns() == rhs.columns()); |
4378 |
11 Oct 23 |
peter |
352 |
lhs = std::move(rhs); |
4378 |
11 Oct 23 |
peter |
353 |
} |
4378 |
11 Oct 23 |
peter |
354 |
|
4378 |
11 Oct 23 |
peter |
355 |
|
4378 |
11 Oct 23 |
peter |
356 |
void test_move_assignment(test::Suite& suite) |
4378 |
11 Oct 23 |
peter |
357 |
{ |
4378 |
11 Oct 23 |
peter |
358 |
utility::Matrix matrix(4,3); |
4378 |
11 Oct 23 |
peter |
359 |
utility::MatrixView view(matrix); |
4378 |
11 Oct 23 |
peter |
360 |
|
4378 |
11 Oct 23 |
peter |
// assign matrix&& to matrix |
4378 |
11 Oct 23 |
peter |
362 |
{ |
4378 |
11 Oct 23 |
peter |
363 |
utility::Matrix m; |
4378 |
11 Oct 23 |
peter |
364 |
utility::Matrix copy(matrix); |
4378 |
11 Oct 23 |
peter |
365 |
m = std::move(copy); |
4378 |
11 Oct 23 |
peter |
366 |
} |
4378 |
11 Oct 23 |
peter |
367 |
|
4378 |
11 Oct 23 |
peter |
// assign matrix&& to view |
4378 |
11 Oct 23 |
peter |
369 |
{ |
4378 |
11 Oct 23 |
peter |
370 |
utility::MatrixView v(matrix); |
4378 |
11 Oct 23 |
peter |
371 |
utility::Matrix copy(matrix); |
4378 |
11 Oct 23 |
peter |
372 |
v = std::move(copy); |
4378 |
11 Oct 23 |
peter |
373 |
} |
4378 |
11 Oct 23 |
peter |
374 |
|
4378 |
11 Oct 23 |
peter |
// assign view&& to matrix |
4378 |
11 Oct 23 |
peter |
376 |
{ |
4378 |
11 Oct 23 |
peter |
377 |
utility::Matrix m; |
4378 |
11 Oct 23 |
peter |
378 |
utility::Matrix view2(view); |
4378 |
11 Oct 23 |
peter |
379 |
m = std::move(view2); |
4378 |
11 Oct 23 |
peter |
380 |
} |
4378 |
11 Oct 23 |
peter |
381 |
|
4378 |
11 Oct 23 |
peter |
// assign view&& to view |
4378 |
11 Oct 23 |
peter |
383 |
{ |
4378 |
11 Oct 23 |
peter |
384 |
utility::Matrix view1(matrix); |
4378 |
11 Oct 23 |
peter |
385 |
utility::Matrix view2(view); |
4378 |
11 Oct 23 |
peter |
386 |
view1 = std::move(view2); |
4378 |
11 Oct 23 |
peter |
387 |
} |
4378 |
11 Oct 23 |
peter |
388 |
|
4378 |
11 Oct 23 |
peter |
// assign MatrixMutabale&& to Matrix |
4378 |
11 Oct 23 |
peter |
390 |
{ |
4378 |
11 Oct 23 |
peter |
391 |
utility::Matrix copy(matrix); |
4378 |
11 Oct 23 |
peter |
392 |
utility::Matrix copy2(matrix); |
4378 |
11 Oct 23 |
peter |
393 |
test_move_assignment1(copy, std::move(copy2)); |
4378 |
11 Oct 23 |
peter |
394 |
utility::MatrixView view2(matrix); |
4378 |
11 Oct 23 |
peter |
395 |
test_move_assignment1(copy, std::move(view2)); |
4378 |
11 Oct 23 |
peter |
396 |
|
4378 |
11 Oct 23 |
peter |
// lhs and rhs have the same owner |
4378 |
11 Oct 23 |
peter |
398 |
utility::Matrix copy3(matrix); |
4378 |
11 Oct 23 |
peter |
399 |
utility::MatrixView view3(copy3); |
4378 |
11 Oct 23 |
peter |
400 |
test_move_assignment1(copy3, std::move(view3)); |
4378 |
11 Oct 23 |
peter |
401 |
} |
4378 |
11 Oct 23 |
peter |
402 |
|
4378 |
11 Oct 23 |
peter |
403 |
|
4378 |
11 Oct 23 |
peter |
// assign MatrixMutabale&& to MatrixView |
4378 |
11 Oct 23 |
peter |
405 |
{ |
4378 |
11 Oct 23 |
peter |
406 |
utility::MatrixView view(matrix); |
4378 |
11 Oct 23 |
peter |
407 |
|
4378 |
11 Oct 23 |
peter |
408 |
utility::Matrix copy(matrix); |
4378 |
11 Oct 23 |
peter |
409 |
test_move_assignment2(view, std::move(copy)); |
4378 |
11 Oct 23 |
peter |
410 |
|
4378 |
11 Oct 23 |
peter |
411 |
utility::Matrix copy2(matrix); |
4378 |
11 Oct 23 |
peter |
412 |
utility::MatrixView view2(copy2); |
4378 |
11 Oct 23 |
peter |
413 |
test_move_assignment2(view, std::move(view2)); |
4378 |
11 Oct 23 |
peter |
414 |
|
4378 |
11 Oct 23 |
peter |
// lhs and rhs have the same owner |
4378 |
11 Oct 23 |
peter |
416 |
utility::Matrix view3(matrix); |
4378 |
11 Oct 23 |
peter |
417 |
utility::MatrixView view4(matrix); |
4378 |
11 Oct 23 |
peter |
418 |
test_move_assignment2(view4, std::move(view3)); |
4378 |
11 Oct 23 |
peter |
419 |
} |
4378 |
11 Oct 23 |
peter |
420 |
|
4378 |
11 Oct 23 |
peter |
421 |
|
4378 |
11 Oct 23 |
peter |
// assign MatrixMutabale&& to MatrixMutable |
4378 |
11 Oct 23 |
peter |
423 |
{ |
4378 |
11 Oct 23 |
peter |
424 |
utility::Matrix copy1(matrix); |
4378 |
11 Oct 23 |
peter |
425 |
utility::Matrix copy2(matrix); |
4378 |
11 Oct 23 |
peter |
426 |
test_move_assignment3(copy1, std::move(copy2)); |
4378 |
11 Oct 23 |
peter |
427 |
|
4378 |
11 Oct 23 |
peter |
428 |
utility::Matrix copy3(matrix); |
4378 |
11 Oct 23 |
peter |
429 |
utility::Matrix view3(matrix); |
4378 |
11 Oct 23 |
peter |
430 |
test_move_assignment3(copy3, std::move(view3)); |
4378 |
11 Oct 23 |
peter |
431 |
|
4378 |
11 Oct 23 |
peter |
432 |
utility::Matrix copy4(matrix); |
4378 |
11 Oct 23 |
peter |
433 |
utility::Matrix view4(matrix); |
4378 |
11 Oct 23 |
peter |
434 |
test_move_assignment3(view4, std::move(copy4)); |
4378 |
11 Oct 23 |
peter |
435 |
|
4378 |
11 Oct 23 |
peter |
436 |
utility::Matrix copy5(matrix); |
4378 |
11 Oct 23 |
peter |
437 |
utility::Matrix view5(copy5); |
4378 |
11 Oct 23 |
peter |
438 |
utility::Matrix copy6(copy5); |
4378 |
11 Oct 23 |
peter |
439 |
utility::Matrix view6(copy6); |
4378 |
11 Oct 23 |
peter |
440 |
test_move_assignment3(view5, std::move(view6)); |
4378 |
11 Oct 23 |
peter |
441 |
|
4378 |
11 Oct 23 |
peter |
442 |
utility::Matrix copy7(matrix); |
4378 |
11 Oct 23 |
peter |
443 |
utility::Matrix view7(copy7); |
4378 |
11 Oct 23 |
peter |
444 |
utility::Matrix view8(copy7); |
4378 |
11 Oct 23 |
peter |
445 |
test_move_assignment3(view7, std::move(view8)); |
4378 |
11 Oct 23 |
peter |
446 |
} |
4378 |
11 Oct 23 |
peter |
447 |
|
4378 |
11 Oct 23 |
peter |
// check that matrix has not been affected by view above |
4378 |
11 Oct 23 |
peter |
449 |
if (matrix.rows() != 4) { |
4378 |
11 Oct 23 |
peter |
450 |
suite.add(false); |
4378 |
11 Oct 23 |
peter |
451 |
suite.err() << "test_move_constructor: ::rows() returns" |
4378 |
11 Oct 23 |
peter |
452 |
<< matrix.rows() << "\n"; |
4378 |
11 Oct 23 |
peter |
453 |
} |
4378 |
11 Oct 23 |
peter |
454 |
if (matrix.columns() != 3) { |
4378 |
11 Oct 23 |
peter |
455 |
suite.add(false); |
4378 |
11 Oct 23 |
peter |
456 |
suite.err() << "test_move_constructor: ::columns() returns" |
4378 |
11 Oct 23 |
peter |
457 |
<< matrix.columns() << "\n"; |
4378 |
11 Oct 23 |
peter |
458 |
} |
4378 |
11 Oct 23 |
peter |
459 |
} |