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