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