[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[6d608d] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 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 |
|
---|
[29cbe9] | 26 | // include headers that implement a archive in simple text format
|
---|
| 27 | #include <boost/archive/text_oarchive.hpp>
|
---|
| 28 | #include <boost/archive/text_iarchive.hpp>
|
---|
| 29 |
|
---|
[dfafe7] | 30 | #include "VectorContentUnitTest.hpp"
|
---|
[9fb860] | 31 |
|
---|
[9b6b2f] | 32 | #ifdef HAVE_TESTRUNNER
|
---|
| 33 | #include "UnitTestMain.hpp"
|
---|
| 34 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 35 |
|
---|
[9fb860] | 36 | /********************************************** Test classes **************************************/
|
---|
| 37 |
|
---|
| 38 | // Registers the fixture into the 'registry'
|
---|
[bbf1bd] | 39 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorContentTest );
|
---|
[9fb860] | 40 |
|
---|
| 41 |
|
---|
[bbf1bd] | 42 | void VectorContentTest::setUp()
|
---|
[9fb860] | 43 | {
|
---|
[bbf1bd] | 44 | v = new VectorContent(3);
|
---|
[9fb860] | 45 | };
|
---|
| 46 |
|
---|
[bbf1bd] | 47 | void VectorContentTest::tearDown()
|
---|
[9fb860] | 48 | {
|
---|
| 49 | delete(v);
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | /** Unit test for accessing elements in the vector.
|
---|
| 53 | *
|
---|
| 54 | */
|
---|
[bbf1bd] | 55 | void VectorContentTest::AccessTest()
|
---|
[9fb860] | 56 | {
|
---|
| 57 | // set
|
---|
| 58 | for (int i=0;i<3;i++)
|
---|
[bbf1bd] | 59 | v->at(i) = (double)i;
|
---|
[9fb860] | 60 |
|
---|
| 61 | // and check
|
---|
| 62 | double *ptr = NULL;
|
---|
| 63 | for (int i=0;i<3;i++) {
|
---|
[bbf1bd] | 64 | CPPUNIT_ASSERT_EQUAL( (double)i, v->at(i) );
|
---|
[9fb860] | 65 | ptr = v->Pointer(i);
|
---|
| 66 | CPPUNIT_ASSERT_EQUAL( (double)i, *ptr );
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | // out of bounds
|
---|
[bbf1bd] | 70 | //CPPUNIT_ASSERT_EQUAL(0., v->at(3) );
|
---|
| 71 | //CPPUNIT_ASSERT_EQUAL(0., v->at(-1) );
|
---|
[9fb860] | 72 | };
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | /** Unit test for initializing the vector.
|
---|
| 76 | *
|
---|
| 77 | */
|
---|
[bbf1bd] | 78 | void VectorContentTest::InitializaionTest()
|
---|
[9fb860] | 79 | {
|
---|
| 80 | // set zero
|
---|
[bbf1bd] | 81 | v->setZero();
|
---|
[9fb860] | 82 | for (int i=0;i<3;i++)
|
---|
[bbf1bd] | 83 | CPPUNIT_ASSERT_EQUAL( 0., v->at(i) );
|
---|
[9fb860] | 84 |
|
---|
| 85 | // set basis
|
---|
| 86 | for (int i=0;i<3;i++) {
|
---|
[bbf1bd] | 87 | v->setBasis(i);
|
---|
[9fb860] | 88 | for (int j=0;j<3;j++)
|
---|
[bbf1bd] | 89 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->at(j) );
|
---|
[9fb860] | 90 | }
|
---|
| 91 |
|
---|
| 92 | // set all
|
---|
[bbf1bd] | 93 | v->setValue(1.5);
|
---|
[9fb860] | 94 | for (int i=0;i<3;i++)
|
---|
[bbf1bd] | 95 | CPPUNIT_ASSERT_EQUAL( 1.5, v->at(i) );
|
---|
[9fb860] | 96 | };
|
---|
| 97 |
|
---|
| 98 | /** Unit test for copying vectors.
|
---|
| 99 | *
|
---|
| 100 | */
|
---|
[bbf1bd] | 101 | void VectorContentTest::CopyTest()
|
---|
[9fb860] | 102 | {
|
---|
| 103 | // set basis
|
---|
[bbf1bd] | 104 | VectorContent *dest = NULL;
|
---|
[9fb860] | 105 | for (int i=0;i<3;i++) {
|
---|
[bbf1bd] | 106 | v->setBasis(i);
|
---|
| 107 | dest = new VectorContent(v);
|
---|
[9fb860] | 108 | for (int j=0;j<3;j++)
|
---|
[bbf1bd] | 109 | CPPUNIT_ASSERT_EQUAL( v->at(j) , dest->at(j) );
|
---|
[9fb860] | 110 |
|
---|
| 111 | delete(dest);
|
---|
| 112 | }
|
---|
| 113 | };
|
---|
| 114 |
|
---|
| 115 | /** Unit test for exchanging elements of a vector.
|
---|
| 116 | *
|
---|
| 117 | */
|
---|
[bbf1bd] | 118 | void VectorContentTest::ExchangeTest()
|
---|
[9fb860] | 119 | {
|
---|
| 120 | // set basis
|
---|
| 121 | for (int i=0;i<3;i++) {
|
---|
[bbf1bd] | 122 | v->setBasis(i);
|
---|
[9fb860] | 123 | v->SwapElements((i)%3,(i+1)%3);
|
---|
| 124 | for (int j=0;j<3;j++)
|
---|
[bbf1bd] | 125 | CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->at(j) );
|
---|
[9fb860] | 126 | }
|
---|
| 127 |
|
---|
| 128 | // set to 1,2,3 and reverse
|
---|
| 129 | for (int i=0;i<3;i++)
|
---|
[bbf1bd] | 130 | v->at(i) = (double)(i+1);
|
---|
[9fb860] | 131 | v->Reverse();
|
---|
| 132 | for (int j=0;j<3;j++)
|
---|
[bbf1bd] | 133 | CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->at(j) );
|
---|
[9fb860] | 134 | };
|
---|
| 135 |
|
---|
[b11d3b] | 136 | /** UnitTest for operators.
|
---|
| 137 | */
|
---|
[bbf1bd] | 138 | void VectorContentTest::OperatorIsTest()
|
---|
[b11d3b] | 139 | {
|
---|
[bbf1bd] | 140 | VectorContent zero(3);
|
---|
| 141 | VectorContent unit(3);
|
---|
| 142 | zero.setZero();
|
---|
| 143 | unit.setZero();
|
---|
| 144 | unit[1] = 1.;
|
---|
[b11d3b] | 145 | // summation and scaling
|
---|
| 146 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
| 147 | CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
|
---|
| 148 | CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
|
---|
| 149 | CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 | /** UnitTest for operators.
|
---|
| 153 | */
|
---|
[bbf1bd] | 154 | void VectorContentTest::OperatorAlgebraTest()
|
---|
[b11d3b] | 155 | {
|
---|
[bbf1bd] | 156 | VectorContent zero(3);
|
---|
| 157 | VectorContent unit(3);
|
---|
| 158 | zero.setZero();
|
---|
| 159 | unit.setZero();
|
---|
| 160 | unit[1] = 1.;
|
---|
[b11d3b] | 161 | // summation and scaling
|
---|
| 162 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
| 163 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
| 164 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
|
---|
| 165 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
|
---|
| 166 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
|
---|
| 167 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL( false, (0.98*unit).IsOne() );
|
---|
| 169 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
|
---|
| 170 | CPPUNIT_ASSERT_EQUAL( true, (1.*unit).IsOne() );
|
---|
| 171 |
|
---|
| 172 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
|
---|
| 173 | CPPUNIT_ASSERT_EQUAL( zero, (zero+zero) );
|
---|
| 174 | CPPUNIT_ASSERT_EQUAL( unit, (unit+zero) );
|
---|
| 175 |
|
---|
| 176 | unit += zero;
|
---|
| 177 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
| 178 | unit *= 1.;
|
---|
| 179 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
| 180 | };
|
---|
[e420f9] | 181 |
|
---|
| 182 | /** Unit test for reading from and writing vector to stream
|
---|
| 183 | *
|
---|
| 184 | */
|
---|
| 185 | void VectorContentTest::ReadWriteTest()
|
---|
| 186 | {
|
---|
| 187 | // set up matrix
|
---|
| 188 | VectorContent v((size_t)3);
|
---|
| 189 | for (size_t i=0; i<3;++i)
|
---|
| 190 | v.at(i) = i+1.;
|
---|
| 191 |
|
---|
| 192 | // write to stream
|
---|
| 193 | std::stringstream vectorstream;
|
---|
| 194 | v.write(vectorstream);
|
---|
| 195 |
|
---|
| 196 | // parse in dimensions and check
|
---|
| 197 | size_t vectordimension = VectorContent::preparseVectorDimensions(vectorstream);
|
---|
| 198 | CPPUNIT_ASSERT_EQUAL( (size_t)3, vectordimension );
|
---|
| 199 | // parse in vector and check
|
---|
| 200 | VectorContent w(3, vectorstream);
|
---|
| 201 | CPPUNIT_ASSERT_EQUAL( v, w );
|
---|
| 202 | }
|
---|
[29cbe9] | 203 |
|
---|
| 204 | /** UnitTest for serialization.
|
---|
| 205 | */
|
---|
| 206 | void VectorContentTest::SerializationTest()
|
---|
| 207 | {
|
---|
| 208 | v->setZero();
|
---|
| 209 | (*v)[0] = 0.;
|
---|
| 210 | (*v)[1] = 1.;
|
---|
| 211 | (*v)[2] = 2.;
|
---|
| 212 | // write element to stream
|
---|
| 213 | std::stringstream stream;
|
---|
| 214 | boost::archive::text_oarchive oa(stream);
|
---|
| 215 | oa << v;
|
---|
| 216 |
|
---|
| 217 | //std::cout << "Contents of archive is " << stream.str() << std::endl;
|
---|
| 218 |
|
---|
| 219 | // create and open an archive for input
|
---|
| 220 | boost::archive::text_iarchive ia(stream);
|
---|
| 221 | // read class state from archive
|
---|
| 222 | VectorContent *newv;
|
---|
| 223 |
|
---|
| 224 | ia >> newv;
|
---|
| 225 |
|
---|
| 226 | CPPUNIT_ASSERT (*v == *newv);
|
---|
| 227 | }
|
---|