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 "vector.hpp"
|
---|
18 | #include "vector_ops.hpp"
|
---|
19 | #include "vectorunittest.hpp"
|
---|
20 | #include "Plane.hpp"
|
---|
21 | #include "Exceptions/LinearDependenceException.hpp"
|
---|
22 | #include "Matrix.hpp"
|
---|
23 |
|
---|
24 | #ifdef HAVE_TESTRUNNER
|
---|
25 | #include "UnitTestMain.hpp"
|
---|
26 | #endif /*HAVE_TESTRUNNER*/
|
---|
27 |
|
---|
28 | #include <iostream>
|
---|
29 |
|
---|
30 | using namespace std;
|
---|
31 |
|
---|
32 | /********************************************** Test classes **************************************/
|
---|
33 |
|
---|
34 | // Registers the fixture into the 'registry'
|
---|
35 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
|
---|
36 |
|
---|
37 |
|
---|
38 | void VectorTest::setUp()
|
---|
39 | {
|
---|
40 | zero = Vector(0.,0.,0.);
|
---|
41 | unit = Vector(1.,0.,0.);
|
---|
42 | otherunit = Vector(0.,1.,0.);
|
---|
43 | notunit = Vector(0.,1.,1.);
|
---|
44 | two = Vector(2.,1.,0.);
|
---|
45 | three = Vector(1,2,3);
|
---|
46 | };
|
---|
47 |
|
---|
48 |
|
---|
49 | void VectorTest::tearDown()
|
---|
50 | {
|
---|
51 | logger::purgeInstance();
|
---|
52 | errorLogger::purgeInstance();
|
---|
53 | };
|
---|
54 |
|
---|
55 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
---|
56 | */
|
---|
57 | void VectorTest::AssignmentTest()
|
---|
58 | {
|
---|
59 | // test with zero
|
---|
60 | zero.at(0) = 0;
|
---|
61 | zero.at(1) = 0;
|
---|
62 | zero.at(2) = 0;
|
---|
63 | double zero_array[3] = {0., 0., 0.};
|
---|
64 |
|
---|
65 | CPPUNIT_ASSERT_EQUAL( zero, Vector(0,0,0));
|
---|
66 | CPPUNIT_ASSERT_EQUAL( zero, Vector(0.,0.,0.));
|
---|
67 | CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array[0], zero_array[1], zero_array[2]));
|
---|
68 | CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array));
|
---|
69 |
|
---|
70 | // test with unit
|
---|
71 | unit.at(0) = 1;
|
---|
72 | unit.at(1) = 0;
|
---|
73 | unit.at(2) = 0;
|
---|
74 | double unit_array[3] = {1., 0., 0.};
|
---|
75 |
|
---|
76 | CPPUNIT_ASSERT_EQUAL( unit, Vector(1,0,0));
|
---|
77 | CPPUNIT_ASSERT_EQUAL( unit, Vector(1.,0.,0.));
|
---|
78 | CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array[0], unit_array[1], unit_array[2]));
|
---|
79 | CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array));
|
---|
80 |
|
---|
81 | // test with two
|
---|
82 | two.at(0) = 2;
|
---|
83 | two.at(1) = 1;
|
---|
84 | two.at(2) = 0;
|
---|
85 | double two_array[3] = {2., 1., 0.};
|
---|
86 |
|
---|
87 | CPPUNIT_ASSERT_EQUAL( two, Vector(2,1,0));
|
---|
88 | CPPUNIT_ASSERT_EQUAL( two, Vector(2.,1.,0.));
|
---|
89 | CPPUNIT_ASSERT_EQUAL( two, Vector(two_array[0], two_array[1], two_array[2]));
|
---|
90 | CPPUNIT_ASSERT_EQUAL( two, Vector(two_array));
|
---|
91 |
|
---|
92 | // test with three
|
---|
93 | three.at(0) = 1;
|
---|
94 | three.at(1) = 2;
|
---|
95 | three.at(2) = 3;
|
---|
96 | double three_array[3] = {1., 2., 3.};
|
---|
97 |
|
---|
98 | CPPUNIT_ASSERT_EQUAL( three, Vector(1,2,3));
|
---|
99 | CPPUNIT_ASSERT_EQUAL( three, Vector(1.,2.,3.));
|
---|
100 | CPPUNIT_ASSERT_EQUAL( three, Vector(three_array[0], three_array[1], three_array[2]));
|
---|
101 | CPPUNIT_ASSERT_EQUAL( three, Vector(three_array));
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
---|
105 | */
|
---|
106 | void VectorTest::UnityTest()
|
---|
107 | {
|
---|
108 | // unity and zero tests
|
---|
109 | CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
|
---|
110 | CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
|
---|
111 | CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
|
---|
112 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
113 | CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() );
|
---|
114 | CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() );
|
---|
115 | CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() );
|
---|
116 | };
|
---|
117 |
|
---|
118 | /** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/
|
---|
119 | */
|
---|
120 | void VectorTest::SimpleAlgebraTest()
|
---|
121 | {
|
---|
122 | double factor;
|
---|
123 | // copy vector
|
---|
124 | fixture = Vector(2.,3.,4.);
|
---|
125 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
|
---|
126 | // summation and scaling
|
---|
127 | fixture = zero + unit;
|
---|
128 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
129 | fixture = zero - unit;
|
---|
130 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
131 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
132 | fixture = zero + zero;
|
---|
133 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
|
---|
134 | fixture = notunit - otherunit;
|
---|
135 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
136 | fixture = unit - otherunit;
|
---|
137 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
138 | fixture = notunit - unit - otherunit;
|
---|
139 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
140 | fixture = 0.98 * unit;
|
---|
141 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
142 | fixture = 1. * unit;
|
---|
143 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
144 | factor = 0.98;
|
---|
145 | fixture = factor * unit;
|
---|
146 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
147 | factor = 1.;
|
---|
148 | fixture = factor * unit;
|
---|
149 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
150 | };
|
---|
151 |
|
---|
152 |
|
---|
153 | /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
|
---|
154 | */
|
---|
155 | void VectorTest::OperatorAlgebraTest()
|
---|
156 | {
|
---|
157 | // summation and scaling
|
---|
158 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
159 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
160 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
|
---|
161 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
|
---|
162 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
|
---|
163 | CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
|
---|
164 | CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
|
---|
165 | CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
|
---|
166 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
|
---|
167 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
|
---|
168 |
|
---|
169 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
|
---|
170 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
|
---|
171 | CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
|
---|
172 | };
|
---|
173 |
|
---|
174 | /** UnitTest for scalar products.
|
---|
175 | */
|
---|
176 | void VectorTest::EuclidianScalarProductTest()
|
---|
177 | {
|
---|
178 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(zero) );
|
---|
179 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(unit) );
|
---|
180 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(otherunit) );
|
---|
181 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(notunit) );
|
---|
182 | CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(unit) );
|
---|
183 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
|
---|
184 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
|
---|
185 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(notunit) );
|
---|
186 | CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(unit) );
|
---|
187 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(otherunit) );
|
---|
188 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(notunit) );
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** UnitTest for norms.
|
---|
192 | */
|
---|
193 | void VectorTest::EuclidianNormTest()
|
---|
194 | {
|
---|
195 | CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
|
---|
196 | CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
|
---|
197 | CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
|
---|
198 | CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
|
---|
199 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
|
---|
200 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
|
---|
201 | CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
|
---|
202 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** UnitTest for distances.
|
---|
206 | */
|
---|
207 | void VectorTest::EuclidianDistancesTest()
|
---|
208 | {
|
---|
209 | CPPUNIT_ASSERT_EQUAL( 1., zero.distance(unit) );
|
---|
210 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.distance(unit) );
|
---|
211 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.distance(notunit) );
|
---|
212 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.distance(notunit) );
|
---|
213 | CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.distance(notunit) );
|
---|
214 | }
|
---|
215 |
|
---|
216 | /** UnitTest for angles.
|
---|
217 | */
|
---|
218 | void VectorTest::EuclidianAnglesTest()
|
---|
219 | {
|
---|
220 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(unit) );
|
---|
221 | CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(unit) );
|
---|
222 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(unit)) < MYEPSILON );
|
---|
223 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(notunit)) < MYEPSILON );
|
---|
224 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(notunit)) < MYEPSILON );
|
---|
225 | };
|
---|
226 |
|
---|
227 | /** UnitTest for projections.
|
---|
228 | */
|
---|
229 | void VectorTest::ProjectionTest()
|
---|
230 | {
|
---|
231 | CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(unit) );
|
---|
232 | CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(unit) );
|
---|
233 | CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(two) );
|
---|
234 | CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(otherunit) );
|
---|
235 | };
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Unittest for operation with normals
|
---|
239 | */
|
---|
240 | void VectorTest::NormalsTest(){
|
---|
241 | Vector testVector;
|
---|
242 | // the zero Vector should produce an error
|
---|
243 | CPPUNIT_ASSERT(!testVector.GetOneNormalVector(zero));
|
---|
244 |
|
---|
245 | // first one-component system
|
---|
246 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(unit));
|
---|
247 | CPPUNIT_ASSERT(testVector.ScalarProduct(unit) < MYEPSILON);
|
---|
248 |
|
---|
249 | // second one-component system
|
---|
250 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit));
|
---|
251 | CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) < MYEPSILON);
|
---|
252 |
|
---|
253 | // first two-component system
|
---|
254 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit));
|
---|
255 | CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) < MYEPSILON);
|
---|
256 |
|
---|
257 | // second two-component system
|
---|
258 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(two));
|
---|
259 | CPPUNIT_ASSERT(testVector.ScalarProduct(two) < MYEPSILON);
|
---|
260 |
|
---|
261 | // three component system
|
---|
262 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(three));
|
---|
263 | CPPUNIT_ASSERT(testVector.ScalarProduct(three) < MYEPSILON);
|
---|
264 | }
|
---|