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