From 0a554d8ad7eb72d2db0cdb15aaaa7c9083411e19 Mon Sep 17 00:00:00 2001
From: athatheo <athatheoc@gmail.com>
Date: Sat, 30 Jul 2022 11:32:24 +0000
Subject: [PATCH] ST[04] report statistics function argument free

---
 inc/Services/ParameterStatisticsService.hpp  | 12 +++-
 src/Services/ParameterStatisticsService.cpp  | 15 ++--
 test/Services/ParameterStatisticsService.cpp | 72 ++++++++++++++++++--
 3 files changed, 87 insertions(+), 12 deletions(-)

diff --git a/inc/Services/ParameterStatisticsService.hpp b/inc/Services/ParameterStatisticsService.hpp
index 4ac9ecbc..ba6d895f 100644
--- a/inc/Services/ParameterStatisticsService.hpp
+++ b/inc/Services/ParameterStatisticsService.hpp
@@ -72,7 +72,7 @@ public:
 	/**
 	 * Returns the periodic statistics reporting status
 	 */
-	 inline bool getPeriodicReportingStatus() {
+	inline bool getPeriodicReportingStatus() {
 		return periodicStatisticsReportingStatus;
 	}
 
@@ -95,6 +95,16 @@ public:
 	 */
 	void reportParameterStatistics(Message& request);
 
+	/**
+	 * Report the parameter statistics, by calling parameterStatisticsReport()
+	 * This is **NOT** the function called by TC. It was created so that this function could be called
+	 * from within a Platform (MCU, x86...) without needing to create a fake TC and pass through multiple functions.
+	 *
+	 * @param reset indicates whether each Statistic should be reset. Simulates the argument contained in the TC[4,1]
+	 * that calls reportParameterStatistics(Message& request)
+	 */
+	void reportParameterStatistics(bool reset);
+
 	/**
 	 * Constructs and stores a TM[4,2] packet containing the parameter statistics report.
 	 */
diff --git a/src/Services/ParameterStatisticsService.cpp b/src/Services/ParameterStatisticsService.cpp
index 5197e9b9..a9336696 100644
--- a/src/Services/ParameterStatisticsService.cpp
+++ b/src/Services/ParameterStatisticsService.cpp
@@ -12,13 +12,16 @@ void ParameterStatisticsService::reportParameterStatistics(Message& request) {
 	request.assertTC(ServiceType, MessageType::ReportParameterStatistics);
 	parameterStatisticsReport();
 
-	if (hasAutomaticStatisticsReset) {
+	if (hasAutomaticStatisticsReset or request.readBoolean()) {
+		resetParameterStatistics();
+	}
+}
+
+void ParameterStatisticsService::reportParameterStatistics(bool reset) {
+	parameterStatisticsReport();
+
+	if (hasAutomaticStatisticsReset or reset) {
 		resetParameterStatistics();
-	} else {
-		bool resetFlagValue = request.readBoolean();
-		if (resetFlagValue) {
-			resetParameterStatistics();
-		}
 	}
 }
 
diff --git a/test/Services/ParameterStatisticsService.cpp b/test/Services/ParameterStatisticsService.cpp
index 1f7d3bba..74a0e81b 100644
--- a/test/Services/ParameterStatisticsService.cpp
+++ b/test/Services/ParameterStatisticsService.cpp
@@ -32,7 +32,7 @@ void resetSystem() {
 }
 
 TEST_CASE("Reporting of statistics") {
-	SECTION("Report statistics, with auto statistic reset disabled") {
+	SECTION("Report statistics, with auto statistic reset disabled with TC") {
 		initializeStatistics(6, 7);
 		Message request = Message(ParameterStatisticsService::ServiceType,
 		                          ParameterStatisticsService::MessageType::ReportParameterStatistics, Message::TC, 1);
@@ -70,7 +70,7 @@ TEST_CASE("Reporting of statistics") {
 		CHECK(not Services.parameterStatistics.statisticsMap[7].statisticsAreInitialized());
 	}
 
-	SECTION("Report statistics, with auto statistics reset enabled") {
+	SECTION("Report statistics, with auto statistics reset enabled with TC") {
 		Message request = Message(ParameterStatisticsService::ServiceType,
 		                          ParameterStatisticsService::MessageType::ReportParameterStatistics, Message::TC, 1);
 		Services.parameterStatistics.hasAutomaticStatisticsReset = true;
@@ -93,13 +93,75 @@ TEST_CASE("Reporting of statistics") {
 
 		MessageParser::execute(request);
 
+		CHECK(Services.parameterStatistics.statisticsMap[5].statisticsAreInitialized());
+		CHECK(Services.parameterStatistics.statisticsMap[7].statisticsAreInitialized());
+	}
+
+	resetSystem();
+	ServiceTests::reset();
+	Services.reset();
+
+	SECTION("Report statistics, with auto statistic reset disabled without TC") {
+		initializeStatistics(6, 7);
+		Services.parameterStatistics.reportParameterStatistics(false);
+		Services.parameterStatistics.hasAutomaticStatisticsReset = false;
+
+		CHECK(ServiceTests::count() == 1);
+
+		Message report = ServiceTests::get(0);
+		CHECK(report.serviceType == ParameterStatisticsService::ServiceType);
+		CHECK(report.messageType == ParameterStatisticsService::MessageType::ParameterStatisticsReport);
+		CHECK(report.readUint64() == 86769000); // start time
+		CHECK(report.readUint64() == 86769000); // end time
+		CHECK(report.readUint16() == 2);        // number of parameters reported
+		// Parameter B
+		CHECK(report.readUint16() == 5);        // ID-2
+		CHECK(report.readUint16() == 6);        // number of samples
+		CHECK(report.readFloat() == 13);        // max value
+		CHECK(report.readUint64() == 86769000); // max time
+		CHECK(report.readFloat() == 3);         // min value
+		CHECK(report.readUint64() == 86769000); // min time
+		CHECK(report.readFloat() == 8);         // mean
+		CHECK(report.readFloat() == Catch::Approx(3.41565).epsilon(0.01));
+		// Parameter A
+		CHECK(report.readUint16() == 7);                  // ID-1
+		CHECK(report.readUint16() == 3);                  // number of samples
+		CHECK(report.readFloat() == 5);                   // max value
+		CHECK(report.readUint64() == 86769000);           // max time
+		CHECK(report.readFloat() == 1);                   // min value
+		CHECK(report.readUint64() == 86769000);           // min time
+		CHECK(report.readFloat() == 3);                   // mean
+		CHECK(static_cast<int>(report.readFloat()) == 1); // stddev
+
+		CHECK(not Services.parameterStatistics.statisticsMap[5].statisticsAreInitialized());
+		CHECK(not Services.parameterStatistics.statisticsMap[7].statisticsAreInitialized());
+	}
+
+	SECTION("Report statistics, with auto statistics reset enabled without TC") {
+		Services.parameterStatistics.hasAutomaticStatisticsReset = true;
+		Services.parameterStatistics.reportParameterStatistics(false);
+
+		CHECK(Services.parameterStatistics.statisticsMap[5].statisticsAreInitialized());
+		CHECK(Services.parameterStatistics.statisticsMap[7].statisticsAreInitialized());
+	}
+
+	SECTION("Report statistics, with auto statistics reset disabled, but reset is given with args, without TC") {
+		Services.parameterStatistics.statisticsMap[5].mean = 5;
+		Services.parameterStatistics.statisticsMap[7].mean = 3;
+		Services.parameterStatistics.hasAutomaticStatisticsReset = false;
+
+		CHECK(not Services.parameterStatistics.statisticsMap[5].statisticsAreInitialized());
+		CHECK(not Services.parameterStatistics.statisticsMap[7].statisticsAreInitialized());
+
+		Services.parameterStatistics.reportParameterStatistics(true);
+
 		CHECK(Services.parameterStatistics.statisticsMap[5].statisticsAreInitialized());
 		CHECK(Services.parameterStatistics.statisticsMap[7].statisticsAreInitialized());
 
-		resetSystem();
-		ServiceTests::reset();
-		Services.reset();
 	}
+	resetSystem();
+	ServiceTests::reset();
+	Services.reset();
 }
 
 TEST_CASE("Resetting the parameter statistics") {
-- 
GitLab