| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [6d608d] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [54a746] | 8 | /* | 
|---|
| [e23075] | 9 | * VectorUnitTest.cpp | 
|---|
| [54a746] | 10 | * | 
|---|
|  | 11 | *  Created on: Aug 17, 2009 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
| [54a746] | 19 |  | 
|---|
|  | 20 | using namespace std; | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 23 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 24 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 25 |  | 
|---|
| [9b410d] | 26 | #include <limits> | 
|---|
|  | 27 |  | 
|---|
| [ad011c] | 28 | #include "CodePatterns/Log.hpp" | 
|---|
| [bf4b9f] | 29 | #include "defs.hpp" | 
|---|
|  | 30 | #include "Plane.hpp" | 
|---|
|  | 31 | #include "RealSpaceMatrix.hpp" | 
|---|
|  | 32 | #include "Vector.hpp" | 
|---|
|  | 33 | #include "vector_ops.hpp" | 
|---|
| [54a746] | 34 |  | 
|---|
| [dfafe7] | 35 | #include "VectorUnitTest.hpp" | 
|---|
|  | 36 |  | 
|---|
| [9b6b2f] | 37 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 38 | #include "UnitTestMain.hpp" | 
|---|
|  | 39 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 40 |  | 
|---|
| [1bd79e] | 41 | #include <iostream> | 
|---|
|  | 42 |  | 
|---|
| [54a746] | 43 | /********************************************** Test classes **************************************/ | 
|---|
|  | 44 |  | 
|---|
|  | 45 | // Registers the fixture into the 'registry' | 
|---|
|  | 46 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest ); | 
|---|
|  | 47 |  | 
|---|
|  | 48 |  | 
|---|
|  | 49 | void VectorTest::setUp() | 
|---|
|  | 50 | { | 
|---|
| [1bd79e] | 51 | zero  = Vector(0.,0.,0.); | 
|---|
|  | 52 | unit = Vector(1.,0.,0.); | 
|---|
|  | 53 | otherunit = Vector(0.,1.,0.); | 
|---|
|  | 54 | notunit = Vector(0.,1.,1.); | 
|---|
|  | 55 | two = Vector(2.,1.,0.); | 
|---|
| [1829c4] | 56 | three = Vector(1,2,3); | 
|---|
| [54a746] | 57 | }; | 
|---|
|  | 58 |  | 
|---|
|  | 59 |  | 
|---|
|  | 60 | void VectorTest::tearDown() | 
|---|
|  | 61 | { | 
|---|
| [e6fdbe] | 62 | logger::purgeInstance(); | 
|---|
|  | 63 | errorLogger::purgeInstance(); | 
|---|
| [54a746] | 64 | }; | 
|---|
|  | 65 |  | 
|---|
| [d74077] | 66 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne(). | 
|---|
|  | 67 | */ | 
|---|
|  | 68 | void VectorTest::AssignmentTest() | 
|---|
|  | 69 | { | 
|---|
|  | 70 | // test with zero | 
|---|
|  | 71 | zero.at(0) = 0; | 
|---|
|  | 72 | zero.at(1) = 0; | 
|---|
|  | 73 | zero.at(2) = 0; | 
|---|
|  | 74 | double zero_array[3] = {0., 0., 0.}; | 
|---|
|  | 75 |  | 
|---|
|  | 76 | CPPUNIT_ASSERT_EQUAL( zero, Vector(0,0,0)); | 
|---|
|  | 77 | CPPUNIT_ASSERT_EQUAL( zero, Vector(0.,0.,0.)); | 
|---|
|  | 78 | CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array[0], zero_array[1], zero_array[2])); | 
|---|
|  | 79 | CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array)); | 
|---|
|  | 80 |  | 
|---|
|  | 81 | // test with unit | 
|---|
|  | 82 | unit.at(0) = 1; | 
|---|
|  | 83 | unit.at(1) = 0; | 
|---|
|  | 84 | unit.at(2) = 0; | 
|---|
|  | 85 | double unit_array[3] = {1., 0., 0.}; | 
|---|
|  | 86 |  | 
|---|
|  | 87 | CPPUNIT_ASSERT_EQUAL( unit, Vector(1,0,0)); | 
|---|
|  | 88 | CPPUNIT_ASSERT_EQUAL( unit, Vector(1.,0.,0.)); | 
|---|
|  | 89 | CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array[0], unit_array[1], unit_array[2])); | 
|---|
|  | 90 | CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array)); | 
|---|
|  | 91 |  | 
|---|
|  | 92 | // test with two | 
|---|
|  | 93 | two.at(0) = 2; | 
|---|
|  | 94 | two.at(1) = 1; | 
|---|
|  | 95 | two.at(2) = 0; | 
|---|
|  | 96 | double two_array[3] = {2., 1., 0.}; | 
|---|
|  | 97 |  | 
|---|
|  | 98 | CPPUNIT_ASSERT_EQUAL( two, Vector(2,1,0)); | 
|---|
|  | 99 | CPPUNIT_ASSERT_EQUAL( two, Vector(2.,1.,0.)); | 
|---|
|  | 100 | CPPUNIT_ASSERT_EQUAL( two, Vector(two_array[0], two_array[1], two_array[2])); | 
|---|
|  | 101 | CPPUNIT_ASSERT_EQUAL( two, Vector(two_array)); | 
|---|
|  | 102 |  | 
|---|
|  | 103 | // test with three | 
|---|
|  | 104 | three.at(0) = 1; | 
|---|
|  | 105 | three.at(1) = 2; | 
|---|
|  | 106 | three.at(2) = 3; | 
|---|
|  | 107 | double three_array[3] = {1., 2., 3.}; | 
|---|
|  | 108 |  | 
|---|
|  | 109 | CPPUNIT_ASSERT_EQUAL( three, Vector(1,2,3)); | 
|---|
|  | 110 | CPPUNIT_ASSERT_EQUAL( three, Vector(1.,2.,3.)); | 
|---|
|  | 111 | CPPUNIT_ASSERT_EQUAL( three, Vector(three_array[0], three_array[1], three_array[2])); | 
|---|
|  | 112 | CPPUNIT_ASSERT_EQUAL( three, Vector(three_array)); | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
| [54a746] | 115 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne(). | 
|---|
|  | 116 | */ | 
|---|
|  | 117 | void VectorTest::UnityTest() | 
|---|
|  | 118 | { | 
|---|
|  | 119 | // unity and zero tests | 
|---|
|  | 120 | CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() ); | 
|---|
|  | 121 | CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() ); | 
|---|
|  | 122 | CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() ); | 
|---|
|  | 123 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() ); | 
|---|
|  | 124 | CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() ); | 
|---|
|  | 125 | CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() ); | 
|---|
|  | 126 | CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() ); | 
|---|
|  | 127 | }; | 
|---|
|  | 128 |  | 
|---|
|  | 129 | /** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/ | 
|---|
|  | 130 | */ | 
|---|
|  | 131 | void VectorTest::SimpleAlgebraTest() | 
|---|
|  | 132 | { | 
|---|
|  | 133 | double factor; | 
|---|
| [78b73c] | 134 | // copy vector | 
|---|
| [1bd79e] | 135 | fixture = Vector(2.,3.,4.); | 
|---|
| [78b73c] | 136 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture ); | 
|---|
| [54a746] | 137 | // summation and scaling | 
|---|
| [273382] | 138 | fixture = zero + unit; | 
|---|
| [78b73c] | 139 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| [273382] | 140 | fixture = zero - unit; | 
|---|
| [78b73c] | 141 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
|  | 142 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() ); | 
|---|
| [273382] | 143 | fixture = zero + zero; | 
|---|
| [78b73c] | 144 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() ); | 
|---|
| [273382] | 145 | fixture = notunit - otherunit; | 
|---|
| [78b73c] | 146 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| [273382] | 147 | fixture = unit - otherunit; | 
|---|
| [78b73c] | 148 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() ); | 
|---|
| [273382] | 149 | fixture = notunit - unit - otherunit; | 
|---|
| [78b73c] | 150 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() ); | 
|---|
| [273382] | 151 | fixture = 0.98 * unit; | 
|---|
| [78b73c] | 152 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() ); | 
|---|
| [273382] | 153 | fixture = 1. * unit; | 
|---|
| [78b73c] | 154 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| [54a746] | 155 | factor = 0.98; | 
|---|
| [273382] | 156 | fixture = factor * unit; | 
|---|
| [78b73c] | 157 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() ); | 
|---|
| [54a746] | 158 | factor = 1.; | 
|---|
| [273382] | 159 | fixture = factor * unit; | 
|---|
| [78b73c] | 160 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| [54a746] | 161 | }; | 
|---|
|  | 162 |  | 
|---|
|  | 163 |  | 
|---|
|  | 164 | /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale(). | 
|---|
|  | 165 | */ | 
|---|
|  | 166 | void VectorTest::OperatorAlgebraTest() | 
|---|
|  | 167 | { | 
|---|
|  | 168 | // summation and scaling | 
|---|
|  | 169 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() ); | 
|---|
| [ef9df36] | 170 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() ); | 
|---|
| [54a746] | 171 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() ); | 
|---|
|  | 172 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() ); | 
|---|
|  | 173 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() ); | 
|---|
|  | 174 | CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() ); | 
|---|
|  | 175 | CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() ); | 
|---|
|  | 176 | CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() ); | 
|---|
|  | 177 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() ); | 
|---|
|  | 178 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() ); | 
|---|
| [ef9df36] | 179 |  | 
|---|
|  | 180 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) ); | 
|---|
|  | 181 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) ); | 
|---|
|  | 182 | CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) ); | 
|---|
| [54a746] | 183 | }; | 
|---|
|  | 184 |  | 
|---|
| [ef9df36] | 185 | /** UnitTest for scalar products. | 
|---|
| [54a746] | 186 | */ | 
|---|
| [ef9df36] | 187 | void VectorTest::EuclidianScalarProductTest() | 
|---|
| [54a746] | 188 | { | 
|---|
| [273382] | 189 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(zero) ); | 
|---|
|  | 190 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(unit) ); | 
|---|
|  | 191 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(otherunit) ); | 
|---|
|  | 192 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(notunit) ); | 
|---|
|  | 193 | CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(unit) ); | 
|---|
|  | 194 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) ); | 
|---|
|  | 195 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) ); | 
|---|
|  | 196 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(notunit) ); | 
|---|
|  | 197 | CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(unit) ); | 
|---|
|  | 198 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(otherunit) ); | 
|---|
|  | 199 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(notunit) ); | 
|---|
| [ef9df36] | 200 | } | 
|---|
| [54a746] | 201 |  | 
|---|
| [ef9df36] | 202 | /** UnitTest for norms. | 
|---|
|  | 203 | */ | 
|---|
|  | 204 | void VectorTest::EuclidianNormTest() | 
|---|
|  | 205 | { | 
|---|
| [54a746] | 206 | CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() ); | 
|---|
|  | 207 | CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() ); | 
|---|
|  | 208 | CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() ); | 
|---|
|  | 209 | CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() ); | 
|---|
|  | 210 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() ); | 
|---|
|  | 211 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() ); | 
|---|
|  | 212 | CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() ); | 
|---|
|  | 213 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() ); | 
|---|
| [ef9df36] | 214 | } | 
|---|
| [54a746] | 215 |  | 
|---|
| [ef9df36] | 216 | /** UnitTest for distances. | 
|---|
|  | 217 | */ | 
|---|
|  | 218 | void VectorTest::EuclidianDistancesTest() | 
|---|
|  | 219 | { | 
|---|
| [1513a74] | 220 | CPPUNIT_ASSERT_EQUAL( 1., zero.distance(unit) ); | 
|---|
|  | 221 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.distance(unit) ); | 
|---|
|  | 222 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.distance(notunit) ); | 
|---|
|  | 223 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.distance(notunit) ); | 
|---|
|  | 224 | CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.distance(notunit) ); | 
|---|
| [e23075] | 225 |  | 
|---|
|  | 226 | // check operator | 
|---|
|  | 227 | CPPUNIT_ASSERT( !(zero < zero) ); | 
|---|
|  | 228 | CPPUNIT_ASSERT( !(unit < otherunit) ); | 
|---|
|  | 229 | CPPUNIT_ASSERT( zero < unit ); | 
|---|
|  | 230 | CPPUNIT_ASSERT( unit < two ); | 
|---|
| [ef9df36] | 231 | } | 
|---|
| [54a746] | 232 |  | 
|---|
| [ef9df36] | 233 | /** UnitTest for angles. | 
|---|
|  | 234 | */ | 
|---|
|  | 235 | void VectorTest::EuclidianAnglesTest() | 
|---|
|  | 236 | { | 
|---|
| [273382] | 237 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(unit) ); | 
|---|
|  | 238 | CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(unit) ); | 
|---|
| [71129f] | 239 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(unit)) <= LINALG_MYEPSILON() ); | 
|---|
|  | 240 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(notunit)) <= LINALG_MYEPSILON() ); | 
|---|
|  | 241 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(notunit)) <= LINALG_MYEPSILON() ); | 
|---|
| [54a746] | 242 | }; | 
|---|
|  | 243 |  | 
|---|
| [ef9df36] | 244 | /** UnitTest for projections. | 
|---|
|  | 245 | */ | 
|---|
|  | 246 | void VectorTest::ProjectionTest() | 
|---|
|  | 247 | { | 
|---|
| [273382] | 248 | CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(unit) ); | 
|---|
|  | 249 | CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(unit) ); | 
|---|
|  | 250 | CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.),  otherunit.Projection(two) ); | 
|---|
|  | 251 | CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.),  two.Projection(otherunit) ); | 
|---|
| [ef9df36] | 252 | }; | 
|---|
|  | 253 |  | 
|---|
| [1829c4] | 254 | /** | 
|---|
|  | 255 | * Unittest for operation with normals | 
|---|
|  | 256 | */ | 
|---|
|  | 257 | void VectorTest::NormalsTest(){ | 
|---|
|  | 258 | Vector testVector; | 
|---|
|  | 259 | // the zero Vector should produce an error | 
|---|
|  | 260 | CPPUNIT_ASSERT(!testVector.GetOneNormalVector(zero)); | 
|---|
|  | 261 |  | 
|---|
|  | 262 | // first one-component system | 
|---|
|  | 263 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(unit)); | 
|---|
| [71129f] | 264 | CPPUNIT_ASSERT(testVector.ScalarProduct(unit) <= LINALG_MYEPSILON()); | 
|---|
| [1829c4] | 265 |  | 
|---|
|  | 266 | // second one-component system | 
|---|
|  | 267 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit)); | 
|---|
| [71129f] | 268 | CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) <= LINALG_MYEPSILON()); | 
|---|
| [1829c4] | 269 |  | 
|---|
|  | 270 | // first two-component system | 
|---|
|  | 271 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit)); | 
|---|
| [71129f] | 272 | CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) <= LINALG_MYEPSILON()); | 
|---|
| [1829c4] | 273 |  | 
|---|
|  | 274 | // second two-component system | 
|---|
|  | 275 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(two)); | 
|---|
| [71129f] | 276 | CPPUNIT_ASSERT(testVector.ScalarProduct(two) <= LINALG_MYEPSILON()); | 
|---|
| [1829c4] | 277 |  | 
|---|
|  | 278 | // three component system | 
|---|
|  | 279 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(three)); | 
|---|
| [71129f] | 280 | CPPUNIT_ASSERT(testVector.ScalarProduct(three) <= LINALG_MYEPSILON()); | 
|---|
| [1829c4] | 281 | } | 
|---|