Skip to content

Commit

Permalink
Refs #11469 Added unit tests to box controller
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonPiccardoSelg committed Apr 7, 2015
1 parent 0d4fe7d commit 73075b6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 19 deletions.
29 changes: 26 additions & 3 deletions Code/Mantid/Framework/API/inc/MantidAPI/BoxController.h
Expand Up @@ -33,7 +33,7 @@ class DLLExport BoxController {
* @return BoxController instance
*/
BoxController(size_t nd)
: nd(nd), m_maxId(0), m_SplitThreshold(1024), m_numSplit(1),
: nd(nd), m_maxId(0), m_SplitThreshold(1024), m_numSplit(1), m_numTopSplit(1),
m_fileIO(boost::shared_ptr<API::IBoxControllerIO>()), m_splitTopInto(boost::none) {
// TODO: Smarter ways to determine all of these values
m_maxDepth = 5;
Expand Down Expand Up @@ -170,9 +170,10 @@ class DLLExport BoxController {
"too high of a dimension index.");
// If the vector is not created, then create it
if (!m_splitTopInto) {
m_splitTopInto = std::vector<size_t>(nd);
m_splitTopInto = std::vector<size_t>(nd,1);
}
m_splitTopInto.get()[dim] = num;
calcNumTopSplit();
}

//-----------------------------------------------------------------------------------
Expand Down Expand Up @@ -403,13 +404,32 @@ class DLLExport BoxController {
resetMaxNumBoxes();
}

/// When you split an MDBox by force, it becomes this many sub boxes
void calcNumTopSplit() {
m_numTopSplit = 1;
for (size_t d = 0; d < nd; d++) {
m_numTopSplit *= m_splitTopInto.get()[d];
}
/// And this changes the max # of boxes too
resetMaxNumBoxes();
}

/// Calculate the vector of the max # of MDBoxes per level.
void resetMaxNumBoxes() {
// Now calculate the max # of boxes
m_maxNumMDBoxes.resize(m_maxDepth + 1, 0); // Reset to 0
m_maxNumMDBoxes[0] = 1;
for (size_t depth = 1; depth < m_maxNumMDBoxes.size(); depth++)
m_maxNumMDBoxes[depth] = m_maxNumMDBoxes[depth - 1] * double(m_numSplit);
{
if (depth ==1 && m_splitTopInto)
{
m_maxNumMDBoxes[depth] = m_maxNumMDBoxes[depth - 1] * double(m_numTopSplit);
}
else
{
m_maxNumMDBoxes[depth] = m_maxNumMDBoxes[depth - 1] * double(m_numSplit);
}
}
}

protected:
Expand Down Expand Up @@ -456,6 +476,9 @@ class DLLExport BoxController {
/// When you split a MDBox, it becomes this many sub-boxes
size_t m_numSplit;

/// When you split a top level MDBox by force, it becomes this many sub boxes
size_t m_numTopSplit;

/// For adding events tasks
size_t m_addingEvents_eventsPerTask;

Expand Down
18 changes: 8 additions & 10 deletions Code/Mantid/Framework/API/src/BoxController.cpp
Expand Up @@ -252,20 +252,14 @@ void BoxController::fromXMLString(const std::string &xml) {
// Need to make sure that we handle box controllers which did not have the SplitTopInto
// attribute
Poco::XML::NodeList* nodes = pBoxElement->getElementsByTagName("SplitTopInto");
if (nodes->length() > 0)
{
if (nodes->length() > 0) {
s = pBoxElement->getChildElement("SplitTopInto")->innerText();
if (s.empty())
{
if (s.empty()) {
this->m_splitTopInto = boost::none;
}
else
{
} else {
this->m_splitTopInto = splitStringIntoVector<size_t>(s);
}
}
else
{
} else {
this->m_splitTopInto = boost::none;
}

Expand All @@ -276,6 +270,10 @@ void BoxController::fromXMLString(const std::string &xml) {
this->m_numMDGridBoxes = splitStringIntoVector<size_t>(s);

this->calcNumSplit();

if (m_splitTopInto) {
this->calcNumTopSplit();
}
}
/** function clears the file-backed status of the box controller */
void BoxController::clearFileBacked() {
Expand Down
60 changes: 54 additions & 6 deletions Code/Mantid/Framework/API/test/BoxControllerTest.h
Expand Up @@ -171,6 +171,58 @@ class BoxControllerTest : public CxxTest::TestSuite
doTest_numBoxes(bc, 11);
}

/// Make sure that the correct number of boxes are recorded when we use splitting
void test_trackNumBoxesWithTopLevelSplitting()
{
BoxController bc(2);
bc.setSplitInto(10);

bc.setSplitTopInto(0,4);
bc.setSplitTopInto(1,12);

// This includes a forced top level split and a subsequent split of two boxes
TSM_ASSERT_DELTA("The average depth should be 0", bc.getAverageDepth(), 0.0, 1e-5 );
bc.trackNumBoxes(0);
TSM_ASSERT_DELTA("The average depth should be about 1", bc.getAverageDepth(), 1.0, 1e-5 );

bc.trackNumBoxes(1);
bc.trackNumBoxes(1);

const std::vector<size_t> & num = bc.getNumMDBoxes();
const std::vector<size_t> & numGridBoxes = bc.getNumMDGridBoxes();
TSM_ASSERT_EQUALS("Should be 1 MDGridBox structure at the 0th level", numGridBoxes[0], 1);
TSM_ASSERT_EQUALS("Should be 48 - 2 MDBox structures at the 1st level", num[1], 46);
TSM_ASSERT_EQUALS("Should be 2 MDGridBox structure at the 1st level", numGridBoxes[1], 2);
TSM_ASSERT_EQUALS("Should be 2 * 100 MDBox structures at the 2nd level.", num[2], 200);
}

void test_trackNumBoxesWithTopLevelSplittingAndSettingMaxDepth()
{
BoxController bc(2);

bc.setMaxDepth(4);
bc.setSplitInto(10);

bc.setSplitTopInto(0,4);
bc.setSplitTopInto(1,12);
bc.setMaxDepth(10);

// This includes a forced top level split and a subsequent split of two boxes
TSM_ASSERT_DELTA("The average depth should be 0", bc.getAverageDepth(), 0.0, 1e-5 );
bc.trackNumBoxes(0);
TSM_ASSERT_DELTA("The average depth should be about 1", bc.getAverageDepth(), 1.0, 1e-5 );

bc.trackNumBoxes(1);
bc.trackNumBoxes(1);

const std::vector<size_t> & num = bc.getNumMDBoxes();
const std::vector<size_t> & numGridBoxes = bc.getNumMDGridBoxes();
TSM_ASSERT_EQUALS("Should be 1 MDGridBox structure at the 0th level", numGridBoxes[0], 1);
TSM_ASSERT_EQUALS("Should be 48 - 2 MDBox structures at the 1st level", num[1], 46);
TSM_ASSERT_EQUALS("Should be 2 MDGridBox structure at the 1st level", numGridBoxes[1], 2);
TSM_ASSERT_EQUALS("Should be 2 * 100 MDBox structures at the 2nd level.", num[2], 200);
}

/// Compare two box controllers and assert each part of them.
void compareBoxControllers(BoxController & a, BoxController & b)
{
Expand All @@ -190,6 +242,7 @@ class BoxControllerTest : public CxxTest::TestSuite
TS_ASSERT_DIFFERS(a.getFileIO(),b.getFileIO());
}

// Check for top level splitting
if (a.getSplitTopInto() && b.getSplitTopInto())
{
for (size_t d=0; d < a.getNDims(); d++)
Expand Down Expand Up @@ -236,7 +289,7 @@ class BoxControllerTest : public CxxTest::TestSuite
TS_ASSERT(!xml.empty());

// Read it back
BoxController b(1);
BoxController b(2);
b.fromXMLString(xml);
// Check that it is the same
compareBoxControllers(a, b);
Expand Down Expand Up @@ -291,9 +344,6 @@ class BoxControllerTest : public CxxTest::TestSuite
// Check that settings are the same but BC are different
compareBoxControllers(*a, *b);
TS_ASSERT(b->isFileBacked());



}

void test_MRU_access()
Expand Down Expand Up @@ -337,8 +387,6 @@ class BoxControllerTest : public CxxTest::TestSuite
TS_ASSERT(!a->isFileBacked());
TSM_ASSERT("Box controller should now close the faked file",!pS->isOpened());
}


};

#endif

0 comments on commit 73075b6

Please sign in to comment.