| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  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 | * 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/LinkedCell_View_ModelWrapper.hpp" | 
|---|
| 33 | #include "LinkedCell/unittests/defs.hpp" | 
|---|
| 34 | #include "LinkedCell/PointCloudAdaptor.hpp" | 
|---|
| 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); | 
|---|
| 62 |  | 
|---|
| 63 | // create empty set | 
|---|
| 64 | emptyset = new PointCloudAdaptor< std::vector<atom *> >(&emptyvector, std::string("emptyset")); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | void LinkedCell_ControllerTest::tearDown() | 
|---|
| 69 | { | 
|---|
| 70 | delete controller; | 
|---|
| 71 | delete domain; | 
|---|
| 72 | delete emptyset; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 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; | 
|---|
| 89 |  | 
|---|
| 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. | 
|---|
| 117 | */ | 
|---|
| 118 | void LinkedCell_ControllerTest::getView_SameViewTest() | 
|---|
| 119 | { | 
|---|
| 120 | /// obtain a view | 
|---|
| 121 | CPPUNIT_ASSERT_EQUAL( (size_t)0, controller->ModelsMap.size() ); | 
|---|
| 122 | LinkedCell::LinkedCell_View view = controller->getView(2., *emptyset); | 
|---|
| 123 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 124 |  | 
|---|
| 125 | { | 
|---|
| 126 | /// get same view again and check that now new instance appears | 
|---|
| 127 | LinkedCell::LinkedCell_View view_again = controller->getView(2., *emptyset); | 
|---|
| 128 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 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() ); | 
|---|
| 138 | LinkedCell::LinkedCell_View view = controller->getView(2., *emptyset); | 
|---|
| 139 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 140 |  | 
|---|
| 141 | { | 
|---|
| 142 | /// pick another view that is not close enough | 
|---|
| 143 | LinkedCell::LinkedCell_View view_other = controller->getView(5., *emptyset); | 
|---|
| 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() ); | 
|---|
| 155 | LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset); | 
|---|
| 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 | 
|---|
| 162 | LinkedCell::LinkedCell_View view_lowerhalf = controller->getView((edgelength + interval.first)/2., *emptyset); | 
|---|
| 163 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 164 | } | 
|---|
| 165 | { | 
|---|
| 166 | /// ... at half upper interval half | 
|---|
| 167 | LinkedCell::LinkedCell_View view_upperhalf = controller->getView((interval.last + edgelength)/2., *emptyset); | 
|---|
| 168 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 169 | } | 
|---|
| 170 | { | 
|---|
| 171 | /// ... close to lower boundary | 
|---|
| 172 | LinkedCell::LinkedCell_View view_closelower = controller->getView(interval.first + std::numeric_limits<double>::round_error(), *emptyset); | 
|---|
| 173 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 174 | } | 
|---|
| 175 | { | 
|---|
| 176 | /// ... close to upper boundary | 
|---|
| 177 | LinkedCell::LinkedCell_View view_closerupper = controller->getView(interval.last - std::numeric_limits<double>::round_error(), *emptyset); | 
|---|
| 178 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 179 | } | 
|---|
| 180 | { | 
|---|
| 181 | /// on lower boundary | 
|---|
| 182 | LinkedCell::LinkedCell_View view_onlower = controller->getView(interval.first, *emptyset); | 
|---|
| 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() ); | 
|---|
| 196 | LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset); | 
|---|
| 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 | 
|---|
| 203 | LinkedCell::LinkedCell_View view_outsidelower = controller->getView(interval.first - std::numeric_limits<double>::round_error(), *emptyset); | 
|---|
| 204 | CPPUNIT_ASSERT_EQUAL( (size_t)2, controller->ModelsMap.size() ); | 
|---|
| 205 | } | 
|---|
| 206 | { | 
|---|
| 207 | /// ... on upper boundary | 
|---|
| 208 | LinkedCell::LinkedCell_View view_onupper = controller->getView(interval.last, *emptyset); | 
|---|
| 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() ); | 
|---|
| 220 | LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset); | 
|---|
| 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 | 
|---|
| 225 | LinkedCell::LinkedCell_View view_onlower = controller->getView(controller->lower_threshold, *emptyset); | 
|---|
| 226 | CPPUNIT_ASSERT_EQUAL( (size_t)1, controller->ModelsMap.size() ); | 
|---|
| 227 | } | 
|---|
| 228 | { | 
|---|
| 229 | /// pick a view below 1. | 
|---|
| 230 | LinkedCell::LinkedCell_View view_beneathlower = controller->getView(0.1*controller->lower_threshold, *emptyset); | 
|---|
| 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() ); | 
|---|
| 242 | LinkedCell::LinkedCell_View view = controller->getView(edgelength, *emptyset); | 
|---|
| 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 | 
|---|
| 247 | LinkedCell::LinkedCell_View view_beyondupper = controller->getView(1.1*controller->upper_threshold, *emptyset); | 
|---|
| 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) ) { | 
|---|
| 255 | LinkedCell::LinkedCell_View view_beneathupper = controller->getView(0.1*controller->upper_threshold, *emptyset); | 
|---|
| 256 | CPPUNIT_ASSERT_EQUAL( (size_t)2, controller->ModelsMap.size() ); | 
|---|
| 257 | } | 
|---|
| 258 | } | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | /** UnitTest for LinkedCell_Controller::updateModels(). | 
|---|
| 262 | */ | 
|---|
| 263 | void LinkedCell_ControllerTest::updateBoxTest() | 
|---|
| 264 | { | 
|---|
| 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() ); | 
|---|
| 277 | } | 
|---|