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