diff --git a/apps/statistics/Makefile b/apps/statistics/Makefile index 0caf06634..8fcdc7d19 100644 --- a/apps/statistics/Makefile +++ b/apps/statistics/Makefile @@ -31,4 +31,9 @@ i18n_files += $(addprefix apps/statistics/,\ base.pt.i18n\ ) +tests += $(addprefix apps/statistics/test/,\ + store.cpp\ +) +test_objs += $(addprefix apps/statistics/, store.o) + app_images += apps/statistics/stat_icon.png diff --git a/apps/statistics/test/store.cpp b/apps/statistics/test/store.cpp new file mode 100644 index 000000000..486bc15ae --- /dev/null +++ b/apps/statistics/test/store.cpp @@ -0,0 +1,207 @@ +#include +#include +#include +#include "../store.h" + +namespace Statistics { + +void assert_value_approximately_equal_to(double d1, double d2) { + assert(d1 == d2 || fabs(d1-d2) < 0.001); +} + +void assert_data_statictics_equal_to(double n[], double v[], int numberOfData, double sumOfOccurrences, double maxValue, double minValue, double range, double mean, double variance, double standardDeviation, double sampleStandardDeviation, double firstQuartile, double thirdQuartile, double quartileRange, double median, double sum, double squaredValueSum) { + Store store; + int seriesIndex = 0; + + // Set the data in the store + for (int i = 0; i < numberOfData; i++) { + store.set(n[i], seriesIndex, 0, i); + store.set(v[i], seriesIndex, 1, i); + } + + // Compare the statistics + assert_value_approximately_equal_to(standardDeviation * standardDeviation, variance); + assert_value_approximately_equal_to(store.sumOfOccurrences(seriesIndex), sumOfOccurrences); + assert_value_approximately_equal_to(store.maxValue(seriesIndex), maxValue); + assert_value_approximately_equal_to(store.minValue(seriesIndex), minValue); + assert_value_approximately_equal_to(store.range(seriesIndex), range); + assert_value_approximately_equal_to(store.mean(seriesIndex), mean); + assert_value_approximately_equal_to(store.variance(seriesIndex), variance); + assert_value_approximately_equal_to(store.standardDeviation(seriesIndex), standardDeviation); + assert_value_approximately_equal_to(store.sampleStandardDeviation(seriesIndex), sampleStandardDeviation); + assert_value_approximately_equal_to(store.firstQuartile(seriesIndex), firstQuartile); + assert_value_approximately_equal_to(store.thirdQuartile(seriesIndex), thirdQuartile); + assert_value_approximately_equal_to(store.quartileRange(seriesIndex), quartileRange); + assert_value_approximately_equal_to(store.median(seriesIndex), median); + assert_value_approximately_equal_to(store.sum(seriesIndex), sum); + assert_value_approximately_equal_to(store.squaredValueSum(seriesIndex), squaredValueSum); +} + +QUIZ_CASE(data_statistics) { + /* 1 2 3 4 + * 1 1 1 1 */ + double n1[4] = {1.0, 2.0, 3.0, 4.0}; + double v1[4] = {1.0, 1.0, 1.0, 1.0}; + assert_data_statictics_equal_to( + n1, + v1, + 4, + /* sumOfOccurrences */ 4.0, + /* maxValue */ 4.0, + /* minValue */ 1.0, + /* range */ 3.0, + /* mean */ 2.5, + /* variance */ 1.25, + /* standardDeviation */ 1.118, + /* sampleStandardDeviation */ 1.291, + /* firstQuartile */ 1.0, + /* thirdQuartile */ 3.0, + /* quartileRange */ 2.0, + /* median */ 2.5, + /* sum */ 10.0, + /* squaredValueSum */ 30.0); + + + /* 1 2 3 4 5 6 7 8 9 10 11 + * 1 1 1 1 1 1 1 1 1 1 1 */ + + double n2[11] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0}; + double v2[11] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; + assert_data_statictics_equal_to( + n2, + v2, + 11, + /* sumOfOccurrences */ 11.0, + /* maxValue */ 11.0, + /* minValue */ 1.0, + /* range */ 10.0, + /* mean */ 6.0, + /* variance */ 10.0, + /* standardDeviation */ 3.1623, + /* sampleStandardDeviation */ 3.3166, + /* firstQuartile */ 3.0, + /* thirdQuartile */ 9.0, + /* quartileRange */ 6.0, + /* median */ 6.0, + /* sum */ 66.0, + /* squaredValueSum */ 506.0); + + /* 1 2 3 4 5 6 7 8 9 10 11 12 + * 1 1 1 1 1 1 1 1 1 1 1 1 */ + + double n3[12] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; + double v3[12] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; + assert_data_statictics_equal_to( + n3, + v3, + 12, + /* sumOfOccurrences */ 12.0, + /* maxValue */ 12.0, + /* minValue */ 1.0, + /* range */ 11.0, + /* mean */ 6.5, + /* variance */ 11.917, + /* standardDeviation */ 3.4521, + /* sampleStandardDeviation */ 3.6056, + /* firstQuartile */ 3.0, + /* thirdQuartile */ 9.0, + /* quartileRange */ 6.0, + /* median */ 6.5, + /* sum */ 78.0, + /* squaredValueSum */ 650.0); + + /* 1 2 3 5 10 + * 0.2 0.05 0.3 0.0001 0.4499 */ + double n4[5] = {1.0, 2.0, 3.0, 5.0, 10.0}; + double v4[5] = {0.2, 0.05, 0.3, 0.0001, 0.4499}; + assert_data_statictics_equal_to( + n4, + v4, + 5, + /* sumOfOccurrences */ 1.0, + /* maxValue */ 10.0, + /* minValue */ 1.0, + /* range */ 9.0, + /* mean */ 5.6995, + /* variance */ 15.6082, + /* standardDeviation */ 3.9507, + /* sampleStandardDeviation */ INFINITY, + /* firstQuartile */ 2.0, + /* thirdQuartile */ 10.0, + /* quartileRange */ 8.0, + /* median */ 3.0, + /* sum */ 5.6995, + /* squaredValueSum */ 48.0925); + + /* 1 -2 3 5 10 + * 0.4 0.00005 0.9 0.4 0.5 */ + double n5[5] = {1.0, -2.0, 3.0, 5.0, 10.0}; + double v5[5] = {0.4, 0.00005, 0.9, 0.4, 0.5}; + assert_data_statictics_equal_to( + n5, + v5, + 5, + /* sumOfOccurrences */ 2.2, + /* maxValue */ 10.0, + /* minValue */ -2.0, + /* range */ 12.0, + /* mean */ 4.5908, + /* variance */ 10.06, + /* standardDeviation */ 3.1719, + /* sampleStandardDeviation */ 4.2947, + /* firstQuartile */ 3.0, + /* thirdQuartile */ 5.0, + /* quartileRange */ 2.0, + /* median */ 3.0, + /* sum */ 10.1, + /* squaredValueSum */ 68.500); + + /* -7 -10 12 5 -2 + * 4 5 3 1 9 */ + double n6[6] = {-7.0, -10.0, 1.0, 2.0, 5.0, -2.0}; + double v6[6] = {4.0, 5.0, 3.0, 0.5, 1.0, 9.0}; + assert_data_statictics_equal_to( + n6, + v6, + 6, + /* sumOfOccurrences */ 22.5, + /* maxValue */ 5.0, + /* minValue */ -10.0, + /* range */ 15.0, + /* mean */ -3.8667, + /* variance */ 18.9155, + /* standardDeviation */ 4.3492, + /* sampleStandardDeviation */ 4.4492, + /* firstQuartile */ -7.0, + /* thirdQuartile */ -2.0, + /* quartileRange */ 5.0, + /* median */ -2.0, + /* sum */ -87.0, + /* squaredValueSum */ 762.0); + + /* 1 1 1 10 3 -1 3 + * 1 1 1 0 0 0 1 */ + double n7[7] = {1.0, 1.0, 1.0, 10.0, 3.0, -1.0, 3.0}; + double v7[7] = {1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0}; + assert_data_statictics_equal_to( + n7, + v7, + 7, + /* sumOfOccurrences */ 4.0, + /* maxValue */ 3.0, + /* minValue */ 1.0, + /* range */ 2.0, + /* mean */ 1.5, + /* variance */ 0.75, + /* standardDeviation */ 0.866, + /* sampleStandardDeviation */ 1.0, + /* firstQuartile */ 1.0, + /* thirdQuartile */ 1.0, + /* quartileRange */ 0.0, + /* median */ 1.0, + /* sum */ 6.0, + /* squaredValueSum */ 12.0); + +} + +}