[fc3b67] | 1 | /*
|
---|
| 2 | * gslmatrixunittest.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jan 8, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[fc3b67] | 13 | using namespace std;
|
---|
| 14 |
|
---|
| 15 | #include <cppunit/CompilerOutputter.h>
|
---|
| 16 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 17 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 18 |
|
---|
| 19 | #include "gslmatrixunittest.hpp"
|
---|
| 20 |
|
---|
[9b6b2f] | 21 | #ifdef HAVE_TESTRUNNER
|
---|
| 22 | #include "UnitTestMain.hpp"
|
---|
| 23 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 24 |
|
---|
[fc3b67] | 25 | /********************************************** Test classes **************************************/
|
---|
| 26 |
|
---|
| 27 | // Registers the fixture into the 'registry'
|
---|
| 28 | CPPUNIT_TEST_SUITE_REGISTRATION( GSLMatrixTest );
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | void GSLMatrixTest::setUp()
|
---|
| 32 | {
|
---|
| 33 | m = new GSLMatrix(4,3);
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | void GSLMatrixTest::tearDown()
|
---|
| 37 | {
|
---|
| 38 | delete(m);
|
---|
| 39 | };
|
---|
| 40 |
|
---|
| 41 | /** Unit Test for accessing matrix elements.
|
---|
| 42 | *
|
---|
| 43 | */
|
---|
| 44 | void GSLMatrixTest::AccessTest()
|
---|
| 45 | {
|
---|
| 46 | // check whether all elements are initially zero
|
---|
| 47 | for (int i=0;i<4;i++)
|
---|
| 48 | for (int j=0;j<3;j++)
|
---|
| 49 | CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
|
---|
| 50 |
|
---|
| 51 | // set
|
---|
| 52 | for (int i=0;i<4;i++)
|
---|
| 53 | for (int j=0;j<3;j++)
|
---|
| 54 | m->Set(i,j, i*3+j );
|
---|
| 55 |
|
---|
| 56 | // and check
|
---|
| 57 | double *ptr = NULL;
|
---|
| 58 | for (int i=0;i<4;i++)
|
---|
| 59 | for (int j=0;j<3;j++) {
|
---|
| 60 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->Get(i,j) );
|
---|
| 61 | ptr = m->Pointer(i,j);
|
---|
| 62 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | // assignment
|
---|
| 66 | for (int i=0;i<4;i++)
|
---|
| 67 | for (int j=0;j<3;j++)
|
---|
| 68 | m->Set(i,j, i*3+j );
|
---|
| 69 | GSLMatrix *dest = new GSLMatrix(4,3);
|
---|
| 70 | *dest = *m;
|
---|
| 71 | for (int i=0;i<4;i++)
|
---|
| 72 | for (int j=0;j<3;j++)
|
---|
| 73 | CPPUNIT_ASSERT_EQUAL( dest->Get(i,j), m->Get(i,j) );
|
---|
| 74 | delete(dest);
|
---|
| 75 |
|
---|
| 76 | // out of bounds
|
---|
| 77 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(5,2) );
|
---|
| 78 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(2,17) );
|
---|
| 79 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(1024,140040) );
|
---|
| 80 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,0) );
|
---|
| 81 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(0,-1) );
|
---|
| 82 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,-1) );
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | /** Unit Test for initializating matrices.
|
---|
| 86 | *
|
---|
| 87 | */
|
---|
| 88 | void GSLMatrixTest::InitializationTest()
|
---|
| 89 | {
|
---|
| 90 | // set zero
|
---|
| 91 | m->SetZero();
|
---|
| 92 | for (int i=0;i<4;i++)
|
---|
| 93 | for (int j=0;j<3;j++)
|
---|
| 94 | CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
|
---|
| 95 |
|
---|
| 96 | // set all
|
---|
| 97 | m->SetAll(1.5);
|
---|
| 98 | for (int i=0;i<4;i++)
|
---|
| 99 | for (int j=0;j<3;j++)
|
---|
| 100 | CPPUNIT_ASSERT_EQUAL( 1.5, m->Get(i,j) );
|
---|
| 101 |
|
---|
| 102 | // set basis
|
---|
| 103 | m->SetIdentity();
|
---|
| 104 | for (int i=0;i<4;i++)
|
---|
| 105 | for (int j=0;j<3;j++)
|
---|
| 106 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
|
---|
| 107 |
|
---|
| 108 | // set from array
|
---|
| 109 | double array[] = { 1., 0., 0.,
|
---|
| 110 | 0., 1., 0.,
|
---|
| 111 | 0., 0., 1.,
|
---|
| 112 | 0., 0., 0. };
|
---|
| 113 | m->SetFromDoubleArray(array);
|
---|
| 114 | for (int i=0;i<4;i++)
|
---|
| 115 | for (int j=0;j<3;j++)
|
---|
| 116 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
|
---|
| 117 |
|
---|
| 118 | };
|
---|
| 119 |
|
---|
| 120 | /** Unit Test for copying matrices.
|
---|
| 121 | *
|
---|
| 122 | */
|
---|
| 123 | void GSLMatrixTest::CopyTest()
|
---|
| 124 | {
|
---|
| 125 | // set basis
|
---|
| 126 | GSLMatrix *dest = NULL;
|
---|
| 127 | for (int i=0;i<4;i++) {
|
---|
| 128 | m->SetAll(i);
|
---|
| 129 | dest = new GSLMatrix(m);
|
---|
| 130 | for (int j=0;j<3;j++)
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) );
|
---|
| 132 |
|
---|
| 133 | delete(dest);
|
---|
| 134 | }
|
---|
| 135 | };
|
---|
| 136 |
|
---|
| 137 | /** Unit Test for exchanging rows and columns.
|
---|
| 138 | *
|
---|
| 139 | */
|
---|
| 140 | void GSLMatrixTest::ExchangeTest()
|
---|
| 141 | {
|
---|
| 142 | // set to 1,1,1,2, ...
|
---|
| 143 | for (int i=0;i<4;i++)
|
---|
| 144 | for (int j=0;j<3;j++)
|
---|
| 145 | m->Set(i,j, i+1 );
|
---|
| 146 |
|
---|
| 147 | // swap such that nothing happens
|
---|
| 148 | CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(1,2) );
|
---|
| 149 | for (int i=0;i<4;i++)
|
---|
| 150 | for (int j=0;j<3;j++)
|
---|
| 151 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
|
---|
| 152 |
|
---|
| 153 | // swap two rows
|
---|
| 154 | CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
|
---|
| 155 | for (int i=0;i<4;i++)
|
---|
| 156 | for (int j=0;j<3;j++)
|
---|
| 157 | switch (i) {
|
---|
| 158 | case 0:
|
---|
| 159 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
|
---|
| 160 | break;
|
---|
| 161 | case 1:
|
---|
| 162 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
|
---|
| 163 | break;
|
---|
| 164 | case 2:
|
---|
| 165 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
|
---|
| 166 | break;
|
---|
| 167 | case 3:
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL( 4., m->Get(i,j) );
|
---|
| 169 | break;
|
---|
| 170 | default:
|
---|
| 171 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
|
---|
| 172 | }
|
---|
| 173 | // check that op is reversable
|
---|
| 174 | CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
|
---|
| 175 | for (int i=0;i<4;i++)
|
---|
| 176 | for (int j=0;j<3;j++)
|
---|
| 177 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
|
---|
| 178 |
|
---|
| 179 | // set to 1,2,3,1, ...
|
---|
| 180 | for (int i=0;i<4;i++)
|
---|
| 181 | for (int j=0;j<3;j++)
|
---|
| 182 | m->Set(i,j, j+1. );
|
---|
| 183 |
|
---|
| 184 | // swap such that nothing happens
|
---|
| 185 | CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(0,2) );
|
---|
| 186 | for (int i=0;i<4;i++)
|
---|
| 187 | for (int j=0;j<3;j++)
|
---|
| 188 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
|
---|
| 189 |
|
---|
| 190 | // swap two columns
|
---|
| 191 | CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
|
---|
| 192 | for (int i=0;i<4;i++)
|
---|
| 193 | for (int j=0;j<3;j++)
|
---|
| 194 | switch (j) {
|
---|
| 195 | case 0:
|
---|
| 196 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
|
---|
| 197 | break;
|
---|
| 198 | case 1:
|
---|
| 199 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
|
---|
| 200 | break;
|
---|
| 201 | case 2:
|
---|
| 202 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
|
---|
| 203 | break;
|
---|
| 204 | default:
|
---|
| 205 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
|
---|
| 206 | }
|
---|
| 207 | // check that op is reversable
|
---|
| 208 | CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
|
---|
| 209 | for (int i=0;i<4;i++)
|
---|
| 210 | for (int j=0;j<3;j++)
|
---|
| 211 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
|
---|
| 212 |
|
---|
| 213 |
|
---|
| 214 | // set to 1,2,3,4, ...
|
---|
| 215 | for (int i=0;i<4;i++)
|
---|
| 216 | for (int j=0;j<3;j++)
|
---|
| 217 | m->Set(i,j, 3*i+j+1 );
|
---|
| 218 | // transpose
|
---|
| 219 | CPPUNIT_ASSERT_EQUAL( true, m->Transpose() );
|
---|
| 220 | for (int i=0;i<4;i++)
|
---|
| 221 | for (int j=0;j<3;j++)
|
---|
| 222 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) );
|
---|
| 223 | // second transpose
|
---|
| 224 | CPPUNIT_ASSERT_EQUAL( true, m->Transpose() );
|
---|
| 225 | for (int i=0;i<4;i++)
|
---|
| 226 | for (int j=0;j<3;j++)
|
---|
| 227 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) );
|
---|
| 228 | };
|
---|
| 229 |
|
---|
| 230 | /** Unit Test for matrix properties.
|
---|
| 231 | *
|
---|
| 232 | */
|
---|
| 233 | void GSLMatrixTest::PropertiesTest()
|
---|
| 234 | {
|
---|
| 235 | // is zero
|
---|
| 236 | m->SetZero();
|
---|
| 237 | CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
|
---|
| 238 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
|
---|
| 239 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
|
---|
| 240 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
|
---|
| 241 |
|
---|
| 242 | // is positive
|
---|
| 243 | m->SetAll(0.5);
|
---|
| 244 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
|
---|
| 245 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
|
---|
| 246 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
|
---|
| 247 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
|
---|
| 248 |
|
---|
| 249 | // is negative
|
---|
| 250 | m->SetAll(-0.1);
|
---|
| 251 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
|
---|
| 252 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
|
---|
| 253 | CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
|
---|
| 254 | CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
|
---|
| 255 |
|
---|
| 256 | // is positive definite
|
---|
| 257 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositiveDefinite() );
|
---|
| 258 | };
|
---|