/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010-2012 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * ContinuousValueTest.cpp * * Created on: Sep 29, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "ContinuousValueTest.hpp" #include #include #include #include "Parameters/ContinuousValue.hpp" #ifdef HAVE_TESTRUNNER #include "UnitTestMain.hpp" #endif /*HAVE_TESTRUNNER*/ // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( ContinuousValueTest ); void ContinuousValueTest::setUp() { // failing asserts should be thrown ASSERT_DO(Assert::Throw); ValidIntRange = new range(1,4); ValidVectorRange = new range(Vector(0,1,2), Vector(10,11,12)); } void ContinuousValueTest::tearDown() { delete ValidIntRange; delete ValidVectorRange; } /************************************ tests ***********************************/ /** Unit test for isValid. * */ void ContinuousValueTest::isValidIntValueTest() { // create instance ContinuousValue test(*ValidIntRange); // checking valid values for (int i=1; i<=4;++i) CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i)); // checking invalid values for (int i=-10; i<=0;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); for (int i=5; i<=0;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); } /** Unit test for isValid. * */ void ContinuousValueTest::isValidIntTest() { // create instance ContinuousValue test(*ValidIntRange); // checking valid values for (int i=1; i<=4;++i) CPPUNIT_ASSERT_EQUAL(true, test.isValid(i)); // checking invalid values for (int i=-10; i<=0;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValid(i)); for (int i=5; i<=0;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValid(i)); } /** Unit test for setting/getting valid range. * */ void ContinuousValueTest::setgetValidIntRangeTest() { { // create instance ContinuousValue test(*ValidIntRange); // extending range and checking for (int i=5; i<=6;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); test.setValidRange(range(1,6)); for (int i=5; i<=6;++i) CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i)); } { // create instance ContinuousValue test(*ValidIntRange); // lowering range with set value test.setValue(4); CPPUNIT_ASSERT_EQUAL(true, test.ValueSet); #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.setValidRange(range(1,3)), Assert::AssertionFailure); #else test.setValidRange(range(1,3)); #endif #ifndef NDEBUG // no value is not set std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure); #endif // value gets invalidated in either case CPPUNIT_ASSERT_EQUAL(false, test.ValueSet); } } /** Unit test for setValue and getValue. * */ void ContinuousValueTest::settergetterIntValueTest() { // create instance ContinuousValue test(*ValidIntRange); // unset calling of get, throws #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.getValue(), Assert::AssertionFailure); #endif // setting invalid, throws #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.setValue(5), Assert::AssertionFailure); #endif #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.setValue(0), Assert::AssertionFailure); #endif CPPUNIT_ASSERT_EQUAL(false, test.ValueSet); // checking all valid ones for (int i=1; i<=4;++i) { test.setValue(i); CPPUNIT_ASSERT_EQUAL(true, test.ValueSet); CPPUNIT_ASSERT_EQUAL(i, test.getValue()); } } /** Unit test for setters and getters. * */ void ContinuousValueTest::settergetterIntTest() { // create instance ContinuousValue test(*ValidIntRange); // unset calling of get, throws #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure); #endif // setting invalid, throws #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.set(5), Assert::AssertionFailure); #endif #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.set(0), Assert::AssertionFailure); #endif // checking all valid ones for (int i=1; i<=4;++i) { test.set(i); CPPUNIT_ASSERT_EQUAL(i, test.get()); } } /** Unit test for comparator. * */ void ContinuousValueTest::comparatorIntTest() { { // create instance ContinuousValue test(*ValidIntRange); ContinuousValue instance(*ValidIntRange); test.setValue(1); instance.setValue(1); // same value, same range { CPPUNIT_ASSERT(test == instance); } // different value, same range { const int oldvalue = instance.getValue(); instance.setValue(2); CPPUNIT_ASSERT(test != instance); instance.setValue(oldvalue); } } { ContinuousValue test(*ValidIntRange); range OtherValidIntRange(1,5); ContinuousValue instance(OtherValidIntRange); test.setValue(1); instance.setValue(1); // same value, same range { CPPUNIT_ASSERT(test != instance); } // different value, same range { const int oldvalue = instance.getValue(); instance.setValue(2); CPPUNIT_ASSERT(test != instance); instance.setValue(oldvalue); } } } /***************************** vector tests ***********************************/ /** Unit test for isValid. * */ void ContinuousValueTest::isValidVectorValueTest() { // create instance ContinuousValue test(*ValidVectorRange); // checking valid values CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(Vector(0,1,2))); CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(Vector(9.9,10.9,11.9))); CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(Vector(5,5,5))); // checking invalid values CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(-0.1,0.9,1.9))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(10.1,11.1,12.1))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,5,-1))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,-1,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(-1,5,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,5,20))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,20,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(20,5,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(0,0,0))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,-1,-1))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(-1,5,-1))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(-1,-1,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(5,20,20))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(20,5,20))); CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(20,20,5))); } /** Unit test for isValid. * */ void ContinuousValueTest::isValidVectorTest() { // create instance ContinuousValue test(*ValidVectorRange); // checking valid values CPPUNIT_ASSERT_EQUAL(true, test.isValid(Vector(0,1,2))); CPPUNIT_ASSERT_EQUAL(true, test.isValid(Vector(9.9,10.9,11.9))); CPPUNIT_ASSERT_EQUAL(true, test.isValid(Vector(5,5,5))); // checking invalid values CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(-0.1,0.9,1.9))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(10.1,11.1,12.1))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,5,-1))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,-1,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(-1,5,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,5,20))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,20,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(20,5,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(0,0,0))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,-1,-1))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(-1,5,-1))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(-1,-1,5))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(5,20,20))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(20,5,20))); CPPUNIT_ASSERT_EQUAL(false, test.isValid(Vector(20,20,5))); } /** Unit test for setting/getting valid range. * */ void ContinuousValueTest::setgetValidVectorRangeTest() { { // create instance ContinuousValue test(*ValidVectorRange); // extending range and checking for (int i=15; i<=16;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(Vector(i,5,5))); test.setValidRange(range(Vector(0,1,2),Vector(20,11,12))); for (int i=15; i<=16;++i) CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(Vector(i,5,5))); } { // create instance ContinuousValue test(*ValidVectorRange); // lowering range with set value test.setValue(Vector(4,4,4)); CPPUNIT_ASSERT_EQUAL(true, test.ValueSet); #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.setValidRange(range(Vector(1,1,1),Vector(3,3,3))), Assert::AssertionFailure); #else test.setValidRange(range(Vector(1,1,1),Vector(3,3,3))); #endif #ifndef NDEBUG // no value is not set std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure); #endif // value gets invalidated in either case CPPUNIT_ASSERT_EQUAL(false, test.ValueSet); } } /** Unit test for setValue and getValue. * */ void ContinuousValueTest::settergetterVectorValueTest() { // create instance ContinuousValue test(*ValidVectorRange); // unset calling of get, throws #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.getValue(), Assert::AssertionFailure); #endif // setting invalid, throws #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.setValue(Vector(5,0,0)), Assert::AssertionFailure); #endif #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.setValue(Vector(5,20,5)), Assert::AssertionFailure); #endif CPPUNIT_ASSERT_EQUAL(false, test.ValueSet); // checking some valid ones for (int i=1; i<=4;++i) { Vector v(i,5,5); test.setValue(v); CPPUNIT_ASSERT_EQUAL(true, test.ValueSet); CPPUNIT_ASSERT_EQUAL(v, test.getValue()); } } /** Unit test for setters and getters. * */ void ContinuousValueTest::settergetterVectorTest() { // create instance ContinuousValue test(*ValidVectorRange); // unset calling of get, throws #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure); #endif // setting invalid, throws #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.set(Vector(5,0,0)), Assert::AssertionFailure); #endif #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; CPPUNIT_ASSERT_THROW(test.set(Vector(5,20,5)), Assert::AssertionFailure); #endif CPPUNIT_ASSERT_EQUAL(false, test.ValueSet); // checking some valid ones for (int i=1; i<=4;++i) { Vector v(i,5,5); test.set(v); CPPUNIT_ASSERT_EQUAL(true, test.ValueSet); CPPUNIT_ASSERT_EQUAL(v, test.get()); } } /** Unit test for comparator. * */ void ContinuousValueTest::comparatorVectorTest() { { // create instance ContinuousValue test(*ValidVectorRange); ContinuousValue instance(*ValidVectorRange); test.setValue(Vector(5,6,7)); instance.setValue(Vector(5,6,7)); // same value, same range { CPPUNIT_ASSERT(test == instance); } // different value, same range { const Vector oldvalue = instance.getValue(); instance.setValue(Vector(2,3,4)); CPPUNIT_ASSERT(test != instance); instance.setValue(oldvalue); } } { ContinuousValue test(*ValidVectorRange); range OtherValidVectorRange(Vector(0,1,2), Vector(20,21,22)); ContinuousValue instance(OtherValidVectorRange); test.setValue(Vector(1,2,3)); instance.setValue(Vector(1,2,3)); // same value, same range { CPPUNIT_ASSERT(test != instance); } // different value, same range { const Vector oldvalue = instance.getValue(); instance.setValue(Vector(2,3,4)); CPPUNIT_ASSERT(test != instance); instance.setValue(oldvalue); } } }