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