Add hash functions for chrono objects
This commit is contained in:
parent
5679460302
commit
c728f0ffe0
|
@ -486,4 +486,14 @@ inline DateTime &DateTime::operator -=(const TimeSpan &timeSpan)
|
|||
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template<> struct hash<ChronoUtilities::DateTime>
|
||||
{
|
||||
inline size_t operator()(const ChronoUtilities::DateTime &dateTime) const
|
||||
{
|
||||
return hash<decltype (dateTime.totalTicks())>()(dateTime.totalTicks());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // CHRONO_UTILITIES_DATETIME_H
|
||||
|
|
|
@ -363,4 +363,14 @@ constexpr inline bool TimeSpan::isInfinity() const
|
|||
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template<> struct hash<ChronoUtilities::TimeSpan>
|
||||
{
|
||||
inline size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
|
||||
{
|
||||
return hash<decltype (timeSpan.totalTicks())>()(timeSpan.totalTicks());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // CHRONO_UTILITIES_TIMESPAN_H
|
||||
|
|
|
@ -24,6 +24,7 @@ class ChronoTests : public TestFixture
|
|||
CPPUNIT_TEST(testDateTime);
|
||||
CPPUNIT_TEST(testTimeSpan);
|
||||
CPPUNIT_TEST(testOperators);
|
||||
CPPUNIT_TEST(testHashing);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
|
@ -33,6 +34,7 @@ public:
|
|||
void testDateTime();
|
||||
void testTimeSpan();
|
||||
void testOperators();
|
||||
void testHashing();
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(ChronoTests);
|
||||
|
@ -116,3 +118,21 @@ void ChronoTests::testOperators()
|
|||
CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
|
||||
CPPUNIT_ASSERT_EQUAL(2, Period(dateTime, dateTime + TimeSpan::fromDays(62)).months());
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Tests hashing DateTime / TimeSpan by using in a set.
|
||||
*/
|
||||
void ChronoTests::testHashing()
|
||||
{
|
||||
set<DateTime> dateTimes;
|
||||
dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
|
||||
dateTimes.emplace(DateTime::fromDate(2500, 2, 2));
|
||||
dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
|
||||
CPPUNIT_ASSERT_EQUAL(2ul, dateTimes.size());
|
||||
|
||||
set<TimeSpan> timeSpans;
|
||||
timeSpans.emplace(TimeSpan::fromDays(5));
|
||||
timeSpans.emplace(TimeSpan::fromDays(10));
|
||||
timeSpans.emplace(TimeSpan::fromDays(5));
|
||||
CPPUNIT_ASSERT_EQUAL(2ul, timeSpans.size());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue