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