[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 |
|
---|
[9fb860] | 8 | /*
|
---|
[bbf1bd] | 9 | * VectorContentUnittest.cpp
|
---|
[9fb860] | 10 | *
|
---|
| 11 | * Created on: Jan 8, 2010
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[9fb860] | 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | #include <cppunit/CompilerOutputter.h>
|
---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 25 |
|
---|
[dfafe7] | 26 | #include "VectorContentUnitTest.hpp"
|
---|
[9fb860] | 27 |
|
---|
[9b6b2f] | 28 | #ifdef HAVE_TESTRUNNER
|
---|
| 29 | #include "UnitTestMain.hpp"
|
---|
| 30 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 31 |
|
---|
[9fb860] | 32 | /********************************************** Test classes **************************************/
|
---|
| 33 |
|
---|
| 34 | // Registers the fixture into the 'registry'
|
---|
[bbf1bd] | 35 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorContentTest );
|
---|
[9fb860] | 36 |
|
---|
| 37 |
|
---|
[bbf1bd] | 38 | void VectorContentTest::setUp()
|
---|
[9fb860] | 39 | {
|
---|
[bbf1bd] | 40 | v = new VectorContent(3);
|
---|
[9fb860] | 41 | };
|
---|
| 42 |
|
---|
[bbf1bd] | 43 | void VectorContentTest::tearDown()
|
---|
[9fb860] | 44 | {
|
---|
| 45 | delete(v);
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | /** Unit test for accessing elements in the vector.
|
---|
| 49 | *
|
---|
| 50 | */
|
---|
[bbf1bd] | 51 | void VectorContentTest::AccessTest()
|
---|
[9fb860] | 52 | {
|
---|
| 53 | // set
|
---|
| 54 | for (int i=0;i<3;i++)
|
---|
[bbf1bd] | 55 | v->at(i) = (double)i;
|
---|
[9fb860] | 56 |
|
---|
| 57 | // and check
|
---|
| 58 | double *ptr = NULL;
|
---|
| 59 | for (int i=0;i<3;i++) {
|
---|
[bbf1bd] | 60 | CPPUNIT_ASSERT_EQUAL( (double)i, v->at(i) );
|
---|
[9fb860] | 61 | ptr = v->Pointer(i);
|
---|
| 62 | CPPUNIT_ASSERT_EQUAL( (double)i, *ptr );
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | // out of bounds
|
---|
[bbf1bd] | 66 | //CPPUNIT_ASSERT_EQUAL(0., v->at(3) );
|
---|
| 67 | //CPPUNIT_ASSERT_EQUAL(0., v->at(-1) );
|
---|
[9fb860] | 68 | };
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | /** Unit test for initializing the vector.
|
---|
| 72 | *
|
---|
| 73 | */
|
---|
[bbf1bd] | 74 | void VectorContentTest::InitializaionTest()
|
---|
[9fb860] | 75 | {
|
---|
| 76 | // set zero
|
---|
[bbf1bd] | 77 | v->setZero();
|
---|
[9fb860] | 78 | for (int i=0;i<3;i++)
|
---|
[bbf1bd] | 79 | CPPUNIT_ASSERT_EQUAL( 0., v->at(i) );
|
---|
[9fb860] | 80 |
|
---|
| 81 | // set basis
|
---|
| 82 | for (int i=0;i<3;i++) {
|
---|
[bbf1bd] | 83 | v->setBasis(i);
|
---|
[9fb860] | 84 | for (int j=0;j<3;j++)
|
---|
[bbf1bd] | 85 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->at(j) );
|
---|
[9fb860] | 86 | }
|
---|
| 87 |
|
---|
| 88 | // set all
|
---|
[bbf1bd] | 89 | v->setValue(1.5);
|
---|
[9fb860] | 90 | for (int i=0;i<3;i++)
|
---|
[bbf1bd] | 91 | CPPUNIT_ASSERT_EQUAL( 1.5, v->at(i) );
|
---|
[9fb860] | 92 | };
|
---|
| 93 |
|
---|
| 94 | /** Unit test for copying vectors.
|
---|
| 95 | *
|
---|
| 96 | */
|
---|
[bbf1bd] | 97 | void VectorContentTest::CopyTest()
|
---|
[9fb860] | 98 | {
|
---|
| 99 | // set basis
|
---|
[bbf1bd] | 100 | VectorContent *dest = NULL;
|
---|
[9fb860] | 101 | for (int i=0;i<3;i++) {
|
---|
[bbf1bd] | 102 | v->setBasis(i);
|
---|
| 103 | dest = new VectorContent(v);
|
---|
[9fb860] | 104 | for (int j=0;j<3;j++)
|
---|
[bbf1bd] | 105 | CPPUNIT_ASSERT_EQUAL( v->at(j) , dest->at(j) );
|
---|
[9fb860] | 106 |
|
---|
| 107 | delete(dest);
|
---|
| 108 | }
|
---|
| 109 | };
|
---|
| 110 |
|
---|
| 111 | /** Unit test for exchanging elements of a vector.
|
---|
| 112 | *
|
---|
| 113 | */
|
---|
[bbf1bd] | 114 | void VectorContentTest::ExchangeTest()
|
---|
[9fb860] | 115 | {
|
---|
| 116 | // set basis
|
---|
| 117 | for (int i=0;i<3;i++) {
|
---|
[bbf1bd] | 118 | v->setBasis(i);
|
---|
[9fb860] | 119 | v->SwapElements((i)%3,(i+1)%3);
|
---|
| 120 | for (int j=0;j<3;j++)
|
---|
[bbf1bd] | 121 | CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->at(j) );
|
---|
[9fb860] | 122 | }
|
---|
| 123 |
|
---|
| 124 | // set to 1,2,3 and reverse
|
---|
| 125 | for (int i=0;i<3;i++)
|
---|
[bbf1bd] | 126 | v->at(i) = (double)(i+1);
|
---|
[9fb860] | 127 | v->Reverse();
|
---|
| 128 | for (int j=0;j<3;j++)
|
---|
[bbf1bd] | 129 | CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->at(j) );
|
---|
[9fb860] | 130 | };
|
---|
| 131 |
|
---|
[b11d3b] | 132 | /** UnitTest for operators.
|
---|
| 133 | */
|
---|
[bbf1bd] | 134 | void VectorContentTest::OperatorIsTest()
|
---|
[b11d3b] | 135 | {
|
---|
[bbf1bd] | 136 | VectorContent zero(3);
|
---|
| 137 | VectorContent unit(3);
|
---|
| 138 | zero.setZero();
|
---|
| 139 | unit.setZero();
|
---|
| 140 | unit[1] = 1.;
|
---|
[b11d3b] | 141 | // summation and scaling
|
---|
| 142 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
| 143 | CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
|
---|
| 144 | CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
|
---|
| 145 | CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
|
---|
| 146 | };
|
---|
| 147 |
|
---|
| 148 | /** UnitTest for operators.
|
---|
| 149 | */
|
---|
[bbf1bd] | 150 | void VectorContentTest::OperatorAlgebraTest()
|
---|
[b11d3b] | 151 | {
|
---|
[bbf1bd] | 152 | VectorContent zero(3);
|
---|
| 153 | VectorContent unit(3);
|
---|
| 154 | zero.setZero();
|
---|
| 155 | unit.setZero();
|
---|
| 156 | unit[1] = 1.;
|
---|
[b11d3b] | 157 | // summation and scaling
|
---|
| 158 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
| 159 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
| 160 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
|
---|
| 161 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
|
---|
| 162 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
|
---|
| 163 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
|
---|
| 164 | CPPUNIT_ASSERT_EQUAL( false, (0.98*unit).IsOne() );
|
---|
| 165 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
|
---|
| 166 | CPPUNIT_ASSERT_EQUAL( true, (1.*unit).IsOne() );
|
---|
| 167 |
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
|
---|
| 169 | CPPUNIT_ASSERT_EQUAL( zero, (zero+zero) );
|
---|
| 170 | CPPUNIT_ASSERT_EQUAL( unit, (unit+zero) );
|
---|
| 171 |
|
---|
| 172 | unit += zero;
|
---|
| 173 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
| 174 | unit *= 1.;
|
---|
| 175 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
| 176 | };
|
---|