| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * MatrixContentSymmetricUnitTest.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Jan 8, 2010 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include "MatrixContentSymmetricUnitTest.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | #include "MatrixContent.hpp" | 
|---|
| 44 |  | 
|---|
| 45 | #ifdef HAVE_TESTRUNNER | 
|---|
| 46 | #include "UnitTestMain.hpp" | 
|---|
| 47 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 48 |  | 
|---|
| 49 | /********************************************** Test classes **************************************/ | 
|---|
| 50 |  | 
|---|
| 51 | // Registers the fixture into the 'registry' | 
|---|
| 52 | CPPUNIT_TEST_SUITE_REGISTRATION( MatrixContentSymmetricTest ); | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | void MatrixContentSymmetricTest::setUp() | 
|---|
| 56 | { | 
|---|
| 57 | m = new MatrixContent(3,3); | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | void MatrixContentSymmetricTest::tearDown() | 
|---|
| 61 | { | 
|---|
| 62 | delete(m); | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 | /** Unit Test for accessing matrix elements. | 
|---|
| 66 | * | 
|---|
| 67 | */ | 
|---|
| 68 | void MatrixContentSymmetricTest::AccessTest() | 
|---|
| 69 | { | 
|---|
| 70 | // check whether all elements are initially zero | 
|---|
| 71 | for (int i=0;i<3;i++) | 
|---|
| 72 | for (int j=0;j<3;j++) | 
|---|
| 73 | CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) ); | 
|---|
| 74 |  | 
|---|
| 75 | // set | 
|---|
| 76 | for (int i=0;i<3;i++) | 
|---|
| 77 | for (int j=0;j<3;j++) | 
|---|
| 78 | m->set(i,j, i*3+j ); | 
|---|
| 79 |  | 
|---|
| 80 | // and check | 
|---|
| 81 | double *ptr = NULL; | 
|---|
| 82 | for (int i=0;i<3;i++) | 
|---|
| 83 | for (int j=0;j<3;j++) { | 
|---|
| 84 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->at(i,j) ); | 
|---|
| 85 | ptr = m->Pointer(i,j); | 
|---|
| 86 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr ); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | // assignment | 
|---|
| 90 | for (int i=0;i<3;i++) | 
|---|
| 91 | for (int j=0;j<3;j++) | 
|---|
| 92 | m->set(i,j, i*3+j ); | 
|---|
| 93 | MatrixContent *dest = new MatrixContent(3,3); | 
|---|
| 94 | *dest = *m; | 
|---|
| 95 | for (int i=0;i<3;i++) | 
|---|
| 96 | for (int j=0;j<3;j++) | 
|---|
| 97 | CPPUNIT_ASSERT_EQUAL( dest->at(i,j), m->at(i,j) ); | 
|---|
| 98 | delete(dest); | 
|---|
| 99 |  | 
|---|
| 100 | // out of bounds | 
|---|
| 101 | //CPPUNIT_ASSERT_EQUAL(0., v->at(4,2) ); | 
|---|
| 102 | //CPPUNIT_ASSERT_EQUAL(0., v->at(2,17) ); | 
|---|
| 103 | //CPPUNIT_ASSERT_EQUAL(0., v->at(1024,140040) ); | 
|---|
| 104 | //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,0) ); | 
|---|
| 105 | //CPPUNIT_ASSERT_EQUAL(0., v->at(0,-1) ); | 
|---|
| 106 | //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,-1) ); | 
|---|
| 107 | }; | 
|---|
| 108 |  | 
|---|
| 109 | /** Unit Test for initializating matrices. | 
|---|
| 110 | * | 
|---|
| 111 | */ | 
|---|
| 112 | void MatrixContentSymmetricTest::InitializationTest() | 
|---|
| 113 | { | 
|---|
| 114 | // set zero | 
|---|
| 115 | m->setZero(); | 
|---|
| 116 | for (int i=0;i<3;i++) | 
|---|
| 117 | for (int j=0;j<3;j++) | 
|---|
| 118 | CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) ); | 
|---|
| 119 |  | 
|---|
| 120 | // set all | 
|---|
| 121 | m->setValue(1.5); | 
|---|
| 122 | for (int i=0;i<3;i++) | 
|---|
| 123 | for (int j=0;j<3;j++) | 
|---|
| 124 | CPPUNIT_ASSERT_EQUAL( 1.5, m->at(i,j) ); | 
|---|
| 125 |  | 
|---|
| 126 | // set basis | 
|---|
| 127 | m->setIdentity(); | 
|---|
| 128 | for (int i=0;i<3;i++) | 
|---|
| 129 | for (int j=0;j<3;j++) | 
|---|
| 130 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) ); | 
|---|
| 131 |  | 
|---|
| 132 | // set from array | 
|---|
| 133 | double array[] = { 1., 0., 0., | 
|---|
| 134 | 0., 1., 0., | 
|---|
| 135 | 0., 0., 1. }; | 
|---|
| 136 | m->setFromDoubleArray(array); | 
|---|
| 137 | for (int i=0;i<3;i++) | 
|---|
| 138 | for (int j=0;j<3;j++) | 
|---|
| 139 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) ); | 
|---|
| 140 |  | 
|---|
| 141 | }; | 
|---|
| 142 |  | 
|---|
| 143 | /** Unit Test for copying matrices. | 
|---|
| 144 | * | 
|---|
| 145 | */ | 
|---|
| 146 | void MatrixContentSymmetricTest::CopyTest() | 
|---|
| 147 | { | 
|---|
| 148 | // set basis | 
|---|
| 149 | MatrixContent *dest = NULL; | 
|---|
| 150 | for (int i=0;i<3;i++) { | 
|---|
| 151 | m->setValue(i); | 
|---|
| 152 | dest = new MatrixContent(m); | 
|---|
| 153 | for (int j=0;j<3;j++) | 
|---|
| 154 | CPPUNIT_ASSERT_EQUAL( m->at(i,j) , dest->at(i,j) ); | 
|---|
| 155 |  | 
|---|
| 156 | delete(dest); | 
|---|
| 157 | } | 
|---|
| 158 | }; | 
|---|
| 159 |  | 
|---|
| 160 | /** Unit Test for exchanging rows and columns. | 
|---|
| 161 | * | 
|---|
| 162 | */ | 
|---|
| 163 | void MatrixContentSymmetricTest::ExchangeTest() | 
|---|
| 164 | { | 
|---|
| 165 | // set to 1,1,1,2, ... | 
|---|
| 166 | for (int i=0;i<3;i++) | 
|---|
| 167 | for (int j=0;j<3;j++) | 
|---|
| 168 | m->set(i,j, i+1 ); | 
|---|
| 169 |  | 
|---|
| 170 | // swap such that nothing happens | 
|---|
| 171 | m->SwapColumns(1,2); | 
|---|
| 172 | for (int i=0;i<3;i++) | 
|---|
| 173 | for (int j=0;j<3;j++) | 
|---|
| 174 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) ); | 
|---|
| 175 |  | 
|---|
| 176 | // swap two rows | 
|---|
| 177 | m->SwapRows(1,2); | 
|---|
| 178 | for (int i=0;i<3;i++) | 
|---|
| 179 | for (int j=0;j<3;j++) | 
|---|
| 180 | switch (j) { | 
|---|
| 181 | case 0: | 
|---|
| 182 | CPPUNIT_ASSERT_EQUAL( 1., m->at(j,i) ); | 
|---|
| 183 | break; | 
|---|
| 184 | case 1: | 
|---|
| 185 | CPPUNIT_ASSERT_EQUAL( 3., m->at(j,i) ); | 
|---|
| 186 | break; | 
|---|
| 187 | case 2: | 
|---|
| 188 | CPPUNIT_ASSERT_EQUAL( 2., m->at(j,i) ); | 
|---|
| 189 | break; | 
|---|
| 190 | default: | 
|---|
| 191 | CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) ); | 
|---|
| 192 | } | 
|---|
| 193 | // check that op is reversable | 
|---|
| 194 | m->SwapRows(1,2); | 
|---|
| 195 | for (int i=0;i<3;i++) | 
|---|
| 196 | for (int j=0;j<3;j++) | 
|---|
| 197 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) ); | 
|---|
| 198 |  | 
|---|
| 199 | // set to 1,2,3,1, ... | 
|---|
| 200 | for (int i=0;i<3;i++) | 
|---|
| 201 | for (int j=0;j<3;j++) | 
|---|
| 202 | m->set(i,j, j+1. ); | 
|---|
| 203 |  | 
|---|
| 204 | // swap such that nothing happens | 
|---|
| 205 | m->SwapRows(0,2); | 
|---|
| 206 | for (int i=0;i<3;i++) | 
|---|
| 207 | for (int j=0;j<3;j++) | 
|---|
| 208 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) ); | 
|---|
| 209 |  | 
|---|
| 210 | // swap two columns | 
|---|
| 211 | m->SwapColumns(0,2); | 
|---|
| 212 | for (int i=0;i<3;i++) | 
|---|
| 213 | for (int j=0;j<3;j++) | 
|---|
| 214 | switch (j) { | 
|---|
| 215 | case 0: | 
|---|
| 216 | CPPUNIT_ASSERT_EQUAL( 3., m->at(i,j) ); | 
|---|
| 217 | break; | 
|---|
| 218 | case 1: | 
|---|
| 219 | CPPUNIT_ASSERT_EQUAL( 2., m->at(i,j) ); | 
|---|
| 220 | break; | 
|---|
| 221 | case 2: | 
|---|
| 222 | CPPUNIT_ASSERT_EQUAL( 1., m->at(i,j) ); | 
|---|
| 223 | break; | 
|---|
| 224 | default: | 
|---|
| 225 | CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) ); | 
|---|
| 226 | } | 
|---|
| 227 | // check that op is reversable | 
|---|
| 228 | m->SwapColumns(0,2); | 
|---|
| 229 | for (int i=0;i<3;i++) | 
|---|
| 230 | for (int j=0;j<3;j++) | 
|---|
| 231 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) ); | 
|---|
| 232 |  | 
|---|
| 233 |  | 
|---|
| 234 | // set to 1,2,3, ... | 
|---|
| 235 | MatrixContent *n =  new MatrixContent(3,3); | 
|---|
| 236 | for (int i=0;i<3;i++) | 
|---|
| 237 | for (int j=0;j<3;j++) { | 
|---|
| 238 | m->set(i,j, 3*i+j+1 ); | 
|---|
| 239 | n->set(i,j, 3*j+i+1 ); | 
|---|
| 240 | } | 
|---|
| 241 | // transpose | 
|---|
| 242 | MatrixContent res = (*m).transposed(); | 
|---|
| 243 | CPPUNIT_ASSERT( *n == res ); | 
|---|
| 244 | // second transpose | 
|---|
| 245 | res.transpose(); | 
|---|
| 246 | CPPUNIT_ASSERT( *m == res ); | 
|---|
| 247 | delete n; | 
|---|
| 248 | }; | 
|---|
| 249 |  | 
|---|
| 250 | /** Unit Test for matrix properties. | 
|---|
| 251 | * | 
|---|
| 252 | */ | 
|---|
| 253 | void MatrixContentSymmetricTest::PropertiesTest() | 
|---|
| 254 | { | 
|---|
| 255 | // is zero | 
|---|
| 256 | m->setZero(); | 
|---|
| 257 | CPPUNIT_ASSERT_EQUAL( true, m->IsNull() ); | 
|---|
| 258 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() ); | 
|---|
| 259 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() ); | 
|---|
| 260 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() ); | 
|---|
| 261 |  | 
|---|
| 262 | // is positive | 
|---|
| 263 | m->setValue(0.5); | 
|---|
| 264 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() ); | 
|---|
| 265 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() ); | 
|---|
| 266 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() ); | 
|---|
| 267 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() ); | 
|---|
| 268 |  | 
|---|
| 269 | // is negative | 
|---|
| 270 | m->setValue(-0.1); | 
|---|
| 271 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() ); | 
|---|
| 272 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() ); | 
|---|
| 273 | CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() ); | 
|---|
| 274 | CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() ); | 
|---|
| 275 |  | 
|---|
| 276 | // is positive definite | 
|---|
| 277 | double array[] = { 1., 0., 0., | 
|---|
| 278 | 0., 1., 1., | 
|---|
| 279 | 0., 0., 1. }; | 
|---|
| 280 | m->setFromDoubleArray(array); | 
|---|
| 281 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositiveDefinite() ); | 
|---|
| 282 |  | 
|---|
| 283 | //determinant | 
|---|
| 284 | m->setIdentity(); | 
|---|
| 285 | CPPUNIT_ASSERT_EQUAL( 1., m->Determinant() ); | 
|---|
| 286 |  | 
|---|
| 287 | m->setZero(); | 
|---|
| 288 | CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() ); | 
|---|
| 289 |  | 
|---|
| 290 | m->set( 0, 0, 1.); | 
|---|
| 291 | m->set( 1, 1, 1.); | 
|---|
| 292 | m->set( 2, 1, 1.); | 
|---|
| 293 | CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() ); | 
|---|
| 294 |  | 
|---|
| 295 | double array2[] = { 2., 0., 1., | 
|---|
| 296 | -3., 1., 1., | 
|---|
| 297 | 1., 5.5, 1. }; | 
|---|
| 298 | m->setFromDoubleArray(array2); | 
|---|
| 299 | CPPUNIT_ASSERT_EQUAL( -26.5, m->Determinant() ); | 
|---|
| 300 | }; | 
|---|