| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * unittest.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Aug 17, 2009 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 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 |  | 
|---|
| 26 | #include "defs.hpp" | 
|---|
| 27 | #include "Helpers/Log.hpp" | 
|---|
| 28 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 29 | #include "LinearAlgebra/vector_ops.hpp" | 
|---|
| 30 | #include "vectorunittest.hpp" | 
|---|
| 31 | #include "LinearAlgebra/Plane.hpp" | 
|---|
| 32 | #include "Exceptions/LinearDependenceException.hpp" | 
|---|
| 33 | #include "LinearAlgebra/Matrix.hpp" | 
|---|
| 34 |  | 
|---|
| 35 | #ifdef HAVE_TESTRUNNER | 
|---|
| 36 | #include "UnitTestMain.hpp" | 
|---|
| 37 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 38 |  | 
|---|
| 39 | #include <iostream> | 
|---|
| 40 |  | 
|---|
| 41 | using namespace std; | 
|---|
| 42 |  | 
|---|
| 43 | /********************************************** Test classes **************************************/ | 
|---|
| 44 |  | 
|---|
| 45 | // Registers the fixture into the 'registry' | 
|---|
| 46 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest ); | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | void VectorTest::setUp() | 
|---|
| 50 | { | 
|---|
| 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.); | 
|---|
| 56 | three = Vector(1,2,3); | 
|---|
| 57 | }; | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | void VectorTest::tearDown() | 
|---|
| 61 | { | 
|---|
| 62 | logger::purgeInstance(); | 
|---|
| 63 | errorLogger::purgeInstance(); | 
|---|
| 64 | }; | 
|---|
| 65 |  | 
|---|
| 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 |  | 
|---|
| 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; | 
|---|
| 134 | // copy vector | 
|---|
| 135 | fixture = Vector(2.,3.,4.); | 
|---|
| 136 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture ); | 
|---|
| 137 | // summation and scaling | 
|---|
| 138 | fixture = zero + unit; | 
|---|
| 139 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| 140 | fixture = zero - unit; | 
|---|
| 141 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| 142 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() ); | 
|---|
| 143 | fixture = zero + zero; | 
|---|
| 144 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() ); | 
|---|
| 145 | fixture = notunit - otherunit; | 
|---|
| 146 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| 147 | fixture = unit - otherunit; | 
|---|
| 148 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() ); | 
|---|
| 149 | fixture = notunit - unit - otherunit; | 
|---|
| 150 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() ); | 
|---|
| 151 | fixture = 0.98 * unit; | 
|---|
| 152 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() ); | 
|---|
| 153 | fixture = 1. * unit; | 
|---|
| 154 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| 155 | factor = 0.98; | 
|---|
| 156 | fixture = factor * unit; | 
|---|
| 157 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() ); | 
|---|
| 158 | factor = 1.; | 
|---|
| 159 | fixture = factor * unit; | 
|---|
| 160 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() ); | 
|---|
| 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() ); | 
|---|
| 170 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() ); | 
|---|
| 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() ); | 
|---|
| 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) ); | 
|---|
| 183 | }; | 
|---|
| 184 |  | 
|---|
| 185 | /** UnitTest for scalar products. | 
|---|
| 186 | */ | 
|---|
| 187 | void VectorTest::EuclidianScalarProductTest() | 
|---|
| 188 | { | 
|---|
| 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) ); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | /** UnitTest for norms. | 
|---|
| 203 | */ | 
|---|
| 204 | void VectorTest::EuclidianNormTest() | 
|---|
| 205 | { | 
|---|
| 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() ); | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | /** UnitTest for distances. | 
|---|
| 217 | */ | 
|---|
| 218 | void VectorTest::EuclidianDistancesTest() | 
|---|
| 219 | { | 
|---|
| 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) ); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | /** UnitTest for angles. | 
|---|
| 228 | */ | 
|---|
| 229 | void VectorTest::EuclidianAnglesTest() | 
|---|
| 230 | { | 
|---|
| 231 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(unit) ); | 
|---|
| 232 | CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(unit) ); | 
|---|
| 233 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(unit)) < MYEPSILON ); | 
|---|
| 234 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(notunit)) < MYEPSILON ); | 
|---|
| 235 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(notunit)) < MYEPSILON ); | 
|---|
| 236 | }; | 
|---|
| 237 |  | 
|---|
| 238 | /** UnitTest for projections. | 
|---|
| 239 | */ | 
|---|
| 240 | void VectorTest::ProjectionTest() | 
|---|
| 241 | { | 
|---|
| 242 | CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(unit) ); | 
|---|
| 243 | CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(unit) ); | 
|---|
| 244 | CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.),  otherunit.Projection(two) ); | 
|---|
| 245 | CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.),  two.Projection(otherunit) ); | 
|---|
| 246 | }; | 
|---|
| 247 |  | 
|---|
| 248 | /** | 
|---|
| 249 | * Unittest for operation with normals | 
|---|
| 250 | */ | 
|---|
| 251 | void VectorTest::NormalsTest(){ | 
|---|
| 252 | Vector testVector; | 
|---|
| 253 | // the zero Vector should produce an error | 
|---|
| 254 | CPPUNIT_ASSERT(!testVector.GetOneNormalVector(zero)); | 
|---|
| 255 |  | 
|---|
| 256 | // first one-component system | 
|---|
| 257 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(unit)); | 
|---|
| 258 | CPPUNIT_ASSERT(testVector.ScalarProduct(unit) < MYEPSILON); | 
|---|
| 259 |  | 
|---|
| 260 | // second one-component system | 
|---|
| 261 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit)); | 
|---|
| 262 | CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) < MYEPSILON); | 
|---|
| 263 |  | 
|---|
| 264 | // first two-component system | 
|---|
| 265 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit)); | 
|---|
| 266 | CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) < MYEPSILON); | 
|---|
| 267 |  | 
|---|
| 268 | // second two-component system | 
|---|
| 269 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(two)); | 
|---|
| 270 | CPPUNIT_ASSERT(testVector.ScalarProduct(two) < MYEPSILON); | 
|---|
| 271 |  | 
|---|
| 272 | // three component system | 
|---|
| 273 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(three)); | 
|---|
| 274 | CPPUNIT_ASSERT(testVector.ScalarProduct(three) < MYEPSILON); | 
|---|
| 275 | } | 
|---|