Relational operators for class hour.
authorArjen Baart <arjen@andromeda.nl>
Sun, 25 Aug 2019 18:55:59 +0000 (20:55 +0200)
committerArjen Baart <arjen@andromeda.nl>
Sun, 25 Aug 2019 18:55:59 +0000 (20:55 +0200)
src/date.h
test/Makefile.am
test/hour_compare.cpp [new file with mode: 0644]
test/hour_compare.exp [new file with mode: 0644]

index 25f023a..2776e12 100644 (file)
@@ -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 &);
index a7cdd2f..4697b37 100644 (file)
@@ -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 (file)
index 0000000..631e02f
--- /dev/null
@@ -0,0 +1,60 @@
+/*******************************************************
+ *  Unit test for the hour class
+ *
+ * test hour relational operators
+ ******************************************************
+ *
+ */
+
+#include "date.h"
+#include <assert.h>
+
+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 (file)
index 0000000..c2ed7bb
--- /dev/null
@@ -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)