| [bcf653] | 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 |  | 
|---|
| [fc3b67] | 8 | /* | 
|---|
|  | 9 | * gslmatrixunittest.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Jan 8, 2010 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [fc3b67] | 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 "gslmatrixsymmetricunittest.hpp" | 
|---|
|  | 27 |  | 
|---|
| [9b6b2f] | 28 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 29 | #include "UnitTestMain.hpp" | 
|---|
|  | 30 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 31 |  | 
|---|
| [fc3b67] | 32 | /********************************************** Test classes **************************************/ | 
|---|
|  | 33 |  | 
|---|
|  | 34 | // Registers the fixture into the 'registry' | 
|---|
|  | 35 | CPPUNIT_TEST_SUITE_REGISTRATION( GSLMatrixSymmetricTest ); | 
|---|
|  | 36 |  | 
|---|
|  | 37 |  | 
|---|
|  | 38 | void GSLMatrixSymmetricTest::setUp() | 
|---|
|  | 39 | { | 
|---|
|  | 40 | m = new GSLMatrix(3,3); | 
|---|
|  | 41 | }; | 
|---|
|  | 42 |  | 
|---|
|  | 43 | void GSLMatrixSymmetricTest::tearDown() | 
|---|
|  | 44 | { | 
|---|
|  | 45 | delete(m); | 
|---|
|  | 46 | }; | 
|---|
|  | 47 |  | 
|---|
|  | 48 | /** Unit Test for accessing matrix elements. | 
|---|
|  | 49 | * | 
|---|
|  | 50 | */ | 
|---|
|  | 51 | void GSLMatrixSymmetricTest::AccessTest() | 
|---|
|  | 52 | { | 
|---|
|  | 53 | // check whether all elements are initially zero | 
|---|
|  | 54 | for (int i=0;i<3;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<3;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<3;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<3;i++) | 
|---|
|  | 74 | for (int j=0;j<3;j++) | 
|---|
|  | 75 | m->Set(i,j, i*3+j ); | 
|---|
|  | 76 | GSLMatrix *dest = new GSLMatrix(3,3); | 
|---|
|  | 77 | *dest = *m; | 
|---|
|  | 78 | for (int i=0;i<3;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(4,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 GSLMatrixSymmetricTest::InitializationTest() | 
|---|
|  | 96 | { | 
|---|
|  | 97 | // set zero | 
|---|
|  | 98 | m->SetZero(); | 
|---|
|  | 99 | for (int i=0;i<3;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<3;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<3;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 | m->SetFromDoubleArray(array); | 
|---|
|  | 120 | for (int i=0;i<3;i++) | 
|---|
|  | 121 | for (int j=0;j<3;j++) | 
|---|
|  | 122 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) ); | 
|---|
|  | 123 |  | 
|---|
|  | 124 | }; | 
|---|
|  | 125 |  | 
|---|
|  | 126 | /** Unit Test for copying matrices. | 
|---|
|  | 127 | * | 
|---|
|  | 128 | */ | 
|---|
|  | 129 | void GSLMatrixSymmetricTest::CopyTest() | 
|---|
|  | 130 | { | 
|---|
|  | 131 | // set basis | 
|---|
|  | 132 | GSLMatrix *dest = NULL; | 
|---|
|  | 133 | for (int i=0;i<3;i++) { | 
|---|
|  | 134 | m->SetAll(i); | 
|---|
|  | 135 | dest = new GSLMatrix(m); | 
|---|
|  | 136 | for (int j=0;j<3;j++) | 
|---|
|  | 137 | CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) ); | 
|---|
|  | 138 |  | 
|---|
|  | 139 | delete(dest); | 
|---|
|  | 140 | } | 
|---|
|  | 141 | }; | 
|---|
|  | 142 |  | 
|---|
|  | 143 | /** Unit Test for exchanging rows and columns. | 
|---|
|  | 144 | * | 
|---|
|  | 145 | */ | 
|---|
|  | 146 | void GSLMatrixSymmetricTest::ExchangeTest() | 
|---|
|  | 147 | { | 
|---|
|  | 148 | // set to 1,1,1,2, ... | 
|---|
|  | 149 | for (int i=0;i<3;i++) | 
|---|
|  | 150 | for (int j=0;j<3;j++) | 
|---|
|  | 151 | m->Set(i,j, i+1 ); | 
|---|
|  | 152 |  | 
|---|
|  | 153 | // swap such that nothing happens | 
|---|
|  | 154 | m->SwapColumns(1,2); | 
|---|
|  | 155 | for (int i=0;i<3;i++) | 
|---|
|  | 156 | for (int j=0;j<3;j++) | 
|---|
|  | 157 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) ); | 
|---|
|  | 158 |  | 
|---|
|  | 159 | // swap two rows | 
|---|
|  | 160 | m->SwapRows(1,2); | 
|---|
|  | 161 | for (int i=0;i<3;i++) | 
|---|
|  | 162 | for (int j=0;j<3;j++) | 
|---|
|  | 163 | switch (j) { | 
|---|
|  | 164 | case 0: | 
|---|
|  | 165 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(j,i) ); | 
|---|
|  | 166 | break; | 
|---|
|  | 167 | case 1: | 
|---|
|  | 168 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(j,i) ); | 
|---|
|  | 169 | break; | 
|---|
|  | 170 | case 2: | 
|---|
|  | 171 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(j,i) ); | 
|---|
|  | 172 | break; | 
|---|
|  | 173 | default: | 
|---|
|  | 174 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) ); | 
|---|
|  | 175 | } | 
|---|
|  | 176 | // check that op is reversable | 
|---|
|  | 177 | m->SwapRows(1,2); | 
|---|
|  | 178 | for (int i=0;i<3;i++) | 
|---|
|  | 179 | for (int j=0;j<3;j++) | 
|---|
|  | 180 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) ); | 
|---|
|  | 181 |  | 
|---|
|  | 182 | // set to 1,2,3,1, ... | 
|---|
|  | 183 | for (int i=0;i<3;i++) | 
|---|
|  | 184 | for (int j=0;j<3;j++) | 
|---|
|  | 185 | m->Set(i,j, j+1. ); | 
|---|
|  | 186 |  | 
|---|
|  | 187 | // swap such that nothing happens | 
|---|
|  | 188 | m->SwapRows(0,2); | 
|---|
|  | 189 | for (int i=0;i<3;i++) | 
|---|
|  | 190 | for (int j=0;j<3;j++) | 
|---|
|  | 191 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) ); | 
|---|
|  | 192 |  | 
|---|
|  | 193 | // swap two columns | 
|---|
|  | 194 | m->SwapColumns(0,2); | 
|---|
|  | 195 | for (int i=0;i<3;i++) | 
|---|
|  | 196 | for (int j=0;j<3;j++) | 
|---|
|  | 197 | switch (j) { | 
|---|
|  | 198 | case 0: | 
|---|
|  | 199 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) ); | 
|---|
|  | 200 | break; | 
|---|
|  | 201 | case 1: | 
|---|
|  | 202 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) ); | 
|---|
|  | 203 | break; | 
|---|
|  | 204 | case 2: | 
|---|
|  | 205 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) ); | 
|---|
|  | 206 | break; | 
|---|
|  | 207 | default: | 
|---|
|  | 208 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) ); | 
|---|
|  | 209 | } | 
|---|
|  | 210 | // check that op is reversable | 
|---|
|  | 211 | m->SwapColumns(0,2); | 
|---|
|  | 212 | for (int i=0;i<3;i++) | 
|---|
|  | 213 | for (int j=0;j<3;j++) | 
|---|
|  | 214 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) ); | 
|---|
|  | 215 |  | 
|---|
|  | 216 |  | 
|---|
|  | 217 | // set to 1,2,3,4, ... | 
|---|
|  | 218 | for (int i=0;i<3;i++) | 
|---|
|  | 219 | for (int j=0;j<3;j++) | 
|---|
|  | 220 | m->Set(i,j, 3*i+j+1 ); | 
|---|
|  | 221 | // transpose | 
|---|
|  | 222 | m->Transpose(); | 
|---|
|  | 223 | for (int i=0;i<3;i++) | 
|---|
|  | 224 | for (int j=0;j<3;j++) | 
|---|
|  | 225 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) ); | 
|---|
|  | 226 | // second transpose | 
|---|
|  | 227 | m->Transpose(); | 
|---|
|  | 228 | for (int i=0;i<3;i++) | 
|---|
|  | 229 | for (int j=0;j<3;j++) | 
|---|
|  | 230 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) ); | 
|---|
|  | 231 | }; | 
|---|
|  | 232 |  | 
|---|
|  | 233 | /** Unit Test for matrix properties. | 
|---|
|  | 234 | * | 
|---|
|  | 235 | */ | 
|---|
|  | 236 | void GSLMatrixSymmetricTest::PropertiesTest() | 
|---|
|  | 237 | { | 
|---|
|  | 238 | // is zero | 
|---|
|  | 239 | m->SetZero(); | 
|---|
|  | 240 | CPPUNIT_ASSERT_EQUAL( true, m->IsNull() ); | 
|---|
|  | 241 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() ); | 
|---|
|  | 242 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() ); | 
|---|
|  | 243 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() ); | 
|---|
|  | 244 |  | 
|---|
|  | 245 | // is positive | 
|---|
|  | 246 | m->SetAll(0.5); | 
|---|
|  | 247 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() ); | 
|---|
|  | 248 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() ); | 
|---|
|  | 249 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() ); | 
|---|
|  | 250 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() ); | 
|---|
|  | 251 |  | 
|---|
|  | 252 | // is negative | 
|---|
|  | 253 | m->SetAll(-0.1); | 
|---|
|  | 254 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() ); | 
|---|
|  | 255 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() ); | 
|---|
|  | 256 | CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() ); | 
|---|
|  | 257 | CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() ); | 
|---|
|  | 258 |  | 
|---|
|  | 259 | // is positive definite | 
|---|
|  | 260 | double array[] = { 1., 0., 0., | 
|---|
|  | 261 | 0., 1., 1., | 
|---|
|  | 262 | 0., 0., 1. }; | 
|---|
|  | 263 | m->SetFromDoubleArray(array); | 
|---|
|  | 264 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositiveDefinite() ); | 
|---|
| [865272f] | 265 |  | 
|---|
|  | 266 | //determinant | 
|---|
|  | 267 | m->SetIdentity(); | 
|---|
|  | 268 | CPPUNIT_ASSERT_EQUAL( 1., m->Determinant() ); | 
|---|
|  | 269 |  | 
|---|
|  | 270 | m->SetZero(); | 
|---|
|  | 271 | CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() ); | 
|---|
|  | 272 |  | 
|---|
|  | 273 | m->Set( 0, 0, 1.); | 
|---|
|  | 274 | m->Set( 1, 1, 1.); | 
|---|
|  | 275 | m->Set( 2, 1, 1.); | 
|---|
|  | 276 | CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() ); | 
|---|
|  | 277 |  | 
|---|
|  | 278 | double array2[] = { 2., 0., 1., | 
|---|
|  | 279 | -3., 1., 1., | 
|---|
|  | 280 | 1., 5.5, 1. }; | 
|---|
|  | 281 | m->SetFromDoubleArray(array2); | 
|---|
|  | 282 | CPPUNIT_ASSERT_EQUAL( -26.5, m->Determinant() ); | 
|---|
| [fc3b67] | 283 | }; | 
|---|