[c68409] | 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 | /*
|
---|
[dbb533] | 9 | * DiscreteValueTest.cpp
|
---|
[c68409] | 10 | *
|
---|
| 11 | * Created on: Sep 28, 2011
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[dbb533] | 20 | #include "DiscreteValueTest.hpp"
|
---|
[c68409] | 21 |
|
---|
| 22 | #include <cppunit/CompilerOutputter.h>
|
---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 25 |
|
---|
[3c5ef5] | 26 | #include "Parameters/Value.hpp"
|
---|
[c68409] | 27 |
|
---|
| 28 | #ifdef HAVE_TESTRUNNER
|
---|
| 29 | #include "UnitTestMain.hpp"
|
---|
| 30 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 31 |
|
---|
| 32 | using namespace std;
|
---|
| 33 |
|
---|
| 34 | // Registers the fixture into the 'registry'
|
---|
| 35 | CPPUNIT_TEST_SUITE_REGISTRATION( DiscreteValueTest );
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | void DiscreteValueTest::setUp()
|
---|
| 39 | {
|
---|
| 40 | // failing asserts should be thrown
|
---|
| 41 | ASSERT_DO(Assert::Throw);
|
---|
| 42 |
|
---|
| 43 | for (int i=1; i<=3;++i) {
|
---|
| 44 | ValidValues.push_back(i);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | void DiscreteValueTest::tearDown()
|
---|
| 49 | {
|
---|
| 50 | ValidValues.clear();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /************************************ tests ***********************************/
|
---|
| 54 |
|
---|
| 55 | /** Unit test for findIndexOfValue.
|
---|
| 56 | *
|
---|
| 57 | */
|
---|
| 58 | void DiscreteValueTest::findIndexOfValueTest()
|
---|
| 59 | {
|
---|
| 60 | // create instance
|
---|
[3c5ef5] | 61 | Value<int> test(ValidValues);
|
---|
[c68409] | 62 |
|
---|
| 63 | // check valid values indices
|
---|
[3c5ef5] | 64 | CPPUNIT_ASSERT_EQUAL((size_t)0, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(1));
|
---|
| 65 | CPPUNIT_ASSERT_EQUAL((size_t)1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(2));
|
---|
| 66 | CPPUNIT_ASSERT_EQUAL((size_t)2, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(3));
|
---|
[c68409] | 67 |
|
---|
| 68 | // check invalid ones
|
---|
| 69 | for (int i=-10; i<=0;++i)
|
---|
[3c5ef5] | 70 | CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
|
---|
[c68409] | 71 | for (int i=4; i<=0;++i)
|
---|
[3c5ef5] | 72 | CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
|
---|
[c68409] | 73 | }
|
---|
| 74 |
|
---|
| 75 | /** Unit test for isValidValue.
|
---|
| 76 | *
|
---|
| 77 | */
|
---|
[047cad] | 78 | void DiscreteValueTest::isValidAsStringTest()
|
---|
[c68409] | 79 | {
|
---|
| 80 | // create instance
|
---|
[3c5ef5] | 81 | Value<int> test(ValidValues);
|
---|
[c68409] | 82 |
|
---|
| 83 | // checking valid values
|
---|
[047cad] | 84 | /*for (int i=1; i<=3;++i)
|
---|
| 85 | CPPUNIT_ASSERT_EQUAL(true, test.isValidAsString(toString(i)));
|
---|
[c68409] | 86 |
|
---|
| 87 | // checking invalid values
|
---|
| 88 | for (int i=-10; i<=0;++i)
|
---|
[047cad] | 89 | CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
|
---|
[c68409] | 90 | for (int i=4; i<=0;++i)
|
---|
[047cad] | 91 | CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));*/
|
---|
[c68409] | 92 | }
|
---|
| 93 |
|
---|
| 94 | /** Unit test for isValid.
|
---|
| 95 | *
|
---|
| 96 | */
|
---|
| 97 | void DiscreteValueTest::isValidTest()
|
---|
| 98 | {
|
---|
| 99 | // create instance
|
---|
[3c5ef5] | 100 | Value<int> test(ValidValues);
|
---|
[c68409] | 101 |
|
---|
| 102 | // checking valid values
|
---|
| 103 | for (int i=1; i<=3;++i)
|
---|
[dbb533] | 104 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
---|
[c68409] | 105 |
|
---|
| 106 | // checking invalid values
|
---|
| 107 | for (int i=-10; i<=0;++i)
|
---|
[dbb533] | 108 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
[c68409] | 109 | for (int i=4; i<=0;++i)
|
---|
[dbb533] | 110 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
[c68409] | 111 | }
|
---|
| 112 |
|
---|
| 113 | /** Unit test for appendValidValue.
|
---|
| 114 | *
|
---|
| 115 | */
|
---|
| 116 | void DiscreteValueTest::appendValidValueTest()
|
---|
| 117 | {
|
---|
| 118 | // create instance
|
---|
[3c5ef5] | 119 | Value<int> test(ValidValues);
|
---|
[c68409] | 120 |
|
---|
| 121 | // adding values 4,5,6
|
---|
| 122 | for (int i=4; i<=6;++i) {
|
---|
[047cad] | 123 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
[3c5ef5] | 124 | dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i);
|
---|
[047cad] | 125 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
---|
[c68409] | 126 | }
|
---|
| 127 |
|
---|
| 128 | // adding same value, throws assertion
|
---|
[3c5ef5] | 129 | const size_t size_before = dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size();
|
---|
[c68409] | 130 | #ifndef NDEBUG
|
---|
| 131 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
| 132 | for (int i=1; i<=6;++i)
|
---|
[3c5ef5] | 133 | CPPUNIT_ASSERT_THROW(dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i), Assert::AssertionFailure);
|
---|
[c68409] | 134 | #endif
|
---|
[3c5ef5] | 135 | CPPUNIT_ASSERT_EQUAL( size_before, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size() );
|
---|
[c68409] | 136 |
|
---|
| 137 | // checking valid values
|
---|
| 138 | for (int i=1; i<=6;++i)
|
---|
[047cad] | 139 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
---|
[c68409] | 140 |
|
---|
| 141 | // checking invalid values
|
---|
| 142 | for (int i=-10; i<=0;++i)
|
---|
[047cad] | 143 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
[c68409] | 144 |
|
---|
| 145 | // checking invalid values
|
---|
| 146 | for (int i=7; i<=10;++i)
|
---|
[047cad] | 147 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
---|
[c68409] | 148 | }
|
---|
| 149 |
|
---|
| 150 | /** Unit test for setters and getters.
|
---|
| 151 | *
|
---|
| 152 | */
|
---|
| 153 | void DiscreteValueTest::settergetterTest()
|
---|
| 154 | {
|
---|
| 155 | // create instance
|
---|
[3c5ef5] | 156 | Value<int> test(ValidValues);
|
---|
[c68409] | 157 |
|
---|
| 158 | // unset calling of get, throws
|
---|
| 159 | #ifndef NDEBUG
|
---|
| 160 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
| 161 | CPPUNIT_ASSERT_THROW(test.get(), Assert::AssertionFailure);
|
---|
| 162 | #endif
|
---|
| 163 |
|
---|
| 164 | // setting invalid, throws
|
---|
| 165 | #ifndef NDEBUG
|
---|
| 166 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
[dbb533] | 167 | CPPUNIT_ASSERT_THROW(test.set(4), Assert::AssertionFailure);
|
---|
[c68409] | 168 | #endif
|
---|
| 169 | #ifndef NDEBUG
|
---|
| 170 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
[dbb533] | 171 | CPPUNIT_ASSERT_THROW(test.set(0), Assert::AssertionFailure);
|
---|
[c68409] | 172 | #endif
|
---|
| 173 |
|
---|
| 174 | // checking all valid ones
|
---|
| 175 | for (int i=1; i<=3;++i) {
|
---|
[dbb533] | 176 | test.set(i);
|
---|
| 177 | CPPUNIT_ASSERT_EQUAL(i, test.get());
|
---|
[c68409] | 178 | }
|
---|
| 179 |
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /** Unit test for setValue and getValue.
|
---|
| 183 | *
|
---|
| 184 | */
|
---|
[047cad] | 185 | void DiscreteValueTest::settergetterAsStringTest()
|
---|
[c68409] | 186 | {
|
---|
| 187 | // create instance
|
---|
[3c5ef5] | 188 | Value<int> test(ValidValues);
|
---|
[c68409] | 189 |
|
---|
| 190 | // unset calling of get, throws
|
---|
| 191 | #ifndef NDEBUG
|
---|
| 192 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
[047cad] | 193 | CPPUNIT_ASSERT_THROW(test.getAsString(), Assert::AssertionFailure);
|
---|
[c68409] | 194 | #endif
|
---|
| 195 |
|
---|
| 196 | // setting invalid, throws
|
---|
[047cad] | 197 | /*#ifndef NDEBUG
|
---|
[c68409] | 198 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
[047cad] | 199 | CPPUNIT_ASSERT_THROW(test.setAsString(toString(4)), Assert::AssertionFailure);
|
---|
[c68409] | 200 | #endif
|
---|
| 201 | #ifndef NDEBUG
|
---|
| 202 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
---|
[047cad] | 203 | CPPUNIT_ASSERT_THROW(test.setAsString(toString(0)), Assert::AssertionFailure);
|
---|
[c68409] | 204 | #endif
|
---|
| 205 |
|
---|
| 206 | // checking all valid ones
|
---|
| 207 | for (int i=1; i<=3;++i) {
|
---|
[047cad] | 208 | test.setAsString(toString(i));
|
---|
| 209 | CPPUNIT_ASSERT_EQUAL(toString(i), test.getAsString());
|
---|
| 210 | }*/
|
---|
[c68409] | 211 | }
|
---|
| 212 |
|
---|
| 213 | /** Unit test for comparator.
|
---|
| 214 | *
|
---|
| 215 | */
|
---|
| 216 | void DiscreteValueTest::comparatorTest()
|
---|
| 217 | {
|
---|
| 218 | {
|
---|
| 219 | // create instance
|
---|
[3c5ef5] | 220 | Value<int> test(ValidValues);
|
---|
| 221 | Value<int> instance(ValidValues);
|
---|
[047cad] | 222 | test.set(1);
|
---|
| 223 | instance.set(1);
|
---|
[c68409] | 224 |
|
---|
| 225 | // same value, same range
|
---|
| 226 | {
|
---|
| 227 | CPPUNIT_ASSERT(test == instance);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | // different value, same range
|
---|
| 231 | {
|
---|
[047cad] | 232 | const int oldvalue = instance.get();
|
---|
| 233 | instance.set(2);
|
---|
[c68409] | 234 | CPPUNIT_ASSERT(test != instance);
|
---|
[047cad] | 235 | instance.set(oldvalue);
|
---|
[c68409] | 236 | }
|
---|
| 237 | }
|
---|
| 238 | {
|
---|
[3c5ef5] | 239 | Value<int> test(ValidValues);
|
---|
| 240 | Value<int> instance(ValidValues);
|
---|
| 241 | dynamic_cast<DiscreteValidator<int> &>(instance.getValidator()).appendValidValue(4);
|
---|
[c68409] | 242 |
|
---|
[047cad] | 243 | test.set(1);
|
---|
| 244 | instance.set(1);
|
---|
[c68409] | 245 |
|
---|
| 246 | // same value, same range
|
---|
| 247 | {
|
---|
| 248 | CPPUNIT_ASSERT(test != instance);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | // different value, same range
|
---|
| 252 | {
|
---|
[047cad] | 253 | const int oldvalue = instance.get();
|
---|
| 254 | instance.set(2);
|
---|
[c68409] | 255 | CPPUNIT_ASSERT(test != instance);
|
---|
[047cad] | 256 | instance.set(oldvalue);
|
---|
[c68409] | 257 | }
|
---|
| 258 | }
|
---|
| 259 | }
|
---|