| [e09d67] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [d103d3] | 4 | * Copyright (C)  2010-2011 University of Bonn. All rights reserved. | 
|---|
| [e09d67] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | /* | 
|---|
|  | 9 | * DiscreteValueUnitTest.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 "DiscreteValueUnitTest.hpp" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 23 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 24 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 25 |  | 
|---|
| [84e752] | 26 | #include "Parser/Parameters/DiscreteValue.hpp" | 
|---|
| [e09d67] | 27 |  | 
|---|
| [a7d753] | 28 | #include "CodePatterns/toString.hpp" | 
|---|
|  | 29 |  | 
|---|
| [e09d67] | 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( DiscreteValueTest ); | 
|---|
|  | 38 |  | 
|---|
|  | 39 |  | 
|---|
|  | 40 | void DiscreteValueTest::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 DiscreteValueTest::tearDown() | 
|---|
|  | 51 | { | 
|---|
|  | 52 | ValidValues.clear(); | 
|---|
|  | 53 | } | 
|---|
|  | 54 |  | 
|---|
|  | 55 | /************************************ tests ***********************************/ | 
|---|
|  | 56 |  | 
|---|
|  | 57 | /** Unit test for findIndexOfValue. | 
|---|
|  | 58 | * | 
|---|
|  | 59 | */ | 
|---|
|  | 60 | void DiscreteValueTest::findIndexOfValueTest() | 
|---|
|  | 61 | { | 
|---|
|  | 62 | // create instance | 
|---|
|  | 63 | DiscreteValue<int> test(ValidValues); | 
|---|
|  | 64 |  | 
|---|
|  | 65 | // check valid values indices | 
|---|
|  | 66 | CPPUNIT_ASSERT_EQUAL((size_t)0, test.findIndexOfValue(1)); | 
|---|
|  | 67 | CPPUNIT_ASSERT_EQUAL((size_t)1, test.findIndexOfValue(2)); | 
|---|
|  | 68 | CPPUNIT_ASSERT_EQUAL((size_t)2, test.findIndexOfValue(3)); | 
|---|
|  | 69 |  | 
|---|
|  | 70 | // check invalid ones | 
|---|
|  | 71 | for (int i=-10; i<=0;++i) | 
|---|
|  | 72 | CPPUNIT_ASSERT_EQUAL((size_t)-1, test.findIndexOfValue(i)); | 
|---|
|  | 73 | for (int i=4; i<=0;++i) | 
|---|
|  | 74 | CPPUNIT_ASSERT_EQUAL((size_t)-1, test.findIndexOfValue(i)); | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
| [a7d753] | 77 | /** Unit test for isValidValue. | 
|---|
|  | 78 | * | 
|---|
|  | 79 | */ | 
|---|
|  | 80 | void DiscreteValueTest::isValidValueTest() | 
|---|
|  | 81 | { | 
|---|
|  | 82 | // create instance | 
|---|
|  | 83 | DiscreteValue<int> test(ValidValues); | 
|---|
|  | 84 |  | 
|---|
|  | 85 | // checking valid values | 
|---|
|  | 86 | for (int i=1; i<=3;++i) | 
|---|
|  | 87 | CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i)); | 
|---|
|  | 88 |  | 
|---|
|  | 89 | // checking invalid values | 
|---|
|  | 90 | for (int i=-10; i<=0;++i) | 
|---|
|  | 91 | CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); | 
|---|
|  | 92 | for (int i=4; i<=0;++i) | 
|---|
|  | 93 | CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
| [e09d67] | 96 | /** Unit test for isValid. | 
|---|
|  | 97 | * | 
|---|
|  | 98 | */ | 
|---|
|  | 99 | void DiscreteValueTest::isValidTest() | 
|---|
|  | 100 | { | 
|---|
|  | 101 | // create instance | 
|---|
|  | 102 | DiscreteValue<int> test(ValidValues); | 
|---|
|  | 103 |  | 
|---|
|  | 104 | // checking valid values | 
|---|
|  | 105 | for (int i=1; i<=3;++i) | 
|---|
| [a7d753] | 106 | CPPUNIT_ASSERT_EQUAL(true, test.isValid(toString(i))); | 
|---|
| [e09d67] | 107 |  | 
|---|
|  | 108 | // checking invalid values | 
|---|
|  | 109 | for (int i=-10; i<=0;++i) | 
|---|
| [a7d753] | 110 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(toString(i))); | 
|---|
| [e09d67] | 111 | for (int i=4; i<=0;++i) | 
|---|
| [a7d753] | 112 | CPPUNIT_ASSERT_EQUAL(false, test.isValid(toString(i))); | 
|---|
| [e09d67] | 113 | } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | /** Unit test for appendValidValue. | 
|---|
|  | 116 | * | 
|---|
|  | 117 | */ | 
|---|
|  | 118 | void DiscreteValueTest::appendValidValueTest() | 
|---|
|  | 119 | { | 
|---|
|  | 120 | // create instance | 
|---|
|  | 121 | DiscreteValue<int> test(ValidValues); | 
|---|
|  | 122 |  | 
|---|
|  | 123 | // adding values 4,5,6 | 
|---|
|  | 124 | for (int i=4; i<=6;++i) { | 
|---|
| [a7d753] | 125 | CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); | 
|---|
| [e09d67] | 126 | test.appendValidValue(i); | 
|---|
| [a7d753] | 127 | CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i)); | 
|---|
| [e09d67] | 128 | } | 
|---|
|  | 129 |  | 
|---|
|  | 130 | // adding same value, throws assertion | 
|---|
| [a7d753] | 131 | const size_t size_before = test.ValidValues.size(); | 
|---|
| [e09d67] | 132 | #ifndef NDEBUG | 
|---|
|  | 133 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; | 
|---|
|  | 134 | for (int i=1; i<=6;++i) | 
|---|
|  | 135 | CPPUNIT_ASSERT_THROW(test.appendValidValue(i), Assert::AssertionFailure); | 
|---|
|  | 136 | #endif | 
|---|
| [a7d753] | 137 | CPPUNIT_ASSERT_EQUAL( size_before, test.ValidValues.size() ); | 
|---|
| [e09d67] | 138 |  | 
|---|
|  | 139 | // checking valid values | 
|---|
|  | 140 | for (int i=1; i<=6;++i) | 
|---|
| [a7d753] | 141 | CPPUNIT_ASSERT_EQUAL(true, test.isValidValue(i)); | 
|---|
| [e09d67] | 142 |  | 
|---|
|  | 143 | // checking invalid values | 
|---|
|  | 144 | for (int i=-10; i<=0;++i) | 
|---|
| [a7d753] | 145 | CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); | 
|---|
| [e09d67] | 146 |  | 
|---|
|  | 147 | // checking invalid values | 
|---|
|  | 148 | for (int i=7; i<=10;++i) | 
|---|
| [a7d753] | 149 | CPPUNIT_ASSERT_EQUAL(false, test.isValidValue(i)); | 
|---|
| [e09d67] | 150 | } | 
|---|
|  | 151 |  | 
|---|
|  | 152 | /** Unit test for setters and getters. | 
|---|
|  | 153 | * | 
|---|
|  | 154 | */ | 
|---|
|  | 155 | void DiscreteValueTest::settergetterTest() | 
|---|
|  | 156 | { | 
|---|
|  | 157 | // create instance | 
|---|
|  | 158 | DiscreteValue<int> test(ValidValues); | 
|---|
|  | 159 |  | 
|---|
|  | 160 | // unset calling of get, throws | 
|---|
|  | 161 | #ifndef NDEBUG | 
|---|
|  | 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.get(), Assert::AssertionFailure); | 
|---|
|  | 164 | #endif | 
|---|
|  | 165 |  | 
|---|
|  | 166 | // setting invalid, throws | 
|---|
|  | 167 | #ifndef NDEBUG | 
|---|
|  | 168 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; | 
|---|
| [a7d753] | 169 | CPPUNIT_ASSERT_THROW(test.set(toString(4)), Assert::AssertionFailure); | 
|---|
| [e09d67] | 170 | #endif | 
|---|
|  | 171 | #ifndef NDEBUG | 
|---|
|  | 172 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; | 
|---|
| [a7d753] | 173 | CPPUNIT_ASSERT_THROW(test.set(toString(0)), Assert::AssertionFailure); | 
|---|
| [e09d67] | 174 | #endif | 
|---|
|  | 175 |  | 
|---|
|  | 176 | // checking all valid ones | 
|---|
|  | 177 | for (int i=1; i<=3;++i) { | 
|---|
| [a7d753] | 178 | test.set(toString(i)); | 
|---|
|  | 179 | CPPUNIT_ASSERT_EQUAL(toString(i), test.get()); | 
|---|
| [e09d67] | 180 | } | 
|---|
| [a7d753] | 181 |  | 
|---|
| [e09d67] | 182 | } | 
|---|
|  | 183 |  | 
|---|
| [a7d753] | 184 | /** Unit test for setValue and getValue. | 
|---|
|  | 185 | * | 
|---|
|  | 186 | */ | 
|---|
|  | 187 | void DiscreteValueTest::settergetterValueTest() | 
|---|
|  | 188 | { | 
|---|
|  | 189 | // create instance | 
|---|
|  | 190 | DiscreteValue<int> test(ValidValues); | 
|---|
| [e09d67] | 191 |  | 
|---|
| [a7d753] | 192 | // unset calling of get, throws | 
|---|
|  | 193 | #ifndef NDEBUG | 
|---|
|  | 194 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; | 
|---|
|  | 195 | CPPUNIT_ASSERT_THROW(test.getValue(), Assert::AssertionFailure); | 
|---|
|  | 196 | #endif | 
|---|
|  | 197 |  | 
|---|
|  | 198 | // setting invalid, throws | 
|---|
|  | 199 | #ifndef NDEBUG | 
|---|
|  | 200 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; | 
|---|
|  | 201 | CPPUNIT_ASSERT_THROW(test.setValue(4), Assert::AssertionFailure); | 
|---|
|  | 202 | #endif | 
|---|
|  | 203 | #ifndef NDEBUG | 
|---|
|  | 204 | std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl; | 
|---|
|  | 205 | CPPUNIT_ASSERT_THROW(test.setValue(0), Assert::AssertionFailure); | 
|---|
|  | 206 | #endif | 
|---|
| [e09d67] | 207 |  | 
|---|
| [a7d753] | 208 | // checking all valid ones | 
|---|
|  | 209 | for (int i=1; i<=3;++i) { | 
|---|
|  | 210 | test.setValue(i); | 
|---|
|  | 211 | CPPUNIT_ASSERT_EQUAL(i, test.getValue()); | 
|---|
|  | 212 | } | 
|---|
|  | 213 | } | 
|---|
| [498ddd] | 214 |  | 
|---|
|  | 215 | /** Unit test for comparator. | 
|---|
|  | 216 | * | 
|---|
|  | 217 | */ | 
|---|
|  | 218 | void DiscreteValueTest::comparatorTest() | 
|---|
|  | 219 | { | 
|---|
|  | 220 | { | 
|---|
|  | 221 | // create instance | 
|---|
|  | 222 | DiscreteValue<int> test(ValidValues); | 
|---|
|  | 223 | DiscreteValue<int> instance(ValidValues); | 
|---|
|  | 224 | test.setValue(1); | 
|---|
|  | 225 | instance.setValue(1); | 
|---|
|  | 226 |  | 
|---|
|  | 227 | // same value, same range | 
|---|
|  | 228 | { | 
|---|
|  | 229 | CPPUNIT_ASSERT(test == instance); | 
|---|
|  | 230 | } | 
|---|
|  | 231 |  | 
|---|
|  | 232 | // different value, same range | 
|---|
|  | 233 | { | 
|---|
|  | 234 | const int oldvalue = instance.getValue(); | 
|---|
|  | 235 | instance.setValue(2); | 
|---|
|  | 236 | CPPUNIT_ASSERT(test != instance); | 
|---|
|  | 237 | instance.setValue(oldvalue); | 
|---|
|  | 238 | } | 
|---|
|  | 239 | } | 
|---|
|  | 240 | { | 
|---|
|  | 241 | DiscreteValue<int> test(ValidValues); | 
|---|
|  | 242 | DiscreteValue<int> instance(ValidValues); | 
|---|
|  | 243 | instance.appendValidValue(4); | 
|---|
|  | 244 |  | 
|---|
|  | 245 | test.setValue(1); | 
|---|
|  | 246 | instance.setValue(1); | 
|---|
|  | 247 |  | 
|---|
|  | 248 | // same value, same range | 
|---|
|  | 249 | { | 
|---|
|  | 250 | CPPUNIT_ASSERT(test != instance); | 
|---|
|  | 251 | } | 
|---|
|  | 252 |  | 
|---|
|  | 253 | // different value, same range | 
|---|
|  | 254 | { | 
|---|
|  | 255 | const int oldvalue = instance.getValue(); | 
|---|
|  | 256 | instance.setValue(2); | 
|---|
|  | 257 | CPPUNIT_ASSERT(test != instance); | 
|---|
|  | 258 | instance.setValue(oldvalue); | 
|---|
|  | 259 | } | 
|---|
|  | 260 | } | 
|---|
|  | 261 | } | 
|---|