Skip to content

Commit

Permalink
Refs #4647 fix tests, handle groups of size 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik Zikovsky committed Jan 25, 2012
1 parent 65b63e5 commit 1cadfc8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
37 changes: 19 additions & 18 deletions Code/Mantid/Framework/API/src/Algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,6 @@ namespace Mantid
// Unroll the groups or single inputs into vectors of workspace
m_groups.clear();
m_groupWorkspaces.clear();
// Count the number of groups that are bigger than one
size_t numGroupsMoreThanOne = 0;
for (size_t i=0; i<m_inputWorkspaceProps.size(); i++)
{
Property * prop = dynamic_cast<Property *>(m_inputWorkspaceProps[i]);
Expand Down Expand Up @@ -933,15 +931,13 @@ namespace Mantid
}
else
{
// "group" with only one member
// Single Workspace. Treat it as a "group" with only one member
if (ws)
thisGroup.push_back(ws);
}

// Add to the list of groups
m_groups.push_back(thisGroup);
if (thisGroup.size() > 1)
numGroupsMoreThanOne++;
m_groupWorkspaces.push_back(wsGroup);
}

Expand All @@ -953,31 +949,36 @@ namespace Mantid
// Index of the single group
m_singleGroup = -1;
// Size of the single or of all the groups
m_groupSize = 0;
m_groupSize = 1;
m_groupsHaveSimilarNames = true;
for (size_t i=0; i<m_groups.size(); i++)
{
std::vector<Workspace_sptr> & thisGroup = m_groups[i];
// We're ok with empty groups if the workspace property is optional
if (thisGroup.size() == 0 && !m_inputWorkspaceProps[i]->isOptional())
throw std::invalid_argument("Empty group passed as input");
if (thisGroup.size() > 1)
if (thisGroup.size() >= 1)
{
// Record the index of the single group.
if (numGroupsMoreThanOne == 1)
WorkspaceGroup_sptr wsGroup = m_groupWorkspaces[i];
if (wsGroup && (numGroups == 1))
m_singleGroup = int(i);
// Check for matching group size
if (m_groupSize > 0)
if (thisGroup.size() != m_groupSize)
throw std::invalid_argument("Input WorkspaceGroups are not of the same size.");

// Save the size for the next group
m_groupSize = thisGroup.size();
// For actual groups (>1 members)
if (thisGroup.size() > 1)
{
// Check for matching group size
if (m_groupSize > 1)
if (thisGroup.size() != m_groupSize)
throw std::invalid_argument("Input WorkspaceGroups are not of the same size.");

// Are ALL the names similar?
WorkspaceGroup_sptr wsGroup = m_groupWorkspaces[i];
if (wsGroup)
m_groupsHaveSimilarNames = m_groupsHaveSimilarNames && wsGroup->areNamesSimilar();
// Are ALL the names similar?
if (wsGroup)
m_groupsHaveSimilarNames = m_groupsHaveSimilarNames && wsGroup->areNamesSimilar();

// Save the size for the next group
m_groupSize = thisGroup.size();
}
}
} // end for each group

Expand Down
35 changes: 29 additions & 6 deletions Code/Mantid/Framework/API/test/AlgorithmTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ class AlgorithmTest : public CxxTest::TestSuite

std::vector<std::string> names;
boost::split( names, contents1, boost::algorithm::detail::is_any_ofF<char>(","));
if (names.size() > 1)
if (names.size() >= 1)
{
WorkspaceGroup_sptr wsGroup = WorkspaceGroup_sptr(new WorkspaceGroup());
std::vector<std::string>::iterator it = names.begin();
Expand All @@ -432,7 +432,8 @@ class AlgorithmTest : public CxxTest::TestSuite
std::string group1, std::string contents1,
std::string group2, std::string contents2,
std::string group3, std::string contents3,
bool expectFail = false
bool expectFail = false,
int expectedNumber = 3
)
{
makeWorkspaceGroup(group1, contents1);
Expand All @@ -458,13 +459,13 @@ class AlgorithmTest : public CxxTest::TestSuite
WorkspaceGroup_sptr group = boost::dynamic_pointer_cast<WorkspaceGroup>(out1);

TS_ASSERT_EQUALS( group->name(), "D" )
TS_ASSERT_EQUALS( group->getNumberOfEntries(), 3 )
if (group->getNumberOfEntries()!=3) return group;

TS_ASSERT_EQUALS( group->getNumberOfEntries(), expectedNumber )
if (group->getNumberOfEntries() < 1) return group;
ws1 = boost::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(0));
if (group->getNumberOfEntries() < 2) return group;
ws2 = boost::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(1));
if (group->getNumberOfEntries() < 3) return group;
ws3 = boost::dynamic_pointer_cast<MatrixWorkspace>(group->getItem(2));

return group;
}

Expand Down Expand Up @@ -551,6 +552,28 @@ class AlgorithmTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS( ws3->getTitle(), "A_3++C_3" );
}

/// One input is a group with only one member (not possible via GUI)
void test_processGroups_onlyOneGroup_withOnlyOneMember()
{
WorkspaceGroup_sptr group = do_test_groups("A", "A_1",
"B", "", "C", "", false, 1);

TS_ASSERT_EQUALS( ws1->name(), "D_1" );
TS_ASSERT_EQUALS( ws1->getTitle(), "A_1+B+C" );
TS_ASSERT_EQUALS( ws1->readY(0)[0], 234 );
}

/// Two inputs are groups with one member (each)
void test_processGroups_twoGroup_withOnlyOneMember()
{
WorkspaceGroup_sptr group = do_test_groups("A", "A_1",
"B", "B_1", "C", "", false, 1);

TS_ASSERT_EQUALS( ws1->name(), "D_1" );
TS_ASSERT_EQUALS( ws1->getTitle(), "A_1+B_1+C" );
TS_ASSERT_EQUALS( ws1->readY(0)[0], 234 );
}



private:
Expand Down
13 changes: 10 additions & 3 deletions Code/Mantid/Framework/Algorithms/test/CheckWorkspacesMatchTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,15 @@ class CheckWorkspacesMatchTest : public CxxTest::TestSuite
cleanupGroup(group);
}

void test_Input_With_Two_Groups_That_Are_Different_Sizes_Gives_Size_Mismatch()
void test_Input_With_Two_Groups_That_Are_Different_Sizes_Fails()
{
// Create a group
const std::string groupOneName("TestGroupOne");
WorkspaceGroup_sptr groupOne = WorkspaceCreationHelper::CreateWorkspaceGroup(2, 2, 2, groupOneName);
const std::string groupTwoName("TestGroupTwo");
WorkspaceGroup_sptr groupTwo = WorkspaceCreationHelper::CreateWorkspaceGroup(3, 2, 2, groupTwoName);

doGroupTest(groupOneName, groupTwoName, "GroupWorkspaces size mismatch.");
doGroupTest(groupOneName, groupTwoName, "GroupWorkspaces size mismatch.", std::map<std::string,std::string>(), true);

cleanupGroup(groupOne);
cleanupGroup(groupTwo);
Expand Down Expand Up @@ -478,7 +478,9 @@ class CheckWorkspacesMatchTest : public CxxTest::TestSuite

void doGroupTest(const std::string & inputWSOne, const std::string & inputWSTwo,
const std::string & expectedResult,
const std::map<std::string,std::string> & otherProps = std::map<std::string,std::string>())
const std::map<std::string,std::string> & otherProps = std::map<std::string,std::string>(),
bool expectFail = false
)
{
Mantid::Algorithms::CheckWorkspacesMatch matcher;
matcher.initialize();
Expand All @@ -492,6 +494,11 @@ class CheckWorkspacesMatchTest : public CxxTest::TestSuite
}

TS_ASSERT_THROWS_NOTHING(matcher.execute());
if (expectFail)
{
TS_ASSERT_EQUALS(matcher.isExecuted(), false);
return;
}
TS_ASSERT_EQUALS(matcher.isExecuted(), true);
TS_ASSERT_EQUALS(matcher.getPropertyValue("Result"), expectedResult);
}
Expand Down
8 changes: 4 additions & 4 deletions Code/Mantid/Framework/Algorithms/test/WorkspaceGroupTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,13 @@ class WorkspaceGroupTest: public CxxTest::TestSuite
WorkspaceGroup_sptr work_out;
TS_ASSERT_THROWS_NOTHING(work_out = boost::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("test_out")));
MatrixWorkspace_sptr work_out1;
TS_ASSERT_THROWS_NOTHING(work_out1= boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("testlhs_in1_testrhs_in_1_test_out")));
TS_ASSERT_THROWS_NOTHING(work_out1= boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("test_out_1")));
MatrixWorkspace_sptr work_out2;
TS_ASSERT_THROWS_NOTHING(work_out2= boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("testlhs_in1_testrhs_in_2_test_out")));
TS_ASSERT_THROWS_NOTHING(work_out2= boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("test_out_2")));
MatrixWorkspace_sptr work_out3;
TS_ASSERT_THROWS_NOTHING(work_out3= boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("testlhs_in1_testrhs_in_3_test_out")));
TS_ASSERT_THROWS_NOTHING(work_out3= boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("test_out_3")));
MatrixWorkspace_sptr work_out4;
TS_ASSERT_THROWS_NOTHING(work_out4= boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("testlhs_in1_testrhs_in_4_test_out")));
TS_ASSERT_THROWS_NOTHING(work_out4= boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("test_out_4")));

checkData(worklhs_in1, workrhs_in1, work_out1);
checkData(worklhs_in1, workrhs_in2, work_out2);
Expand Down

0 comments on commit 1cadfc8

Please sign in to comment.