Skip to content

Commit

Permalink
Refs #11510. Fix unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wedel committed Apr 10, 2015
1 parent a7b9131 commit 0798572
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 112 deletions.
Expand Up @@ -13,149 +13,152 @@ using namespace Mantid::Kernel;
using ::testing::Return;
using ::testing::A;

class BraggScattererInCrystalStructureTest : public CxxTest::TestSuite
{
class BraggScattererInCrystalStructureTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static BraggScattererInCrystalStructureTest *createSuite() { return new BraggScattererInCrystalStructureTest(); }
static void destroySuite( BraggScattererInCrystalStructureTest *suite ) { delete suite; }

static BraggScattererInCrystalStructureTest *createSuite() {
return new BraggScattererInCrystalStructureTest();
}
static void destroySuite(BraggScattererInCrystalStructureTest *suite) {
delete suite;
}

void testInitialization()
{
BraggScattererInCrystalStructure_sptr scatterer = getDefaultScatterer();
void testInitialization() {
BraggScattererInCrystalStructure_sptr scatterer = getDefaultScatterer();

TS_ASSERT(!scatterer->isInitialized());
TS_ASSERT_THROWS_NOTHING(scatterer->initialize());
TS_ASSERT(scatterer->isInitialized());
TS_ASSERT(!scatterer->isInitialized());
TS_ASSERT_THROWS_NOTHING(scatterer->initialize());
TS_ASSERT(scatterer->isInitialized());

TS_ASSERT(scatterer->existsProperty("Position"));
TS_ASSERT(scatterer->existsProperty("UnitCell"));
TS_ASSERT(scatterer->existsProperty("SpaceGroup"));
TS_ASSERT(scatterer->existsProperty("Position"));
TS_ASSERT(scatterer->existsProperty("UnitCell"));
TS_ASSERT(scatterer->existsProperty("SpaceGroup"));
}

void testAfterScattererPropertySet()
{
//BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();
//MockBraggScatterer *mockScatterer = dynamic_cast<MockBraggScatterer *>(scatterer.get());
//EXPECT_CALL(mockScatterer, afterScattererPropertySet)
void testAfterScattererPropertySet() {
// BraggScattererInCrystalStructure_sptr scatterer =
// getInitializedScatterer();
// MockBraggScatterer *mockScatterer = dynamic_cast<MockBraggScatterer
// *>(scatterer.get());
// EXPECT_CALL(mockScatterer, afterScattererPropertySet)
}

void testGetSetPosition()
{
BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();
void testGetSetPosition() {
BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();

V3D goodPosition(0.2, 0.4, 0.3);
TS_ASSERT_THROWS_NOTHING(scatterer->setProperty("Position", goodPosition));
V3D goodPosition(0.2, 0.4, 1./3.);
scatterer->setProperty("Position", "0.2, 0.4, 1/3");

V3D testPos;
TS_ASSERT_THROWS_NOTHING(testPos = scatterer->getPosition());
TS_ASSERT_EQUALS(testPos, goodPosition);
V3D testPos;
TS_ASSERT_THROWS_NOTHING(testPos = scatterer->getPosition());
TS_ASSERT_EQUALS(testPos, goodPosition);

V3D badPosition(1.2, 4.3, -6.2);
TS_ASSERT_THROWS_NOTHING(scatterer->setProperty("Position", badPosition));
TS_ASSERT_THROWS_NOTHING(testPos = scatterer->getPosition());
TS_ASSERT_DIFFERS(testPos, badPosition);
TS_ASSERT_EQUALS(testPos, V3D(0.2, 0.3, 0.8));
V3D badPosition(1.2, 4.3, -6.2);
TS_ASSERT_THROWS_NOTHING(
scatterer->setProperty("Position", "[1.2, 4.3, -6.2]"));
TS_ASSERT_THROWS_NOTHING(testPos = scatterer->getPosition());
TS_ASSERT_DIFFERS(testPos, badPosition);
TS_ASSERT_EQUALS(testPos, V3D(0.2, 0.3, 0.8));
}

void testGetSetCell()
{
BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();
void testGetSetCell() {
BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();

UnitCell cell(5.43, 5.43, 5.43);
UnitCell cell(5.43, 5.43, 5.43);

TS_ASSERT_THROWS_NOTHING(scatterer->setProperty("UnitCell", unitCellToStr(cell)));
TS_ASSERT_EQUALS(scatterer->getCell().getG(), cell.getG());
TS_ASSERT_THROWS_NOTHING(
scatterer->setProperty("UnitCell", unitCellToStr(cell)));
TS_ASSERT_EQUALS(scatterer->getCell().getG(), cell.getG());
}

void testGetSetSpaceGroup()
{
BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();
void testGetSetSpaceGroup() {
BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();

SpaceGroup_const_sptr testGroup = SpaceGroupFactory::Instance().createSpaceGroup("P m -3 m");
SpaceGroup_const_sptr testGroup =
SpaceGroupFactory::Instance().createSpaceGroup("P m -3 m");

TS_ASSERT_THROWS_NOTHING(scatterer->setProperty("SpaceGroup", "P m -3 m"));
TS_ASSERT_EQUALS(scatterer->getSpaceGroup()->hmSymbol(), testGroup->hmSymbol());
TS_ASSERT_THROWS_NOTHING(scatterer->setProperty("SpaceGroup", "P m -3 m"));
TS_ASSERT_EQUALS(scatterer->getSpaceGroup()->hmSymbol(),
testGroup->hmSymbol());
}

void testEquivalentPositions()
{
BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();
void testEquivalentPositions() {
BraggScattererInCrystalStructure_sptr scatterer = getInitializedScatterer();

V3D generalPosition(0.3, 0.32, 0.45);
V3D generalPosition(0.3, 0.32, 0.45);

// No space group set - no equivalent positions
scatterer->setProperty("Position", generalPosition);
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().size(), 1);
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().front(), generalPosition);
// No space group set - no equivalent positions
scatterer->setProperty("Position", "[0.3, 0.32, 0.45]");
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().size(), 1);
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().front(),
generalPosition);

// Assigning a space group must cause recalculation of equivalent positions
SpaceGroup_const_sptr testGroup = SpaceGroupFactory::Instance().createSpaceGroup("P m -3 m");
scatterer->setProperty("SpaceGroup", "P m -3 m");
// Assigning a space group must cause recalculation of equivalent positions
SpaceGroup_const_sptr testGroup =
SpaceGroupFactory::Instance().createSpaceGroup("P m -3 m");
scatterer->setProperty("SpaceGroup", "P m -3 m");

TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().size(), testGroup->order());
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().size(),
testGroup->order());

// Re-setting the position also recalculates
V3D specialPosition(0.0, 0.0, 0.0);
// Re-setting the position also recalculates
V3D specialPosition(0.0, 0.0, 0.0);

scatterer->setProperty("Position", specialPosition);
// Pm-3m does not contain translations, so (0,0,0) is not transformed by any symmetry operation of the group
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().size(), 1);
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().front(), specialPosition);
scatterer->setProperty("Position", "[0, 0, 0]");
// Pm-3m does not contain translations, so (0,0,0) is not transformed by any
// symmetry operation of the group
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().size(), 1);
TS_ASSERT_EQUALS(scatterer->getEquivalentPositions().front(),
specialPosition);
}

void testUnitCellStringValidator()
{
IValidator_sptr validator = boost::make_shared<UnitCellStringValidator>();

// non-working examples
TS_ASSERT_DIFFERS(validator->isValid("1.0"), "");
TS_ASSERT_DIFFERS(validator->isValid("1.0 1.0"), "");
TS_ASSERT_DIFFERS(validator->isValid("1.0 1.0 1.0 1.0"), "");
TS_ASSERT_DIFFERS(validator->isValid("1.0 1.0 1.0 1.0 1.0"), "");
TS_ASSERT_DIFFERS(validator->isValid("1.0.3 1.0 1.0"), "");

// Working examples
TS_ASSERT_EQUALS(validator->isValid("1.0 1.0 1.0"), "");
TS_ASSERT_EQUALS(validator->isValid("1.0 1.0 1.0 90.0 90.0 90.0"), "");
TS_ASSERT_EQUALS(validator->isValid("1 2 3 90 90 90"), "");
TS_ASSERT_EQUALS(validator->isValid("1.1 2.2 3.2 90 90 90"), "");
TS_ASSERT_EQUALS(validator->isValid("1.0 1.0 1.0 90.0 90.0 90.0 "), "");
void testUnitCellStringValidator() {
IValidator_sptr validator = boost::make_shared<UnitCellStringValidator>();

// non-working examples
TS_ASSERT_DIFFERS(validator->isValid("1.0"), "");
TS_ASSERT_DIFFERS(validator->isValid("1.0 1.0"), "");
TS_ASSERT_DIFFERS(validator->isValid("1.0 1.0 1.0 1.0"), "");
TS_ASSERT_DIFFERS(validator->isValid("1.0 1.0 1.0 1.0 1.0"), "");
TS_ASSERT_DIFFERS(validator->isValid("1.0.3 1.0 1.0"), "");

// Working examples
TS_ASSERT_EQUALS(validator->isValid("1.0 1.0 1.0"), "");
TS_ASSERT_EQUALS(validator->isValid("1.0 1.0 1.0 90.0 90.0 90.0"), "");
TS_ASSERT_EQUALS(validator->isValid("1 2 3 90 90 90"), "");
TS_ASSERT_EQUALS(validator->isValid("1.1 2.2 3.2 90 90 90"), "");
TS_ASSERT_EQUALS(validator->isValid("1.0 1.0 1.0 90.0 90.0 90.0 "), "");
}

private:
BraggScattererInCrystalStructure_sptr getDefaultScatterer()
{
boost::shared_ptr<MockBraggScatterer> mockScatterer = boost::make_shared<MockBraggScatterer>();
EXPECT_CALL(*mockScatterer, afterScattererPropertySet(A<const std::string &>()))
.WillRepeatedly(Return());

return mockScatterer;
BraggScattererInCrystalStructure_sptr getDefaultScatterer() {
boost::shared_ptr<MockBraggScatterer> mockScatterer =
boost::make_shared<MockBraggScatterer>();
EXPECT_CALL(*mockScatterer,
afterScattererPropertySet(A<const std::string &>()))
.WillRepeatedly(Return());

return mockScatterer;
}

BraggScattererInCrystalStructure_sptr getInitializedScatterer()
{
BraggScattererInCrystalStructure_sptr raw = getDefaultScatterer();
raw->initialize();
BraggScattererInCrystalStructure_sptr getInitializedScatterer() {
BraggScattererInCrystalStructure_sptr raw = getDefaultScatterer();
raw->initialize();

return raw;
return raw;
}

class MockBraggScatterer : public BraggScattererInCrystalStructure
{
class MockBraggScatterer : public BraggScattererInCrystalStructure {
public:
MockBraggScatterer() : BraggScattererInCrystalStructure() { }
~MockBraggScatterer() { }
MockBraggScatterer() : BraggScattererInCrystalStructure() {}
~MockBraggScatterer() {}

MOCK_CONST_METHOD0(name, std::string());
MOCK_CONST_METHOD0(clone, BraggScatterer_sptr());
MOCK_CONST_METHOD1(calculateStructureFactor, StructureFactor(const V3D&));
MOCK_METHOD1(afterScattererPropertySet, void(const std::string &));
MOCK_CONST_METHOD0(name, std::string());
MOCK_CONST_METHOD0(clone, BraggScatterer_sptr());
MOCK_CONST_METHOD1(calculateStructureFactor, StructureFactor(const V3D &));
MOCK_METHOD1(afterScattererPropertySet, void(const std::string &));
};

};


#endif /* MANTID_GEOMETRY_BRAGGSCATTERERINCRYSTALSTRUCTURETEST_H_ */
Expand Up @@ -34,8 +34,8 @@ class CompositeBraggScattererTest : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING(CompositeBraggScatterer_sptr scatterer = CompositeBraggScatterer::create());

std::vector<BraggScatterer_sptr> scatterers;
scatterers.push_back(getInitializedScatterer("Si", V3D(0.35, 0, 0)));
scatterers.push_back(getInitializedScatterer("Si", V3D(0.25, 0.25, 0.25)));
scatterers.push_back(getInitializedScatterer("Si", "0.35, 0, 0"));
scatterers.push_back(getInitializedScatterer("Si", "1/4, 1/4, 1/4"));

CompositeBraggScatterer_sptr scatterer = CompositeBraggScatterer::create(scatterers);
TS_ASSERT_EQUALS(scatterer->nScatterers(), 2);
Expand Down Expand Up @@ -64,7 +64,7 @@ class CompositeBraggScattererTest : public CxxTest::TestSuite
CompositeBraggScatterer_sptr scatterer = CompositeBraggScatterer::create();
TS_ASSERT_EQUALS(scatterer->propertyCount(), 0);

IsotropicAtomBraggScatterer_sptr siOne = getInitializedScatterer("Si", V3D(0, 0, 0));
IsotropicAtomBraggScatterer_sptr siOne = getInitializedScatterer("Si", "[0, 0, 0]");
TS_ASSERT_DIFFERS(siOne->getSpaceGroup()->hmSymbol(), spaceGroup->hmSymbol());

size_t oldCount = scatterer->nScatterers();
Expand Down Expand Up @@ -128,7 +128,7 @@ class CompositeBraggScattererTest : public CxxTest::TestSuite
SpaceGroup_const_sptr spaceGroup = SpaceGroupFactory::Instance().createSpaceGroup("P 1 2/m 1");

CompositeBraggScatterer_sptr coll = CompositeBraggScatterer::create();
coll->addScatterer(getInitializedScatterer("Si", V3D(0.2, 0.3, 0.4), 0.01267));
coll->addScatterer(getInitializedScatterer("Si", "[0.2, 0.3, 0.4]", 0.01267));

coll->setProperty("SpaceGroup", spaceGroup->hmSymbol());
coll->setProperty("UnitCell", unitCellToStr(cell));
Expand All @@ -146,7 +146,7 @@ class CompositeBraggScattererTest : public CxxTest::TestSuite
}

private:
IsotropicAtomBraggScatterer_sptr getInitializedScatterer(const std::string &element, const V3D &position, double U = 0.0, double occ = 1.0)
IsotropicAtomBraggScatterer_sptr getInitializedScatterer(const std::string &element, const std::string &position, double U = 0.0, double occ = 1.0)
{
IsotropicAtomBraggScatterer_sptr scatterer = boost::make_shared<IsotropicAtomBraggScatterer>();
scatterer->initialize();
Expand All @@ -161,8 +161,8 @@ class CompositeBraggScattererTest : public CxxTest::TestSuite
CompositeBraggScatterer_sptr getCompositeScatterer()
{
std::vector<BraggScatterer_sptr> scatterers;
scatterers.push_back(getInitializedScatterer("Si", V3D(0.35, 0, 0)));
scatterers.push_back(getInitializedScatterer("Si", V3D(0.25, 0.25, 0.25)));
scatterers.push_back(getInitializedScatterer("Si", "[0.35, 0, 0]"));
scatterers.push_back(getInitializedScatterer("Si", "1/4, 1/4, 1/4"));

return CompositeBraggScatterer::create(scatterers);
}
Expand Down
Expand Up @@ -78,7 +78,7 @@ class IsotropicAtomBraggScattererTest : public CxxTest::TestSuite

void testCreate()
{
IsotropicAtomBraggScatterer_sptr isotropic = getInitializedScatterer("Si", V3D(0.3, 0.1, 0.12), 1.0, 0.5);
IsotropicAtomBraggScatterer_sptr isotropic = getInitializedScatterer("Si", "[0.3, 0.1, 0.12]", 1.0, 0.5);

TS_ASSERT(isotropic);
TS_ASSERT_EQUALS(isotropic->getElement(), "Si");
Expand All @@ -92,7 +92,7 @@ class IsotropicAtomBraggScattererTest : public CxxTest::TestSuite
UnitCell cell(5.43, 5.43, 5.43);
SpaceGroup_const_sptr spaceGroup = SpaceGroupFactory::Instance().createSpaceGroup("P m -3 m");

IsotropicAtomBraggScatterer_sptr scatterer = getInitializedScatterer("H", V3D(1.0, 0, 0), 0.0);
IsotropicAtomBraggScatterer_sptr scatterer = getInitializedScatterer("H", "[1, 0, 0]", 0.0);
scatterer->setProperty("U", 3.04);
scatterer->setProperty("Occupancy", 0.5);
scatterer->setProperty("UnitCell", unitCellToStr(cell));
Expand All @@ -116,7 +116,7 @@ class IsotropicAtomBraggScattererTest : public CxxTest::TestSuite

void testCalculateStructureFactor()
{
IsotropicAtomBraggScatterer_sptr scatterer = getInitializedScatterer("Si", V3D(0.0, 0.0, 0.0), 0.0);
IsotropicAtomBraggScatterer_sptr scatterer = getInitializedScatterer("Si", "0, 0, 0", 0.0);

double bSi = scatterer->getNeutronAtom().coh_scatt_length_real;

Expand Down Expand Up @@ -192,7 +192,7 @@ class IsotropicAtomBraggScattererTest : public CxxTest::TestSuite
return scatterer;
}

IsotropicAtomBraggScatterer_sptr getInitializedScatterer(const std::string &element, const V3D &position, double U = 0.0, double occ = 1.0)
IsotropicAtomBraggScatterer_sptr getInitializedScatterer(const std::string &element, const std::string &position, double U = 0.0, double occ = 1.0)
{
IsotropicAtomBraggScatterer_sptr scatterer = getInitializedScatterer();

Expand Down

0 comments on commit 0798572

Please sign in to comment.