[fc3b67] | 1 | /*
|
---|
| 2 | * gslmatrixunittest.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jan 8, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | using namespace std;
|
---|
| 9 |
|
---|
| 10 | #include <cppunit/CompilerOutputter.h>
|
---|
| 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 13 |
|
---|
| 14 | #include "gslmatrixunittest.hpp"
|
---|
| 15 |
|
---|
| 16 | /********************************************** Test classes **************************************/
|
---|
| 17 |
|
---|
| 18 | // Registers the fixture into the 'registry'
|
---|
| 19 | CPPUNIT_TEST_SUITE_REGISTRATION( GSLMatrixTest );
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | void GSLMatrixTest::setUp()
|
---|
| 23 | {
|
---|
| 24 | m = new GSLMatrix(4,3);
|
---|
| 25 | };
|
---|
| 26 |
|
---|
| 27 | void GSLMatrixTest::tearDown()
|
---|
| 28 | {
|
---|
| 29 | delete(m);
|
---|
| 30 | };
|
---|
| 31 |
|
---|
| 32 | /** Unit Test for accessing matrix elements.
|
---|
| 33 | *
|
---|
| 34 | */
|
---|
| 35 | void GSLMatrixTest::AccessTest()
|
---|
| 36 | {
|
---|
| 37 | // check whether all elements are initially zero
|
---|
| 38 | for (int i=0;i<4;i++)
|
---|
| 39 | for (int j=0;j<3;j++)
|
---|
| 40 | CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
|
---|
| 41 |
|
---|
| 42 | // set
|
---|
| 43 | for (int i=0;i<4;i++)
|
---|
| 44 | for (int j=0;j<3;j++)
|
---|
| 45 | m->Set(i,j, i*3+j );
|
---|
| 46 |
|
---|
| 47 | // and check
|
---|
| 48 | double *ptr = NULL;
|
---|
| 49 | for (int i=0;i<4;i++)
|
---|
| 50 | for (int j=0;j<3;j++) {
|
---|
| 51 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->Get(i,j) );
|
---|
| 52 | ptr = m->Pointer(i,j);
|
---|
| 53 | CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // assignment
|
---|
| 57 | for (int i=0;i<4;i++)
|
---|
| 58 | for (int j=0;j<3;j++)
|
---|
| 59 | m->Set(i,j, i*3+j );
|
---|
| 60 | GSLMatrix *dest = new GSLMatrix(4,3);
|
---|
| 61 | *dest = *m;
|
---|
| 62 | for (int i=0;i<4;i++)
|
---|
| 63 | for (int j=0;j<3;j++)
|
---|
| 64 | CPPUNIT_ASSERT_EQUAL( dest->Get(i,j), m->Get(i,j) );
|
---|
| 65 | delete(dest);
|
---|
| 66 |
|
---|
| 67 | // out of bounds
|
---|
| 68 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(5,2) );
|
---|
| 69 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(2,17) );
|
---|
| 70 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(1024,140040) );
|
---|
| 71 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,0) );
|
---|
| 72 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(0,-1) );
|
---|
| 73 | //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,-1) );
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | /** Unit Test for initializating matrices.
|
---|
| 77 | *
|
---|
| 78 | */
|
---|
| 79 | void GSLMatrixTest::InitializationTest()
|
---|
| 80 | {
|
---|
| 81 | // set zero
|
---|
| 82 | m->SetZero();
|
---|
| 83 | for (int i=0;i<4;i++)
|
---|
| 84 | for (int j=0;j<3;j++)
|
---|
| 85 | CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
|
---|
| 86 |
|
---|
| 87 | // set all
|
---|
| 88 | m->SetAll(1.5);
|
---|
| 89 | for (int i=0;i<4;i++)
|
---|
| 90 | for (int j=0;j<3;j++)
|
---|
| 91 | CPPUNIT_ASSERT_EQUAL( 1.5, m->Get(i,j) );
|
---|
| 92 |
|
---|
| 93 | // set basis
|
---|
| 94 | m->SetIdentity();
|
---|
| 95 | for (int i=0;i<4;i++)
|
---|
| 96 | for (int j=0;j<3;j++)
|
---|
| 97 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
|
---|
| 98 |
|
---|
| 99 | // set from array
|
---|
| 100 | double array[] = { 1., 0., 0.,
|
---|
| 101 | 0., 1., 0.,
|
---|
| 102 | 0., 0., 1.,
|
---|
| 103 | 0., 0., 0. };
|
---|
| 104 | m->SetFromDoubleArray(array);
|
---|
| 105 | for (int i=0;i<4;i++)
|
---|
| 106 | for (int j=0;j<3;j++)
|
---|
| 107 | CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
|
---|
| 108 |
|
---|
| 109 | };
|
---|
| 110 |
|
---|
| 111 | /** Unit Test for copying matrices.
|
---|
| 112 | *
|
---|
| 113 | */
|
---|
| 114 | void GSLMatrixTest::CopyTest()
|
---|
| 115 | {
|
---|
| 116 | // set basis
|
---|
| 117 | GSLMatrix *dest = NULL;
|
---|
| 118 | for (int i=0;i<4;i++) {
|
---|
| 119 | m->SetAll(i);
|
---|
| 120 | dest = new GSLMatrix(m);
|
---|
| 121 | for (int j=0;j<3;j++)
|
---|
| 122 | CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) );
|
---|
| 123 |
|
---|
| 124 | delete(dest);
|
---|
| 125 | }
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 | /** Unit Test for exchanging rows and columns.
|
---|
| 129 | *
|
---|
| 130 | */
|
---|
| 131 | void GSLMatrixTest::ExchangeTest()
|
---|
| 132 | {
|
---|
| 133 | // set to 1,1,1,2, ...
|
---|
| 134 | for (int i=0;i<4;i++)
|
---|
| 135 | for (int j=0;j<3;j++)
|
---|
| 136 | m->Set(i,j, i+1 );
|
---|
| 137 |
|
---|
| 138 | // swap such that nothing happens
|
---|
| 139 | CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(1,2) );
|
---|
| 140 | for (int i=0;i<4;i++)
|
---|
| 141 | for (int j=0;j<3;j++)
|
---|
| 142 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
|
---|
| 143 |
|
---|
| 144 | // swap two rows
|
---|
| 145 | CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
|
---|
| 146 | for (int i=0;i<4;i++)
|
---|
| 147 | for (int j=0;j<3;j++)
|
---|
| 148 | switch (i) {
|
---|
| 149 | case 0:
|
---|
| 150 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
|
---|
| 151 | break;
|
---|
| 152 | case 1:
|
---|
| 153 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
|
---|
| 154 | break;
|
---|
| 155 | case 2:
|
---|
| 156 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
|
---|
| 157 | break;
|
---|
| 158 | case 3:
|
---|
| 159 | CPPUNIT_ASSERT_EQUAL( 4., m->Get(i,j) );
|
---|
| 160 | break;
|
---|
| 161 | default:
|
---|
| 162 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
|
---|
| 163 | }
|
---|
| 164 | // check that op is reversable
|
---|
| 165 | CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
|
---|
| 166 | for (int i=0;i<4;i++)
|
---|
| 167 | for (int j=0;j<3;j++)
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
|
---|
| 169 |
|
---|
| 170 | // set to 1,2,3,1, ...
|
---|
| 171 | for (int i=0;i<4;i++)
|
---|
| 172 | for (int j=0;j<3;j++)
|
---|
| 173 | m->Set(i,j, j+1. );
|
---|
| 174 |
|
---|
| 175 | // swap such that nothing happens
|
---|
| 176 | CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(0,2) );
|
---|
| 177 | for (int i=0;i<4;i++)
|
---|
| 178 | for (int j=0;j<3;j++)
|
---|
| 179 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
|
---|
| 180 |
|
---|
| 181 | // swap two columns
|
---|
| 182 | CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
|
---|
| 183 | for (int i=0;i<4;i++)
|
---|
| 184 | for (int j=0;j<3;j++)
|
---|
| 185 | switch (j) {
|
---|
| 186 | case 0:
|
---|
| 187 | CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
|
---|
| 188 | break;
|
---|
| 189 | case 1:
|
---|
| 190 | CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
|
---|
| 191 | break;
|
---|
| 192 | case 2:
|
---|
| 193 | CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
|
---|
| 194 | break;
|
---|
| 195 | default:
|
---|
| 196 | CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
|
---|
| 197 | }
|
---|
| 198 | // check that op is reversable
|
---|
| 199 | CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
|
---|
| 200 | for (int i=0;i<4;i++)
|
---|
| 201 | for (int j=0;j<3;j++)
|
---|
| 202 | CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | // set to 1,2,3,4, ...
|
---|
| 206 | for (int i=0;i<4;i++)
|
---|
| 207 | for (int j=0;j<3;j++)
|
---|
| 208 | m->Set(i,j, 3*i+j+1 );
|
---|
| 209 | // transpose
|
---|
| 210 | CPPUNIT_ASSERT_EQUAL( true, m->Transpose() );
|
---|
| 211 | for (int i=0;i<4;i++)
|
---|
| 212 | for (int j=0;j<3;j++)
|
---|
| 213 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) );
|
---|
| 214 | // second transpose
|
---|
| 215 | CPPUNIT_ASSERT_EQUAL( true, m->Transpose() );
|
---|
| 216 | for (int i=0;i<4;i++)
|
---|
| 217 | for (int j=0;j<3;j++)
|
---|
| 218 | CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) );
|
---|
| 219 | };
|
---|
| 220 |
|
---|
| 221 | /** Unit Test for matrix properties.
|
---|
| 222 | *
|
---|
| 223 | */
|
---|
| 224 | void GSLMatrixTest::PropertiesTest()
|
---|
| 225 | {
|
---|
| 226 | // is zero
|
---|
| 227 | m->SetZero();
|
---|
| 228 | CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
|
---|
| 229 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
|
---|
| 230 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
|
---|
| 231 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
|
---|
| 232 |
|
---|
| 233 | // is positive
|
---|
| 234 | m->SetAll(0.5);
|
---|
| 235 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
|
---|
| 236 | CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
|
---|
| 237 | CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
|
---|
| 238 | CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
|
---|
| 239 |
|
---|
| 240 | // is negative
|
---|
| 241 | m->SetAll(-0.1);
|
---|
| 242 | CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
|
---|
| 243 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
|
---|
| 244 | CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
|
---|
| 245 | CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
|
---|
| 246 |
|
---|
| 247 | // is positive definite
|
---|
| 248 | CPPUNIT_ASSERT_EQUAL( false, m->IsPositiveDefinite() );
|
---|
| 249 | };
|
---|
| 250 |
|
---|
| 251 | /********************************************** Main routine **************************************/
|
---|
| 252 |
|
---|
| 253 | int main(int argc, char **argv)
|
---|
| 254 | {
|
---|
| 255 | // Get the top level suite from the registry
|
---|
| 256 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
| 257 |
|
---|
| 258 | // Adds the test to the list of test to run
|
---|
| 259 | CppUnit::TextUi::TestRunner runner;
|
---|
| 260 | runner.addTest( suite );
|
---|
| 261 |
|
---|
| 262 | // Change the default outputter to a compiler error format outputter
|
---|
| 263 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
| 264 | std::cerr ) );
|
---|
| 265 | // Run the tests.
|
---|
| 266 | bool wasSucessful = runner.run();
|
---|
| 267 |
|
---|
| 268 | // Return error code 1 if the one of test failed.
|
---|
| 269 | return wasSucessful ? 0 : 1;
|
---|
| 270 | };
|
---|