LCOV - code coverage report
Current view: top level - utility/src - mathUtility.cpp (source / functions) Coverage Total Hit
Test: calculator unit test code coverage - genhtml (Linux) Lines: 100.0 % 35 35
Test Date: 2026-09-30 03:06:01 Functions: 100.0 % 4 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 88.9 % 18 16

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             :  * @file mathUtility.cpp
       3                 :             :  *
       4                 :             :  * @brief Provides functions for math. utilities
       5                 :             :  */
       6                 :             : 
       7                 :             : #include <cstring>
       8                 :             : 
       9                 :             : #include "../header/mathUtility.h"
      10                 :             : 
      11                 :             : using namespace Coruh::Utility;
      12                 :             : 
      13                 :             : 
      14                 :           1 : double MathUtility::calculateMean(const double data[], int datalen) {
      15                 :           1 :         double sum = 0.0;
      16                 :             : 
      17         [ +  + ]:           6 :         for (int i = 0; i < datalen; ++i) {
      18                 :           5 :                 sum += data[i];
      19                 :             :         }
      20                 :             : 
      21                 :           1 :         return sum / datalen;
      22                 :             : }
      23                 :             : 
      24                 :           2 : double MathUtility::calculateMedian(const double data[], int datalen) {
      25                 :             : 
      26                 :             :         //TODO: Move this memory allocation to control function
      27         [ +  - ]:           2 :         double* sortedData = new double[datalen];
      28                 :           2 :         double median = 0;
      29                 :             : 
      30                 :           2 :         memcpy(sortedData, data, sizeof(double) * datalen);
      31                 :             : 
      32                 :           2 :         qsort(sortedData, datalen, sizeof(double), compareDouble);
      33                 :             : 
      34         [ +  + ]:           2 :         if (datalen % 2 == 0) {
      35                 :           1 :                 int index1 = datalen / 2 - 1;
      36                 :           1 :                 int index2 = datalen / 2;
      37                 :           1 :                 median = (sortedData[index1] + sortedData[index2]) / 2.0;
      38                 :             :         }else {
      39                 :           1 :                 int index = datalen / 2;
      40                 :           1 :                 median = sortedData[index];
      41                 :             :         }
      42                 :             : 
      43         [ +  - ]:           2 :         delete[] sortedData;
      44                 :           2 :         return median;
      45                 :             : }
      46                 :             : 
      47                 :          12 : int MathUtility::compareDouble(const void* a, const void* b) {
      48                 :             :         
      49                 :          12 :         double val1 = *(const double*)a;
      50                 :          12 :         double val2 = *(const double*)b;
      51                 :             : 
      52         [ +  + ]:          12 :         if (val1 < val2) {
      53                 :          10 :                 return -1;
      54         [ +  + ]:           2 :         }else if (val1 > val2) {
      55                 :           1 :                 return 1;
      56                 :             :         }else {
      57                 :           1 :                 return 0;
      58                 :             :         }
      59                 :             : }
      60                 :             : 
      61                 :           2 : void MathUtility::calculateMinMax(const double data[], int datalen, double* min, double* max) {
      62                 :             :         
      63                 :           2 :         *min = data[0];
      64                 :           2 :         *max = data[0];
      65                 :             : 
      66         [ +  + ]:          10 :         for (int i = 1; i < datalen; ++i) {
      67                 :             :         
      68         [ +  + ]:           8 :                 if (data[i] < *min) {
      69                 :           3 :                         *min = data[i];
      70                 :             :                 }
      71                 :             :         
      72         [ +  + ]:           8 :                 if (data[i] > *max) {
      73                 :           5 :                         *max = data[i];
      74                 :             :                 }
      75                 :             :         }
      76                 :           2 : }
      77                 :             : 
        

Generated by: LCOV version 2.0-1