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