/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2012 University of Bonn. All rights reserved. * * * This file is part of MoleCuilder. * * MoleCuilder is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * MoleCuilder is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MoleCuilder. If not, see . */ /* * SamplingGridUnitTest.cpp * * Created on: Jul 29, 2012 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif using namespace std; #include #include #include #include "SamplingGridUnitTest.hpp" #include "CodePatterns/Assert.hpp" #include #include #ifdef HAVE_TESTRUNNER #include "UnitTestMain.hpp" #endif /*HAVE_TESTRUNNER*/ using namespace boost::assign; /********************************************** Test classes **************************************/ const double grid_value=1.; #define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3)) // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( SamplingGridTest ); void SamplingGridTest::setUp() { // failing asserts should be thrown ASSERT_DO(Assert::Throw); // create the grid const double begin[3] = { 0., 0., 0. }; const double end[3] = { 1., 1., 1. }; for (size_t i=0; i< pow(1,3)*NUMBEROFSAMPLES(2); ++i) values += grid_value; grid = new SamplingGrid(begin, end, 2, values); CPPUNIT_ASSERT_EQUAL( grid_value, *(grid->sampled_grid.begin()) ); } void SamplingGridTest::tearDown() { delete grid; } /** UnitTest on compatible combination of props and values */ void SamplingGridTest::compatibleGrids_Test() { // check illegal grid const double begin[3] = { 0., 0., 0. }; const double end[3] = { 2., 2., 2. }; SamplingGridProperties illegal_props(begin, end, 1); SamplingGridProperties legal_props(begin, end, 2); CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) ); CPPUNIT_ASSERT( grid->isCompatible(legal_props) ); SamplingGrid::sampledvalues_t illegal_values; for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i) illegal_values += 1.5; SamplingGrid::sampledvalues_t legal_values; for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i) illegal_values += 1.5; #ifndef NDEBUG // throws because props and size of illegal_values don't match std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, illegal_values), Assert::AssertionFailure ); std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(legal_props, illegal_values), Assert::AssertionFailure ); std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, legal_values), Assert::AssertionFailure ); #endif // check that grid is still the same for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin(); iter != grid->sampled_grid.end(); ++iter) CPPUNIT_ASSERT_EQUAL( grid_value, *iter ); } /** UnitTest for getVolume_Test */ void SamplingGridTest::getVolume_Test() { CPPUNIT_ASSERT_EQUAL( 1., grid->getVolume() ); } /** UnitTest for getWindowSize_Test */ void SamplingGridTest::getWindowSize_Test() { // check size of default grid CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(2), grid->getWindowGridPoints() ); // create another one and check its size, too const double begin[3] = { 0., 0., 0. }; const double end[3] = { 1., 1., 1. }; for (size_t level = 3; level<=6; ++level) { values.clear(); for (size_t i=0; i< NUMBEROFSAMPLES(level); ++i) values += grid_value; delete grid; grid = new SamplingGrid(begin, end, level, values); CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(level), grid->getWindowGridPoints() ); } } /** UnitTest for extendWindow() */ void SamplingGridTest::extendWindow_Test() { // we have a grid with size of two, extend to twice the size and check const double begin[3] = { 0., 0., 0. }; const double end[3] = { 2., 2., 2. }; grid->extendWindow(begin, end); // check integral CPPUNIT_ASSERT_EQUAL( NUMBEROFSAMPLES(2)*grid_value, grid->integral() ); // check number of points CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(3), grid->getWindowGridPoints() ); } /** UnitTest for addOntoWindow() */ void SamplingGridTest::addOntoWindow_Test() { // first window is from (0,0,0) to (1,1,1) CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() ); // create another window from (.5,.5,.5) to (1., 1., 1.) const double begin[3] = { .5, .5, .5 }; const double end[3] = { 1., 1., 1. }; values.clear(); for (size_t i=0; i< NUMBEROFSAMPLES(2)/pow(2,3); ++i) // cut in half values += grid_value; // check that too large a window throws #ifndef NDEBUG const double wrongend[3] = { 1.5, 1.5, 1.5 }; std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values), Assert::AssertionFailure ); #endif // now perform working operation grid->addOntoWindow(begin, end, values); // check integral to be one and half times the old value CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, grid->integral() ); } /** UnitTest for operator+=() */ void SamplingGridTest::operatorPlusEqual_Test() { // create other grid const double begin[3] = { 0., 0., 0. }; const double end[3] = { 1., 1., 1. }; SamplingGrid::sampledvalues_t othervalues; const double othergrid_value = 1.5; for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i) othervalues += othergrid_value; SamplingGrid othergrid(begin, end, 2, othervalues); CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) ); // perform operation CPPUNIT_ASSERT_NO_THROW( *grid += othergrid ); // check the contents of the grid const double sum = grid_value+othergrid_value; for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin(); iter != grid->sampled_grid.end(); ++iter) CPPUNIT_ASSERT_EQUAL( sum, *iter ); } /** UnitTest for operator-=() */ void SamplingGridTest::operatorMinusEqual_Test() { // create other grid const double begin[3] = { 0., 0., 0. }; const double end[3] = { 1., 1., 1. }; SamplingGrid::sampledvalues_t othervalues; const double othergrid_value = 1.5; for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i) othervalues += othergrid_value; SamplingGrid othergrid(begin, end, 2, othervalues); CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) ); // perform operation CPPUNIT_ASSERT_NO_THROW( *grid -= othergrid ); // check the contents of the grid const double difference = grid_value-othergrid_value; for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin(); iter != grid->sampled_grid.end(); ++iter) CPPUNIT_ASSERT_EQUAL( difference, *iter ); }