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