Added coverage tests
- Implemented coverage test with output - Added tests for normal equation
This commit is contained in:
parent
d31bedd63e
commit
5928fd99cc
@ -1,79 +1,98 @@
|
|||||||
// test/test_dataAcquisition.c
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "../include/dataAcquisition.h"
|
#include "../include/dataAcquisition.h"
|
||||||
|
|
||||||
|
#define TEST_NUMBER 18
|
||||||
|
|
||||||
#define NUMBER_OF_SENSORS 5
|
#define NUMBER_OF_SENSORS 5
|
||||||
#define SLIDING_WINDOW_SIZE 10
|
#define SLIDING_WINDOW_SIZE 10
|
||||||
|
|
||||||
#define AVERAGE_UNCERTAINTY 0.01
|
#define AVERAGE_UNCERTAINTY 0.01
|
||||||
#define STD_UNCERTAINTY 0.01
|
#define STD_UNCERTAINTY 0.01
|
||||||
|
|
||||||
// Testing the inizialization and the instanciacion of the sensors' number and sliding window size
|
#define M_PI 3.14159265358979323846
|
||||||
void test_initializeReadings() {
|
|
||||||
|
#define NORMAL_DISTRIBUTION_MEAN 10.0
|
||||||
|
#define NORMAL_DISTRIBUTION_STDDEV 2.0
|
||||||
|
|
||||||
|
// Testing the initialization and the instantiation of the sensors' number and sliding window size for uniform distribution
|
||||||
|
void test_initializeReadings_uniform()
|
||||||
|
{
|
||||||
initializeReadings(NUMBER_OF_SENSORS, SLIDING_WINDOW_SIZE);
|
initializeReadings(NUMBER_OF_SENSORS, SLIDING_WINDOW_SIZE);
|
||||||
assert(getSensorsNumber() == NUMBER_OF_SENSORS);
|
assert(getSensorsNumber() == NUMBER_OF_SENSORS);
|
||||||
assert(getSlidingWindowSize() == SLIDING_WINDOW_SIZE);
|
assert(getSlidingWindowSize() == SLIDING_WINDOW_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing the logic of add readings to see if the slidinw window is full
|
// Testing the logic of add readings to see if the sliding window is full for uniform distribution
|
||||||
void test_addReading() {
|
void test_addReading_uniform()
|
||||||
for (int sensor = 0; sensor < NUMBER_OF_SENSORS; sensor++) {
|
{
|
||||||
for (int value = 1; value <= SLIDING_WINDOW_SIZE; value++) {
|
for (int sensor = 0; sensor < NUMBER_OF_SENSORS; sensor++)
|
||||||
|
{
|
||||||
|
for (int value = 1; value <= SLIDING_WINDOW_SIZE; value++)
|
||||||
|
{
|
||||||
addReading(value, sensor);
|
addReading(value, sensor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert(isFull(NUMBER_OF_SENSORS - 1) == true); // Assuming the last sensor acquired the data
|
assert(isFull(NUMBER_OF_SENSORS - 1) == true); // Assuming the last sensor acquired the data
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing the logic of average methods
|
// Testing the logic of average methods for uniform distribution
|
||||||
void test_averageOnSensor() {
|
void test_averageOnSensor_uniform()
|
||||||
printf("Average on sensor %d: %f\n", NUMBER_OF_SENSORS-1, getAverageOnSensor(NUMBER_OF_SENSORS-1));
|
{
|
||||||
|
//printf("Average on sensor %d: %f\n", NUMBER_OF_SENSORS - 1, getAverageOnSensor(NUMBER_OF_SENSORS - 1));
|
||||||
float average = getAverageOnSensor(NUMBER_OF_SENSORS - 1);
|
float average = getAverageOnSensor(NUMBER_OF_SENSORS - 1);
|
||||||
float expected_average = (SLIDING_WINDOW_SIZE + 1) / 2.0;
|
float expected_average = (SLIDING_WINDOW_SIZE + 1) / 2.0;
|
||||||
assert(fabs(average - expected_average) < AVERAGE_UNCERTAINTY);
|
assert(fabs(average - expected_average) < AVERAGE_UNCERTAINTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_standardDeviationOnSensor() {
|
void test_standardDeviationOnSensor_uniform()
|
||||||
printf("Standard deviation on sensor %d: %f\n", NUMBER_OF_SENSORS-1, getStandardDeviationOnSensor(NUMBER_OF_SENSORS-1));
|
{
|
||||||
|
//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 standard_deviation = getStandardDeviationOnSensor(NUMBER_OF_SENSORS - 1);
|
||||||
float expected_standard_deviation = 2.872281323269;
|
float expected_standard_deviation = 2.872281323269;
|
||||||
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
|
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_averageOnAllSensors() {
|
void test_averageOnAllSensors_uniform()
|
||||||
printf("Average on all sensors: %f\n", getAverageOnAllSensors());
|
{
|
||||||
|
//printf("Average on all sensors: %f\n", getAverageOnAllSensors());
|
||||||
float average = getAverageOnAllSensors();
|
float average = getAverageOnAllSensors();
|
||||||
float expected_average = SLIDING_WINDOW_SIZE;
|
float expected_average = SLIDING_WINDOW_SIZE;
|
||||||
assert(fabs(average - expected_average) < AVERAGE_UNCERTAINTY);
|
assert(fabs(average - expected_average) < AVERAGE_UNCERTAINTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_standardDeviationOnAllSensors() {
|
void test_standardDeviationOnAllSensors_uniform()
|
||||||
printf("Standard deviation on all sensors: %f\n", getStandardDeviationOnAllSensors());
|
{
|
||||||
|
//printf("Standard deviation on all sensors: %f\n", getStandardDeviationOnAllSensors());
|
||||||
float standard_deviation = getStandardDeviationOnAllSensors();
|
float standard_deviation = getStandardDeviationOnAllSensors();
|
||||||
float expected_standard_deviation = 0;
|
float expected_standard_deviation = 0;
|
||||||
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
|
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_overallAverage(){
|
void test_overallAverage_uniform()
|
||||||
printf("Overall average on all sensors: %f\n", getOverallAverage());
|
{
|
||||||
|
//printf("Overall average on all sensors: %f\n", getOverallAverage());
|
||||||
float average = getOverallAverage();
|
float average = getOverallAverage();
|
||||||
float expected_overall_average = (SLIDING_WINDOW_SIZE + 1) / 2.0;
|
float expected_overall_average = (SLIDING_WINDOW_SIZE + 1) / 2.0;
|
||||||
assert(fabs(average - expected_overall_average) < AVERAGE_UNCERTAINTY);
|
assert(fabs(average - expected_overall_average) < AVERAGE_UNCERTAINTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_overallStandardDeviation(){
|
void test_overallStandardDeviation_uniform()
|
||||||
printf("Overall standard deviation: %f\n", getOverallStandardDeviation());
|
{
|
||||||
|
//printf("Overall standard deviation: %f\n", getOverallStandardDeviation());
|
||||||
float standard_deviation = getStandardDeviationOnSensor(NUMBER_OF_SENSORS - 1);
|
float standard_deviation = getStandardDeviationOnSensor(NUMBER_OF_SENSORS - 1);
|
||||||
float expected_standard_deviation = 2.872281323269;
|
float expected_standard_deviation = 2.872281323269;
|
||||||
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
|
assert(fabs(standard_deviation - expected_standard_deviation) < STD_UNCERTAINTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_anomalyDetect(){
|
void test_anomalyDetect_uniform()
|
||||||
|
{
|
||||||
float average = getOverallAverage();
|
float average = getOverallAverage();
|
||||||
float standard_deviation = getOverallStandardDeviation();
|
float standard_deviation = getOverallStandardDeviation();
|
||||||
anomalyDetect(average, standard_deviation);
|
anomalyDetect(average, standard_deviation);
|
||||||
printf("Outlier count: %i\n", getOutlierCount());
|
//printf("Outlier count: %i\n", getOutlierCount());
|
||||||
assert(fabs(getOutlierCount() - 0) < 0.01);
|
assert(fabs(getOutlierCount() - 0) < 0.01);
|
||||||
|
|
||||||
// Adding an outlier
|
// Adding an outlier
|
||||||
@ -81,27 +100,140 @@ void test_anomalyDetect(){
|
|||||||
average = getOverallAverage();
|
average = getOverallAverage();
|
||||||
standard_deviation = getOverallStandardDeviation();
|
standard_deviation = getOverallStandardDeviation();
|
||||||
anomalyDetect(average, standard_deviation);
|
anomalyDetect(average, standard_deviation);
|
||||||
printf("Outlier count: %i\n", getOutlierCount());
|
//printf("Outlier count: %i\n", getOutlierCount());
|
||||||
assert(fabs(getOutlierCount() - 1) < 0.01);
|
assert(fabs(getOutlierCount() - 1) < 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_freeReadings()
|
||||||
|
{
|
||||||
|
assert(freeReadings() == true);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Test all the functions with a normal distribution
|
// TODO: Test all the functions with a normal distribution
|
||||||
|
|
||||||
// TODO: Evaluate the normal distribution with the anomaly detection
|
// TODO: Evaluate the normal distribution with the anomaly detection
|
||||||
void test_freeReadings() {
|
|
||||||
assert(freeReadings() == true);
|
void test_initializeReadings_normal()
|
||||||
|
{
|
||||||
|
initializeReadings(NUMBER_OF_SENSORS, SLIDING_WINDOW_SIZE);
|
||||||
|
assert(getSensorsNumber() == NUMBER_OF_SENSORS);
|
||||||
|
assert(getSlidingWindowSize() == SLIDING_WINDOW_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_addReading_normal()
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int sensor = 0; sensor < NUMBER_OF_SENSORS; sensor++)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < SLIDING_WINDOW_SIZE; i++)
|
||||||
|
{
|
||||||
|
float u1 = (float)rand() / RAND_MAX;
|
||||||
|
float u2 = (float)rand() / RAND_MAX;
|
||||||
|
float z0 = sqrt(-2.0 * log(u1)) * cos(2.0 * M_PI * u2);
|
||||||
|
float value = NORMAL_DISTRIBUTION_MEAN + z0 * NORMAL_DISTRIBUTION_STDDEV;
|
||||||
|
addReading(value, sensor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert(isFull(NUMBER_OF_SENSORS - 1) == true); // Assuming the last sensor acquired the data
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_averageOnSensor_normal()
|
||||||
|
{
|
||||||
|
//printf("Average on sensor %d: %f\n", NUMBER_OF_SENSORS - 1, getAverageOnSensor(NUMBER_OF_SENSORS - 1));
|
||||||
|
float average = getAverageOnSensor(NUMBER_OF_SENSORS - 1);
|
||||||
|
assert(fabs(average - NORMAL_DISTRIBUTION_MEAN) < AVERAGE_UNCERTAINTY * 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_standardDeviationOnSensor_normal()
|
||||||
|
{
|
||||||
|
//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);
|
||||||
|
assert(fabs(standard_deviation - NORMAL_DISTRIBUTION_STDDEV) < STD_UNCERTAINTY * 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_averageOnAllSensors_normal()
|
||||||
|
{
|
||||||
|
//printf("Average on all sensors: %f\n", getAverageOnAllSensors());
|
||||||
|
float average = getAverageOnAllSensors();
|
||||||
|
assert(fabs(average - NORMAL_DISTRIBUTION_MEAN) < AVERAGE_UNCERTAINTY * 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_standardDeviationOnAllSensors_normal()
|
||||||
|
{
|
||||||
|
//printf("Standard deviation on all sensors: %f\n", getStandardDeviationOnAllSensors());
|
||||||
|
float standard_deviation = getStandardDeviationOnAllSensors();
|
||||||
|
assert(fabs(standard_deviation - NORMAL_DISTRIBUTION_STDDEV) < STD_UNCERTAINTY * 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_overallAverage_normal()
|
||||||
|
{
|
||||||
|
//printf("Overall average on all sensors: %f\n", getOverallAverage());
|
||||||
|
float average = getOverallAverage();
|
||||||
|
assert(fabs(average - NORMAL_DISTRIBUTION_MEAN) < AVERAGE_UNCERTAINTY * 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_overallStandardDeviation_normal()
|
||||||
|
{
|
||||||
|
//printf("Overall standard deviation: %f\n", getOverallStandardDeviation());
|
||||||
|
float standard_deviation = getStandardDeviationOnSensor(NUMBER_OF_SENSORS - 1);
|
||||||
|
assert(fabs(standard_deviation - NORMAL_DISTRIBUTION_STDDEV) < STD_UNCERTAINTY * 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_anomalyDetect_normal()
|
||||||
|
{
|
||||||
|
float average = getOverallAverage();
|
||||||
|
float standard_deviation = getOverallStandardDeviation();
|
||||||
|
anomalyDetect(average, standard_deviation);
|
||||||
|
//printf("Outlier count: %i\n", getOutlierCount());
|
||||||
|
assert(fabs(NUMBER_OF_SENSORS * SLIDING_WINDOW_SIZE * 0.05 >= getOutlierCount())); // Assuming 5% of the data is outliers
|
||||||
|
|
||||||
|
// Adding an outlier
|
||||||
|
addReading(NORMAL_DISTRIBUTION_MEAN * 100, NUMBER_OF_SENSORS - 1);
|
||||||
|
average = getOverallAverage();
|
||||||
|
standard_deviation = getOverallStandardDeviation();
|
||||||
|
anomalyDetect(average, standard_deviation);
|
||||||
|
//printf("Outlier count: %i\n", getOutlierCount());
|
||||||
|
assert(fabs(NUMBER_OF_SENSORS * SLIDING_WINDOW_SIZE * 0.05 + 1 >= getOutlierCount())); // Assuming 5% of the data is outliers and adding one more
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
test_initializeReadings();
|
int tests_run = 0;
|
||||||
test_addReading();
|
int tests_passed = 0;
|
||||||
test_averageOnSensor();
|
|
||||||
test_standardDeviationOnSensor();
|
printf("=== Test Suite ===\n");
|
||||||
test_averageOnAllSensors();
|
|
||||||
test_standardDeviationOnAllSensors();
|
#define RUN_TEST(test) do { \
|
||||||
test_overallAverage();
|
printf("Progress: %.2f%%, Running %s...", (tests_run / (float)TEST_NUMBER) * 100, #test); \
|
||||||
test_overallStandardDeviation();
|
test(); \
|
||||||
test_anomalyDetect();
|
tests_run++; \
|
||||||
test_freeReadings();
|
tests_passed++; \
|
||||||
|
printf("OK\n"); \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
srand(42);
|
||||||
|
|
||||||
|
RUN_TEST(test_initializeReadings_uniform);
|
||||||
|
RUN_TEST(test_addReading_uniform);
|
||||||
|
RUN_TEST(test_averageOnSensor_uniform);
|
||||||
|
RUN_TEST(test_standardDeviationOnSensor_uniform);
|
||||||
|
RUN_TEST(test_averageOnAllSensors_uniform);
|
||||||
|
RUN_TEST(test_standardDeviationOnAllSensors_uniform);
|
||||||
|
RUN_TEST(test_overallAverage_uniform);
|
||||||
|
RUN_TEST(test_overallStandardDeviation_uniform);
|
||||||
|
RUN_TEST(test_anomalyDetect_uniform);
|
||||||
|
RUN_TEST(test_addReading_normal);
|
||||||
|
RUN_TEST(test_averageOnSensor_normal);
|
||||||
|
RUN_TEST(test_standardDeviationOnSensor_normal);
|
||||||
|
RUN_TEST(test_averageOnAllSensors_normal);
|
||||||
|
RUN_TEST(test_standardDeviationOnAllSensors_normal);
|
||||||
|
RUN_TEST(test_overallAverage_normal);
|
||||||
|
RUN_TEST(test_overallStandardDeviation_normal);
|
||||||
|
RUN_TEST(test_anomalyDetect_normal);
|
||||||
|
RUN_TEST(test_freeReadings);
|
||||||
|
|
||||||
|
printf("\n=== Results ===\n");
|
||||||
|
printf("Tests run: %d\n", tests_run);
|
||||||
|
printf("Tests passed: %d\n", tests_passed);
|
||||||
|
printf("Test coverage: %.2f%%\n", (tests_passed / (float)tests_run) * 100);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user