Add comparison operators for GncDate

Note the operands are const GncDate& because the GncDate has no copy
constructor/assigment operator (any other definition would fail
when used with variables of type boost::optional<GncDate>)
This commit is contained in:
Geert Janssens 2017-04-29 18:01:12 +02:00
parent 1a3595cbeb
commit 5070037314
3 changed files with 69 additions and 0 deletions

View File

@ -213,6 +213,12 @@ private:
Date m_greg;
friend GncDateTimeImpl::GncDateTimeImpl(const GncDateImpl&, DayPart);
friend bool operator<(const GncDateImpl&, const GncDateImpl&);
friend bool operator>(const GncDateImpl&, const GncDateImpl&);
friend bool operator==(const GncDateImpl&, const GncDateImpl&);
friend bool operator<=(const GncDateImpl&, const GncDateImpl&);
friend bool operator>=(const GncDateImpl&, const GncDateImpl&);
friend bool operator!=(const GncDateImpl&, const GncDateImpl&);
};
/* Member function definitions for GncDateTimeImpl.
@ -427,6 +433,13 @@ GncDateImpl::format(const char* format) const
return ss.str();
}
bool operator<(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg < b.m_greg; }
bool operator>(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg > b.m_greg; }
bool operator==(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg == b.m_greg; }
bool operator<=(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg <= b.m_greg; }
bool operator>=(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg >= b.m_greg; }
bool operator!=(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg != b.m_greg; }
/* =================== Presentation-class Implementations ====================*/
/* GncDateTime */
@ -519,3 +532,10 @@ GncDate::year_month_day() const
{
return m_impl->year_month_day();
}
bool operator<(const GncDate& a, const GncDate& b) { return *(a.m_impl) < *(b.m_impl); }
bool operator>(const GncDate& a, const GncDate& b) { return *(a.m_impl) > *(b.m_impl); }
bool operator==(const GncDate& a, const GncDate& b) { return *(a.m_impl) == *(b.m_impl); }
bool operator<=(const GncDate& a, const GncDate& b) { return *(a.m_impl) <= *(b.m_impl); }
bool operator>=(const GncDate& a, const GncDate& b) { return *(a.m_impl) >= *(b.m_impl); }
bool operator!=(const GncDate& a, const GncDate& b) { return *(a.m_impl) != *(b.m_impl); }

View File

@ -270,6 +270,23 @@ private:
std::unique_ptr<GncDateImpl> m_impl;
friend GncDateTime::GncDateTime(const GncDate&, DayPart);
friend bool operator<(const GncDate&, const GncDate&);
friend bool operator>(const GncDate&, const GncDate&);
friend bool operator==(const GncDate&, const GncDate&);
friend bool operator<=(const GncDate&, const GncDate&);
friend bool operator>=(const GncDate&, const GncDate&);
friend bool operator!=(const GncDate&, const GncDate&);
};
/**@{
* Standard comparison operators working on GncDate objects.
*/
bool operator<(const GncDate& a, const GncDate& b);
bool operator>(const GncDate& a, const GncDate& b);
bool operator==(const GncDate& a, const GncDate& b);
bool operator<=(const GncDate& a, const GncDate& b);
bool operator>=(const GncDate& a, const GncDate& b);
bool operator!=(const GncDate& a, const GncDate& b);
/**@}*/
#endif // __GNC_DATETIME_HPP__

View File

@ -183,6 +183,38 @@ TEST(gnc_date_constructors, test_str_format_constructor)
}
}
TEST(gnc_date_operators, test_equality)
{
GncDate a(2017, 1, 6);
GncDate b(2017, 1, 6);
GncDate c(2015, 6, 13);
EXPECT_TRUE (a == b);
EXPECT_FALSE (a == c);
EXPECT_TRUE (a != c);
EXPECT_FALSE (a != b);
}
TEST(gnc_date_operators, test_more_less_than)
{
GncDate a(2017, 1, 6);
GncDate b(2017, 1, 6);
GncDate c(2015, 6, 13);
EXPECT_TRUE (a >= b);
EXPECT_TRUE (a <= b);
EXPECT_FALSE (a > b);
EXPECT_FALSE (a < b);
EXPECT_TRUE (a > c);
EXPECT_TRUE (a >= c);
EXPECT_FALSE (a < c);
EXPECT_FALSE (a <= c);
EXPECT_TRUE (c < a);
EXPECT_TRUE (c <= a);
EXPECT_FALSE (c > a);
EXPECT_FALSE (c >= a);
}
TEST(gnc_datetime_constructors, test_default_constructor)
{
GncDateTime atime;