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