Index: src/Jobs/Grid/unittests/SamplingGridUnitTest.cpp
===================================================================
--- src/Jobs/Grid/unittests/SamplingGridUnitTest.cpp	(revision de6dfb38ac7746ff442a0ddb707c6faa2fdcb721)
+++ src/Jobs/Grid/unittests/SamplingGridUnitTest.cpp	(revision b123a57242414c8d345a18a31cf4a42a81222702)
@@ -43,6 +43,7 @@
 #include "CodePatterns/Assert.hpp"
 
+#include <boost/assign.hpp>
 #include <cmath>
-#include <boost/assign.hpp>
+#include <numeric>
 
 #ifdef HAVE_TESTRUNNER
@@ -57,4 +58,5 @@
 
 #define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3))
+#define DOMAINVOLUME(l) (size_t)pow(l,3)
 
 // Registers the fixture into the 'registry'
@@ -70,5 +72,5 @@
   const double begin[3] = { 0., 0., 0. };
   const double end[3] = { 1., 1., 1. };
-  for (size_t i=0; i< pow(1,3)*NUMBEROFSAMPLES(2); ++i)
+  for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
     values += grid_value;
   grid = new SamplingGrid(begin, end, 2, values);
@@ -89,5 +91,5 @@
   const double begin[3] = { 0., 0., 0. };
   const double end[3] = { 2., 2., 2. };
-  SamplingGridProperties illegal_props(begin, end, 1);
+  SamplingGridProperties illegal_props(begin, end, 5);
   SamplingGridProperties legal_props(begin, end, 2);
   CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
@@ -98,5 +100,5 @@
   SamplingGrid::sampledvalues_t legal_values;
   for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
-    illegal_values += 1.5;
+    legal_values += 1.5;
 #ifndef NDEBUG
   // throws because props and size of illegal_values don't match
@@ -114,5 +116,14 @@
 }
 
-/** UnitTest for getVolume_Test
+/** UnitTest for integral()
+ */
+void SamplingGridTest::integral_Test()
+{
+  double sum = 0.;
+  sum = std::accumulate( grid->sampled_grid.begin(), grid->sampled_grid.end(), sum );
+  CPPUNIT_ASSERT_EQUAL( sum*grid->getVolume()/grid->getWindowGridPoints(), grid->integral() );
+}
+
+/** UnitTest for getVolume()
  */
 void SamplingGridTest::getVolume_Test()
@@ -121,5 +132,5 @@
 }
 
-/** UnitTest for getWindowSize_Test
+/** UnitTest for getWindowSize()
  */
 void SamplingGridTest::getWindowSize_Test()
@@ -147,15 +158,33 @@
 void SamplingGridTest::extendWindow_Test()
 {
-  // we have a grid with size of two, extend to twice the size and check
+  // we have a grid with size of one, extend to twice the size and check
   const double begin[3] = { 0., 0., 0. };
   const double size = 2.;
   const double end[3] = { size, size, size };
-  grid->extendWindow(begin, end);
-
-  // check integral
-  CPPUNIT_ASSERT_EQUAL( grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)), grid->integral() );
-
-  // check number of points
-  CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(grid->level), grid->getWindowGridPoints() );
+  double offset[3];
+  for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
+    for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
+      for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
+        const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
+        const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
+        SamplingGrid newgrid(begin, end, 2);
+        newgrid.setWindowSize(window_begin, window_end);
+        // resize values by hand to new window size. Otherwise they get zero'd.
+        newgrid.sampled_grid = values;
+        newgrid.sampled_grid.resize(NUMBEROFSAMPLES(1));
+        newgrid.extendWindow(begin, end);
+
+        // check integral
+        CPPUNIT_ASSERT_EQUAL(
+            grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)),
+            grid->integral()
+            );
+
+        // check number of points
+        CPPUNIT_ASSERT_EQUAL(
+            (size_t)NUMBEROFSAMPLES(grid->level),
+            grid->getWindowGridPoints()
+            );
+      }
 }
 
@@ -164,13 +193,28 @@
 void SamplingGridTest::extendWindow_asymmetric_Test()
 {
+  std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
   const double begin[3] = { 0., 0., 0. };
   const double end[3] = { 2., 2., 2. };
-  grid->sampled_grid.clear();
-  SamplingGrid::sampledvalues_t::iterator griditer = grid->sampled_grid.begin();
-  for (size_t i=0; i< NUMBEROFSAMPLES(grid->level); ++i)
-     *griditer++ += grid_value*i;
-  grid->extendWindow(begin, end);
-
-  CPPUNIT_ASSERT_EQUAL( grid_value, grid->integral() );
+  double offset[3];
+  for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
+    for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
+      for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
+        const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
+        const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
+        SamplingGrid newgrid(begin, end, 2);
+        CPPUNIT_ASSERT_EQUAL( (size_t)0, newgrid.getWindowGridPoints() );
+        newgrid.setWindowSize(window_begin, window_end);
+        // window size is only half of domain size
+        const size_t max_samples = NUMBEROFSAMPLES(newgrid.level)*pow(0.5,3);
+        for (size_t i=0; i< max_samples; ++i)
+          newgrid.sampled_grid += grid_value*i;
+        const size_t sum_weight = (max_samples)*(max_samples-1)/2;
+        const double integral = newgrid.integral();
+        newgrid.extendWindow(begin, end);
+
+        // check that integral has remained the same
+        CPPUNIT_ASSERT_EQUAL( integral, newgrid.integral() );
+        CPPUNIT_ASSERT_EQUAL( grid_value*sum_weight/DOMAINVOLUME(2), newgrid.integral() );
+      }
 }
 
@@ -182,21 +226,32 @@
   CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() );
 
-  // create another window from (.5,.5,.5) to (1., 1., 1.)
-  const double begin[3] = { .5, .5, .5 };
-  const double end[3] = { 1., 1., 1. };
+  // create values for half-sized window
   values.clear();
-  for (size_t i=0; i< NUMBEROFSAMPLES(2)/pow(2,3); ++i) // cut in half
+  for (size_t i=0; i< (size_t)pow(.5*pow(2,2),3); ++i)
     values += grid_value;
+
   // check that too large a window throws
 #ifndef NDEBUG
+  const double begin[3] = { .5, .5, .5 };
   const double wrongend[3] = { 1.5, 1.5, 1.5 };
   std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
   CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
 #endif
-  // now perform working operation
-  grid->addOntoWindow(begin, end, values, +1.);
-
-  // check integral to be one and half times the old value
-  CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, grid->integral() );
+
+  // create another window from (.5,.5,.5) to (1., 1., 1.)
+  double offset[3];
+  for (offset[0] = 0.; offset[0] <= .5; offset[0] += .5)
+    for (offset[1] = 0.; offset[1] <= .5; offset[1] += .5)
+      for (offset[2] = 0.; offset[2] <= .5; offset[2] += .5) {
+        const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
+        const double window_end[3] = { .5+offset[0], .5+offset[1], .5+offset[2]};
+
+        SamplingGrid newgrid(*grid);
+        // now perform working operation
+        newgrid.addOntoWindow(window_begin, window_end, values, +1.);
+
+        // check integral to be one and one eighth times the old value
+        CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, newgrid.integral() );
+      }
 }
 
@@ -205,7 +260,8 @@
 void SamplingGridTest::addOntoWindow_asymmetric_Test()
 {
+  const size_t size = grid->end[0]-grid->begin[0];
   // check with asymmetric values
   grid->sampled_grid.clear();
-  grid->sampled_grid.resize(NUMBEROFSAMPLES(2), 0.);
+  grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
 
   for (size_t i=0;i<grid->level*(grid->end[0]-grid->begin[0]);++i)
@@ -217,10 +273,9 @@
 
   const double integral = grid->integral();
-  const double begin[3] = { 0., 0., 0. };
-  for(double size=2.; size<=5.; size += 1.) {
-    const double newend[3] = {size, size, size};
-    grid->extendWindow(begin, newend);
-    CPPUNIT_ASSERT_EQUAL( pow(size,3)*integral, grid->integral() );
-  }
+
+  // now perform working operation
+  grid->addOntoWindow(grid->begin, grid->end, values, +1.);
+  // values is equal to integral of 1.
+  CPPUNIT_ASSERT_EQUAL( 1.+integral, grid->integral() );
 }
 
