1 | /*
|
---|
2 | * gslvectorunittest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jan 8, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | #include <cppunit/CompilerOutputter.h>
|
---|
11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
13 |
|
---|
14 | #include "gslvectorunittest.hpp"
|
---|
15 |
|
---|
16 | /********************************************** Test classes **************************************/
|
---|
17 |
|
---|
18 | // Registers the fixture into the 'registry'
|
---|
19 | CPPUNIT_TEST_SUITE_REGISTRATION( GSLVectorTest );
|
---|
20 |
|
---|
21 |
|
---|
22 | void GSLVectorTest::setUp()
|
---|
23 | {
|
---|
24 | v = new GSLVector(3);
|
---|
25 | };
|
---|
26 |
|
---|
27 | void GSLVectorTest::tearDown()
|
---|
28 | {
|
---|
29 | delete(v);
|
---|
30 | };
|
---|
31 |
|
---|
32 | /** Unit test for accessing elements in the vector.
|
---|
33 | *
|
---|
34 | */
|
---|
35 | void GSLVectorTest::AccessTest()
|
---|
36 | {
|
---|
37 | // set
|
---|
38 | for (int i=0;i<3;i++)
|
---|
39 | v->Set(i,i);
|
---|
40 |
|
---|
41 | // and check
|
---|
42 | double *ptr = NULL;
|
---|
43 | for (int i=0;i<3;i++) {
|
---|
44 | CPPUNIT_ASSERT_EQUAL( (double)i, v->Get(i) );
|
---|
45 | ptr = v->Pointer(i);
|
---|
46 | CPPUNIT_ASSERT_EQUAL( (double)i, *ptr );
|
---|
47 | }
|
---|
48 |
|
---|
49 | // out of bounds
|
---|
50 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(3) );
|
---|
51 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1) );
|
---|
52 | };
|
---|
53 |
|
---|
54 |
|
---|
55 | /** Unit test for initializing the vector.
|
---|
56 | *
|
---|
57 | */
|
---|
58 | void GSLVectorTest::InitializaionTest()
|
---|
59 | {
|
---|
60 | // set zero
|
---|
61 | v->SetZero();
|
---|
62 | for (int i=0;i<3;i++)
|
---|
63 | CPPUNIT_ASSERT_EQUAL( 0., v->Get(i) );
|
---|
64 |
|
---|
65 | // set basis
|
---|
66 | for (int i=0;i<3;i++) {
|
---|
67 | v->SetBasis(i);
|
---|
68 | for (int j=0;j<3;j++)
|
---|
69 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->Get(j) );
|
---|
70 | }
|
---|
71 |
|
---|
72 | // set all
|
---|
73 | v->SetAll(1.5);
|
---|
74 | for (int i=0;i<3;i++)
|
---|
75 | CPPUNIT_ASSERT_EQUAL( 1.5, v->Get(i) );
|
---|
76 | };
|
---|
77 |
|
---|
78 | /** Unit test for copying vectors.
|
---|
79 | *
|
---|
80 | */
|
---|
81 | void GSLVectorTest::CopyTest()
|
---|
82 | {
|
---|
83 | // set basis
|
---|
84 | GSLVector *dest = NULL;
|
---|
85 | for (int i=0;i<3;i++) {
|
---|
86 | v->SetBasis(i);
|
---|
87 | dest = new GSLVector(v);
|
---|
88 | for (int j=0;j<3;j++)
|
---|
89 | CPPUNIT_ASSERT_EQUAL( v->Get(j) , dest->Get(j) );
|
---|
90 |
|
---|
91 | delete(dest);
|
---|
92 | }
|
---|
93 | };
|
---|
94 |
|
---|
95 | /** Unit test for exchanging elements of a vector.
|
---|
96 | *
|
---|
97 | */
|
---|
98 | void GSLVectorTest::ExchangeTest()
|
---|
99 | {
|
---|
100 | // set basis
|
---|
101 | for (int i=0;i<3;i++) {
|
---|
102 | v->SetBasis(i);
|
---|
103 | v->SwapElements((i)%3,(i+1)%3);
|
---|
104 | for (int j=0;j<3;j++)
|
---|
105 | CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->Get(j) );
|
---|
106 | }
|
---|
107 |
|
---|
108 | // set to 1,2,3 and reverse
|
---|
109 | for (int i=0;i<3;i++)
|
---|
110 | v->Set(i,i+1);
|
---|
111 | v->Reverse();
|
---|
112 | for (int j=0;j<3;j++)
|
---|
113 | CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->Get(j) );
|
---|
114 | };
|
---|
115 |
|
---|
116 |
|
---|
117 | /** UnitTest for operators.
|
---|
118 | */
|
---|
119 | void GSLVectorTest::OperatorIsTest()
|
---|
120 | {
|
---|
121 | GSLVector zero(3);
|
---|
122 | GSLVector unit(3);
|
---|
123 | zero.SetZero();
|
---|
124 | unit.SetZero();
|
---|
125 | unit.Set(1,1.);
|
---|
126 | // summation and scaling
|
---|
127 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
128 | CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
|
---|
129 | CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
|
---|
130 | CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
|
---|
131 | };
|
---|
132 |
|
---|
133 | /** UnitTest for operators.
|
---|
134 | */
|
---|
135 | void GSLVectorTest::OperatorAlgebraTest()
|
---|
136 | {
|
---|
137 | GSLVector zero(3);
|
---|
138 | GSLVector unit(3);
|
---|
139 | zero.SetZero();
|
---|
140 | unit.SetZero();
|
---|
141 | unit.Set(1,1.);
|
---|
142 | // summation and scaling
|
---|
143 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
144 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
145 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
|
---|
146 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
|
---|
147 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
|
---|
148 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
|
---|
149 | CPPUNIT_ASSERT_EQUAL( false, (0.98*unit).IsOne() );
|
---|
150 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
|
---|
151 | CPPUNIT_ASSERT_EQUAL( true, (1.*unit).IsOne() );
|
---|
152 |
|
---|
153 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
|
---|
154 | CPPUNIT_ASSERT_EQUAL( zero, (zero+zero) );
|
---|
155 | CPPUNIT_ASSERT_EQUAL( unit, (unit+zero) );
|
---|
156 |
|
---|
157 | unit += zero;
|
---|
158 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
159 | unit *= 1.;
|
---|
160 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
161 | };
|
---|
162 |
|
---|
163 | /********************************************** Main routine **************************************/
|
---|
164 |
|
---|
165 | int main(int argc, char **argv)
|
---|
166 | {
|
---|
167 | // Get the top level suite from the registry
|
---|
168 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
169 |
|
---|
170 | // Adds the test to the list of test to run
|
---|
171 | CppUnit::TextUi::TestRunner runner;
|
---|
172 | runner.addTest( suite );
|
---|
173 |
|
---|
174 | // Change the default outputter to a compiler error format outputter
|
---|
175 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
176 | std::cerr ) );
|
---|
177 | // Run the tests.
|
---|
178 | bool wasSucessful = runner.run();
|
---|
179 |
|
---|
180 | // Return error code 1 if the one of test failed.
|
---|
181 | return wasSucessful ? 0 : 1;
|
---|
182 | };
|
---|