Added coverage tests
- Implemented coverage test with output - Added tests for normal equation
This commit is contained in:
parent
d31bedd63e
commit
5928fd99cc
@ -1,107 +1,239 @@
|
||||
// test/test_dataAcquisition.c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include "../include/dataAcquisition.h"
|
||||
|
||||
#define TEST_NUMBER 18
|
||||
|
||||
#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
|
||||
void test_initializeReadings() {
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
||||
#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);
|
||||
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++) {
|
||||
// Testing the logic of add readings to see if the sliding window is full for uniform distribution
|
||||
void test_addReading_uniform()
|
||||
{
|
||||
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
|
||||
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);
|
||||
// Testing the logic of average methods for uniform distribution
|
||||
void test_averageOnSensor_uniform()
|
||||
{
|
||||
//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);
|
||||
}
|
||||
|
||||
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);
|
||||
void test_standardDeviationOnSensor_uniform()
|
||||
{
|
||||
//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());
|
||||
void test_averageOnAllSensors_uniform()
|
||||
{
|
||||
//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());
|
||||
void test_standardDeviationOnAllSensors_uniform()
|
||||
{
|
||||
//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());
|
||||
void test_overallAverage_uniform()
|
||||
{
|
||||
//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);
|
||||
void test_overallStandardDeviation_uniform()
|
||||
{
|
||||
//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(){
|
||||
void test_anomalyDetect_uniform()
|
||||
{
|
||||
float average = getOverallAverage();
|
||||
float standard_deviation = getOverallStandardDeviation();
|
||||
anomalyDetect(average, standard_deviation);
|
||||
printf("Outlier count: %i\n", getOutlierCount());
|
||||
//printf("Outlier count: %i\n", getOutlierCount());
|
||||
assert(fabs(getOutlierCount() - 0) < 0.01);
|
||||
|
||||
// Adding an outlier
|
||||
addReading(20, NUMBER_OF_SENSORS-1);
|
||||
addReading(20, NUMBER_OF_SENSORS - 1);
|
||||
average = getOverallAverage();
|
||||
standard_deviation = getOverallStandardDeviation();
|
||||
anomalyDetect(average, standard_deviation);
|
||||
printf("Outlier count: %i\n", getOutlierCount());
|
||||
//printf("Outlier count: %i\n", getOutlierCount());
|
||||
assert(fabs(getOutlierCount() - 1) < 0.01);
|
||||
}
|
||||
|
||||
void test_freeReadings()
|
||||
{
|
||||
assert(freeReadings() == true);
|
||||
}
|
||||
|
||||
// TODO: Test all the functions with a normal distribution
|
||||
|
||||
// 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() {
|
||||
test_initializeReadings();
|
||||
test_addReading();
|
||||
test_averageOnSensor();
|
||||
test_standardDeviationOnSensor();
|
||||
test_averageOnAllSensors();
|
||||
test_standardDeviationOnAllSensors();
|
||||
test_overallAverage();
|
||||
test_overallStandardDeviation();
|
||||
test_anomalyDetect();
|
||||
test_freeReadings();
|
||||
int tests_run = 0;
|
||||
int tests_passed = 0;
|
||||
|
||||
printf("=== Test Suite ===\n");
|
||||
|
||||
#define RUN_TEST(test) do { \
|
||||
printf("Progress: %.2f%%, Running %s...", (tests_run / (float)TEST_NUMBER) * 100, #test); \
|
||||
test(); \
|
||||
tests_run++; \
|
||||
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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user