1 | /*
|
---|
2 | * gslmatrixunittest.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 "gslmatrixsymmetricunittest.hpp"
|
---|
15 |
|
---|
16 | /********************************************** Test classes **************************************/
|
---|
17 |
|
---|
18 | // Registers the fixture into the 'registry'
|
---|
19 | CPPUNIT_TEST_SUITE_REGISTRATION( GSLMatrixSymmetricTest );
|
---|
20 |
|
---|
21 |
|
---|
22 | void GSLMatrixSymmetricTest::setUp()
|
---|
23 | {
|
---|
24 | m = new GSLMatrix(3,3);
|
---|
25 | };
|
---|
26 |
|
---|
27 | void GSLMatrixSymmetricTest::tearDown()
|
---|
28 | {
|
---|
29 | delete(m);
|
---|
30 | };
|
---|
31 |
|
---|
32 | /** Unit Test for accessing matrix elements.
|
---|
33 | *
|
---|
34 | */
|
---|
35 | void GSLMatrixSymmetricTest::AccessTest()
|
---|
36 | {
|
---|
37 | // check whether all elements are initially zero
|
---|
38 | for (int i=0;i<3;i++)
|
---|
39 | for (int j=0;j<3;j++)
|
---|
40 | CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
|
---|
41 |
|
---|
42 | // set
|
---|
43 | for (int i=0;i<3;i++)
|
---|
44 | for (int j=0;j<3;j++)
|
---|
45 | m->Set(i,j, i*3+j );
|
---|
46 |
|
---|
47 | // and check
|
---|
48 | double *ptr = NULL;
|
---|
49 | for (int i=0;i<3;i++)
|
---|
50 | for (int j=0;j<3;j++) {
|
---|
51 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->Get(i,j) );
|
---|
52 | ptr = m->Pointer(i,j);
|
---|
53 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
|
---|
54 | }
|
---|
55 |
|
---|
56 | // assignment
|
---|
57 | for (int i=0;i<3;i++)
|
---|
58 | for (int j=0;j<3;j++)
|
---|
59 | m->Set(i,j, i*3+j );
|
---|
60 | GSLMatrix *dest = new GSLMatrix(3,3);
|
---|
61 | *dest = *m;
|
---|
62 | for (int i=0;i<3;i++)
|
---|
63 | for (int j=0;j<3;j++)
|
---|
64 | CPPUNIT_ASSERT_EQUAL( dest->Get(i,j), m->Get(i,j) );
|
---|
65 | delete(dest);
|
---|
66 |
|
---|
67 | // out of bounds
|
---|
68 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(4,2) );
|
---|
69 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(2,17) );
|
---|
70 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(1024,140040) );
|
---|
71 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,0) );
|
---|
72 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(0,-1) );
|
---|
73 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,-1) );
|
---|
74 | };
|
---|
75 |
|
---|
76 | /** Unit Test for initializating matrices.
|
---|
77 | *
|
---|
78 | */
|
---|
79 | void GSLMatrixSymmetricTest::InitializationTest()
|
---|
80 | {
|
---|
81 | // set zero
|
---|
82 | m->SetZero();
|
---|
83 | for (int i=0;i<3;i++)
|
---|
84 | for (int j=0;j<3;j++)
|
---|
85 | CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
|
---|
86 |
|
---|
87 | // set all
|
---|
88 | m->SetAll(1.5);
|
---|
89 | for (int i=0;i<3;i++)
|
---|
90 | for (int j=0;j<3;j++)
|
---|
91 | CPPUNIT_ASSERT_EQUAL( 1.5, m->Get(i,j) );
|
---|
92 |
|
---|
93 | // set basis
|
---|
94 | m->SetIdentity();
|
---|
95 | for (int i=0;i<3;i++)
|
---|
96 | for (int j=0;j<3;j++)
|
---|
97 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
|
---|
98 |
|
---|
99 | // set from array
|
---|
100 | double array[] = { 1., 0., 0.,
|
---|
101 | 0., 1., 0.,
|
---|
102 | 0., 0., 1. };
|
---|
103 | m->SetFromDoubleArray(array);
|
---|
104 | for (int i=0;i<3;i++)
|
---|
105 | for (int j=0;j<3;j++)
|
---|
106 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
|
---|
107 |
|
---|
108 | };
|
---|
109 |
|
---|
110 | /** Unit Test for copying matrices.
|
---|
111 | *
|
---|
112 | */
|
---|
113 | void GSLMatrixSymmetricTest::CopyTest()
|
---|
114 | {
|
---|
115 | // set basis
|
---|
116 | GSLMatrix *dest = NULL;
|
---|
117 | for (int i=0;i<3;i++) {
|
---|
118 | m->SetAll(i);
|
---|
119 | dest = new GSLMatrix(m);
|
---|
120 | for (int j=0;j<3;j++)
|
---|
121 | CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) );
|
---|
122 |
|
---|
123 | delete(dest);
|
---|
124 | }
|
---|
125 | };
|
---|
126 |
|
---|
127 | /** Unit Test for exchanging rows and columns.
|
---|
128 | *
|
---|
129 | */
|
---|
130 | void GSLMatrixSymmetricTest::ExchangeTest()
|
---|
131 | {
|
---|
132 | // set to 1,1,1,2, ...
|
---|
133 | for (int i=0;i<3;i++)
|
---|
134 | for (int j=0;j<3;j++)
|
---|
135 | m->Set(i,j, i+1 );
|
---|
136 |
|
---|
137 | // swap such that nothing happens
|
---|
138 | m->SwapColumns(1,2);
|
---|
139 | for (int i=0;i<3;i++)
|
---|
140 | for (int j=0;j<3;j++)
|
---|
141 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
|
---|
142 |
|
---|
143 | // swap two rows
|
---|
144 | m->SwapRows(1,2);
|
---|
145 | for (int i=0;i<3;i++)
|
---|
146 | for (int j=0;j<3;j++)
|
---|
147 | switch (j) {
|
---|
148 | case 0:
|
---|
149 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(j,i) );
|
---|
150 | break;
|
---|
151 | case 1:
|
---|
152 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(j,i) );
|
---|
153 | break;
|
---|
154 | case 2:
|
---|
155 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(j,i) );
|
---|
156 | break;
|
---|
157 | default:
|
---|
158 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
|
---|
159 | }
|
---|
160 | // check that op is reversable
|
---|
161 | m->SwapRows(1,2);
|
---|
162 | for (int i=0;i<3;i++)
|
---|
163 | for (int j=0;j<3;j++)
|
---|
164 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
|
---|
165 |
|
---|
166 | // set to 1,2,3,1, ...
|
---|
167 | for (int i=0;i<3;i++)
|
---|
168 | for (int j=0;j<3;j++)
|
---|
169 | m->Set(i,j, j+1. );
|
---|
170 |
|
---|
171 | // swap such that nothing happens
|
---|
172 | m->SwapRows(0,2);
|
---|
173 | for (int i=0;i<3;i++)
|
---|
174 | for (int j=0;j<3;j++)
|
---|
175 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
|
---|
176 |
|
---|
177 | // swap two columns
|
---|
178 | m->SwapColumns(0,2);
|
---|
179 | for (int i=0;i<3;i++)
|
---|
180 | for (int j=0;j<3;j++)
|
---|
181 | switch (j) {
|
---|
182 | case 0:
|
---|
183 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
|
---|
184 | break;
|
---|
185 | case 1:
|
---|
186 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
|
---|
187 | break;
|
---|
188 | case 2:
|
---|
189 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
|
---|
190 | break;
|
---|
191 | default:
|
---|
192 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
|
---|
193 | }
|
---|
194 | // check that op is reversable
|
---|
195 | m->SwapColumns(0,2);
|
---|
196 | for (int i=0;i<3;i++)
|
---|
197 | for (int j=0;j<3;j++)
|
---|
198 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
|
---|
199 |
|
---|
200 |
|
---|
201 | // set to 1,2,3,4, ...
|
---|
202 | for (int i=0;i<3;i++)
|
---|
203 | for (int j=0;j<3;j++)
|
---|
204 | m->Set(i,j, 3*i+j+1 );
|
---|
205 | // transpose
|
---|
206 | m->Transpose();
|
---|
207 | for (int i=0;i<3;i++)
|
---|
208 | for (int j=0;j<3;j++)
|
---|
209 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) );
|
---|
210 | // second transpose
|
---|
211 | m->Transpose();
|
---|
212 | for (int i=0;i<3;i++)
|
---|
213 | for (int j=0;j<3;j++)
|
---|
214 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) );
|
---|
215 | };
|
---|
216 |
|
---|
217 | /** Unit Test for matrix properties.
|
---|
218 | *
|
---|
219 | */
|
---|
220 | void GSLMatrixSymmetricTest::PropertiesTest()
|
---|
221 | {
|
---|
222 | // is zero
|
---|
223 | m->SetZero();
|
---|
224 | CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
|
---|
225 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
|
---|
226 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
|
---|
227 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
|
---|
228 |
|
---|
229 | // is positive
|
---|
230 | m->SetAll(0.5);
|
---|
231 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
|
---|
232 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
|
---|
233 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
|
---|
234 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
|
---|
235 |
|
---|
236 | // is negative
|
---|
237 | m->SetAll(-0.1);
|
---|
238 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
|
---|
239 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
|
---|
240 | CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
|
---|
241 | CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
|
---|
242 |
|
---|
243 | // is positive definite
|
---|
244 | double array[] = { 1., 0., 0.,
|
---|
245 | 0., 1., 1.,
|
---|
246 | 0., 0., 1. };
|
---|
247 | m->SetFromDoubleArray(array);
|
---|
248 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositiveDefinite() );
|
---|
249 |
|
---|
250 | //determinant
|
---|
251 | m->SetIdentity();
|
---|
252 | CPPUNIT_ASSERT_EQUAL( 1., m->Determinant() );
|
---|
253 |
|
---|
254 | m->SetZero();
|
---|
255 | CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
|
---|
256 |
|
---|
257 | m->Set( 0, 0, 1.);
|
---|
258 | m->Set( 1, 1, 1.);
|
---|
259 | m->Set( 2, 1, 1.);
|
---|
260 | CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
|
---|
261 |
|
---|
262 | double array2[] = { 2., 0., 1.,
|
---|
263 | -3., 1., 1.,
|
---|
264 | 1., 5.5, 1. };
|
---|
265 | m->SetFromDoubleArray(array2);
|
---|
266 | CPPUNIT_ASSERT_EQUAL( -26.5, m->Determinant() );
|
---|
267 | };
|
---|
268 |
|
---|
269 | /********************************************** Main routine **************************************/
|
---|
270 |
|
---|
271 | int main(int argc, char **argv)
|
---|
272 | {
|
---|
273 | // Get the top level suite from the registry
|
---|
274 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
275 |
|
---|
276 | // Adds the test to the list of test to run
|
---|
277 | CppUnit::TextUi::TestRunner runner;
|
---|
278 | runner.addTest( suite );
|
---|
279 |
|
---|
280 | // Change the default outputter to a compiler error format outputter
|
---|
281 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
282 | std::cerr ) );
|
---|
283 | // Run the tests.
|
---|
284 | bool wasSucessful = runner.run();
|
---|
285 |
|
---|
286 | // Return error code 1 if the one of test failed.
|
---|
287 | return wasSucessful ? 0 : 1;
|
---|
288 | };
|
---|