/* * gslvectorunittest.cpp * * Created on: Jan 8, 2010 * Author: heber */ using namespace std; #include #include #include #include "gslvectorunittest.hpp" /********************************************** Test classes **************************************/ // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( GSLVectorTest ); void GSLVectorTest::setUp() { v = new GSLVector(3); }; void GSLVectorTest::tearDown() { delete(v); }; /** Unit test for accessing elements in the vector. * */ void GSLVectorTest::AccessTest() { // set for (int i=0;i<3;i++) v->Set(i,i); // and check double *ptr = NULL; for (int i=0;i<3;i++) { CPPUNIT_ASSERT_EQUAL( (double)i, v->Get(i) ); ptr = v->Pointer(i); CPPUNIT_ASSERT_EQUAL( (double)i, *ptr ); } // out of bounds //CPPUNIT_ASSERT_EQUAL(0., v->Get(3) ); //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1) ); }; /** Unit test for initializing the vector. * */ void GSLVectorTest::InitializaionTest() { // set zero v->SetZero(); for (int i=0;i<3;i++) CPPUNIT_ASSERT_EQUAL( 0., v->Get(i) ); // set basis for (int i=0;i<3;i++) { v->SetBasis(i); for (int j=0;j<3;j++) CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , v->Get(j) ); } // set all v->SetAll(1.5); for (int i=0;i<3;i++) CPPUNIT_ASSERT_EQUAL( 1.5, v->Get(i) ); }; /** Unit test for copying vectors. * */ void GSLVectorTest::CopyTest() { // set basis GSLVector *dest = NULL; for (int i=0;i<3;i++) { v->SetBasis(i); dest = new GSLVector(v); for (int j=0;j<3;j++) CPPUNIT_ASSERT_EQUAL( v->Get(j) , dest->Get(j) ); delete(dest); } }; /** Unit test for exchanging elements of a vector. * */ void GSLVectorTest::ExchangeTest() { // set basis for (int i=0;i<3;i++) { v->SetBasis(i); v->SwapElements((i)%3,(i+1)%3); for (int j=0;j<3;j++) CPPUNIT_ASSERT_EQUAL( (i+1)%3 == j ? 1. : 0. , v->Get(j) ); } // set to 1,2,3 and reverse for (int i=0;i<3;i++) v->Set(i,i+1); v->Reverse(); for (int j=0;j<3;j++) CPPUNIT_ASSERT_EQUAL( (double)(3-j), v->Get(j) ); }; /********************************************** Main routine **************************************/ int main(int argc, char **argv) { // Get the top level suite from the registry CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); // Adds the test to the list of test to run CppUnit::TextUi::TestRunner runner; runner.addTest( suite ); // Change the default outputter to a compiler error format outputter runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) ); // Run the tests. bool wasSucessful = runner.run(); // Return error code 1 if the one of test failed. return wasSucessful ? 0 : 1; };