embeddedLibrary/test/test_dataAcquisition.c

107 lines
4.0 KiB
C
Raw Normal View History

2024-11-20 01:17:53 +01:00
// test/test_dataAcquisition.c
#include <assert.h>
#include <math.h>
#include "../include/dataAcquisition.h"
#define NUMBER_OF_SENSORS 5
#define SLIDING_WINDOW_SIZE 10
#define AVERAGE_UNCERTAINTY 0.01
#define STD_UNCERTAINTY 0.01
// Testing the inizialization and the instanciacion of the sensors' number and sliding window size
2024-11-20 01:17:53 +01:00
void test_initializeReadings() {
initializeReadings(NUMBER_OF_SENSORS, SLIDING_WINDOW_SIZE);
assert(getSensorsNumber() == NUMBER_OF_SENSORS);
assert(getSlidingWindowSize() == SLIDING_WINDOW_SIZE);
}
// Testing the logic of add readings to see if the slidinw window is full
void test_addReading() {
for (int sensor = 0; sensor < NUMBER_OF_SENSORS; sensor++) {
for (int value = 1; value <= SLIDING_WINDOW_SIZE; value++) {
addReading(value, sensor);
}
}
assert(isFull(NUMBER_OF_SENSORS-1) == true); // Assuming the last sensor acquired the data
}
// Testing the logic of average methods
void test_averageOnSensor() {
printf("Average on sensor %d: %f\n", NUMBER_OF_SENSORS-1, getAverageOnSensor(NUMBER_OF_SENSORS-1));
float average = getAverageOnSensor(NUMBER_OF_SENSORS-1);
float expected_average = (SLIDING_WINDOW_SIZE + 1) / 2.0;
assert(fabs(average - expected_average) < AVERAGE_UNCERTAINTY);
2024-11-20 01:17:53 +01:00
}
void test_standardDeviationOnSensor() {
printf("Standard deviation on sensor %d: %f\n", NUMBER_OF_SENSORS-1, getStandardDeviationOnSensor(NUMBER_OF_SENSORS-1));
float standard_deviation = getStandardDeviationOnSensor(NUMBER_OF_SENSORS-1);
float expected_standard_deviation = 2.872281323269;
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
}
void test_averageOnAllSensors() {
printf("Average on all sensors: %f\n", getAverageOnAllSensors());
float average = getAverageOnAllSensors();
float expected_average = SLIDING_WINDOW_SIZE;
assert(fabs(average - expected_average) < AVERAGE_UNCERTAINTY);
}
void test_standardDeviationOnAllSensors() {
printf("Standard deviation on all sensors: %f\n", getStandardDeviationOnAllSensors());
float standard_deviation = getStandardDeviationOnAllSensors();
float expected_standard_deviation = 0;
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
}
void test_overallAverage(){
printf("Overall average on all sensors: %f\n", getOverallAverage());
float average = getOverallAverage();
float expected_overall_average = (SLIDING_WINDOW_SIZE + 1) / 2.0;
assert(fabs(average - expected_overall_average) < AVERAGE_UNCERTAINTY);
}
void test_overallStandardDeviation(){
printf("Overall standard deviation: %f\n", getOverallStandardDeviation());
float standard_deviation = getStandardDeviationOnSensor(NUMBER_OF_SENSORS-1);
float expected_standard_deviation = 2.872281323269;
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
}
void test_anomalyDetect(){
float average = getOverallAverage();
float standard_deviation = getOverallStandardDeviation();
anomalyDetect(average, standard_deviation);
printf("Outlier count: %i\n", getOutlierCount());
assert(fabs(getOutlierCount() - 0) < 0.01);
// Adding an outlier
addReading(20, NUMBER_OF_SENSORS-1);
average = getOverallAverage();
standard_deviation = getOverallStandardDeviation();
anomalyDetect(average, standard_deviation);
printf("Outlier count: %i\n", getOutlierCount());
assert(fabs(getOutlierCount() - 1) < 0.01);
}
// TODO: Test all the functions with a normal distribution
// TODO: Evaluate the normal distribution with the anomaly detection
void test_freeReadings() {
assert(freeReadings() == true);
}
2024-11-20 01:17:53 +01:00
int main() {
test_initializeReadings();
test_addReading();
test_averageOnSensor();
test_standardDeviationOnSensor();
test_averageOnAllSensors();
test_standardDeviationOnAllSensors();
test_overallAverage();
test_overallStandardDeviation();
test_anomalyDetect();
test_freeReadings();
2024-11-20 01:17:53 +01:00
return 0;
}