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