From bff34baf019261d4df4b80b21a1f9d305fe7d127 Mon Sep 17 00:00:00 2001
From: Theodoros Katzalis <thodkatz@gmail.com>
Date: Fri, 15 Mar 2019 17:40:28 +0200
Subject: [PATCH] Added tests for the new functions

---
 inc/Helpers/TimeHelper.hpp  |   4 +-
 src/Helpers/TimeHelper.cpp  |  48 +++++------
 test/Helpers/TimeHelper.cpp | 155 +++++++++++++++++++++++++++++++++++-
 3 files changed, 180 insertions(+), 27 deletions(-)

diff --git a/inc/Helpers/TimeHelper.hpp b/inc/Helpers/TimeHelper.hpp
index c337585f..051464b2 100644
--- a/inc/Helpers/TimeHelper.hpp
+++ b/inc/Helpers/TimeHelper.hpp
@@ -92,7 +92,7 @@ public:
 	 * @return true if \p Now is greater than \p DateExamined. The equality depends on the \p
 	 * equalCondition
 	 */
-	bool IsAfter(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition);
+	static bool IsAfter(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition);
 
 	/**
 	 * @param Now the date provided from the Real Time Clock(UTC format)
@@ -103,7 +103,7 @@ public:
 	 * @return true if \p Now is smaller than \p DateExamined. The equality depends on the \p
 	 * equalCondition
 	 */
-	bool IsBefore(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition);
+	static bool IsBefore(struct TimeAndDate &Now, struct TimeAndDate &Date, bool equalCondition);
 
 	/**
 	 * Generate the CDS time format(3.3 in CCSDS 301.0-B-4 standard).
diff --git a/src/Helpers/TimeHelper.cpp b/src/Helpers/TimeHelper.cpp
index 2a04f4be..6bf5fef9 100644
--- a/src/Helpers/TimeHelper.cpp
+++ b/src/Helpers/TimeHelper.cpp
@@ -82,44 +82,44 @@ struct TimeAndDate TimeHelper::utcTime(uint32_t seconds) {
 bool TimeHelper::IsAfter(struct TimeAndDate &Now, struct TimeAndDate &Date, bool
 equalCondition) {
 	// compare years
-	if (Now.year > Date.year) {
+	if (Now.year < Date.year) {
 		return true;
-	} else if (Now.year < Date.year) {
+	} else if (Now.year > Date.year) {
 		return false;
 	}
 
 	// compare months
-	if (Now.month > Date.month) {
+	if (Now.month < Date.month) {
 		return true;
-	} else if (Now.month < Date.month) {
+	} else if (Now.month > Date.month) {
 		return false;
 	}
 
 	// compare days
-	if (Now.day > Date.day) {
+	if (Now.day < Date.day) {
 		return true;
-	} else if (Now.day < Date.day) {
+	} else if (Now.day > Date.day) {
 		return false;
 	}
 
 	// compare hours
-	if (Now.hour > Date.hour) {
+	if (Now.hour < Date.hour) {
 		return true;
-	} else if (Now.hour < Date.hour) {
+	} else if (Now.hour > Date.hour) {
 		return false;
 	}
 
 	// compare minutes
-	if (Now.minute > Date.minute) {
+	if (Now.minute < Date.minute) {
 		return true;
-	} else if (Now.minute < Date.minute) {
+	} else if (Now.minute > Date.minute) {
 		return false;
 	}
 
 	// compare seconds
-	if (Now.second > Date.second) {
+	if (Now.second < Date.second) {
 		return true;
-	} else if (Now.second < Date.second) {
+	} else if (Now.second > Date.second) {
 		return false;
 	} else if (Now.second == Date.second)
 		return equalCondition;
@@ -128,44 +128,44 @@ equalCondition) {
 bool TimeHelper::IsBefore(struct TimeAndDate &Now, struct TimeAndDate &Date, bool
 equalCondition) {
 	// compare years
-	if (Now.year < Date.year) {
+	if (Now.year > Date.year) {
 		return true;
-	} else if (Now.year > Date.year) {
+	} else if (Now.year < Date.year) {
 		return false;
 	}
 
 	// compare months
-	if (Now.month < Date.month) {
+	if (Now.month > Date.month) {
 		return true;
-	} else if (Now.month > Date.month) {
+	} else if (Now.month < Date.month) {
 		return false;
 	}
 
 	// compare days
-	if (Now.day < Date.day) {
+	if (Now.day > Date.day) {
 		return true;
-	} else if (Now.day > Date.day) {
+	} else if (Now.day < Date.day) {
 		return false;
 	}
 
 	// compare hours
-	if (Now.hour < Date.hour) {
+	if (Now.hour > Date.hour) {
 		return true;
-	} else if (Now.hour > Date.hour) {
+	} else if (Now.hour < Date.hour) {
 		return false;
 	}
 
 	// compare minutes
-	if (Now.minute < Date.minute) {
+	if (Now.minute > Date.minute) {
 		return true;
-	} else if (Now.minute > Date.minute) {
+	} else if (Now.minute < Date.minute) {
 		return false;
 	}
 
 	// compare seconds
-	if (Now.second < Date.second) {
+	if (Now.second > Date.second) {
 		return true;
-	} else if (Now.second > Date.second) {
+	} else if (Now.second < Date.second) {
 		return false;
 	} else if (Now.second == Date.second)
 		return equalCondition;
diff --git a/test/Helpers/TimeHelper.cpp b/test/Helpers/TimeHelper.cpp
index cc124eae..0d733167 100644
--- a/test/Helpers/TimeHelper.cpp
+++ b/test/Helpers/TimeHelper.cpp
@@ -37,7 +37,7 @@ TEST_CASE("Time format implementation", "[CUC]") {
 		CHECK(TimeHelper::generateCDStimeFormat(TimeInfo) == timeFormat);
 	}
 
-	SECTION("Convert elapsed seconds since Unix epoch to UTC date"){
+	SECTION("Convert elapsed seconds since Unix epoch to UTC date") {
 		uint32_t seconds = 1586513700; // elapsed seconds between 10/04/2020 10:15:00 and Unix epoch
 
 		TimeHelper time;
@@ -121,4 +121,157 @@ TEST_CASE("Time format implementation", "[CUC]") {
 
 	}
 
+	SECTION("Date comparison") {
+		SECTION("Different year") {
+			struct TimeAndDate Now = {0};
+			// 10/04/2021 10:15:00
+			Now.year = 2021;
+			Now.month = 4;
+			Now.day = 10;
+			Now.hour = 10;
+			Now.minute = 15;
+			Now.second = 0;
+
+			struct TimeAndDate Date = {0};
+			// 10/04/2020 10:15:00
+			Date.year = 2020;
+			Date.month = 4;
+			Date.day = 10;
+			Date.hour = 10;
+			Date.minute = 15;
+			Date.second = 0;
+
+			CHECK(TimeHelper::IsAfter(Now, Date, true) == false);
+			CHECK(TimeHelper::IsAfter(Date, Now, true) == true);
+			CHECK(TimeHelper::IsBefore(Now, Date, true) == true);
+			CHECK(TimeHelper::IsBefore(Date, Now, true) == false);
+		}
+
+		SECTION("Different month") {
+
+			struct TimeAndDate Now = {0};
+			// 10/05/2020 10:15:00
+			Now.year = 2020;
+			Now.month = 5;
+			Now.day = 10;
+			Now.hour = 10;
+			Now.minute = 15;
+			Now.second = 0;
+
+			struct TimeAndDate Date = {0};
+			// 10/04/2020 10:15:00
+			Date.year = 2020;
+			Date.month = 4;
+			Date.day = 10;
+			Date.hour = 10;
+			Date.minute = 15;
+			Date.second = 0;
+
+			CHECK(TimeHelper::IsAfter(Now, Date, true) == false);
+			CHECK(TimeHelper::IsAfter(Date, Now, true) == true);
+			CHECK(TimeHelper::IsBefore(Now, Date, true) == true);
+			CHECK(TimeHelper::IsBefore(Date, Now, true) == false);
+		}
+
+		SECTION("Different day") {
+			struct TimeAndDate Now = {0};
+			// 11/04/2020 10:15:00
+			Now.year = 2020;
+			Now.month = 5;
+			Now.day = 11;
+			Now.hour = 10;
+			Now.minute = 15;
+			Now.second = 0;
+
+			struct TimeAndDate Date = {0};
+			// 10/04/2020 10:15:00
+			Date.year = 2020;
+			Date.month = 4;
+			Date.day = 10;
+			Date.hour = 10;
+			Date.minute = 15;
+			Date.second = 0;
+
+			CHECK(TimeHelper::IsAfter(Now, Date, true) == false);
+			CHECK(TimeHelper::IsAfter(Date, Now, true) == true);
+			CHECK(TimeHelper::IsBefore(Now, Date, true) == true);
+			CHECK(TimeHelper::IsBefore(Date, Now, true) == false);
+		}
+
+		SECTION("Different hour") {
+			struct TimeAndDate Now = {0};
+			// 10/04/2020 11:15:00
+			Now.year = 2020;
+			Now.month = 4;
+			Now.day = 10;
+			Now.hour = 11;
+			Now.minute = 15;
+			Now.second = 0;
+
+			struct TimeAndDate Date = {0};
+			// 10/04/2020 10:15:00
+			Date.year = 2020;
+			Date.month = 4;
+			Date.day = 10;
+			Date.hour = 10;
+			Date.minute = 15;
+			Date.second = 0;
+
+			CHECK(TimeHelper::IsAfter(Now, Date, true) == false);
+			CHECK(TimeHelper::IsAfter(Date, Now, true) == true);
+			CHECK(TimeHelper::IsBefore(Now, Date, true) == true);
+			CHECK(TimeHelper::IsBefore(Date, Now, true) == false);
+		}
+
+		SECTION("Different minute") {
+			struct TimeAndDate Now = {0};
+			// 11/04/2020 10:16:00
+			Now.year = 2020;
+			Now.month = 4;
+			Now.day = 10;
+			Now.hour = 10;
+			Now.minute = 16;
+			Now.second = 0;
+
+			struct TimeAndDate Date = {0};
+			// 10/04/2020 10:15:00
+			Date.year = 2020;
+			Date.month = 4;
+			Date.day = 10;
+			Date.hour = 10;
+			Date.minute = 15;
+			Date.second = 0;
+
+			CHECK(TimeHelper::IsAfter(Now, Date, true) == false);
+			CHECK(TimeHelper::IsAfter(Date, Now, true) == true);
+			CHECK(TimeHelper::IsBefore(Now, Date, true) == true);
+			CHECK(TimeHelper::IsBefore(Date, Now, true) == false);
+		}
+
+		SECTION("Different second") {
+			struct TimeAndDate Now = {0};
+			// 11/04/2020 10:15:01
+			Now.year = 2020;
+			Now.month = 4;
+			Now.day = 10;
+			Now.hour = 10;
+			Now.minute = 15;
+			Now.second = 1;
+
+			struct TimeAndDate Date = {0};
+			// 10/04/2020 10:15:00
+			Date.year = 2020;
+			Date.month = 4;
+			Date.day = 10;
+			Date.hour = 10;
+			Date.minute = 15;
+			Date.second = 0;
+
+			CHECK(TimeHelper::IsAfter(Now, Date, true) == false);
+			CHECK(TimeHelper::IsAfter(Date, Now, true) == true);
+			CHECK(TimeHelper::IsBefore(Now, Date, true) == true);
+			CHECK(TimeHelper::IsBefore(Date, Now, true) == false);
+		}
+	}
+
 }
-- 
GitLab