| 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 | * DiscreteValueTest.cpp
|
|---|
| 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 |
|
|---|
| 20 | #include "DiscreteValueTest.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include <cppunit/CompilerOutputter.h>
|
|---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
|---|
| 24 | #include <cppunit/ui/text/TestRunner.h>
|
|---|
| 25 |
|
|---|
| 26 | #include "Parameters/ParameterExceptions.hpp"
|
|---|
| 27 | #include "Parameters/Value.hpp"
|
|---|
| 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
|
|---|
| 62 | Value<int> test(ValidValues);
|
|---|
| 63 |
|
|---|
| 64 | // check valid values indices
|
|---|
| 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));
|
|---|
| 68 |
|
|---|
| 69 | // check invalid ones
|
|---|
| 70 | for (int i=-10; i<=0;++i)
|
|---|
| 71 | CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
|
|---|
| 72 | for (int i=4; i<=0;++i)
|
|---|
| 73 | CPPUNIT_ASSERT_EQUAL((size_t)-1, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).findIndexOfValue(i));
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /** Unit test for isValidValue.
|
|---|
| 77 | *
|
|---|
| 78 | */
|
|---|
| 79 | void DiscreteValueTest::isValidAsStringTest()
|
|---|
| 80 | {
|
|---|
| 81 | // create instance
|
|---|
| 82 | Value<int> test(ValidValues);
|
|---|
| 83 |
|
|---|
| 84 | // checking valid values
|
|---|
| 85 | for (int i=1; i<=3;++i)
|
|---|
| 86 | CPPUNIT_ASSERT_EQUAL(true, test.isValidAsString(toString(i)));
|
|---|
| 87 |
|
|---|
| 88 | // checking invalid values
|
|---|
| 89 | for (int i=-10; i<=0;++i)
|
|---|
| 90 | CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
|
|---|
| 91 | for (int i=4; i<=0;++i)
|
|---|
| 92 | CPPUNIT_ASSERT_EQUAL(false, test.isValidAsString(toString(i)));
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /** Unit test for isValid.
|
|---|
| 96 | *
|
|---|
| 97 | */
|
|---|
| 98 | void DiscreteValueTest::isValidTest()
|
|---|
| 99 | {
|
|---|
| 100 | // create instance
|
|---|
| 101 | Value<int> test(ValidValues);
|
|---|
| 102 |
|
|---|
| 103 | // checking valid values
|
|---|
| 104 | for (int i=1; i<=3;++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 | for (int i=4; i<=0;++i)
|
|---|
| 111 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /** Unit test for appendValidValue.
|
|---|
| 115 | *
|
|---|
| 116 | */
|
|---|
| 117 | void DiscreteValueTest::appendValidValueTest()
|
|---|
| 118 | {
|
|---|
| 119 | // create instance
|
|---|
| 120 | Value<int> test(ValidValues);
|
|---|
| 121 |
|
|---|
| 122 | // adding values 4,5,6
|
|---|
| 123 | for (int i=4; i<=6;++i) {
|
|---|
| 124 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
|---|
| 125 | dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i);
|
|---|
| 126 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | // adding same value, throws assertion
|
|---|
| 130 | const size_t size_before = dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size();
|
|---|
| 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)
|
|---|
| 133 | CPPUNIT_ASSERT_THROW(dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).appendValidValue(i), ParameterValidatorException);
|
|---|
| 134 | CPPUNIT_ASSERT_EQUAL( size_before, dynamic_cast<DiscreteValidator<int> &>(test.getValidator()).getValidValues().size() );
|
|---|
| 135 |
|
|---|
| 136 | // checking valid values
|
|---|
| 137 | for (int i=1; i<=6;++i)
|
|---|
| 138 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(i));
|
|---|
| 139 |
|
|---|
| 140 | // checking invalid values
|
|---|
| 141 | for (int i=-10; i<=0;++i)
|
|---|
| 142 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
|---|
| 143 |
|
|---|
| 144 | // checking invalid values
|
|---|
| 145 | for (int i=7; i<=10;++i)
|
|---|
| 146 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(i));
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /** Unit test for setters and getters.
|
|---|
| 150 | *
|
|---|
| 151 | */
|
|---|
| 152 | void DiscreteValueTest::settergetterTest()
|
|---|
| 153 | {
|
|---|
| 154 | // create instance
|
|---|
| 155 | Value<int> test(ValidValues);
|
|---|
| 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;
|
|---|
| 159 | CPPUNIT_ASSERT_THROW(test.get(), ParameterValueException);
|
|---|
| 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;
|
|---|
| 163 | CPPUNIT_ASSERT_THROW(test.set(4), ParameterValueException);
|
|---|
| 164 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
|---|
| 165 | CPPUNIT_ASSERT_THROW(test.set(0), ParameterValueException);
|
|---|
| 166 |
|
|---|
| 167 | // checking all valid ones
|
|---|
| 168 | for (int i=1; i<=3;++i) {
|
|---|
| 169 | test.set(i);
|
|---|
| 170 | CPPUNIT_ASSERT_EQUAL(i, test.get());
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /** Unit test for setValue and getValue.
|
|---|
| 176 | *
|
|---|
| 177 | */
|
|---|
| 178 | void DiscreteValueTest::settergetterAsStringTest()
|
|---|
| 179 | {
|
|---|
| 180 | // create instance
|
|---|
| 181 | Value<int> test(ValidValues);
|
|---|
| 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;
|
|---|
| 185 | CPPUNIT_ASSERT_THROW(test.getAsString(), ParameterValueException);
|
|---|
| 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;
|
|---|
| 189 | CPPUNIT_ASSERT_THROW(test.setAsString(toString(4)), ParameterValueException);
|
|---|
| 190 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
|
|---|
| 191 | CPPUNIT_ASSERT_THROW(test.setAsString(toString(0)), ParameterValueException);
|
|---|
| 192 |
|
|---|
| 193 | // checking all valid ones
|
|---|
| 194 | for (int i=1; i<=3;++i) {
|
|---|
| 195 | test.setAsString(toString(i));
|
|---|
| 196 | CPPUNIT_ASSERT_EQUAL(toString(i), test.getAsString());
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /** Unit test for comparator.
|
|---|
| 201 | *
|
|---|
| 202 | */
|
|---|
| 203 | void DiscreteValueTest::comparatorTest()
|
|---|
| 204 | {
|
|---|
| 205 | {
|
|---|
| 206 | // create instance
|
|---|
| 207 | Value<int> test(ValidValues);
|
|---|
| 208 | Value<int> instance(ValidValues);
|
|---|
| 209 | test.set(1);
|
|---|
| 210 | instance.set(1);
|
|---|
| 211 |
|
|---|
| 212 | // same value, same range
|
|---|
| 213 | {
|
|---|
| 214 | CPPUNIT_ASSERT(test == instance);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | // different value, same range
|
|---|
| 218 | {
|
|---|
| 219 | const int oldvalue = instance.get();
|
|---|
| 220 | instance.set(2);
|
|---|
| 221 | CPPUNIT_ASSERT(test != instance);
|
|---|
| 222 | instance.set(oldvalue);
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 | {
|
|---|
| 226 | Value<int> test(ValidValues);
|
|---|
| 227 | Value<int> instance(ValidValues);
|
|---|
| 228 | dynamic_cast<DiscreteValidator<int> &>(instance.getValidator()).appendValidValue(4);
|
|---|
| 229 |
|
|---|
| 230 | test.set(1);
|
|---|
| 231 | instance.set(1);
|
|---|
| 232 |
|
|---|
| 233 | // same value, same range
|
|---|
| 234 | {
|
|---|
| 235 | CPPUNIT_ASSERT(test != instance);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | // different value, same range
|
|---|
| 239 | {
|
|---|
| 240 | const int oldvalue = instance.get();
|
|---|
| 241 | instance.set(2);
|
|---|
| 242 | CPPUNIT_ASSERT(test != instance);
|
|---|
| 243 | instance.set(oldvalue);
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|