Skip to content
Snippets Groups Projects
Commit 3ec9a185 authored by Owen Arnold's avatar Owen Arnold
Browse files

Sample operator== and operator!=

parent e1a8b461
No related branches found
No related tags found
No related merge requests found
......@@ -116,6 +116,9 @@ public:
/// Delete the oriented lattice
void clearOrientedLattice();
bool operator==(const Sample &other) const;
bool operator!=(const Sample &other) const;
private:
/// The sample name
std::string m_name;
......
......@@ -423,5 +423,37 @@ void Sample::clearOrientedLattice() {
m_lattice.reset(nullptr);
}
}
bool Sample::operator==(const Sample &other) const {
if (m_samples.size() != other.m_samples.size())
return false;
for (size_t i = 0; i < m_samples.size(); ++i) {
if (*m_samples[i] != *other.m_samples[i])
return false;
}
auto compare = [](const auto &a, const auto &b, auto call_on) {
// both null or both not null
if (bool(a) ^ bool(b))
return false;
if (a)
return call_on(a) == call_on(b);
else
return true;
};
return *m_lattice == *other.m_lattice && this->m_name == other.m_name &&
this->m_height == other.m_height && this->m_width == other.m_width &&
this->m_thick == other.m_thick && m_geom_id == other.m_geom_id &&
compare(m_environment, other.m_environment,
[](const auto &x) { return x->name(); }) &&
compare(m_shape, other.m_shape,
[](const auto &x) { return x->shape(); }) &&
compare(m_crystalStructure, other.m_crystalStructure,
[](const auto &x) { return *(x->spaceGroup()); });
}
bool Sample::operator!=(const Sample &other) const {
return !this->operator==(other);
}
} // namespace API
} // namespace Mantid
......@@ -359,4 +359,126 @@ public:
TS_ASSERT(loaded.getName().empty());
}
void test_equal_when_sample_identical() {
Sample a;
Sample b;
TS_ASSERT_EQUALS(a, b);
}
void test_not_equal_when_sample_differs_in_extents() {
Sample a;
auto b = a;
a.setHeight(10);
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
b = a;
a.setWidth(10);
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
b = a;
a.setThickness(10);
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
}
void test_not_equal_when_sample_differs_in_geom_id() {
Sample a;
auto b = a;
TS_ASSERT_EQUALS(a, b);
a.setGeometryFlag(1);
b.setGeometryFlag(2);
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
}
void test_not_equal_when_sample_differs_in_name() {
Sample a;
auto b = a;
b.setName("something");
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
}
void test_not_equal_when_sample_differs_in_environment() {
auto kit1 = std::make_unique<SampleEnvironment>(
"Env", boost::make_shared<const Container>(""));
auto kit2 = std::make_unique<SampleEnvironment>(
"Env2", boost::make_shared<const Container>(""));
// same as kit1
auto kit3 = std::make_unique<SampleEnvironment>(
kit1->name(), boost::make_shared<const Container>(""));
Sample a;
auto b = a;
b.setEnvironment(std::move(kit1));
// A has no environment
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
// A has valid but different same environment
a.setEnvironment(std::move(kit2));
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
// A has valid but different same environment
a.setEnvironment(std::move(kit3));
TS_ASSERT_EQUALS(a, b);
TS_ASSERT(!(a != b));
}
void test_not_equal_when_sample_differs_in_shape() {
IObject_sptr shape1 = ComponentCreationHelper::createCappedCylinder(
0.0127, 1.0, V3D(), V3D(0.0, 1.0, 0.0), "cyl");
IObject_sptr shape2 = ComponentCreationHelper::createCappedCylinder(
0.0137, 1.0, V3D(), V3D(0.0, 0.0, 0.0), "cyl");
Sample a;
auto b = a;
a.setShape(shape1);
// b has no shape
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
// b has different shape
b.setShape(shape2);
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
// b has same shape
b.setShape(IObject_sptr(shape1->clone()));
TS_ASSERT_EQUALS(a, b);
TS_ASSERT(!(a != b));
}
void test_not_equal_when_sample_differs_in_space_group() {
CrystalStructure structure1("3 4 5 90 90 90", "C m m m",
"Fe 0.12 0.23 0.121");
// Same as above
CrystalStructure structure2("3 4 5 90 90 90", "C m m m",
"Fe 0.12 0.23 0.121");
// Different
CrystalStructure structure3("5.431 5.431 5.431", "F d -3 m",
"Si 0 0 0 1.0 0.02");
Sample a;
auto b = a;
// b has no structure
a.setCrystalStructure(structure1);
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
// b has different structure
b.setCrystalStructure(structure3);
TS_ASSERT_DIFFERS(a, b);
TS_ASSERT(!(a == b));
// b has same structure
b = Sample{};
b.setCrystalStructure(structure2);
TS_ASSERT_EQUALS(a, b);
TS_ASSERT(!(a != b));
}
};
......@@ -77,6 +77,8 @@ public:
PointGroup_sptr getPointGroup() const;
Group_const_sptr getSiteSymmetryGroup(const Kernel::V3D &position) const;
bool operator==(const SpaceGroup &other) const;
bool operator!=(const SpaceGroup &other) const;
protected:
size_t m_number;
......
......@@ -114,6 +114,15 @@ Group_const_sptr SpaceGroup::getSiteSymmetryGroup(const V3D &position) const {
return GroupFactory::create<Group>(siteSymmetryOps);
}
bool SpaceGroup::operator==(const SpaceGroup &other) const {
return Group::operator==(other) && this->m_number == other.m_number &&
this->m_hmSymbol == other.hmSymbol();
}
bool SpaceGroup::operator!=(const SpaceGroup &other) const {
return !this->operator==(other);
}
std::ostream &operator<<(std::ostream &stream, const SpaceGroup &self) {
stream << "Space group with Hermann-Mauguin symbol: " << self.hmSymbol();
return stream;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment