/* * 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. */ /* * DiscreteValueUnitTest.cpp * * Created on: Sep 28, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "DiscreteValueUnitTest.hpp" #include #include #include #include "Parser/Parameters/DiscreteValue.hpp" #include "CodePatterns/toString.hpp" #ifdef HAVE_TESTRUNNER #include "UnitTestMain.hpp" #endif /*HAVE_TESTRUNNER*/ using namespace std; // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( DiscreteValueTest ); void DiscreteValueTest::setUp() { // failing asserts should be thrown ASSERT_DO(Assert::Throw); for (int i=1; i<=3;++i) { ValidValues.push_back(i); } } void DiscreteValueTest::tearDown() { ValidValues.clear(); } /************************************ tests ***********************************/ /** Unit test for findIndexOfValue. * */ void DiscreteValueTest::findIndexOfValueTest() { // create instance DiscreteValue test(ValidValues); // check valid values indices CPPUNIT_ASSERT_EQUAL((size_t)0, test.findIndexOfValue(1)); CPPUNIT_ASSERT_EQUAL((size_t)1, test.findIndexOfValue(2)); CPPUNIT_ASSERT_EQUAL((size_t)2, test.findIndexOfValue(3)); // check invalid ones for (int i=-10; i<=0;++i) CPPUNIT_ASSERT_EQUAL((size_t)-1, test.findIndexOfValue(i)); for (int i=4; i<=0;++i) CPPUNIT_ASSERT_EQUAL((size_t)-1, test.findIndexOfValue(i)); } /** Unit test for isValidValue. * */ void DiscreteValueTest::isValidValueTest() { // create instance DiscreteValue test(ValidValues); // checking valid values for (int i=1; i<=3;++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=4; i<=0;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); } /** Unit test for isValid. * */ void DiscreteValueTest::isValidTest() { // create instance DiscreteValue test(ValidValues); // checking valid values for (int i=1; i<=3;++i) CPPUNIT_ASSERT_EQUAL(true, test.isValid(toString(i))); // checking invalid values for (int i=-10; i<=0;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValid(toString(i))); for (int i=4; i<=0;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValid(toString(i))); } /** Unit test for appendValidValue. * */ void DiscreteValueTest::appendValidValueTest() { // create instance DiscreteValue test(ValidValues); // adding values 4,5,6 for (int i=4; i<=6;++i) { CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); test.appendValidValue(i); CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i)); } // adding same value, throws assertion const size_t size_before = test.ValidValues.size(); #ifndef NDEBUG std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; for (int i=1; i<=6;++i) CPPUNIT_ASSERT_THROW(test.appendValidValue(i), Assert::AssertionFailure); #endif CPPUNIT_ASSERT_EQUAL( size_before, test.ValidValues.size() ); // checking valid values for (int i=1; i<=6;++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)); // checking invalid values for (int i=7; i<=10;++i) CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); } /** Unit test for setters and getters. * */ void DiscreteValueTest::settergetterTest() { // create instance DiscreteValue test(ValidValues); // 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(toString(4)), 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(toString(0)), Assert::AssertionFailure); #endif // checking all valid ones for (int i=1; i<=3;++i) { test.set(toString(i)); CPPUNIT_ASSERT_EQUAL(toString(i), test.get()); } } /** Unit test for setValue and getValue. * */ void DiscreteValueTest::settergetterValueTest() { // create instance DiscreteValue test(ValidValues); // 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(4), 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 // checking all valid ones for (int i=1; i<=3;++i) { test.setValue(i); CPPUNIT_ASSERT_EQUAL(i, test.getValue()); } } /** Unit test for comparator. * */ void DiscreteValueTest::comparatorTest() { { // create instance DiscreteValue test(ValidValues); DiscreteValue instance(ValidValues); 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); } } { DiscreteValue test(ValidValues); DiscreteValue instance(ValidValues); instance.appendValidValue(4); 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); } } }