1114 |
21 Feb 08 |
peter |
// $Id$ |
1114 |
21 Feb 08 |
peter |
2 |
|
1114 |
21 Feb 08 |
peter |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2012, 2017, 2022, 2023 Peter Johansson |
1114 |
21 Feb 08 |
peter |
6 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
1114 |
21 Feb 08 |
peter |
8 |
|
1114 |
21 Feb 08 |
peter |
The yat library is free software; you can redistribute it and/or |
1114 |
21 Feb 08 |
peter |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
1114 |
21 Feb 08 |
peter |
License, or (at your option) any later version. |
1114 |
21 Feb 08 |
peter |
13 |
|
1114 |
21 Feb 08 |
peter |
The yat library is distributed in the hope that it will be useful, |
1114 |
21 Feb 08 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1114 |
21 Feb 08 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1114 |
21 Feb 08 |
peter |
General Public License for more details. |
1114 |
21 Feb 08 |
peter |
18 |
|
1114 |
21 Feb 08 |
peter |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
1114 |
21 Feb 08 |
peter |
21 |
*/ |
1114 |
21 Feb 08 |
peter |
22 |
|
2881 |
18 Nov 12 |
peter |
23 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
24 |
|
1248 |
19 Mar 08 |
peter |
25 |
#include "Suite.h" |
1248 |
19 Mar 08 |
peter |
26 |
|
1121 |
22 Feb 08 |
peter |
27 |
#include "yat/utility/Matrix.h" |
1114 |
21 Feb 08 |
peter |
28 |
#include "yat/utility/VectorBase.h" |
1114 |
21 Feb 08 |
peter |
29 |
#include "yat/utility/VectorMutable.h" |
1114 |
21 Feb 08 |
peter |
30 |
#include "yat/utility/VectorView.h" |
1114 |
21 Feb 08 |
peter |
31 |
#include "yat/utility/VectorConstView.h" |
1114 |
21 Feb 08 |
peter |
32 |
|
1119 |
21 Feb 08 |
peter |
33 |
#include <fstream> |
1114 |
21 Feb 08 |
peter |
34 |
#include <iostream> |
1114 |
21 Feb 08 |
peter |
35 |
#include <string> |
1114 |
21 Feb 08 |
peter |
36 |
|
1720 |
15 Jan 09 |
peter |
37 |
using namespace theplu::yat; |
1720 |
15 Jan 09 |
peter |
38 |
void test_rvalue(test::Suite&); |
1720 |
15 Jan 09 |
peter |
39 |
void test_rvalue_func(test::Suite& suite, const utility::VectorBase& vec); |
4269 |
13 Jan 23 |
peter |
40 |
void test_ticket987(test::Suite& suite); |
1720 |
15 Jan 09 |
peter |
41 |
|
1248 |
19 Mar 08 |
peter |
42 |
int main(int argc, char* argv[]) |
4141 |
01 Feb 22 |
peter |
43 |
{ |
1119 |
21 Feb 08 |
peter |
44 |
using namespace theplu::yat::utility; |
1248 |
19 Mar 08 |
peter |
45 |
theplu::yat::test::Suite suite(argc, argv); |
1248 |
19 Mar 08 |
peter |
46 |
suite.err() << "testing vector" << std::endl; |
1114 |
21 Feb 08 |
peter |
47 |
|
1121 |
22 Feb 08 |
peter |
48 |
Matrix data1; |
1114 |
21 Feb 08 |
peter |
49 |
data1.resize(3,1); |
1114 |
21 Feb 08 |
peter |
50 |
for(size_t i=0;i<3;i++) { |
1114 |
21 Feb 08 |
peter |
51 |
data1(i,0)=7+i; |
1114 |
21 Feb 08 |
peter |
52 |
} |
1114 |
21 Feb 08 |
peter |
53 |
|
4200 |
19 Aug 22 |
peter |
54 |
|
1121 |
22 Feb 08 |
peter |
55 |
Matrix data2(3,1,1.0); |
1114 |
21 Feb 08 |
peter |
56 |
|
1114 |
21 Feb 08 |
peter |
57 |
VectorConstView a=data1.column_const_view(0); |
1114 |
21 Feb 08 |
peter |
58 |
VectorView b=data2.column_view(0); |
1137 |
24 Feb 08 |
peter |
59 |
VectorView bajs(data2, 0, false); |
1137 |
24 Feb 08 |
peter |
60 |
if (a(0)!=data1(0,0)){ |
1248 |
19 Mar 08 |
peter |
61 |
suite.err() << "Matrix::column_const_view failed\n"; |
1248 |
19 Mar 08 |
peter |
62 |
suite.add(false); |
1137 |
24 Feb 08 |
peter |
63 |
} |
1137 |
24 Feb 08 |
peter |
64 |
if (b(0)!=data2(0,0)){ |
1248 |
19 Mar 08 |
peter |
65 |
suite.err() << "Matrix::column_view failed\n"; |
1248 |
19 Mar 08 |
peter |
66 |
suite.add(false); |
1137 |
24 Feb 08 |
peter |
67 |
} |
1114 |
21 Feb 08 |
peter |
68 |
|
1119 |
21 Feb 08 |
peter |
69 |
data1.column_const_view(0); |
1119 |
21 Feb 08 |
peter |
70 |
if (b.size()!=3){ |
1248 |
19 Mar 08 |
peter |
71 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
72 |
suite.err() << "ERROR: expected size to be 3 - found " << b.size() << std::endl; |
1119 |
21 Feb 08 |
peter |
73 |
} |
1119 |
21 Feb 08 |
peter |
74 |
if ( !(b(0)==1 && b(1)==(1) && b(2)==1) ) |
1248 |
19 Mar 08 |
peter |
75 |
suite.add(false); |
1131 |
23 Feb 08 |
peter |
76 |
|
1137 |
24 Feb 08 |
peter |
77 |
if (a.begin()!=data1.begin_column(0)){ |
1248 |
19 Mar 08 |
peter |
78 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
79 |
suite.err() << "VectorConstView::begin should equal " |
1137 |
24 Feb 08 |
peter |
80 |
<< "Matrix::begin_column(size_t)\n"; |
1137 |
24 Feb 08 |
peter |
81 |
} |
1137 |
24 Feb 08 |
peter |
82 |
if (a.end()!=data1.end_column(0)) { |
1248 |
19 Mar 08 |
peter |
83 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
84 |
suite.err() << "VectorConstView::end should equal " |
1137 |
24 Feb 08 |
peter |
85 |
<< "Matrix::end_column(size_t)\n"; |
1137 |
24 Feb 08 |
peter |
86 |
} |
1137 |
24 Feb 08 |
peter |
87 |
if (b.begin()!=data2.begin_column(0)){ |
1248 |
19 Mar 08 |
peter |
88 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
89 |
suite.err() << "VectorView::begin " |
1137 |
24 Feb 08 |
peter |
90 |
<< b.begin().base() << " should equal\n" |
1137 |
24 Feb 08 |
peter |
91 |
<< "Matrix::begin_column(size_t) " |
1137 |
24 Feb 08 |
peter |
92 |
<< data1.begin_column(0).base() << std::endl; |
1137 |
24 Feb 08 |
peter |
93 |
} |
1137 |
24 Feb 08 |
peter |
94 |
if (b.end()!=data2.end_column(0)){ |
1248 |
19 Mar 08 |
peter |
95 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
96 |
suite.err() << "VectorView::end should equal " |
1137 |
24 Feb 08 |
peter |
97 |
<< "Matrix::end_column(size_t)\n"; |
1137 |
24 Feb 08 |
peter |
98 |
} |
1137 |
24 Feb 08 |
peter |
99 |
VectorConstView c=data1.row_const_view(0); |
1137 |
24 Feb 08 |
peter |
100 |
VectorView d=data2.row_view(0); |
1137 |
24 Feb 08 |
peter |
101 |
if (c.begin()!=data1.begin_row(0)) { |
1248 |
19 Mar 08 |
peter |
102 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
103 |
suite.err() << "VectorConstView::begin should equal " |
1137 |
24 Feb 08 |
peter |
104 |
<< "Matrix::begin_row(size_t)\n"; |
1137 |
24 Feb 08 |
peter |
105 |
} |
1137 |
24 Feb 08 |
peter |
106 |
if (c.end()!=data1.end_row(0)){ |
1248 |
19 Mar 08 |
peter |
107 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
108 |
suite.err() << "VectorConstView::end should equal " |
1137 |
24 Feb 08 |
peter |
109 |
<< "Matrix::end_row(size_t)\n"; |
1137 |
24 Feb 08 |
peter |
110 |
} |
1137 |
24 Feb 08 |
peter |
111 |
if (d.begin()!=data2.begin_row(0)){ |
1248 |
19 Mar 08 |
peter |
112 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
113 |
suite.err() << "VectorView::begin should equal " |
1137 |
24 Feb 08 |
peter |
114 |
<< "Matrix::begin_row(size_t)\n"; |
1137 |
24 Feb 08 |
peter |
115 |
} |
1137 |
24 Feb 08 |
peter |
116 |
if (d.end()!=data2.end_row(0)){ |
1248 |
19 Mar 08 |
peter |
117 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
118 |
suite.err() << "VectorView::end should equal " |
1137 |
24 Feb 08 |
peter |
119 |
<< "Matrix::end_row(size_t)\n"; |
1137 |
24 Feb 08 |
peter |
120 |
} |
1137 |
24 Feb 08 |
peter |
121 |
|
1137 |
24 Feb 08 |
peter |
122 |
|
1131 |
23 Feb 08 |
peter |
// test that VectorConstView created from VectorView is independent |
1131 |
23 Feb 08 |
peter |
// of VectorView |
1131 |
23 Feb 08 |
peter |
125 |
{ |
1131 |
23 Feb 08 |
peter |
126 |
Vector vec(10,1.2); |
1131 |
23 Feb 08 |
peter |
127 |
VectorView* vv = new VectorView(vec); |
1131 |
23 Feb 08 |
peter |
128 |
VectorConstView vcv(*vv); |
1131 |
23 Feb 08 |
peter |
129 |
delete vv; |
1131 |
23 Feb 08 |
peter |
130 |
if (vcv.size()!=10) |
1248 |
19 Mar 08 |
peter |
131 |
suite.add(false); |
1131 |
23 Feb 08 |
peter |
132 |
if (vcv(0)!=1.2) |
1248 |
19 Mar 08 |
peter |
133 |
suite.add(false); |
1131 |
23 Feb 08 |
peter |
134 |
} |
1720 |
15 Jan 09 |
peter |
135 |
test_rvalue(suite); |
1114 |
21 Feb 08 |
peter |
136 |
|
4141 |
01 Feb 22 |
peter |
// test view from double* or gsl_vector* |
4141 |
01 Feb 22 |
peter |
138 |
std::vector<double> vec(100,0); |
4141 |
01 Feb 22 |
peter |
139 |
VectorView view2(&vec.front(), 5); |
4141 |
01 Feb 22 |
peter |
140 |
VectorConstView cview2(&vec.front(), 5); |
4141 |
01 Feb 22 |
peter |
141 |
if (view2.size() != 5) { |
4141 |
01 Feb 22 |
peter |
142 |
suite.err() << "error: view2 incorrect dimensions\n"; |
4141 |
01 Feb 22 |
peter |
143 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
144 |
} |
4141 |
01 Feb 22 |
peter |
145 |
else { |
4141 |
01 Feb 22 |
peter |
146 |
for (size_t i=0; i<view2.size(); ++i) { |
4141 |
01 Feb 22 |
peter |
147 |
double x = 12*i; |
4141 |
01 Feb 22 |
peter |
148 |
view2(i) = x; |
4141 |
01 Feb 22 |
peter |
149 |
if (vec[i] != x) { |
4141 |
01 Feb 22 |
peter |
150 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
151 |
} |
4141 |
01 Feb 22 |
peter |
152 |
} |
4141 |
01 Feb 22 |
peter |
153 |
} |
4141 |
01 Feb 22 |
peter |
154 |
if (view2 != cview2) { |
4141 |
01 Feb 22 |
peter |
155 |
suite.err() << "error: view2 != cview2\n"; |
4141 |
01 Feb 22 |
peter |
156 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
157 |
} |
4141 |
01 Feb 22 |
peter |
158 |
|
4141 |
01 Feb 22 |
peter |
159 |
size_t stride = 3; |
4141 |
01 Feb 22 |
peter |
160 |
VectorView view3(&vec.front(), 5, stride); |
4141 |
01 Feb 22 |
peter |
161 |
VectorConstView cview3(&vec.front(), 5, stride); |
4141 |
01 Feb 22 |
peter |
162 |
if (view3.size() != 5) { |
4141 |
01 Feb 22 |
peter |
163 |
suite.err() << "error: view3 incorrect dimensions\n"; |
4141 |
01 Feb 22 |
peter |
164 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
165 |
} |
4141 |
01 Feb 22 |
peter |
166 |
else { |
4141 |
01 Feb 22 |
peter |
167 |
for (size_t i=0; i<view3.size(); ++i) { |
4141 |
01 Feb 22 |
peter |
168 |
double x = 12*i; |
4141 |
01 Feb 22 |
peter |
169 |
view3(i) = x; |
4141 |
01 Feb 22 |
peter |
170 |
if (vec[stride*i] != x) { |
4141 |
01 Feb 22 |
peter |
171 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
172 |
} |
4141 |
01 Feb 22 |
peter |
173 |
} |
4141 |
01 Feb 22 |
peter |
174 |
} |
4141 |
01 Feb 22 |
peter |
175 |
if (view3 != cview3) { |
4141 |
01 Feb 22 |
peter |
176 |
suite.err() << "error: view3 != cview3\n"; |
4141 |
01 Feb 22 |
peter |
177 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
178 |
} |
4141 |
01 Feb 22 |
peter |
179 |
|
4141 |
01 Feb 22 |
peter |
180 |
Vector v(5); |
4141 |
01 Feb 22 |
peter |
181 |
gsl_vector* p = v.gsl_vector_p(); |
4141 |
01 Feb 22 |
peter |
182 |
const gsl_vector* cp = v.gsl_vector_p(); |
4141 |
01 Feb 22 |
peter |
183 |
VectorView view4(p); |
4141 |
01 Feb 22 |
peter |
184 |
VectorConstView cview4(cp); |
4141 |
01 Feb 22 |
peter |
185 |
view4(0) = 3.12; |
4141 |
01 Feb 22 |
peter |
186 |
view4(3) = 4.28; |
4141 |
01 Feb 22 |
peter |
187 |
if (view4 != v) { |
4141 |
01 Feb 22 |
peter |
188 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
189 |
suite.err() << "error: v not equal view4\n"; |
4141 |
01 Feb 22 |
peter |
190 |
} |
4141 |
01 Feb 22 |
peter |
191 |
if (view4 != cview4) { |
4141 |
01 Feb 22 |
peter |
192 |
suite.err() << "error: view4 != cview4\n"; |
4141 |
01 Feb 22 |
peter |
193 |
suite.add(false); |
4141 |
01 Feb 22 |
peter |
194 |
} |
4141 |
01 Feb 22 |
peter |
195 |
|
4269 |
13 Jan 23 |
peter |
196 |
test_ticket987(suite); |
4269 |
13 Jan 23 |
peter |
197 |
|
1248 |
19 Mar 08 |
peter |
198 |
return suite.return_value(); |
1114 |
21 Feb 08 |
peter |
199 |
} |
1114 |
21 Feb 08 |
peter |
200 |
|
3595 |
21 Jan 17 |
peter |
201 |
|
3595 |
21 Jan 17 |
peter |
202 |
void test_func(const utility::VectorConstView& vcv) |
3595 |
21 Jan 17 |
peter |
203 |
{ |
3595 |
21 Jan 17 |
peter |
204 |
|
3595 |
21 Jan 17 |
peter |
205 |
} |
3595 |
21 Jan 17 |
peter |
206 |
|
3595 |
21 Jan 17 |
peter |
207 |
|
1720 |
15 Jan 09 |
peter |
208 |
void test_rvalue(test::Suite& suite) |
1720 |
15 Jan 09 |
peter |
209 |
{ |
1720 |
15 Jan 09 |
peter |
210 |
using namespace utility; |
1720 |
15 Jan 09 |
peter |
211 |
suite.err() << "test rvalue" << std::endl; |
1720 |
15 Jan 09 |
peter |
212 |
Matrix m(10,10); |
1720 |
15 Jan 09 |
peter |
213 |
VectorView vv(m.row_view(0)); |
3594 |
21 Jan 17 |
peter |
214 |
vv = m.row_view(1); |
1114 |
21 Feb 08 |
peter |
215 |
|
3594 |
21 Jan 17 |
peter |
216 |
Vector vec(m.row_view(0)); |
3594 |
21 Jan 17 |
peter |
217 |
vec = m.row_view(1); |
1720 |
15 Jan 09 |
peter |
218 |
|
3594 |
21 Jan 17 |
peter |
219 |
VectorConstView vcv(m.row_view(0)); |
4200 |
19 Aug 22 |
peter |
220 |
test_rvalue_func(suite, m.row_view(0)); |
1720 |
15 Jan 09 |
peter |
221 |
|
1720 |
15 Jan 09 |
peter |
222 |
test_rvalue_func(suite, m.row_const_view(0)); |
1720 |
15 Jan 09 |
peter |
223 |
|
3595 |
21 Jan 17 |
peter |
224 |
test_func(m.row_const_view(0)); |
3595 |
21 Jan 17 |
peter |
225 |
test_func(m.row_view(0)); |
3595 |
21 Jan 17 |
peter |
226 |
|
1720 |
15 Jan 09 |
peter |
227 |
} |
1720 |
15 Jan 09 |
peter |
228 |
|
1720 |
15 Jan 09 |
peter |
229 |
void test_rvalue_func(test::Suite& suite, const utility::VectorBase& vec) |
1720 |
15 Jan 09 |
peter |
230 |
{ |
1720 |
15 Jan 09 |
peter |
231 |
suite.err() << "test_rvalue_func: size: " << vec.size() << std::endl; |
1720 |
15 Jan 09 |
peter |
232 |
} |
4269 |
13 Jan 23 |
peter |
233 |
|
4269 |
13 Jan 23 |
peter |
234 |
|
4269 |
13 Jan 23 |
peter |
// see https://dev.thep.lu.se/yat/ticket/987 |
4269 |
13 Jan 23 |
peter |
236 |
void test_ticket987(test::Suite& suite) |
4269 |
13 Jan 23 |
peter |
237 |
{ |
4269 |
13 Jan 23 |
peter |
238 |
suite.out() << "test ticket987\n"; |
4269 |
13 Jan 23 |
peter |
239 |
utility::Vector x(42); |
4269 |
13 Jan 23 |
peter |
240 |
utility::VectorConstView view(x, 1, 4); |
4269 |
13 Jan 23 |
peter |
241 |
utility::Vector y(view); |
4269 |
13 Jan 23 |
peter |
// just to avoid compiler warning |
4269 |
13 Jan 23 |
peter |
243 |
suite.out() << y << "\n"; |
4269 |
13 Jan 23 |
peter |
244 |
y = view; |
4269 |
13 Jan 23 |
peter |
245 |
#ifdef YAT_TICKET987 |
4269 |
13 Jan 23 |
peter |
246 |
utility::Vector y2 = view; |
4269 |
13 Jan 23 |
peter |
247 |
suite.out() << y2 << "\n"; |
4269 |
13 Jan 23 |
peter |
248 |
suite.add(y2 == view); |
4269 |
13 Jan 23 |
peter |
249 |
utility::Vector y3 = utility::VectorConstView(x, 1, 4); |
4269 |
13 Jan 23 |
peter |
250 |
suite.add(y3 == view); |
4269 |
13 Jan 23 |
peter |
251 |
suite.out() << y3 << "\n"; |
4269 |
13 Jan 23 |
peter |
252 |
#endif |
4269 |
13 Jan 23 |
peter |
253 |
} |