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 | * VectorContentUnittest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Jan 8, 2010
|
---|
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 headers that implement a archive in simple text format
|
---|
27 | #include <boost/archive/text_oarchive.hpp>
|
---|
28 | #include <boost/archive/text_iarchive.hpp>
|
---|
29 |
|
---|
30 | #include "VectorContentUnitTest.hpp"
|
---|
31 |
|
---|
32 | #ifdef HAVE_TESTRUNNER
|
---|
33 | #include "UnitTestMain.hpp"
|
---|
34 | #endif /*HAVE_TESTRUNNER*/
|
---|
35 |
|
---|
36 | /********************************************** Test classes **************************************/
|
---|
37 |
|
---|
38 | // Registers the fixture into the 'registry'
|
---|
39 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorContentTest );
|
---|
40 |
|
---|
41 |
|
---|
42 | void VectorContentTest::setUp()
|
---|
43 | {
|
---|
44 | v = new VectorContent(3);
|
---|
45 | };
|
---|
46 |
|
---|
47 | void VectorContentTest::tearDown()
|
---|
48 | {
|
---|
49 | delete(v);
|
---|
50 | };
|
---|
51 |
|
---|
52 | /** Unit test for accessing elements in the vector.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | void VectorContentTest::AccessTest()
|
---|
56 | {
|
---|
57 | // set
|
---|
58 | for (int i=0;i<3;i++)
|
---|
59 | v->at(i) = (double)i;
|
---|
60 |
|
---|
61 | // and check
|
---|
62 | double *ptr = NULL;
|
---|
63 | for (int i=0;i<3;i++) {
|
---|
64 | CPPUNIT_ASSERT_EQUAL( (double)i, v->at(i) );
|
---|
65 | ptr = v->Pointer(i);
|
---|
66 | CPPUNIT_ASSERT_EQUAL( (double)i, *ptr );
|
---|
67 | }
|
---|
68 |
|
---|
69 | // out of bounds
|
---|
70 | //CPPUNIT_ASSERT_EQUAL(0., v->at(3) );
|
---|
71 | //CPPUNIT_ASSERT_EQUAL(0., v->at(-1) );
|
---|
72 | };
|
---|
73 |
|
---|
74 |
|
---|
75 | /** Unit test for initializing the vector.
|
---|
76 | *
|
---|
77 | */
|
---|
78 | void VectorContentTest::InitializaionTest()
|
---|
79 | {
|
---|
80 | // set zero
|
---|
81 | v->setZero();
|
---|
82 | for (int i=0;i<3;i++)
|
---|
83 | CPPUNIT_ASSERT_EQUAL( 0., v->at(i) );
|
---|
84 |
|
---|
85 | // set basis
|
---|
86 | for (int i=0;i<3;i++) {
|
---|
87 | v->setBasis(i);
|
---|
88 | for (int j=0;j<3;j++)
|
---|
89 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->at(j) );
|
---|
90 | }
|
---|
91 |
|
---|
92 | // set all
|
---|
93 | v->setValue(1.5);
|
---|
94 | for (int i=0;i<3;i++)
|
---|
95 | CPPUNIT_ASSERT_EQUAL( 1.5, v->at(i) );
|
---|
96 | };
|
---|
97 |
|
---|
98 | /** Unit test for copying vectors.
|
---|
99 | *
|
---|
100 | */
|
---|
101 | void VectorContentTest::CopyTest()
|
---|
102 | {
|
---|
103 | // set basis
|
---|
104 | VectorContent *dest = NULL;
|
---|
105 | for (int i=0;i<3;i++) {
|
---|
106 | v->setBasis(i);
|
---|
107 | dest = new VectorContent(v);
|
---|
108 | for (int j=0;j<3;j++)
|
---|
109 | CPPUNIT_ASSERT_EQUAL( v->at(j) , dest->at(j) );
|
---|
110 |
|
---|
111 | delete(dest);
|
---|
112 | }
|
---|
113 | };
|
---|
114 |
|
---|
115 | /** Unit test for exchanging elements of a vector.
|
---|
116 | *
|
---|
117 | */
|
---|
118 | void VectorContentTest::ExchangeTest()
|
---|
119 | {
|
---|
120 | // set basis
|
---|
121 | for (int i=0;i<3;i++) {
|
---|
122 | v->setBasis(i);
|
---|
123 | v->SwapElements((i)%3,(i+1)%3);
|
---|
124 | for (int j=0;j<3;j++)
|
---|
125 | CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->at(j) );
|
---|
126 | }
|
---|
127 |
|
---|
128 | // set to 1,2,3 and reverse
|
---|
129 | for (int i=0;i<3;i++)
|
---|
130 | v->at(i) = (double)(i+1);
|
---|
131 | v->Reverse();
|
---|
132 | for (int j=0;j<3;j++)
|
---|
133 | CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->at(j) );
|
---|
134 | };
|
---|
135 |
|
---|
136 | /** UnitTest for operators.
|
---|
137 | */
|
---|
138 | void VectorContentTest::OperatorIsTest()
|
---|
139 | {
|
---|
140 | VectorContent zero(3);
|
---|
141 | VectorContent unit(3);
|
---|
142 | zero.setZero();
|
---|
143 | unit.setZero();
|
---|
144 | unit[1] = 1.;
|
---|
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 | */
|
---|
154 | void VectorContentTest::OperatorAlgebraTest()
|
---|
155 | {
|
---|
156 | VectorContent zero(3);
|
---|
157 | VectorContent unit(3);
|
---|
158 | zero.setZero();
|
---|
159 | unit.setZero();
|
---|
160 | unit[1] = 1.;
|
---|
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 | };
|
---|
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 | }
|
---|
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 | }
|
---|