LCOV - code coverage report
Current view: top level - tests/stats - t_describe.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 198 198 100.0 %
Date: 2024-05-13 05:06:06 Functions: 132 132 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /**
       2             :  * Unit tests for the Statistics module's description methods
       3             :  */
       4             : 
       5             : #include <cmath>
       6             : #include <cstdlib>
       7             : #include <gtest/gtest.h>
       8             : #include <iostream>
       9             : #include <openGPMP/stats/describe.hpp>
      10             : #include <stdexcept>
      11             : #include <vector>
      12             : 
      13           4 : TEST(DescribeTest, MeanArithEmptyVector) {
      14             :     gpmp::stats::Describe describe;
      15           1 :     std::vector<double> data;
      16           1 :     EXPECT_TRUE(std::isnan(describe.mean_arith(data)));
      17           1 : }
      18             : 
      19           4 : TEST(DescribeTest, MeanArithSingleElement) {
      20             :     gpmp::stats::Describe describe;
      21           1 :     std::vector<double> data = {5.0};
      22           1 :     EXPECT_DOUBLE_EQ(describe.mean_arith(data), 5.0);
      23           1 : }
      24             : 
      25           4 : TEST(DescribeTest, MeanArithMultipleElements) {
      26             :     gpmp::stats::Describe describe;
      27           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
      28           1 :     EXPECT_DOUBLE_EQ(describe.mean_arith(data), 3.0);
      29           1 : }
      30             : 
      31           4 : TEST(DescribeTest, MeanGeoSingleElement) {
      32             :     gpmp::stats::Describe describe;
      33           1 :     std::vector<double> data = {5.0};
      34           1 :     EXPECT_DOUBLE_EQ(describe.mean_geo(data), 5.0);
      35           1 : }
      36             : 
      37           4 : TEST(DescribeTest, MeanGeoMultipleElements) {
      38             :     gpmp::stats::Describe describe;
      39           1 :     std::vector<double> data = {1.0, 2.0, 4.0};
      40           1 :     EXPECT_DOUBLE_EQ(describe.mean_geo(data), 2.0);
      41           1 : }
      42           4 : TEST(DescribeTest, MeanCubicEmptyVector) {
      43             :     gpmp::stats::Describe describe;
      44           1 :     std::vector<double> data;
      45           1 :     double p = 3.0; // Arbitrary value for p
      46           1 :     EXPECT_TRUE(std::isnan(describe.mean_cubic(data, p)));
      47           1 : }
      48             : 
      49           4 : TEST(DescribeTest, MeanCubicSingleElement) {
      50             :     gpmp::stats::Describe describe;
      51           1 :     std::vector<double> data = {5.0};
      52           1 :     double p = 3.0; // Arbitrary value for p
      53           1 :     EXPECT_DOUBLE_EQ(describe.mean_cubic(data, p), 5.0);
      54           1 : }
      55             : 
      56           4 : TEST(DescribeTest, MeanCubicMultipleElements) {
      57             :     gpmp::stats::Describe describe;
      58           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
      59           1 :     double p = 3.0; // Arbitrary value for p
      60           1 :     double expected = 3.5568933044900626;
      61           1 :     double tolerance = std::numeric_limits<double>::epsilon();
      62           1 :     EXPECT_NEAR(describe.mean_cubic(data, p), expected, tolerance);
      63           1 : }
      64             : 
      65           4 : TEST(DescribeTest, MeanCubicDifferentP) {
      66             :     gpmp::stats::Describe describe;
      67           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
      68           1 :     double p = 2.0; // Different value for p
      69           1 :     double expected = 3.3166247903553998;
      70           1 :     double tolerance = std::numeric_limits<double>::epsilon();
      71           1 :     EXPECT_NEAR(describe.mean_cubic(data, p), expected, tolerance);
      72           1 : }
      73             : 
      74           4 : TEST(DescribeTest, MeanGeoPowSingleElement) {
      75             :     gpmp::stats::Describe describe;
      76           1 :     std::vector<double> data = {5.0};
      77           1 :     double p = 2.0; // Arbitrary value for p
      78           1 :     EXPECT_DOUBLE_EQ(describe.mean_geo_pow(data, p), 25.0);
      79           1 : }
      80             : 
      81           4 : TEST(DescribeTest, MeanHarmonicSingleElement) {
      82             :     gpmp::stats::Describe describe;
      83           1 :     std::vector<double> data = {5.0};
      84           1 :     EXPECT_DOUBLE_EQ(describe.mean_harmonic(data), 5.0);
      85           1 : }
      86             : 
      87           4 : TEST(DescribeTest, MeanHarmonicMultipleElements) {
      88             :     gpmp::stats::Describe describe;
      89           1 :     std::vector<double> data = {1.0, 2.0, 4.0};
      90           1 :     EXPECT_NEAR(describe.mean_harmonic(data), 1.714285, 1e-4);
      91           1 : }
      92             : 
      93           4 : TEST(DescribeTest, MeanHeronianSingleElement) {
      94             :     gpmp::stats::Describe describe;
      95           1 :     std::vector<double> data = {5.0};
      96           1 :     EXPECT_DOUBLE_EQ(describe.mean_heronian(data), 5.0);
      97           1 : }
      98             : 
      99           4 : TEST(DescribeTest, MeanLehmerSingleElement) {
     100             :     gpmp::stats::Describe describe;
     101           1 :     std::vector<double> data = {5.0};
     102           1 :     double p = 2.0; // Arbitrary value for p
     103           1 :     EXPECT_DOUBLE_EQ(describe.mean_lehmer(data, p), 25.0);
     104           1 : }
     105             : 
     106           4 : TEST(DescribeTest, MedianOddSize) {
     107           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     108             :     gpmp::stats::Describe describe;
     109           1 :     double expected_median = 3.0;
     110           1 :     double actual_median = describe.Median(data);
     111           1 :     EXPECT_EQ(expected_median, actual_median);
     112           1 : }
     113             : 
     114           4 : TEST(DescribeTest, MedianEvenSize) {
     115           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0};
     116             :     gpmp::stats::Describe describe;
     117           1 :     double expected_median = 2.5;
     118           1 :     double actual_median = describe.Median(data);
     119           1 :     EXPECT_EQ(expected_median, actual_median);
     120           1 : }
     121           4 : TEST(DescribeTest, AvgAbsDev) {
     122           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     123             :     gpmp::stats::Describe describe;
     124           1 :     double expected_avg_abs_dev = 1.2;
     125           1 :     double actual_avg_abs_dev = describe.avg_abs_dev(data);
     126           1 :     double tolerance = 1e-3;
     127           1 :     EXPECT_NEAR(expected_avg_abs_dev, actual_avg_abs_dev, tolerance);
     128           1 : }
     129             : 
     130           4 : TEST(DescribeTest, AvgAbsDevEmptyVector) {
     131           1 :     std::vector<double> data;
     132             :     gpmp::stats::Describe describe;
     133           1 :     double expected_avg_abs_dev = 0.0;
     134           1 :     double actual_avg_abs_dev = describe.avg_abs_dev(data);
     135           1 :     EXPECT_TRUE(std::isnan(actual_avg_abs_dev) ||
     136           1 :                 actual_avg_abs_dev == expected_avg_abs_dev);
     137           1 : }
     138             : 
     139           4 : TEST(DescribeTest, VarCoeff) {
     140           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     141             :     gpmp::stats::Describe describe;
     142           1 :     double expected_var_coeff = 47.1405;
     143           1 :     double actual_var_coeff = describe.var_coeff(data);
     144           1 :     double tolerance = 1e-4;
     145           1 :     EXPECT_NEAR(expected_var_coeff, actual_var_coeff, tolerance);
     146           1 : }
     147             : 
     148           4 : TEST(DescribeTest, VarCoeffEmptyVector) {
     149           1 :     std::vector<double> data;
     150             :     gpmp::stats::Describe describe;
     151           1 :     double expected_var_coeff = std::numeric_limits<double>::quiet_NaN();
     152           1 :     double actual_var_coeff = describe.var_coeff(data);
     153           1 :     EXPECT_TRUE(std::isnan(actual_var_coeff) ||
     154           1 :                 actual_var_coeff == expected_var_coeff);
     155           1 : }
     156             : 
     157           4 : TEST(DescribeTest, IqRange) {
     158           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     159             :     gpmp::stats::Describe describe;
     160           1 :     double expected_iq_range = 2.0;
     161           1 :     double actual_iq_range = describe.iq_range(data);
     162           1 :     EXPECT_EQ(expected_iq_range, actual_iq_range);
     163           1 : }
     164             : 
     165           4 : TEST(DescribeTest, Percentile) {
     166           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     167             :     gpmp::stats::Describe describe;
     168           1 :     double expected_percentile = 3.0;
     169           1 :     double actual_percentile = describe.percentile(data, 0.5);
     170           1 :     EXPECT_EQ(expected_percentile, actual_percentile);
     171           1 : }
     172             : 
     173           4 : TEST(DescribeTest, Range) {
     174           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     175             :     gpmp::stats::Describe describe;
     176           1 :     double expected_range = 4.0;
     177           1 :     double actual_range = describe.range(data);
     178           1 :     EXPECT_EQ(expected_range, actual_range);
     179           1 : }
     180             : 
     181           4 : TEST(DescribeTest, Stdev) {
     182           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     183             :     gpmp::stats::Describe describe;
     184           1 :     double mean = describe.mean_arith(data);
     185           1 :     double expected_stdev = 1.41421;
     186           1 :     double actual_stdev = describe.stdev(data, mean);
     187           1 :     double tolerance = 1e-3;
     188           1 :     EXPECT_NEAR(expected_stdev, actual_stdev, tolerance);
     189           1 : }
     190             : 
     191           4 : TEST(DescribeTest, StdevEmptyVector) {
     192           1 :     std::vector<double> data;
     193             :     gpmp::stats::Describe describe;
     194           1 :     double expected_stdev = std::numeric_limits<double>::quiet_NaN();
     195           1 :     double actual_stdev = describe.stdev(data, 0.0);
     196           1 :     EXPECT_TRUE(std::isnan(actual_stdev) || actual_stdev == expected_stdev);
     197           1 : }
     198             : 
     199           4 : TEST(DescribeTest, VarianceEmptyVector) {
     200           1 :     std::vector<double> data;
     201             :     gpmp::stats::Describe describe;
     202           1 :     double expected_variance = std::numeric_limits<double>::quiet_NaN();
     203           1 :     double actual_variance = describe.variance(data, 0.0);
     204           1 :     EXPECT_TRUE(std::isnan(actual_variance) ||
     205           1 :                 actual_variance == expected_variance);
     206           1 : }
     207             : 
     208           4 : TEST(DescribeTest, Clt) {
     209           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     210           1 :     int numSamples = 100;
     211             :     gpmp::stats::Describe describe;
     212           1 :     double expected_clt = 0.1414;
     213           1 :     double actual_clt = describe.clt(data, numSamples);
     214           1 :     double tolerance = 1e-2;
     215           1 :     EXPECT_NEAR(expected_clt, actual_clt, tolerance);
     216           1 : }
     217             : 
     218           4 : TEST(DescribeTest, Kurtosis) {
     219           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     220             :     gpmp::stats::Describe describe;
     221           1 :     double mean = describe.mean_arith(data);
     222           1 :     double expected_kurtosis = -1.3;
     223           1 :     double actual_kurtosis = describe.kurtosis(data, mean);
     224           1 :     double tolerance = 1e-3;
     225           1 :     EXPECT_NEAR(expected_kurtosis, actual_kurtosis, tolerance);
     226           1 : }
     227             : 
     228           4 : TEST(DescribeTest, Lmoment1) {
     229           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     230             :     gpmp::stats::Describe describe;
     231           1 :     double mean = describe.mean_arith(data);
     232           1 :     double expected_lmoment1 = 0.0;
     233           1 :     double actual_lmoment1 = describe.lmoment1(data, mean);
     234           1 :     EXPECT_EQ(expected_lmoment1, actual_lmoment1);
     235           1 : }
     236             : 
     237           4 : TEST(DescribeTest, Lmoment2) {
     238           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     239             :     gpmp::stats::Describe describe;
     240           1 :     double mean = describe.mean_arith(data);
     241           1 :     double expected_lmoment2 = 6.8;
     242           1 :     double actual_lmoment2 = describe.lmoment2(data, mean);
     243           1 :     EXPECT_EQ(expected_lmoment2, actual_lmoment2);
     244           1 : }
     245             : 
     246           4 : TEST(DescribeTest, Skewness) {
     247           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     248             :     gpmp::stats::Describe describe;
     249           1 :     double mean = describe.mean_arith(data);
     250           1 :     double stddev = describe.stdev(data, mean);
     251           1 :     double expected_skewness = 0.0;
     252           1 :     double actual_skewness = describe.skewness(data, mean, stddev);
     253           1 :     double tolerance = 1e-3;
     254           1 :     EXPECT_NEAR(expected_skewness, actual_skewness, tolerance);
     255           1 : }
     256             : 
     257           4 : TEST(DescribeTest, RankData) {
     258           1 :     std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
     259             :     gpmp::stats::Describe describe;
     260             :     std::vector<size_t> expected_ranks =
     261           1 :         {1, 2, 3, 4, 5}; // Expected ranks based on input data
     262           1 :     std::vector<size_t> actual_ranks = describe.rank_data(data);
     263           1 :     EXPECT_EQ(expected_ranks, actual_ranks);
     264           1 : }
     265             : 
     266           4 : TEST(DescribeTest, Ppmc) {
     267           1 :     std::vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0};
     268           1 :     std::vector<double> y = {2.0, 3.0, 4.0, 5.0, 6.0};
     269             :     gpmp::stats::Describe describe;
     270           1 :     double expected_ppmc = 1.0;
     271           1 :     double actual_ppmc = describe.ppmc(x, y);
     272           1 :     double tolerance = 1e-3;
     273           1 :     EXPECT_NEAR(expected_ppmc, actual_ppmc, tolerance);
     274           1 : }

Generated by: LCOV version 1.14