Peano 4
Loading...
Searching...
No Matches
VectorTest.cpp
Go to the documentation of this file.
2#include "tarch/la/Vector.h"
4#include <string>
5#include <sstream>
6#include <map>
7
8
10 TestCase ("tarch::la::tests::VectorTest") {
11}
12
13
15{
16 testMethod (testConstruction);
17 testMethod (testAssignment);
18 testMethod (testVectorOperations);
19 testMethod (testVectorScalarOperations);
20 testMethod (testVectorVectorOperations);
21 testMethod (testWrappedVector);
22 testMethod (testVectorCompare);
23
24 testMethod (testVectorVectorCompare );
25}
26
27
29// Vector<2,double> vector00;
30// Vector<2,double> vector01;
31// Vector<2,double> vector10;
32// Vector<2,double> vector11;
33//
34// Vector<2,double> vectorM;
35//
36// std::map<tarch::la::Vector<2,double> , int, tarch::la::VectorCompare<2> > _vertexMap;
37//
38// validate( _vertexMap.empty() );
39//
40// assignList(vector00) = 0.0, 0.0;
41// validate( _vertexMap.find(vector00)==_vertexMap.end() );
42//
43// assignList(vectorM) = 0.0, 0.3;
44// _vertexMap[vectorM] = 2;
45// validate( !_vertexMap.empty() );
46// validate( _vertexMap.size()==1 );
47// validate( _vertexMap.find(vectorM)!=_vertexMap.end() );
48// validate( _vertexMap.find(vector00)==_vertexMap.end() );
49}
50
51
53 // Test construction with uniform initialization
54 Vector<1,int> vector1(1);
55 validateEquals (vector1[0], 1);
56
57 Vector<2,int> vector2(1);
58 validateEquals (vector2[0], 1);
59 validateEquals (vector2[1], 1);
60
61 // Test construction with direct individual initialization
62 Vector<2,int> vector3 = {1, 2};
63 validateEquals (vector3[0], 1);
64 validateEquals (vector3[1], 2);
65
66 Vector<3,int> vector4 = {1, 2, 3};
67 validateEquals (vector4[0], 1);
68 validateEquals (vector4[1], 2);
69 validateEquals (vector4[2], 3);
70
71 // Test copy construction
72 Vector<3,int> vector5(vector4);
73 validateEquals (vector5[0], 1);
74 validateEquals (vector5[1], 2);
75 validateEquals (vector5[2], 3);
76
77 Vector<3,int> vector6 = vector5;
78 validateEquals (vector6[0], 1);
79 validateEquals (vector6[1], 2);
80 validateEquals (vector6[2], 3);
81}
82
83
85 // Assignment using functionality from VectorAssign.h:
86 // Assign a list of scalars
87 Vector<3,int> vector = {1, 2, 3};
88 validateEquals (vector[0], 1);
89 validateEquals (vector[1], 2);
90 validateEquals (vector[2], 3);
91
92 vector = {1, 2, 3};
93 validateEqualsWithParams1(vector[0], 1, ::toString(vector));
94 validateEqualsWithParams1(vector[1], 2, ::toString(vector));
95 validateEqualsWithParams1(vector[2], 3, ::toString(vector));
96//
97//
98// // Assign a scalar to all vector
99// vector = 0;
100// validateEquals (vector[0], 0);
101// validateEquals (vector[1], 0);
102// validateEquals (vector[2], 0);
103// // Assign a scalar to part of vector
104// assignRange(vector,0,2) = 0;
105// validateEquals (vector[0], 0);
106// validateEquals (vector[1], 0);
107// validateEquals (vector[2], 0);
108// // Assign a vector to part of vector
109// Vector<3,int> vector2;
110// vector2 = 1;
111// assignRange(vector,0,2) = vector2;
112// validateEquals (vector[0], 1);
113// validateEquals (vector[1], 1);
114// validateEquals (vector[2], 1);
115//
116// assignList(vector) = 1, 2, 3;
117//
118// vector = 1;
119// validateEquals (vector[0], 1);
120// validateEquals (vector[1], 1);
121// validateEquals (vector[2], 1);
122}
123
124
126 Vector<3,int> vector = {1, -2, 2};
127
128 int result = sum(vector);
129 validateEquals (result, 1);
130
131 result = norm1(vector);
132 validateEquals (result, 5.0);
133
134 // Test indexMin, indexMax, min, and max methods
135 vector = {1, 2, 3};
136 validateEquals (indexMin(vector), 0);
137 validateEquals (indexMax(vector), 2);
138 validateEquals (min(vector), 1);
139 validateEquals (max(vector), 3);
140
141 Vector<3,int> vector3 = {1, 2, 3};
142 std::ostringstream stream;
143 stream << vector3;
144 std::string vector3string ( stream.str() );
145 validateEquals ( vector3string, std::string("[1,2,3]") );
146}
147
148
150 Vector<3,int> vector(0);
151
152 // Compound assignment operators
153 vector += 1;
154 validateEquals (vector[0], 1);
155 validateEquals (vector[1], 1);
156 validateEquals (vector[2], 1);
157
158 vector -= 2;
159 validateEquals (vector[0], -1);
160 validateEquals (vector[1], -1);
161 validateEquals (vector[2], -1);
162
163 vector *= -2;
164 validateEquals (vector[0], 2);
165 validateEquals (vector[1], 2);
166 validateEquals (vector[2], 2);
167
168 vector /= 2;
169 validateEquals (vector[0], 1);
170 validateEquals (vector[1], 1);
171 validateEquals (vector[2], 1);
172
173 // Arithmetic operators
174 vector = vector * 2;
175 validateEquals (vector[0], 2);
176 validateEquals (vector[1], 2);
177 validateEquals (vector[2], 2);
178
179 vector = 2 * vector;
180 validateEquals (vector[0], 4);
181 validateEquals (vector[1], 4);
182 validateEquals (vector[2], 4);
183
184 vector = vector / 4;
185 validateEquals (vector[0], 1);
186 validateEquals (vector[1], 1);
187 validateEquals (vector[2], 1);
188
189 vector = vector + 1;
190 validateEquals (vector[0], 2);
191 validateEquals (vector[1], 2);
192 validateEquals (vector[2], 2);
193
194 vector = vector - 1;
195 validateEqualsWithParams1(vector[0], 1, ::toString(vector));
196 validateEqualsWithParams1(vector[1], 1, ::toString(vector));
197 validateEqualsWithParams1(vector[2], 1, ::toString(vector));
198}
199
200
202 Vector<3,int> vector(1);
203
204 // Addition assignment
205 vector += vector;
206 validateEquals (vector[0], 2);
207 validateEquals (vector[1], 2);
208 validateEquals (vector[2], 2);
209
210 // Subtraction assignment
211 vector -= vector;
212 validateEquals (vector[0], 0);
213 validateEquals (vector[1], 0);
214 validateEquals (vector[2], 0);
215
216 // Addition
217 vector = {1, 2, 3};
218 vector = vector + vector;
219 validateEquals (vector[0], 2);
220 validateEquals (vector[1], 4);
221 validateEquals (vector[2], 6);
222
223 // Subtraction
224 vector = {1, 2, 3};
225 vector = vector - vector;
226 validateEquals (vector[0], 0);
227 validateEquals (vector[1], 0);
228 validateEquals (vector[2], 0);
229
230 // Dot product
231 vector = {2, 3, 4};
232 int dotresult = dot(vector, vector);
233 validateEquals (dotresult, 29);
234 validateEquals (dotresult, 29);
235 dotresult = vector * vector;
236 validateEquals (dotresult, 29);
237
238 // Cross product (only works for same types)
239 Vector<3,int> b = {2, 3, 4};
240 Vector<3,int> a = {1, 2, 3};
241 Vector<3,int> crossresult(0);
242 crossresult = cross(a, b);
243 validateEquals (crossresult[0], a[1]*b[2] - a[2]*b[1]);
244 validateEquals (crossresult[1], a[2]*b[0] - a[0]*b[2]);
245 validateEquals (crossresult[2], a[0]*b[1] - a[1]*b[0]);
246
247 // Comparisons
248 Vector<3,double> vec0 = {1.0, 2.0, 3.0};
249 Vector<3,double> vec1(vec0);
250 validate (equals(vec0,vec1));
251 validate (! oneGreater(vec0,vec1));
252 validate (oneGreaterEquals(vec0,vec1));
253 validate (! allGreater(vec0,vec1));
254 validate (! firstGreater(vec0,vec1));
255
256 vec0 = {2.0, 2.0, 3.0};
257 validate (! equals(vec0,vec1));
258 validate (oneGreater(vec0,vec1));
259 validate (oneGreaterEquals(vec0,vec1));
260 validate (! allGreater(vec0,vec1));
261 validate (firstGreater(vec0,vec1));
262
263 vec0 = {2.0, 3.0, 4.0};
264 validate (! equals(vec0,vec1));
265 validate (oneGreater(vec0,vec1));
266 validate (oneGreaterEquals(vec0,vec1));
267 validate (allGreater(vec0,vec1));
268
269 // up to here vec1=vec0
270 const double tolerance = 1e-14;
271 vec0(0) = vec1(0);
272 vec0(1) = vec1(1);
273 vec0(2) = vec1(2) + 0.99 * tolerance;
274 validateWithParams3(equals(vec0,vec1,tolerance),vec0,vec1,vec0(2)-vec1(2));
275 validate (! oneGreater(vec0,vec1,tolerance));
276 validate (oneGreaterEquals(vec0,vec1));
277 validate (! allGreater(vec0,vec1,tolerance));
278 validateWithParams2(! firstGreater(vec0,vec1,tolerance), vec0, vec1);
279
280 vec0(2) = vec1(2) + 10.0 * tolerance;
281 validate (! equals(vec0,vec1,tolerance));
282 validate (oneGreater(vec0,vec1,tolerance));
283 validate (oneGreaterEquals(vec0,vec1));
284 validate (! allGreater(vec0,vec1,tolerance));
285 validateWithParams3 (firstGreater(vec0,vec1,tolerance), vec0, vec1,vec0(2)-vec1(2));
286
287 vec0 = {1.0, 2.0, 3.0};
288 vec0 += 10.0 * tolerance;
289 validate (! equals(vec0,vec1,tolerance));
290 validate (oneGreater(vec0,vec1,tolerance));
291 validate (oneGreaterEquals(vec0,vec1));
292 validate (allGreater(vec0,vec1,tolerance));
293 validate (firstGreater(vec0,vec1,tolerance));
294
295 vec0 = {1.0, 2.0, 3.0};
296 vec0 -= 0.99 * tolerance;
297 validate (oneGreaterEquals(vec0,vec1));
298
299 vec0 = {1.0, 1.0, 4.0};
300 validate (! firstGreater(vec0,vec1));
301 vec0 = {2.0, 0.0, 0.0};
302 validate (firstGreater(vec0,vec1));
303 vec0 = {1.0, 2.0 + 10.0 * tolerance, 2.0};
304 validate (firstGreater(vec0,vec1,tolerance));
305
306 //Test equalsReturnIndex
307 vec0 = {1.0, 2.0, 3.0 + 0.99 * tolerance};
308 vec1 = {1.0, 2.0, 3.0};
309 int i=equalsReturnIndex(vec0,vec1,tolerance);
310 validateEquals (i,-1);
311}
312
314 Vector<3,int> vector = {2, 3, 4};
315 Vector<6,int> longVector(1);
316 slice(longVector,vector,3);
317 validateEqualsWithParams1(longVector[0], 1, ::toString(longVector) );
318 validateEquals (longVector[1], 1);
319 validateEquals (longVector[2], 1);
320 validateEquals (longVector[3], 2);
321 validateEquals (longVector[4], 3);
322 validateEquals (longVector[5], 4);
323
324 Vector<3,int> vectorCopy = slice<3>(longVector,3);
325 validateEquals (vectorCopy[0], 2);
326 validateEquals (vectorCopy[1], 3);
327 validateEquals (vectorCopy[2], 4);
328}
329
330
332/*
333 typedef Vector<2,double> Vector;
334 std::map<Vector,int,VectorCompare<2> > vectors;
335 double eps = NUMERICAL_ZERO_DIFFERENCE;
336 Vector vec0;
337 tarch::la::assignList(vec0) = 1.0, 2.0;
338 Vector vec1;
339 tarch::la::assignList(vec1) = 1.0 + 10.0 * eps, 1.0;
340 Vector vec2;
341 tarch::la::assignList(vec2) = 1.0 + 10.0 * eps, 1.5;
342 Vector vec3;
343 tarch::la::assignList(vec3) = 2.0, 0.0;
344*/
345// vectors[vec2] = 2;
346// vectors[vec3] = 3;
347// vectors[vec0] = 0;
348// vectors[vec1] = 1;
349//
350// validateEquals (vectors[vec0], 0);
351// validateEquals (vectors[vec1], 1);
352// validateEquals (vectors[vec2], 2);
353// validateEquals (vectors[vec3], 3);
354}
#define validateEqualsWithParams1(actualValue, validValue, param0)
Definition TestMacros.h:318
#define validate(booleanExpr)
Definition TestMacros.h:37
#define testMethod(name)
Run a test method and check for errors.
Definition TestMacros.h:24
#define validateWithParams2(booleanExpr, param0, param1)
Definition TestMacros.h:69
#define validateWithParams3(booleanExpr, param0, param1, param2)
Definition TestMacros.h:80
#define validateEquals(actualValue, validValue)
Definition TestMacros.h:299
void testConstruction()
Tests constructors.
void testVectorScalarOperations()
Tests methods from VectorScalarOperations.h.
virtual void run()
This routine is triggered by the TestCaseCollection.
void testVectorOperations()
Tests methods from VectorOperations.h.
void testWrappedVector()
Tests wrapping a raw array with (static) vector semantics.
void testVectorCompare()
Tests methods from VectorCompare.h.
void testAssignment()
Tests methods from VectorAssign.h and operator= from Vector types.
void testVectorVectorOperations()
Tests methods from VectorVectorOperations.h.
Scalar dot(const Vector< Size, Scalar > &lhs, const Vector< Size, Scalar > &rhs)
Performs the dot (=inner) product of two vectors.
Scalar sum(const Matrix< Rows, Cols, Scalar > &matrix)
Computes the sum of all entries of the matrix.
bool allGreater(const Vector< Size, Scalar > &lhs, const Scalar &cmp, const Scalar tolerance=NUMERICAL_ZERO_DIFFERENCE)
Vector< 3, Scalar > cross(const Vector< 3, Scalar > &lhs, const Vector< 3, Scalar > &rhs)
Performs the cross product of two 3D vectors into result.
int indexMin(const Vector< Size, Scalar > &vector)
Returns the index of the element with minimal value (NOT absolute value).
int indexMax(const Vector< Size, Scalar > &vector)
Returns the index of the element with maximal value (NOT absolute value).
Vector< SizeLhs, Scalar > slice(const Vector< SizeRhs, Scalar > &vector, int fromIndex, int stride=1)
Returns subpart of the vector.
double max(double a, double b, double c)
I need the maximum of three values all the time, to I decided to write a function for this.
Definition Scalar.cpp:8
bool firstGreater(const Vector< Size, Scalar > &lhs, const Vector< Size, Scalar > &rhs, const Scalar tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares sequentially every component pair of lVector and rVector, and stops as soon as one is greate...
bool equals(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs, const Scalar &tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares to matrices on equality by means of a numerical accuracy.
std::pair< int, int > equalsReturnIndex(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs, const Scalar &tolerance=NUMERICAL_ZERO_DIFFERENCE)
Return Index of element which is not equals.
Scalar norm1(const Vector< Size, Scalar > &vector)
Computes the 1-norm of the vector, i.e.
bool oneGreaterEquals(const Vector< Size, Scalar > &lhs, const Scalar &cmp, const Scalar tolerance=NUMERICAL_ZERO_DIFFERENCE)
bool oneGreater(const Vector< Size, Scalar > &lhs, const Scalar &cmp, const Scalar tolerance=NUMERICAL_ZERO_DIFFERENCE)
Scalar min(const Vector< Size, Scalar > &vector)
Returns the element with minimal value (NOT absolute value).
std::string toString(MemoryLocation value)
Simple vector class.
Definition Vector.h:134