From 29157860579563d117d5b1325b2456a46a9d8a5a Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Sun, 25 Aug 2019 20:55:59 +0200 Subject: [PATCH] Relational operators for class hour. --- src/date.h | 25 ++++++++++++++++++ test/Makefile.am | 3 ++- test/hour_compare.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++ test/hour_compare.exp | 13 ++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 test/hour_compare.cpp create mode 100644 test/hour_compare.exp diff --git a/src/date.h b/src/date.h index 25f023a..2776e12 100644 --- a/src/date.h +++ b/src/date.h @@ -289,6 +289,31 @@ public: return compare(h) == 0; } + bool operator!=(hour h) + { + return compare(h) != 0; + } + + bool operator<(hour h) + { + return compare(h) < 0; + } + + bool operator>(hour h) + { + return compare(h) > 0; + } + + bool operator<=(hour h) + { + return compare(h) <= 0; + } + + bool operator>=(hour h) + { + return compare(h) >= 0; + } + friend hour operator+(hour &, hour &); hour operator += (hour h); friend hour operator-(hour &, hour &); diff --git a/test/Makefile.am b/test/Makefile.am index a7cdd2f..4697b37 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,7 +4,7 @@ AM_CPPFLAGS = -I../src LDADD = ../src/.libs/libACL.la check_PROGRAMS = string_assign string_basics string_compare string_cat string_substring string_regex \ date_assign date_parse date_compare date_arithmetic date_attributes date_check_today \ - hour_assign hour_parse + hour_assign hour_parse hour_compare string_assign_SOURCES = string_assign.cpp string_basics_SOURCES = string_basics.cpp @@ -25,3 +25,4 @@ date_today : date_check_today hour_assign_SOURCES = hour_assign.cpp hour_parse_SOURCES = hour_parse.cpp +hour_compare_SOURCES = hour_compare.cpp diff --git a/test/hour_compare.cpp b/test/hour_compare.cpp new file mode 100644 index 0000000..631e02f --- /dev/null +++ b/test/hour_compare.cpp @@ -0,0 +1,60 @@ +/******************************************************* + * Unit test for the hour class + * + * test hour relational operators + ****************************************************** + * + */ + +#include "date.h" +#include + +int main() +{ + hour h0(9, 20, 45); + hour h1(9, 20, 45); + hour h2(15,10, 40); + + // equal dates + + std::cout << h0 << " == " << h1 << " = " << (h0 == h1) << "\n"; + assert((h0 == h1) == true); + + std::cout << h0 << " != " << h1 << " = " << (h0 != h1) << "\n"; + assert((h0 != h1) == false); + + std::cout << h0 << " < " << h1 << " = " << (h0 < h1) << "\n"; + assert((h0 < h1) == false); + + std::cout << h0 << " <= " << h1 << " = " << (h0 <= h1) << "\n"; + assert((h0 <= h1) == true); + + std::cout << h0 << " > " << h1 << " = " << (h0 > h1) << "\n"; + assert((h0 > h1) == false); + + std::cout << h0 << " >= " << h1 << " = " << (h0 >= h1) << "\n"; + assert((h0 >= h1) == true); + + // unequal dates + + std::cout << h0 << " == " << h2 << " = " << (h0 == h2) << "\n"; + assert((h0 == h2) == false); + + std::cout << h0 << " != " << h2 << " = " << (h0 != h2) << "\n"; + assert((h0 != h2) == true); + + std::cout << h0 << " < " << h2 << " = " << (h0 < h2) << "\n"; + assert((h0 < h2) == true); + + std::cout << h0 << " <= " << h2 << " = " << (h0 <= h2) << "\n"; + assert((h0 <= h2) == true); + + std::cout << h0 << " > " << h2 << " = " << (h0 > h2) << "\n"; + assert((h0 > h2) == false); + + std::cout << h0 << " >= " << h2 << " = " << (h0 >= h2) << "\n"; + assert((h0 >= h2) == false); + + return 0; +} + diff --git a/test/hour_compare.exp b/test/hour_compare.exp new file mode 100644 index 0000000..c2ed7bb --- /dev/null +++ b/test/hour_compare.exp @@ -0,0 +1,13 @@ +09:20:45 == 09:20:45 = 1 +09:20:45 != 09:20:45 = 0 +09:20:45 < 09:20:45 = 0 +09:20:45 <= 09:20:45 = 1 +09:20:45 > 09:20:45 = 0 +09:20:45 >= 09:20:45 = 1 +09:20:45 == 15:10:40 = 0 +09:20:45 != 15:10:40 = 1 +09:20:45 < 15:10:40 = 1 +09:20:45 <= 15:10:40 = 1 +09:20:45 > 15:10:40 = 0 +09:20:45 >= 15:10:40 = 0 +PASS hour_compare (exit status: 0) -- 2.20.1