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