/* * 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 . */ /* * Box_BoundaryConditionsUnitTest.cpp * * Created on: Jan 2, 2012 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "Box_BoundaryConditions.hpp" #include "CodePatterns/Assert.hpp" #include "LinearAlgebra/defs.hpp" #include "Box_BoundaryConditionsUnitTest.hpp" #ifdef HAVE_TESTRUNNER #include "UnitTestMain.hpp" #endif /*HAVE_TESTRUNNER*/ /********************************************** Test classes **************************************/ // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( Box_BoundaryConditionsTest ); void Box_BoundaryConditionsTest::setUp(){ // failing asserts should be thrown ASSERT_DO(Assert::Throw); CPPUNIT_ASSERT_NO_THROW( bc = new BoundaryConditions::BCContainer ); } void Box_BoundaryConditionsTest::tearDown() { delete bc; } void Box_BoundaryConditionsTest::getTest() { // check default values for (size_t index = 0; index < NDIM; ++index) CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(index)); // check throw #ifndef NDEBUG std::cout << "The following assertions are intended and do not represent a failure of the test.\n"; CPPUNIT_ASSERT_THROW( bc->get(4), Assert::AssertionFailure ); CPPUNIT_ASSERT_THROW( bc->get(-1), Assert::AssertionFailure ); #endif } void Box_BoundaryConditionsTest::setTest() { // set to other values bc->set(1, BoundaryConditions::Ignore); bc->set(2, BoundaryConditions::Bounce); // check that is not Wrap anymore CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(1) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(2) ); // check throw #ifndef NDEBUG std::cout << "The following assertions are intended and do not represent a failure of the test.\n"; CPPUNIT_ASSERT_THROW( bc->set(4, BoundaryConditions::Wrap), Assert::AssertionFailure ); CPPUNIT_ASSERT_THROW( bc->set(-1, BoundaryConditions::Wrap), Assert::AssertionFailure ); #endif // set all at once BoundaryConditions::Conditions_t conditions(3, BoundaryConditions::Wrap); // check throw conditions.resize(NDIM+1, BoundaryConditions::Wrap); #ifndef NDEBUG std::cout << "The following assertion is intended and does not represent a failure of the test.\n"; CPPUNIT_ASSERT_THROW( bc->set(conditions), Assert::AssertionFailure ); #endif conditions.resize(NDIM-1, BoundaryConditions::Wrap); #ifndef NDEBUG std::cout << "The following assertion is intended and does not represent a failure of the test.\n"; CPPUNIT_ASSERT_THROW( bc->set(conditions), Assert::AssertionFailure ); #endif } void Box_BoundaryConditionsTest::outputTest() { std::stringstream outstream; outstream << *bc; CPPUNIT_ASSERT( outstream.str() == std::string("Wrap Wrap Wrap")); } void Box_BoundaryConditionsTest::converterTest() { // enum to string for (size_t index = (size_t)0; index < (size_t)BoundaryConditions::MAX_BoundaryCondition_t;index++) CPPUNIT_ASSERT_EQUAL( bc->ConverterBiMap[(BoundaryConditions::BoundaryCondition_t)index], bc->getName((BoundaryConditions::BoundaryCondition_t)index) ); // check throw #ifndef NDEBUG std::cout << "The following assertion is intended and does not represent a failure of the test.\n"; CPPUNIT_ASSERT_THROW( bc->getName(BoundaryConditions::MAX_BoundaryCondition_t), Assert::AssertionFailure ); #endif // enum to string CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->getEnum(std::string("Wrap")) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->getEnum(std::string("Bounce")) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->getEnum(std::string("Ignore")) ); // check throw #ifndef NDEBUG std::cout << "The following assertion is intended and does not represent a failure of the test.\n"; CPPUNIT_ASSERT_THROW( bc->getEnum("Unknown"), Assert::AssertionFailure ); #endif } void Box_BoundaryConditionsTest::inputTest() { // set by string std::stringstream inputstream(" Bounce,Wrap,Ignore"); std::stringstream otherinputstream(" Bounce Wrap Ignore"); std::stringstream anotherinputstream(" Bounce, Wrap, Ignore"); // check CPPUNIT_ASSERT_NO_THROW( inputstream >> *bc ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) ); // check CPPUNIT_ASSERT_NO_THROW( otherinputstream >> *bc ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) ); // check CPPUNIT_ASSERT_NO_THROW( anotherinputstream >> *bc ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) ); // check throw std::stringstream invalidstream1("Bounce"); std::stringstream invalidstream2("Bounce, Bounce, Wrap, Ignore"); #ifndef NDEBUG std::cout << "The following assertions are intended and do not represent a failure of the test.\n"; CPPUNIT_ASSERT_THROW( invalidstream1 >> *bc, Assert::AssertionFailure ); CPPUNIT_ASSERT_THROW( invalidstream2 >> *bc, Assert::AssertionFailure ); #endif // check that all is still the same CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Bounce, bc->get(0) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Wrap, bc->get(1) ); CPPUNIT_ASSERT_EQUAL( BoundaryConditions::Ignore, bc->get(2) ); }