| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2011 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * LinkedCell_ControllerUnitTest.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Nov 29, 2011 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | using namespace std; | 
|---|
| 21 |  | 
|---|
| 22 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 24 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include "Atom/atom.hpp" | 
|---|
| 27 | #include "Box.hpp" | 
|---|
| 28 | #include "CodePatterns/Assert.hpp" | 
|---|
| 29 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| 30 | #include "LinkedCell/LinkedCell_Controller.hpp" | 
|---|
| 31 | #include "LinkedCell/LinkedCell_View.hpp" | 
|---|
| 32 | #include "LinkedCell/unittests/defs.hpp" | 
|---|
| 33 | #include "LinkedCell/PointCloudAdaptor.hpp" | 
|---|
| 34 |  | 
|---|
| 35 | #include "LinkedCell_ControllerUnitTest.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #ifdef HAVE_TESTRUNNER | 
|---|
| 38 | #include "UnitTestMain.hpp" | 
|---|
| 39 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 40 |  | 
|---|
| 41 | /********************************************** Test classes **************************************/ | 
|---|
| 42 |  | 
|---|
| 43 | // Registers the fixture into the 'registry' | 
|---|
| 44 | CPPUNIT_TEST_SUITE_REGISTRATION( LinkedCell_ControllerTest ); | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | void LinkedCell_ControllerTest::setUp() | 
|---|
| 48 | { | 
|---|
| 49 | // failing asserts should be thrown | 
|---|
| 50 | ASSERT_DO(Assert::Throw); | 
|---|
| 51 |  | 
|---|
| 52 | // create diag(20.) matrix | 
|---|
| 53 | BoxM = new RealSpaceMatrix; | 
|---|
| 54 | BoxM->setIdentity(); | 
|---|
| 55 | (*BoxM) *= DOMAINLENGTH; | 
|---|
| 56 |  | 
|---|
| 57 | // create Box with this matrix | 
|---|
| 58 | domain = new Box(*BoxM); | 
|---|
| 59 |  | 
|---|
| 60 | controller = new LinkedCell::LinkedCell_Controller(*domain); | 
|---|
| 61 |  | 
|---|
| 62 | // create empty set | 
|---|
| 63 | emptyset = new PointCloudAdaptor< std::vector<atom *> >(&emptyvector, std::string("emptyset")); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | void LinkedCell_ControllerTest::tearDown() | 
|---|
| 68 | { | 
|---|
| 69 | delete controller; | 
|---|
| 70 | delete domain; | 
|---|
| 71 | delete emptyset; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | /** UnitTest for LinkedCell_Controller's lower and upper thresholds. | 
|---|
| 75 | */ | 
|---|
| 76 | void LinkedCell_ControllerTest::thresholdTest() | 
|---|
| 77 | { | 
|---|
| 78 | /// re-create instances | 
|---|
| 79 | delete controller; | 
|---|
| 80 | delete domain; | 
|---|
| 81 |  | 
|---|
| 82 | /// create diag(..) matrix beyond upper_threshold | 
|---|
| 83 | const double old_threshold = controller->upper_threshold; | 
|---|
| 84 | controller->lower_threshold = old_threshold*0.9; | 
|---|
| 85 | RealSpaceMatrix BoxM; | 
|---|
| 86 | BoxM.setIdentity(); | 
|---|
| 87 | BoxM *= controller->upper_threshold*.5; | 
|---|
| 88 |  | 
|---|
| 89 | /// create Box with this matrix | 
|---|
| 90 | domain = new Box(BoxM); | 
|---|
| 91 |  | 
|---|
| 92 | controller = new LinkedCell::LinkedCell_Controller(*domain); | 
|---|
| 93 |  | 
|---|
| 94 | /// check that thresholds have been adapted | 
|---|
| 95 | CPPUNIT_ASSERT( controller->upper_threshold != old_threshold ); | 
|---|
| 96 | CPPUNIT_ASSERT( controller->lower_threshold != old_threshold*0.9 ); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | /** UnitTest for LinkedCell_Controller::getHeuristicRange(). | 
|---|
| 100 | */ | 
|---|
| 101 | void LinkedCell_ControllerTest::getHeuristicRangeTest() | 
|---|
| 102 | { | 
|---|
| 103 | /// re-implementing function to check is nonsense here, instead try some | 
|---|
| 104 | /// hard-coded, working values; | 
|---|
| 105 | controller->lower_threshold = 1.; | 
|---|
| 106 | controller->upper_threshold = 20.; | 
|---|
| 107 | const double inbetween = 9.5; // half and twice is definitely within both thresholds. | 
|---|
| 108 |  | 
|---|
| 109 | /// check distance in between | 
|---|
| 110 | range<double> interval = controller->getHeuristicRange(inbetween); | 
|---|
| 111 | CPPUNIT_ASSERT ( interval.first != controller->lower_threshold ); | 
|---|
| 112 | CPPUNIT_ASSERT ( interval.last != controller->upper_threshold ); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | /** UnitTest for LinkedCell_Controller::getViewTest() for getting twice the same view. | 
|---|
| 116 | */ | 
|---|
| 117 | void LinkedCell_ControllerTest::getView_SameViewTest() | 
|---|
| 118 | { | 
|---|
| 119 | /// obtain a view | 
|---|
| 120 | CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() ); | 
|---|
| 121 | LinkedCell::LinkedCell_View view = controller->getView(2., *emptyset); | 
|---|
| 122 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 123 |  | 
|---|
| 124 | { | 
|---|
| 125 | /// get same view again and check that now new instance appears | 
|---|
| 126 | LinkedCell::LinkedCell_View view_again = controller->getView(2., *emptyset); | 
|---|
| 127 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking two different views. | 
|---|
| 132 | */ | 
|---|
| 133 | void LinkedCell_ControllerTest::getView_DifferentViewTest() | 
|---|
| 134 | { | 
|---|
| 135 | /// obtain a view | 
|---|
| 136 | CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() ); | 
|---|
| 137 | LinkedCell::LinkedCell_View view = controller->getView(2., *emptyset); | 
|---|
| 138 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 139 |  | 
|---|
| 140 | { | 
|---|
| 141 | /// pick another view that is not close enough | 
|---|
| 142 | LinkedCell::LinkedCell_View view_other = controller->getView(5., *emptyset); | 
|---|
| 143 | CPPUNIT_ASSERT_EQUAL( (size_t)2, controller->ModelsMap.size() ); | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking further views in range of present one. | 
|---|
| 148 | */ | 
|---|
| 149 | void LinkedCell_ControllerTest::getView_InRangeViewTest() | 
|---|
| 150 | { | 
|---|
| 151 | /// obtain a view | 
|---|
| 152 | const double edgelength = 2.; | 
|---|
| 153 | CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() ); | 
|---|
| 154 | LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset); | 
|---|
| 155 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 156 |  | 
|---|
| 157 | /// pick views that are close enough | 
|---|
| 158 | range<double> interval = controller->getHeuristicRange(edgelength); | 
|---|
| 159 | { | 
|---|
| 160 | /// ... at half lower interval half | 
|---|
| 161 | LinkedCell::LinkedCell_View view_lowerhalf = controller->getView((edgelength + interval.first)/2., *emptyset); | 
|---|
| 162 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 163 | } | 
|---|
| 164 | { | 
|---|
| 165 | /// ... at half upper interval half | 
|---|
| 166 | LinkedCell::LinkedCell_View view_upperhalf = controller->getView((interval.last + edgelength)/2., *emptyset); | 
|---|
| 167 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 168 | } | 
|---|
| 169 | { | 
|---|
| 170 | /// ... close to lower boundary | 
|---|
| 171 | LinkedCell::LinkedCell_View view_closelower = controller->getView(interval.first + std::numeric_limits<double>::round_error(), *emptyset); | 
|---|
| 172 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 173 | } | 
|---|
| 174 | { | 
|---|
| 175 | /// ... close to upper boundary | 
|---|
| 176 | LinkedCell::LinkedCell_View view_closerupper = controller->getView(interval.last - std::numeric_limits<double>::round_error(), *emptyset); | 
|---|
| 177 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 178 | } | 
|---|
| 179 | { | 
|---|
| 180 | /// on lower boundary | 
|---|
| 181 | LinkedCell::LinkedCell_View view_onlower = controller->getView(interval.first, *emptyset); | 
|---|
| 182 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 183 | } | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking further views outside range. | 
|---|
| 187 | */ | 
|---|
| 188 | void LinkedCell_ControllerTest::getView_OutOfRangeViewTest() | 
|---|
| 189 | { | 
|---|
| 190 | /// Here we need half of the edge length to be greater than lower_threshold | 
|---|
| 191 | const double edgelength = 2.5; | 
|---|
| 192 | CPPUNIT_ASSERT( (edgelength/2.) > controller->lower_threshold ); | 
|---|
| 193 | /// obtain a view | 
|---|
| 194 | CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() ); | 
|---|
| 195 | LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset); | 
|---|
| 196 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 197 |  | 
|---|
| 198 | /// pick views that are not close enough and check for new instance | 
|---|
| 199 | range<double> interval = controller->getHeuristicRange(edgelength); | 
|---|
| 200 | { | 
|---|
| 201 | /// ... outside lower boundary | 
|---|
| 202 | LinkedCell::LinkedCell_View view_outsidelower = controller->getView(interval.first - std::numeric_limits<double>::round_error(), *emptyset); | 
|---|
| 203 | CPPUNIT_ASSERT_EQUAL( (size_t)2, controller->ModelsMap.size() ); | 
|---|
| 204 | } | 
|---|
| 205 | { | 
|---|
| 206 | /// ... on upper boundary | 
|---|
| 207 | LinkedCell::LinkedCell_View view_onupper = controller->getView(interval.last, *emptyset); | 
|---|
| 208 | CPPUNIT_ASSERT_EQUAL( (size_t)3, controller->ModelsMap.size() ); | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking views beneath lower threshold. | 
|---|
| 213 | */ | 
|---|
| 214 | void LinkedCell_ControllerTest::getView_LowerThresholdViewTest() | 
|---|
| 215 | { | 
|---|
| 216 | /// obtain a view | 
|---|
| 217 | const double edgelength = 1.9*controller->lower_threshold; | 
|---|
| 218 | CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() ); | 
|---|
| 219 | LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset); | 
|---|
| 220 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 221 |  | 
|---|
| 222 | { | 
|---|
| 223 | /// get a view at threshold and check that no new instance has been created | 
|---|
| 224 | LinkedCell::LinkedCell_View view_onlower = controller->getView(controller->lower_threshold, *emptyset); | 
|---|
| 225 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 226 | } | 
|---|
| 227 | { | 
|---|
| 228 | /// pick a view below 1. | 
|---|
| 229 | LinkedCell::LinkedCell_View view_beneathlower = controller->getView(0.1*controller->lower_threshold, *emptyset); | 
|---|
| 230 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 231 | } | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | /** UnitTest for LinkedCell_Controller::getViewTest() for picking views above upper threshold. | 
|---|
| 235 | */ | 
|---|
| 236 | void LinkedCell_ControllerTest::getView_UpperThresholdViewTest() | 
|---|
| 237 | { | 
|---|
| 238 | /// obtain a view | 
|---|
| 239 | const double edgelength = controller->upper_threshold; | 
|---|
| 240 | CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() ); | 
|---|
| 241 | LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset); | 
|---|
| 242 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 243 |  | 
|---|
| 244 | { | 
|---|
| 245 | /// get a view beyond threshold and check that no new instance has been created | 
|---|
| 246 | LinkedCell::LinkedCell_View view_beyondupper = controller->getView(1.1*controller->upper_threshold, *emptyset); | 
|---|
| 247 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | { | 
|---|
| 251 | /// pick a view below threshold and check for new instance (if we make it outside acceptable range) | 
|---|
| 252 | range<double> interval = controller->getHeuristicRange(edgelength); | 
|---|
| 253 | if ( !interval.isInRange(0.1*controller->upper_threshold) ) { | 
|---|
| 254 | LinkedCell::LinkedCell_View view_beneathupper = controller->getView(0.1*controller->upper_threshold, *emptyset); | 
|---|
| 255 | CPPUNIT_ASSERT_EQUAL( (size_t)2, controller->ModelsMap.size() ); | 
|---|
| 256 | } | 
|---|
| 257 | } | 
|---|
| 258 | } | 
|---|