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