1 | /*
|
---|
2 | * unittest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Aug 17, 2009
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | #include <cppunit/CompilerOutputter.h>
|
---|
12 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
13 | #include <cppunit/ui/text/TestRunner.h>
|
---|
14 |
|
---|
15 | #include "defs.hpp"
|
---|
16 | #include "log.hpp"
|
---|
17 | #include "memoryusageobserver.hpp"
|
---|
18 | #include "vector.hpp"
|
---|
19 | #include "vectorunittest.hpp"
|
---|
20 |
|
---|
21 | #ifdef HAVE_TESTRUNNER
|
---|
22 | #include "UnitTestMain.hpp"
|
---|
23 | #endif /*HAVE_TESTRUNNER*/
|
---|
24 |
|
---|
25 | /********************************************** Test classes **************************************/
|
---|
26 |
|
---|
27 | // Registers the fixture into the 'registry'
|
---|
28 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
|
---|
29 |
|
---|
30 |
|
---|
31 | void VectorTest::setUp()
|
---|
32 | {
|
---|
33 | zero.Init(0.,0.,0.);
|
---|
34 | unit.Init(1.,0.,0.);
|
---|
35 | otherunit.Init(0.,1.,0.);
|
---|
36 | notunit.Init(0.,1.,1.);
|
---|
37 | two.Init(2.,1.,0.);
|
---|
38 | };
|
---|
39 |
|
---|
40 |
|
---|
41 | void VectorTest::tearDown()
|
---|
42 | {
|
---|
43 | MemoryUsageObserver::purgeInstance();
|
---|
44 | logger::purgeInstance();
|
---|
45 | errorLogger::purgeInstance();
|
---|
46 | };
|
---|
47 |
|
---|
48 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
---|
49 | */
|
---|
50 | void VectorTest::UnityTest()
|
---|
51 | {
|
---|
52 | // unity and zero tests
|
---|
53 | CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
|
---|
54 | CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
|
---|
55 | CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
|
---|
56 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
57 | CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() );
|
---|
58 | CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() );
|
---|
59 | CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() );
|
---|
60 | };
|
---|
61 |
|
---|
62 | /** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/
|
---|
63 | */
|
---|
64 | void VectorTest::SimpleAlgebraTest()
|
---|
65 | {
|
---|
66 | double factor;
|
---|
67 | // copy vector
|
---|
68 | fixture.Init(2.,3.,4.);
|
---|
69 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
|
---|
70 | // summation and scaling
|
---|
71 | fixture.CopyVector(&zero);
|
---|
72 | fixture.AddVector(&unit);
|
---|
73 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
74 | fixture.CopyVector(&zero);
|
---|
75 | fixture.SubtractVector(&unit);
|
---|
76 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
77 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
78 | fixture.CopyVector(&zero);
|
---|
79 | fixture.AddVector(&zero);
|
---|
80 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
|
---|
81 | fixture.CopyVector(¬unit);
|
---|
82 | fixture.SubtractVector(&otherunit);
|
---|
83 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
84 | fixture.CopyVector(&unit);
|
---|
85 | fixture.AddVector(&otherunit);
|
---|
86 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
87 | fixture.CopyVector(¬unit);
|
---|
88 | fixture.SubtractVector(&unit);
|
---|
89 | fixture.SubtractVector(&otherunit);
|
---|
90 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
91 | fixture.CopyVector(&unit);
|
---|
92 | fixture.Scale(0.98);
|
---|
93 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
94 | fixture.CopyVector(&unit);
|
---|
95 | fixture.Scale(1.);
|
---|
96 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
97 | fixture.CopyVector(&unit);
|
---|
98 | factor = 0.98;
|
---|
99 | fixture.Scale(factor);
|
---|
100 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
101 | fixture.CopyVector(&unit);
|
---|
102 | factor = 1.;
|
---|
103 | fixture.Scale(factor);
|
---|
104 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
105 | };
|
---|
106 |
|
---|
107 |
|
---|
108 | /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
|
---|
109 | */
|
---|
110 | void VectorTest::OperatorAlgebraTest()
|
---|
111 | {
|
---|
112 | // summation and scaling
|
---|
113 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
114 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
115 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
|
---|
116 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
|
---|
117 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
|
---|
118 | CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
|
---|
119 | CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
|
---|
120 | CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
|
---|
121 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
|
---|
122 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
|
---|
123 |
|
---|
124 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
|
---|
125 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
|
---|
126 | CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
|
---|
127 | };
|
---|
128 |
|
---|
129 | /** UnitTest for scalar products.
|
---|
130 | */
|
---|
131 | void VectorTest::EuclidianScalarProductTest()
|
---|
132 | {
|
---|
133 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&zero) );
|
---|
134 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&unit) );
|
---|
135 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&otherunit) );
|
---|
136 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(¬unit) );
|
---|
137 | CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(&unit) );
|
---|
138 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
|
---|
139 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
|
---|
140 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(¬unit) );
|
---|
141 | CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(&unit) );
|
---|
142 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&otherunit) );
|
---|
143 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(¬unit) );
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** UnitTest for norms.
|
---|
147 | */
|
---|
148 | void VectorTest::EuclidianNormTest()
|
---|
149 | {
|
---|
150 | CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
|
---|
151 | CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
|
---|
152 | CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
|
---|
153 | CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
|
---|
154 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
|
---|
155 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
|
---|
156 | CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
|
---|
157 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** UnitTest for distances.
|
---|
161 | */
|
---|
162 | void VectorTest::EuclidianDistancesTest()
|
---|
163 | {
|
---|
164 | CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(&unit) );
|
---|
165 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(&unit) );
|
---|
166 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(¬unit) );
|
---|
167 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(¬unit) );
|
---|
168 | CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(¬unit) );
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** UnitTest for angles.
|
---|
172 | */
|
---|
173 | void VectorTest::EuclidianAnglesTest()
|
---|
174 | {
|
---|
175 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) );
|
---|
176 | CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(&unit) );
|
---|
177 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(&unit)) < MYEPSILON );
|
---|
178 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(¬unit)) < MYEPSILON );
|
---|
179 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(¬unit)) < MYEPSILON );
|
---|
180 | };
|
---|
181 |
|
---|
182 | /** UnitTest for projections.
|
---|
183 | */
|
---|
184 | void VectorTest::ProjectionTest()
|
---|
185 | {
|
---|
186 | CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(&unit) );
|
---|
187 | CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(&unit) );
|
---|
188 | CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(&two) );
|
---|
189 | CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(&otherunit) );
|
---|
190 | };
|
---|
191 |
|
---|
192 | /** UnitTest for line intersections.
|
---|
193 | */
|
---|
194 | void VectorTest::LineIntersectionTest()
|
---|
195 | {
|
---|
196 | // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
|
---|
197 | CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionWithPlane(&unit, &zero, &zero, &two) );
|
---|
198 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
199 |
|
---|
200 | // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
|
---|
201 | CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionWithPlane(&otherunit, &two, &unit, ¬unit) );
|
---|
202 | CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
|
---|
203 |
|
---|
204 | // four vectors equal to zero
|
---|
205 | CPPUNIT_ASSERT_EQUAL( false, fixture.GetIntersectionOfTwoLinesOnPlane(&zero, &zero, &zero, &zero, NULL) );
|
---|
206 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
207 |
|
---|
208 | // four vectors equal to unit
|
---|
209 | CPPUNIT_ASSERT_EQUAL( false, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &unit, &unit, &unit, NULL) );
|
---|
210 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
211 |
|
---|
212 | // two equal lines
|
---|
213 | CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &unit, &two, NULL) );
|
---|
214 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
---|
215 |
|
---|
216 | // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
|
---|
217 | CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &unit, &otherunit, NULL) );
|
---|
218 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
---|
219 |
|
---|
220 | // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
|
---|
221 | CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &zero, &zero, &two, NULL) );
|
---|
222 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
223 |
|
---|
224 | // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
|
---|
225 | CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &zero, &otherunit, NULL) );
|
---|
226 | CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), fixture );
|
---|
227 | };
|
---|
228 |
|
---|
229 | /** UnitTest for vector rotations.
|
---|
230 | */
|
---|
231 | void VectorTest::VectorRotationTest()
|
---|
232 | {
|
---|
233 | fixture.Init(-1.,0.,0.);
|
---|
234 |
|
---|
235 | // zero vector does not change
|
---|
236 | fixture.CopyVector(&zero);
|
---|
237 | fixture.RotateVector(&unit, 1.);
|
---|
238 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
239 |
|
---|
240 | fixture.RotateVector(&two, 1.);
|
---|
241 | CPPUNIT_ASSERT_EQUAL( zero, fixture);
|
---|
242 |
|
---|
243 | // vector on axis does not change
|
---|
244 | fixture.CopyVector(&unit);
|
---|
245 | fixture.RotateVector(&unit, 1.);
|
---|
246 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
---|
247 |
|
---|
248 | fixture.RotateVector(&unit, 1.);
|
---|
249 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
---|
250 |
|
---|
251 | // rotations
|
---|
252 | fixture.CopyVector(&otherunit);
|
---|
253 | fixture.RotateVector(&unit, M_PI);
|
---|
254 | CPPUNIT_ASSERT_EQUAL( Vector(0.,-1.,0.), fixture );
|
---|
255 |
|
---|
256 | fixture.CopyVector(&otherunit);
|
---|
257 | fixture.RotateVector(&unit, 2. * M_PI);
|
---|
258 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
---|
259 |
|
---|
260 | fixture.CopyVector(&otherunit);
|
---|
261 | fixture.RotateVector(&unit, 0);
|
---|
262 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
---|
263 |
|
---|
264 | fixture.Init(0.,0.,1.);
|
---|
265 | fixture.RotateVector(¬unit, M_PI);
|
---|
266 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * UnitTest for Vector::IsInParallelepiped().
|
---|
271 | */
|
---|
272 | void VectorTest::IsInParallelepipedTest()
|
---|
273 | {
|
---|
274 | double parallelepiped[NDIM*NDIM];
|
---|
275 | parallelepiped[0] = 1;
|
---|
276 | parallelepiped[1] = 0;
|
---|
277 | parallelepiped[2] = 0;
|
---|
278 | parallelepiped[3] = 0;
|
---|
279 | parallelepiped[4] = 1;
|
---|
280 | parallelepiped[5] = 0;
|
---|
281 | parallelepiped[6] = 0;
|
---|
282 | parallelepiped[7] = 0;
|
---|
283 | parallelepiped[8] = 1;
|
---|
284 |
|
---|
285 | fixture.CopyVector(zero);
|
---|
286 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
287 | fixture.Init(2.5,2.5,2.5);
|
---|
288 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
289 | fixture.Init(1.,1.,1.);
|
---|
290 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
291 | fixture.Init(3.5,3.5,3.5);
|
---|
292 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
293 | fixture.Init(2.,2.,2.);
|
---|
294 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
295 | fixture.Init(2.,3.,2.);
|
---|
296 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
297 | fixture.Init(-2.,2.,-1.);
|
---|
298 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
299 | }
|
---|
300 |
|
---|