127 |
03 Aug 04 |
peter |
// $Id$ |
127 |
03 Aug 04 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
831 |
27 Mar 07 |
peter |
Copyright (C) 2004 Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér |
2121 |
13 Dec 09 |
peter |
Copyright (C) 2008, 2009 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2012, 2016, 2017, 2020 Peter Johansson |
127 |
03 Aug 04 |
peter |
10 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
12 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
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 |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
17 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
22 |
|
675 |
10 Oct 06 |
jari |
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/>. |
675 |
10 Oct 06 |
jari |
25 |
*/ |
675 |
10 Oct 06 |
jari |
26 |
|
2881 |
18 Nov 12 |
peter |
27 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
28 |
|
1248 |
19 Mar 08 |
peter |
29 |
#include "Suite.h" |
1248 |
19 Mar 08 |
peter |
30 |
|
675 |
10 Oct 06 |
jari |
31 |
#include "yat/utility/Exception.h" |
710 |
20 Dec 06 |
jari |
32 |
#include "yat/utility/FileUtil.h" |
675 |
10 Oct 06 |
jari |
33 |
#include "yat/utility/utility.h" |
1120 |
21 Feb 08 |
peter |
34 |
#include "yat/utility/Vector.h" |
1027 |
02 Feb 08 |
peter |
35 |
#include "yat/utility/VectorConstView.h" |
1015 |
01 Feb 08 |
peter |
36 |
#include "yat/utility/VectorView.h" |
675 |
10 Oct 06 |
jari |
37 |
|
1261 |
08 Apr 08 |
peter |
38 |
#include <cstddef> |
328 |
30 May 05 |
jari |
39 |
#include <fstream> |
1793 |
11 Feb 09 |
peter |
40 |
#include <iostream> |
341 |
07 Jun 05 |
jari |
41 |
#include <sstream> |
328 |
30 May 05 |
jari |
42 |
|
680 |
11 Oct 06 |
jari |
43 |
using namespace theplu::yat; |
301 |
30 Apr 05 |
peter |
44 |
|
328 |
30 May 05 |
jari |
45 |
void check_file_access(std::string& str) |
328 |
30 May 05 |
jari |
46 |
{ |
4200 |
19 Aug 22 |
peter |
47 |
|
711 |
21 Dec 06 |
jari |
48 |
if (utility::FileUtil(str).permissions("r")) { |
328 |
30 May 05 |
jari |
49 |
std::cerr << "test_nni: Cannot access file " << str << std::endl; |
328 |
30 May 05 |
jari |
50 |
exit(-1); |
328 |
30 May 05 |
jari |
51 |
} |
328 |
30 May 05 |
jari |
52 |
} |
328 |
30 May 05 |
jari |
53 |
|
3871 |
01 Mar 20 |
peter |
54 |
void test_norm2_squared(test::Suite&); |
1954 |
07 May 09 |
jari |
55 |
void test_sort(test::Suite&); |
3467 |
10 Feb 16 |
peter |
56 |
void test_vector_mutable_assignment(test::Suite&); |
3612 |
31 Jan 17 |
peter |
57 |
void test_aliasing(test::Suite&); |
1954 |
07 May 09 |
jari |
58 |
|
1248 |
19 Mar 08 |
peter |
59 |
int main(int argc,char* argv[]) |
4200 |
19 Aug 22 |
peter |
60 |
{ |
1248 |
19 Mar 08 |
peter |
61 |
test::Suite suite(argc, argv); |
1248 |
19 Mar 08 |
peter |
62 |
suite.err() << "testing vector" << std::endl; |
257 |
04 Mar 05 |
jari |
63 |
|
1120 |
21 Feb 08 |
peter |
64 |
utility::Vector vec(12); |
4200 |
19 Aug 22 |
peter |
65 |
for (size_t i=0; i<vec.size(); i++) |
127 |
03 Aug 04 |
peter |
66 |
vec(i)=i; |
257 |
04 Mar 05 |
jari |
67 |
|
1360 |
26 Jun 08 |
peter |
68 |
suite.add(suite.test_stream(vec)); |
1360 |
26 Jun 08 |
peter |
69 |
|
257 |
04 Mar 05 |
jari |
// checking that shuffle works |
1248 |
19 Mar 08 |
peter |
71 |
suite.err() << "shuffle" << std::endl; |
782 |
05 Mar 07 |
jari |
72 |
double sum_before = utility::sum(vec); |
259 |
04 Mar 05 |
jari |
73 |
shuffle(vec); |
782 |
05 Mar 07 |
jari |
74 |
double sum_after = utility::sum(vec); |
1027 |
02 Feb 08 |
peter |
75 |
if (sum_after!=sum_before){ |
1248 |
19 Mar 08 |
peter |
76 |
suite.err() << "shuffle failed" << std::endl; |
1248 |
19 Mar 08 |
peter |
77 |
suite.add(false); |
1027 |
02 Feb 08 |
peter |
78 |
} |
257 |
04 Mar 05 |
jari |
79 |
|
257 |
04 Mar 05 |
jari |
// checking that view works |
1248 |
19 Mar 08 |
peter |
81 |
suite.err() << "view" << std::endl; |
257 |
04 Mar 05 |
jari |
82 |
sum_before=0; |
4200 |
19 Aug 22 |
peter |
83 |
for (size_t i=0; i<vec.size(); i+=2) |
1151 |
25 Feb 08 |
peter |
84 |
sum_before+=vec(i); |
1015 |
01 Feb 08 |
peter |
85 |
utility::VectorView vec_view(vec,0,6,2); |
782 |
05 Mar 07 |
jari |
86 |
sum_after=utility::sum(vec_view); |
1248 |
19 Mar 08 |
peter |
87 |
suite.add(suite.equal(sum_after, sum_before)); |
1151 |
25 Feb 08 |
peter |
88 |
vec(0)=0; |
1151 |
25 Feb 08 |
peter |
89 |
vec_view(0)=24; |
1248 |
19 Mar 08 |
peter |
90 |
suite.add(suite.equal(vec(0), vec_view(0))); |
257 |
04 Mar 05 |
jari |
91 |
|
789 |
10 Mar 07 |
jari |
// Test of const view implementation (make sure no zero pointer is |
789 |
10 Mar 07 |
jari |
// used). Here we use the bad style of not making the view const! |
793 |
11 Mar 07 |
jari |
// If test fails with a null pointer exception it is an uncatchable |
793 |
11 Mar 07 |
jari |
// core dump! |
789 |
10 Mar 07 |
jari |
96 |
{ |
1248 |
19 Mar 08 |
peter |
97 |
suite.err() << "const view implementation" << std::endl; |
1120 |
21 Feb 08 |
peter |
98 |
const utility::Vector vv(10,3.0); |
1027 |
02 Feb 08 |
peter |
99 |
utility::VectorConstView vview(vv,0,5,1); |
1120 |
21 Feb 08 |
peter |
// const utility::Vector vview(vv,0,5,1); // this is the proper line |
1120 |
21 Feb 08 |
peter |
101 |
utility::Vector vv2(5,2.0); |
789 |
10 Mar 07 |
jari |
102 |
vv2.mul(vview); // should work even without const since const arg passing |
789 |
10 Mar 07 |
jari |
103 |
vv2.div(vview); // should work even without const since const arg passing |
1248 |
19 Mar 08 |
peter |
104 |
suite.add(suite.equal(vview*vview, 3.0*3.0*vview.size())); |
789 |
10 Mar 07 |
jari |
105 |
} |
257 |
04 Mar 05 |
jari |
106 |
|
438 |
15 Dec 05 |
markus |
// checking that copy constructor creates an independent object when |
789 |
10 Mar 07 |
jari |
// a non-view vector is copied |
789 |
10 Mar 07 |
jari |
109 |
{ |
1248 |
19 Mar 08 |
peter |
110 |
suite.err() << "copy constructor" << std::endl; |
1120 |
21 Feb 08 |
peter |
111 |
utility::Vector vec2(vec); |
1248 |
19 Mar 08 |
peter |
112 |
suite.add(vec.size()==vec2.size()); |
1248 |
19 Mar 08 |
peter |
113 |
suite.add(suite.equal_range(vec2.begin(), vec2.end(), vec.begin())); |
1248 |
19 Mar 08 |
peter |
114 |
suite.add(&vec2 != &vec); |
1248 |
19 Mar 08 |
peter |
115 |
suite.add(!vec2.isview()); |
789 |
10 Mar 07 |
jari |
116 |
} |
257 |
04 Mar 05 |
jari |
117 |
|
789 |
10 Mar 07 |
jari |
// checking that copy constructor creates an independent object when |
789 |
10 Mar 07 |
jari |
// a view vector is copied |
789 |
10 Mar 07 |
jari |
120 |
{ |
1248 |
19 Mar 08 |
peter |
121 |
suite.err() << "copy contructor on view" << std::endl; |
1120 |
21 Feb 08 |
peter |
122 |
utility::Vector vec3(vec_view); |
1248 |
19 Mar 08 |
peter |
123 |
suite.add(vec_view.size()==vec3.size()); |
1248 |
19 Mar 08 |
peter |
124 |
suite.add(suite.equal_range(vec3.begin(), vec3.end(), vec_view.begin())); |
1248 |
19 Mar 08 |
peter |
125 |
suite.add(!vec3.isview()); |
789 |
10 Mar 07 |
jari |
126 |
} |
789 |
10 Mar 07 |
jari |
127 |
|
789 |
10 Mar 07 |
jari |
// checking that assignment operator throws an exception if vectors |
789 |
10 Mar 07 |
jari |
// differ in size |
789 |
10 Mar 07 |
jari |
130 |
{ |
1248 |
19 Mar 08 |
peter |
131 |
suite.err() << "assignment operator" << std::endl; |
790 |
10 Mar 07 |
jari |
// GSL will catch the error in this test there for the GSL error |
790 |
10 Mar 07 |
jari |
// handling must be disabled until after the exception is |
790 |
10 Mar 07 |
jari |
// catched. The GSL error handler is reinstated after the |
790 |
10 Mar 07 |
jari |
// try-catch construct. |
790 |
10 Mar 07 |
jari |
136 |
gsl_error_handler_t* err_handler=gsl_set_error_handler_off(); |
789 |
10 Mar 07 |
jari |
137 |
bool exception_happens=false; |
789 |
10 Mar 07 |
jari |
138 |
try { |
1120 |
21 Feb 08 |
peter |
139 |
utility::Vector v(vec_view.size()+1,0.0); |
1027 |
02 Feb 08 |
peter |
140 |
vec_view=v; |
789 |
10 Mar 07 |
jari |
141 |
} catch (utility::GSL_error& err) { |
789 |
10 Mar 07 |
jari |
142 |
exception_happens=true; |
789 |
10 Mar 07 |
jari |
143 |
} |
789 |
10 Mar 07 |
jari |
144 |
if (!exception_happens) { |
1248 |
19 Mar 08 |
peter |
145 |
suite.err() << "Vector assignment operator did not throw expected exception" |
789 |
10 Mar 07 |
jari |
146 |
<< std::endl; |
1248 |
19 Mar 08 |
peter |
147 |
suite.add(false); |
789 |
10 Mar 07 |
jari |
148 |
} |
790 |
10 Mar 07 |
jari |
149 |
gsl_set_error_handler(err_handler); |
789 |
10 Mar 07 |
jari |
150 |
} |
789 |
10 Mar 07 |
jari |
151 |
|
789 |
10 Mar 07 |
jari |
// checking that assignment operator changes the underlying object when |
789 |
10 Mar 07 |
jari |
// a view is changed. |
789 |
10 Mar 07 |
jari |
154 |
{ |
1248 |
19 Mar 08 |
peter |
155 |
suite.err() << "assignment operator on view" << std::endl; |
1151 |
25 Feb 08 |
peter |
156 |
vec(3)=vec(4)=vec(5)=13; |
1015 |
01 Feb 08 |
peter |
157 |
utility::VectorView vec_view(vec,3,3,1); |
1120 |
21 Feb 08 |
peter |
158 |
utility::Vector vec2(3,123.0); |
789 |
10 Mar 07 |
jari |
159 |
vec_view=vec2; |
1151 |
25 Feb 08 |
peter |
160 |
if (vec(3)!=vec_view(0) || vec(4)!=vec_view(1) || vec(5)!=vec_view(2)){ |
1248 |
19 Mar 08 |
peter |
161 |
suite.err() << " failed\n"; |
1248 |
19 Mar 08 |
peter |
162 |
suite.add(false); |
1009 |
01 Feb 08 |
peter |
163 |
} |
789 |
10 Mar 07 |
jari |
164 |
} |
789 |
10 Mar 07 |
jari |
165 |
|
789 |
10 Mar 07 |
jari |
// checking clone functionality |
789 |
10 Mar 07 |
jari |
167 |
{ |
1248 |
19 Mar 08 |
peter |
168 |
suite.err() << "clone functionality" << std::endl; |
789 |
10 Mar 07 |
jari |
169 |
bool this_ok=true; |
1248 |
19 Mar 08 |
peter |
170 |
suite.err() << "\tcloning normal vector" << std::endl; |
1120 |
21 Feb 08 |
peter |
171 |
utility::Vector vec2(3,123.0); |
1009 |
01 Feb 08 |
peter |
172 |
vec2 = vec; |
789 |
10 Mar 07 |
jari |
173 |
if (vec.size()!=vec2.size()) |
789 |
10 Mar 07 |
jari |
174 |
this_ok=false; |
789 |
10 Mar 07 |
jari |
175 |
else |
988 |
26 Oct 07 |
jari |
176 |
for (size_t i=0; i<vec.size(); ++i) |
789 |
10 Mar 07 |
jari |
177 |
if (vec(i)!=vec2(i)) |
789 |
10 Mar 07 |
jari |
178 |
this_ok=false; |
789 |
10 Mar 07 |
jari |
179 |
if (vec.gsl_vector_p()==vec2.gsl_vector_p()) |
789 |
10 Mar 07 |
jari |
180 |
this_ok=false; |
1248 |
19 Mar 08 |
peter |
181 |
suite.err() << "\tcloning vector view" << std::endl; |
1015 |
01 Feb 08 |
peter |
182 |
utility::VectorView* vec_view=new utility::VectorView(vec,3,3,1); |
1015 |
01 Feb 08 |
peter |
183 |
utility::VectorView vec_view2(*vec_view); |
789 |
10 Mar 07 |
jari |
184 |
if (!vec_view2.isview()) |
789 |
10 Mar 07 |
jari |
185 |
this_ok=false; |
789 |
10 Mar 07 |
jari |
186 |
if (vec_view->size()!=vec_view2.size()) |
789 |
10 Mar 07 |
jari |
187 |
this_ok=false; |
789 |
10 Mar 07 |
jari |
188 |
else |
1261 |
08 Apr 08 |
peter |
189 |
for (size_t i=0; i<vec_view2.size(); ++i) |
789 |
10 Mar 07 |
jari |
190 |
if ((*vec_view)(i)!=vec_view2(i)) |
789 |
10 Mar 07 |
jari |
191 |
this_ok=false; |
1248 |
19 Mar 08 |
peter |
192 |
suite.err() << "\tcloned vector view independence" << std::endl; |
789 |
10 Mar 07 |
jari |
193 |
delete vec_view; |
1151 |
25 Feb 08 |
peter |
194 |
if (vec(3)!=vec_view2(0) || vec(4)!=vec_view2(1) || vec(5)!=vec_view2(2)) |
789 |
10 Mar 07 |
jari |
195 |
this_ok=false; |
789 |
10 Mar 07 |
jari |
196 |
|
789 |
10 Mar 07 |
jari |
197 |
if (!this_ok) { |
1248 |
19 Mar 08 |
peter |
198 |
suite.err() << "FAIL: clone test" << std::endl; |
1248 |
19 Mar 08 |
peter |
199 |
suite.add(false); |
789 |
10 Mar 07 |
jari |
200 |
} |
789 |
10 Mar 07 |
jari |
201 |
} |
789 |
10 Mar 07 |
jari |
202 |
|
341 |
07 Jun 05 |
jari |
// checking that reading vectors from properly formatted files works |
341 |
07 Jun 05 |
jari |
204 |
try { |
1248 |
19 Mar 08 |
peter |
205 |
suite.err() << "stream" << std::endl; |
1251 |
03 Apr 08 |
peter |
206 |
std::string data1(test::filename("data/vector1.data")); |
1251 |
03 Apr 08 |
peter |
207 |
std::string data2(test::filename("data/vector2.data")); |
1251 |
03 Apr 08 |
peter |
208 |
std::string data3(test::filename("data/vector3.data")); |
1251 |
03 Apr 08 |
peter |
209 |
std::string data4(test::filename("data/vector4.data")); |
341 |
07 Jun 05 |
jari |
210 |
check_file_access(data1); |
341 |
07 Jun 05 |
jari |
211 |
check_file_access(data2); |
341 |
07 Jun 05 |
jari |
212 |
check_file_access(data3); |
341 |
07 Jun 05 |
jari |
213 |
check_file_access(data4); |
341 |
07 Jun 05 |
jari |
214 |
std::ifstream data_stream1(data1.c_str()); |
341 |
07 Jun 05 |
jari |
215 |
std::ifstream data_stream2(data2.c_str()); |
341 |
07 Jun 05 |
jari |
216 |
std::ifstream data_stream3(data3.c_str()); |
341 |
07 Jun 05 |
jari |
217 |
std::ifstream data_stream4(data4.c_str()); |
1120 |
21 Feb 08 |
peter |
218 |
utility::Vector vec1(data_stream1); |
1248 |
19 Mar 08 |
peter |
219 |
suite.add(vec1.size()==9); |
1120 |
21 Feb 08 |
peter |
220 |
vec1=utility::Vector(data_stream2); |
1248 |
19 Mar 08 |
peter |
221 |
suite.add(vec1.size()==9); |
1120 |
21 Feb 08 |
peter |
222 |
utility::Vector vec2(data_stream3); |
1248 |
19 Mar 08 |
peter |
223 |
suite.add(vec2.size()==12); |
1120 |
21 Feb 08 |
peter |
224 |
vec2=utility::Vector(data_stream4); |
1248 |
19 Mar 08 |
peter |
225 |
suite.add(vec2.size()==12); |
341 |
07 Jun 05 |
jari |
226 |
} catch (utility::IO_error& err) { |
1248 |
19 Mar 08 |
peter |
227 |
suite.err() << err.what() << std::endl; |
1248 |
19 Mar 08 |
peter |
228 |
suite.add(false); |
341 |
07 Jun 05 |
jari |
229 |
} |
341 |
07 Jun 05 |
jari |
230 |
|
341 |
07 Jun 05 |
jari |
// Checking that vector stream input operator can read whatever |
341 |
07 Jun 05 |
jari |
// vector stream output operator produces. |
789 |
10 Mar 07 |
jari |
233 |
{ |
1248 |
19 Mar 08 |
peter |
234 |
suite.err() << "checking that output stream is valid as an input stream" |
789 |
10 Mar 07 |
jari |
235 |
<< std::endl; |
789 |
10 Mar 07 |
jari |
236 |
std::stringstream s; |
789 |
10 Mar 07 |
jari |
237 |
s << vec; |
1120 |
21 Feb 08 |
peter |
238 |
utility::Vector vec2(s); |
1248 |
19 Mar 08 |
peter |
239 |
suite.add(suite.equal_range(vec.begin(), vec.end(), vec2.begin())); |
789 |
10 Mar 07 |
jari |
240 |
} |
341 |
07 Jun 05 |
jari |
241 |
|
341 |
07 Jun 05 |
jari |
// Checking that badly formatted files are not accepted, or at least |
341 |
07 Jun 05 |
jari |
// throws an exception for the programmer to catch. |
1248 |
19 Mar 08 |
peter |
244 |
suite.err() << "checking that bad stream are rejected" << std::endl; |
341 |
07 Jun 05 |
jari |
245 |
bool this_ok=false; |
4200 |
19 Aug 22 |
peter |
// Checking that unexpected characters in a stream gives error. |
341 |
07 Jun 05 |
jari |
247 |
try { |
1251 |
03 Apr 08 |
peter |
248 |
std::string data5(test::filename("data/vector5.data")); |
438 |
15 Dec 05 |
markus |
249 |
check_file_access(data5); |
438 |
15 Dec 05 |
markus |
250 |
std::ifstream data_stream5(data5.c_str()); |
1120 |
21 Feb 08 |
peter |
251 |
utility::Vector dummy(data_stream5); // this will give an exception |
438 |
15 Dec 05 |
markus |
252 |
} catch (utility::IO_error& err) { |
1248 |
19 Mar 08 |
peter |
253 |
suite.err() << err.what() << std::endl; |
879 |
21 Sep 07 |
markus |
254 |
this_ok=true; // good, exception thrown, test passed |
438 |
15 Dec 05 |
markus |
255 |
} |
438 |
15 Dec 05 |
markus |
256 |
try { |
1251 |
03 Apr 08 |
peter |
257 |
std::string data(test::filename("data/vector6.data")); |
341 |
07 Jun 05 |
jari |
258 |
check_file_access(data); |
341 |
07 Jun 05 |
jari |
259 |
std::ifstream data_stream(data.c_str()); |
1120 |
21 Feb 08 |
peter |
260 |
utility::Vector dummy(data_stream); // this will give an exception |
341 |
07 Jun 05 |
jari |
261 |
} catch (utility::IO_error& err) { |
1248 |
19 Mar 08 |
peter |
262 |
suite.err() << err.what() << std::endl; |
341 |
07 Jun 05 |
jari |
263 |
this_ok=true; // good, exceoption thrown, test passed |
341 |
07 Jun 05 |
jari |
264 |
} |
1248 |
19 Mar 08 |
peter |
265 |
suite.add(this_ok); |
789 |
10 Mar 07 |
jari |
266 |
|
341 |
07 Jun 05 |
jari |
267 |
this_ok=false; |
341 |
07 Jun 05 |
jari |
268 |
try { |
1251 |
03 Apr 08 |
peter |
269 |
std::string data(test::filename("data/vector7.data")); |
341 |
07 Jun 05 |
jari |
270 |
check_file_access(data); |
341 |
07 Jun 05 |
jari |
271 |
std::ifstream data_stream(data.c_str()); |
1120 |
21 Feb 08 |
peter |
272 |
vec=utility::Vector(data_stream); // this will give an exception |
341 |
07 Jun 05 |
jari |
273 |
} catch (utility::IO_error& err) { |
1248 |
19 Mar 08 |
peter |
274 |
suite.err() << err.what() << std::endl; |
341 |
07 Jun 05 |
jari |
275 |
this_ok=true; // good, exceoption thrown, test passed |
341 |
07 Jun 05 |
jari |
276 |
} |
1248 |
19 Mar 08 |
peter |
277 |
suite.add(this_ok); |
786 |
09 Mar 07 |
peter |
278 |
|
879 |
21 Sep 07 |
markus |
279 |
|
879 |
21 Sep 07 |
markus |
// Test sort algorithms |
879 |
21 Sep 07 |
markus |
281 |
{ |
1251 |
03 Apr 08 |
peter |
282 |
std::string data3(test::filename("data/vector3.data")); |
879 |
21 Sep 07 |
markus |
283 |
std::ifstream data_stream3(data3.c_str()); |
1120 |
21 Feb 08 |
peter |
284 |
utility::Vector vec3(data_stream3); |
879 |
21 Sep 07 |
markus |
285 |
std::vector<size_t> dummy; |
879 |
21 Sep 07 |
markus |
286 |
dummy.push_back(100); // To make sure it works starting with a non-empty vector |
879 |
21 Sep 07 |
markus |
287 |
|
879 |
21 Sep 07 |
markus |
288 |
utility::sort_index(dummy,vec3); |
879 |
21 Sep 07 |
markus |
289 |
if(dummy.size()!=vec3.size()) { |
1248 |
19 Mar 08 |
peter |
290 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
291 |
suite.err() << "Vector of sorted indices has incorrect size" << std::endl; |
879 |
21 Sep 07 |
markus |
292 |
} |
879 |
21 Sep 07 |
markus |
293 |
if(dummy[0]!=11 || dummy[dummy.size()-1]!=8) { |
1248 |
19 Mar 08 |
peter |
294 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
295 |
suite.err() << "Vector of sorted indices incorrect" << std::endl; |
879 |
21 Sep 07 |
markus |
296 |
} |
879 |
21 Sep 07 |
markus |
297 |
size_t k=3; |
879 |
21 Sep 07 |
markus |
298 |
|
879 |
21 Sep 07 |
markus |
299 |
utility::sort_smallest_index(dummy,k,vec3); |
879 |
21 Sep 07 |
markus |
300 |
if(dummy.size()!=k) { |
1248 |
19 Mar 08 |
peter |
301 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
302 |
suite.err() << "Vector of sorted smallest indices has incorrect size" << std::endl; |
879 |
21 Sep 07 |
markus |
303 |
} |
879 |
21 Sep 07 |
markus |
304 |
if(dummy[0]!=11 || dummy[dummy.size()-1]!=9) { |
1248 |
19 Mar 08 |
peter |
305 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
306 |
suite.err() << "Vector of sorted smallest indices incorrect" << std::endl; |
879 |
21 Sep 07 |
markus |
307 |
} |
879 |
21 Sep 07 |
markus |
308 |
|
879 |
21 Sep 07 |
markus |
309 |
k=4; |
879 |
21 Sep 07 |
markus |
310 |
utility::sort_largest_index(dummy,k,vec3); |
879 |
21 Sep 07 |
markus |
311 |
if(dummy.size()!=k) { |
1248 |
19 Mar 08 |
peter |
312 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
313 |
suite.err() << "Vector of sorted largest indices has incorrect size" << std::endl; |
879 |
21 Sep 07 |
markus |
314 |
} |
879 |
21 Sep 07 |
markus |
315 |
if(dummy[0]!=8 || dummy[dummy.size()-1]!=5) { |
1248 |
19 Mar 08 |
peter |
316 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
317 |
suite.err() << "Vector of sorted largest indices incorrect" << std::endl; |
879 |
21 Sep 07 |
markus |
318 |
} |
877 |
20 Sep 07 |
markus |
319 |
} |
877 |
20 Sep 07 |
markus |
320 |
|
1132 |
23 Feb 08 |
peter |
321 |
{ |
1132 |
23 Feb 08 |
peter |
322 |
utility::Vector vec(10, 2.3); |
1132 |
23 Feb 08 |
peter |
323 |
utility::VectorView vv(vec); |
1132 |
23 Feb 08 |
peter |
324 |
vec = vv; |
1132 |
23 Feb 08 |
peter |
325 |
if (vec.size()!=10){ |
1248 |
19 Mar 08 |
peter |
326 |
suite.err() << "indirect self-assignment failed - size should be 10\n"; |
1248 |
19 Mar 08 |
peter |
327 |
suite.add(false); |
1132 |
23 Feb 08 |
peter |
328 |
} |
1132 |
23 Feb 08 |
peter |
329 |
if (vec(0)!=2.3){ |
1248 |
19 Mar 08 |
peter |
330 |
suite.err() << "indirect self-assignment failed - vec(0) should be 2.3\n"; |
1248 |
19 Mar 08 |
peter |
331 |
suite.add(false); |
1132 |
23 Feb 08 |
peter |
332 |
} |
1132 |
23 Feb 08 |
peter |
333 |
utility::VectorView vv2(vec, 0, 5); |
1132 |
23 Feb 08 |
peter |
334 |
vec = vv2; |
1132 |
23 Feb 08 |
peter |
335 |
if (vec.size()!=5){ |
1248 |
19 Mar 08 |
peter |
336 |
suite.err() << "indirect self-assignment failed - size should be 5\n"; |
1248 |
19 Mar 08 |
peter |
337 |
suite.add(false); |
1132 |
23 Feb 08 |
peter |
338 |
} |
1132 |
23 Feb 08 |
peter |
339 |
if (vec(0)!=2.3){ |
1248 |
19 Mar 08 |
peter |
340 |
suite.err() << "indirect self-assignment failed - vec(0) should be 2.3\n"; |
1248 |
19 Mar 08 |
peter |
341 |
suite.add(false); |
1132 |
23 Feb 08 |
peter |
342 |
} |
1132 |
23 Feb 08 |
peter |
343 |
} |
1132 |
23 Feb 08 |
peter |
344 |
|
1056 |
07 Feb 08 |
peter |
// test for ticket:285 |
1056 |
07 Feb 08 |
peter |
346 |
{ |
1120 |
21 Feb 08 |
peter |
347 |
theplu::yat::utility::Vector vec(10); |
1120 |
21 Feb 08 |
peter |
348 |
theplu::yat::utility::Vector vec2; |
1056 |
07 Feb 08 |
peter |
349 |
vec2 = vec; |
1056 |
07 Feb 08 |
peter |
350 |
} |
1056 |
07 Feb 08 |
peter |
351 |
|
1506 |
16 Sep 08 |
peter |
// test for zero size constructor |
1506 |
16 Sep 08 |
peter |
353 |
{ |
1506 |
16 Sep 08 |
peter |
354 |
theplu::yat::utility::Vector vec(0, 1.2); |
1506 |
16 Sep 08 |
peter |
355 |
theplu::yat::utility::Vector vec2(vec); |
3987 |
26 Aug 20 |
peter |
// test ticket #956 |
3987 |
26 Aug 20 |
peter |
357 |
vec.resize(0); |
1506 |
16 Sep 08 |
peter |
358 |
} |
1954 |
07 May 09 |
jari |
359 |
|
1954 |
07 May 09 |
jari |
// test that sort can handle NaN properly |
1954 |
07 May 09 |
jari |
361 |
test_sort(suite); |
3467 |
10 Feb 16 |
peter |
// test for ticket 840 |
3467 |
10 Feb 16 |
peter |
363 |
test_vector_mutable_assignment(suite); |
3612 |
31 Jan 17 |
peter |
364 |
test_aliasing(suite); |
3871 |
01 Mar 20 |
peter |
365 |
test_norm2_squared(suite); |
1954 |
07 May 09 |
jari |
366 |
|
1248 |
19 Mar 08 |
peter |
367 |
return suite.return_value(); |
127 |
03 Aug 04 |
peter |
368 |
} |
1954 |
07 May 09 |
jari |
369 |
|
3871 |
01 Mar 20 |
peter |
370 |
|
3871 |
01 Mar 20 |
peter |
371 |
void test_norm2_squared(test::Suite& suite) |
3871 |
01 Mar 20 |
peter |
372 |
{ |
3871 |
01 Mar 20 |
peter |
373 |
utility::Vector u(10, 2.0); |
3871 |
01 Mar 20 |
peter |
374 |
double d = norm2_squared(u); |
3871 |
01 Mar 20 |
peter |
375 |
if (!suite.equal(d, 10 * 4.0)) { |
3871 |
01 Mar 20 |
peter |
376 |
suite.add(false); |
3871 |
01 Mar 20 |
peter |
377 |
suite.err() << "error: norm2_squared: " << d << "\n"; |
3871 |
01 Mar 20 |
peter |
378 |
} |
3871 |
01 Mar 20 |
peter |
379 |
utility::Vector v(10); |
3871 |
01 Mar 20 |
peter |
380 |
d = norm2_squared(utility::Vector(u-v)); |
3871 |
01 Mar 20 |
peter |
381 |
if (!suite.equal(d, 10 * 4.0)) { |
3871 |
01 Mar 20 |
peter |
382 |
suite.add(false); |
3871 |
01 Mar 20 |
peter |
383 |
suite.err() << "error: norm2_squared: " << d << "\n"; |
3871 |
01 Mar 20 |
peter |
384 |
} |
3871 |
01 Mar 20 |
peter |
385 |
} |
3871 |
01 Mar 20 |
peter |
386 |
|
3871 |
01 Mar 20 |
peter |
387 |
|
1954 |
07 May 09 |
jari |
388 |
void test_sort(test::Suite& suite) |
1954 |
07 May 09 |
jari |
389 |
{ |
1954 |
07 May 09 |
jari |
390 |
utility::Vector vec(5); |
1954 |
07 May 09 |
jari |
391 |
vec(0) = 0; |
1954 |
07 May 09 |
jari |
392 |
vec(1) = 10; |
1954 |
07 May 09 |
jari |
393 |
vec(2) = std::numeric_limits<double>::quiet_NaN(); |
1954 |
07 May 09 |
jari |
394 |
vec(3) = 2; |
1954 |
07 May 09 |
jari |
395 |
vec(4) = 5; |
1954 |
07 May 09 |
jari |
396 |
|
1954 |
07 May 09 |
jari |
397 |
sort(vec); |
1954 |
07 May 09 |
jari |
398 |
for (size_t i=0; i<vec.size(); ++i) |
1954 |
07 May 09 |
jari |
399 |
for (size_t j=i+1; j<vec.size(); ++j) { |
1954 |
07 May 09 |
jari |
400 |
if (vec(j)<vec(i)) { |
4200 |
19 Aug 22 |
peter |
401 |
suite.err() << "Vector: '" << vec << "' is not sorted because '" |
1954 |
07 May 09 |
jari |
402 |
<< vec(j) << "' is less than '" << vec(i) << "'\n"; |
1954 |
07 May 09 |
jari |
403 |
suite.add(false); |
1954 |
07 May 09 |
jari |
404 |
} |
1954 |
07 May 09 |
jari |
405 |
} |
1954 |
07 May 09 |
jari |
406 |
} |
3467 |
10 Feb 16 |
peter |
407 |
|
3467 |
10 Feb 16 |
peter |
408 |
|
3467 |
10 Feb 16 |
peter |
409 |
void test_assign(utility::VectorMutable& lhs, const utility::Vector& rhs) |
3467 |
10 Feb 16 |
peter |
410 |
{ |
3467 |
10 Feb 16 |
peter |
411 |
lhs = rhs; |
3467 |
10 Feb 16 |
peter |
412 |
} |
3467 |
10 Feb 16 |
peter |
413 |
|
3467 |
10 Feb 16 |
peter |
414 |
|
3467 |
10 Feb 16 |
peter |
415 |
void test_assign2(utility::VectorMutable& lhs, const utility::VectorView& rhs) |
3467 |
10 Feb 16 |
peter |
416 |
{ |
3467 |
10 Feb 16 |
peter |
417 |
lhs = rhs; |
3467 |
10 Feb 16 |
peter |
418 |
} |
3467 |
10 Feb 16 |
peter |
419 |
|
3467 |
10 Feb 16 |
peter |
420 |
|
3467 |
10 Feb 16 |
peter |
421 |
void test_assign3(utility::VectorMutable& lhs, const utility::VectorBase& rhs) |
3467 |
10 Feb 16 |
peter |
422 |
{ |
3467 |
10 Feb 16 |
peter |
423 |
lhs = rhs; |
3467 |
10 Feb 16 |
peter |
424 |
} |
3467 |
10 Feb 16 |
peter |
425 |
|
3467 |
10 Feb 16 |
peter |
426 |
|
3467 |
10 Feb 16 |
peter |
427 |
void test_assign4(utility::VectorMutable& lhs, const utility::VectorMutable& rhs) |
3467 |
10 Feb 16 |
peter |
428 |
{ |
3467 |
10 Feb 16 |
peter |
429 |
lhs = rhs; |
3467 |
10 Feb 16 |
peter |
430 |
} |
3467 |
10 Feb 16 |
peter |
431 |
|
3467 |
10 Feb 16 |
peter |
432 |
|
3467 |
10 Feb 16 |
peter |
433 |
void test_vector_mutable_assignment(test::Suite&) |
3467 |
10 Feb 16 |
peter |
434 |
{ |
3467 |
10 Feb 16 |
peter |
435 |
utility::Vector vec(10, 1); |
3467 |
10 Feb 16 |
peter |
436 |
utility::Vector vec2(10, 2); |
3467 |
10 Feb 16 |
peter |
437 |
utility::VectorView view(vec2); |
3467 |
10 Feb 16 |
peter |
438 |
utility::VectorConstView const_view(vec2); |
3467 |
10 Feb 16 |
peter |
439 |
test_assign(vec2, vec); |
3467 |
10 Feb 16 |
peter |
440 |
test_assign(view, vec); |
3467 |
10 Feb 16 |
peter |
441 |
|
3467 |
10 Feb 16 |
peter |
442 |
test_assign2(view, view); |
3467 |
10 Feb 16 |
peter |
443 |
test_assign2(vec, view); |
3467 |
10 Feb 16 |
peter |
444 |
|
3467 |
10 Feb 16 |
peter |
445 |
test_assign3(view, view); |
3467 |
10 Feb 16 |
peter |
446 |
test_assign3(vec, view); |
3467 |
10 Feb 16 |
peter |
447 |
test_assign3(vec2, vec); |
3467 |
10 Feb 16 |
peter |
448 |
test_assign3(view, vec); |
3467 |
10 Feb 16 |
peter |
449 |
test_assign3(vec, const_view); |
3467 |
10 Feb 16 |
peter |
450 |
|
3467 |
10 Feb 16 |
peter |
451 |
test_assign4(view, view); |
3467 |
10 Feb 16 |
peter |
452 |
test_assign4(vec, view); |
3467 |
10 Feb 16 |
peter |
453 |
test_assign4(vec2, vec); |
3467 |
10 Feb 16 |
peter |
454 |
test_assign4(view, vec); |
3467 |
10 Feb 16 |
peter |
455 |
} |
3612 |
31 Jan 17 |
peter |
456 |
|
3623 |
09 Feb 17 |
peter |
457 |
|
3612 |
31 Jan 17 |
peter |
458 |
void test_aliasing(test::Suite& suite) |
3612 |
31 Jan 17 |
peter |
459 |
{ |
3623 |
09 Feb 17 |
peter |
460 |
suite.out() << "test_aliasing\n"; |
3612 |
31 Jan 17 |
peter |
461 |
utility::Vector vec(10, 1); |
3612 |
31 Jan 17 |
peter |
462 |
utility::VectorView view(vec, 0, 5, 2); |
3612 |
31 Jan 17 |
peter |
463 |
utility::VectorConstView const_view(vec, 0, 5, 1); |
3623 |
09 Feb 17 |
peter |
// vec = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} |
3623 |
09 Feb 17 |
peter |
// view = { 1, 1, 1, 1, 1} |
3623 |
09 Feb 17 |
peter |
// const_view{ 1, 1, 1, 1, 1} |
3612 |
31 Jan 17 |
peter |
467 |
view += const_view; |
3623 |
09 Feb 17 |
peter |
468 |
suite.out() << "vec: " << vec << "\n"; |
3623 |
09 Feb 17 |
peter |
469 |
suite.out() << "view: " << view << "\n"; |
3623 |
09 Feb 17 |
peter |
470 |
suite.out() << "const_view: " << const_view << "\n"; |
3612 |
31 Jan 17 |
peter |
471 |
for (size_t i=0; i<view.size(); ++i) { |
3623 |
09 Feb 17 |
peter |
472 |
suite.add(suite.equal(view(i), 2)); |
3612 |
31 Jan 17 |
peter |
473 |
} |
3623 |
09 Feb 17 |
peter |
474 |
|
3623 |
09 Feb 17 |
peter |
// vec = { 2, 1, 2, 1, 2, 1, 2, 1, 2, 1} |
3623 |
09 Feb 17 |
peter |
// view = { 2, 2, 2, 2, 2} |
3623 |
09 Feb 17 |
peter |
// const_view{ 2, 1, 2, 1, 2} |
3623 |
09 Feb 17 |
peter |
478 |
suite.out() << "vec: " << vec << "\n"; |
3623 |
09 Feb 17 |
peter |
479 |
suite.out() << "view: " << view << "\n"; |
3623 |
09 Feb 17 |
peter |
480 |
suite.out() << "const_view: " << const_view << "\n"; |
3623 |
09 Feb 17 |
peter |
481 |
|
3623 |
09 Feb 17 |
peter |
482 |
view = const_view; |
3623 |
09 Feb 17 |
peter |
// view = { 2, 1, 2, 1, 2} |
3623 |
09 Feb 17 |
peter |
484 |
utility::Vector correct(5); |
3623 |
09 Feb 17 |
peter |
485 |
correct(0) = 2; |
3623 |
09 Feb 17 |
peter |
486 |
correct(1) = 1; |
3623 |
09 Feb 17 |
peter |
487 |
correct(2) = 2; |
3623 |
09 Feb 17 |
peter |
488 |
correct(3) = 1; |
3623 |
09 Feb 17 |
peter |
489 |
correct(4) = 2; |
3623 |
09 Feb 17 |
peter |
490 |
if (view != correct) { |
3623 |
09 Feb 17 |
peter |
491 |
suite.add(false); |
3623 |
09 Feb 17 |
peter |
492 |
suite.err() << "error: view: \n" << view << "\nexpected: " << correct |
3623 |
09 Feb 17 |
peter |
493 |
<< "\n"; |
3623 |
09 Feb 17 |
peter |
494 |
} |
3612 |
31 Jan 17 |
peter |
495 |
} |