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