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