| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /*
|
|---|
| 9 | * ValueTest.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Apr 13, 2012
|
|---|
| 12 | * Author: ankele
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | #include "ValueTest.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include <cppunit/CompilerOutputter.h>
|
|---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
|---|
| 24 | #include <cppunit/ui/text/TestRunner.h>
|
|---|
| 25 |
|
|---|
| 26 | #include "Parameters/Value.hpp"
|
|---|
| 27 | #include "Parameters/Validators/DiscreteValidator.hpp"
|
|---|
| 28 | #include "Parameters/Validators/DummyValidator.hpp"
|
|---|
| 29 |
|
|---|
| 30 | #ifdef HAVE_TESTRUNNER
|
|---|
| 31 | #include "UnitTestMain.hpp"
|
|---|
| 32 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 33 |
|
|---|
| 34 | using namespace std;
|
|---|
| 35 |
|
|---|
| 36 | // Registers the fixture into the 'registry'
|
|---|
| 37 | CPPUNIT_TEST_SUITE_REGISTRATION( ValueTest );
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | void ValueTest::setUp()
|
|---|
| 41 | {
|
|---|
| 42 | // failing asserts should be thrown
|
|---|
| 43 | ASSERT_DO(Assert::Throw);
|
|---|
| 44 |
|
|---|
| 45 | for (int i=1; i<=3;++i) {
|
|---|
| 46 | ValidValues.push_back(i);
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | void ValueTest::tearDown()
|
|---|
| 51 | {
|
|---|
| 52 | ValidValues.clear();
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /************************************ tests ***********************************/
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | /** Unit test for isValid.
|
|---|
| 59 | *
|
|---|
| 60 | */
|
|---|
| 61 | void ValueTest::isValidTest()
|
|---|
| 62 | {
|
|---|
| 63 | // create instance
|
|---|
| 64 | Value<int> test(ValidValues);
|
|---|
| 65 |
|
|---|
| 66 | // checking valid values
|
|---|
| 67 | for (int i=1; i<=3;++i)
|
|---|
| 68 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
|---|
| 69 |
|
|---|
| 70 | // checking invalid values
|
|---|
| 71 | for (int i=-10; i<=0;++i)
|
|---|
| 72 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
|---|
| 73 | for (int i=4; i<=0;++i)
|
|---|
| 74 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Unit test for appendValidValue.
|
|---|
| 78 | *
|
|---|
| 79 | */
|
|---|
| 80 | void ValueTest::appendValidValueTest()
|
|---|
| 81 | {
|
|---|
| 82 | // create instance
|
|---|
| 83 | Value<int> test(ValidValues);
|
|---|
| 84 |
|
|---|
| 85 | // adding values 4,5,6
|
|---|
| 86 | for (int i=4; i<=6;++i) {
|
|---|
| 87 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
|---|
| 88 | dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i);
|
|---|
| 89 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /* // adding same value, throws assertion
|
|---|
| 93 | const size_t size_before = test.ValidValues.size();
|
|---|
| 94 | #ifndef NDEBUG
|
|---|
| 95 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
|---|
| 96 | for (int i=1; i<=6;++i)
|
|---|
| 97 | CPPUNIT_ASSERT_THROW(test.appendValidValue(i), Assert::AssertionFailure);
|
|---|
| 98 | #endif
|
|---|
| 99 | CPPUNIT_ASSERT_EQUAL( size_before, test.ValidValues.size() );
|
|---|
| 100 |
|
|---|
| 101 | // checking valid values
|
|---|
| 102 | for (int i=1; i<=6;++i)
|
|---|
| 103 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
|---|
| 104 |
|
|---|
| 105 | // checking invalid values
|
|---|
| 106 | for (int i=-10; i<=0;++i)
|
|---|
| 107 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
|---|
| 108 |
|
|---|
| 109 | // checking invalid values
|
|---|
| 110 | for (int i=7; i<=10;++i)
|
|---|
| 111 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));*/
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /** Unit test for setters and getters.
|
|---|
| 115 | *
|
|---|
| 116 | */
|
|---|
| 117 | void ValueTest::settergetterTest()
|
|---|
| 118 | {
|
|---|
| 119 | // create instance
|
|---|
| 120 | Value<int> test(ValidValues);
|
|---|
| 121 |
|
|---|
| 122 | // unset calling of get, throws
|
|---|
| 123 | #ifndef NDEBUG
|
|---|
| 124 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
|---|
| 125 | CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure);
|
|---|
| 126 | #endif
|
|---|
| 127 |
|
|---|
| 128 | // setting invalid, throws
|
|---|
| 129 | #ifndef NDEBUG
|
|---|
| 130 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
|---|
| 131 | CPPUNIT_ASSERT_THROW(test.set(4), Assert::AssertionFailure);
|
|---|
| 132 | #endif
|
|---|
| 133 | #ifndef NDEBUG
|
|---|
| 134 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
|---|
| 135 | CPPUNIT_ASSERT_THROW(test.set(0), Assert::AssertionFailure);
|
|---|
| 136 | #endif
|
|---|
| 137 |
|
|---|
| 138 | // checking all valid ones
|
|---|
| 139 | for (int i=1; i<=3;++i) {
|
|---|
| 140 | test.set(i);
|
|---|
| 141 | CPPUNIT_ASSERT_EQUAL(i, test.get());
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /** Unit test for comparator.
|
|---|
| 147 | *
|
|---|
| 148 | */
|
|---|
| 149 | void ValueTest::comparatorTest()
|
|---|
| 150 | {
|
|---|
| 151 | {
|
|---|
| 152 | // create instance
|
|---|
| 153 | Value<int> test(ValidValues);
|
|---|
| 154 | Value<int> instance(ValidValues);
|
|---|
| 155 | test.set(1);
|
|---|
| 156 | instance.set(1);
|
|---|
| 157 |
|
|---|
| 158 | // same value, same range
|
|---|
| 159 | {
|
|---|
| 160 | CPPUNIT_ASSERT(test == instance);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | // different value, same range
|
|---|
| 164 | {
|
|---|
| 165 | const int oldvalue = instance.get();
|
|---|
| 166 | instance.set(2);
|
|---|
| 167 | CPPUNIT_ASSERT(test != instance);
|
|---|
| 168 | instance.set(oldvalue);
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 | {
|
|---|
| 172 | Value<int> test(ValidValues);
|
|---|
| 173 | Value<int> instance(ValidValues);
|
|---|
| 174 | dynamic_cast<DiscreteValidator<int> &>(instance.getValidator()).appendValidValue(4);
|
|---|
| 175 |
|
|---|
| 176 | test.set(1);
|
|---|
| 177 | instance.set(1);
|
|---|
| 178 |
|
|---|
| 179 | // same value, same range
|
|---|
| 180 | {
|
|---|
| 181 | //CPPUNIT_ASSERT(test != instance);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | // different value, same range
|
|---|
| 185 | {
|
|---|
| 186 | const int oldvalue = instance.get();
|
|---|
| 187 | instance.set(2);
|
|---|
| 188 | CPPUNIT_ASSERT(test != instance);
|
|---|
| 189 | instance.set(oldvalue);
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|