GCC Code Coverage Report


Directory: src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 80 / 0 / 80
Functions: 100.0% 27 / 0 / 27
Branches: 24.1% 28 / 0 / 116

tests/utility/utility_test.cpp
Line Branch Exec Source
1 //#define ENABLE_UTILITY_TEST
2
3 #include "gtest/gtest.h"
4 #include "../../utility/header/commonTypes.h"
5 #include "../../utility/header/mathUtility.h"
6
7 #include <fstream>
8 #include <sys/stat.h>
9 #include <cstdio>
10 #ifdef _WIN32
11 #include <direct.h>
12 #elif __linux__
13 #include <unistd.h>
14 #endif
15
16
17 using namespace Coruh::Utility;
18
19 class MathUtilityTest : public ::testing::Test {
20 public:
21 int a;
22 protected:
23 8 void SetUp() override {
24 //Setup test data
25 8 }
26
27 8 void TearDown() override {
28 // Clean up test data
29 8 }
30 };
31
32
33
34 4 TEST_F(MathUtilityTest, CalculateMean) {
35 // Test data
36 1 const double data[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
37 1 const int datalen = sizeof(data) / sizeof(data[0]);
38 // Perform the calculation
39
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 24 not taken.
1 double result = MathUtility::calculateMean(data, datalen);
40 // Check the result
41
2/10
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 23 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 13 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 20 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 17 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 15 not taken.
1 EXPECT_DOUBLE_EQ(result, 3.0);
42 1 }
43
44
45 4 TEST_F(MathUtilityTest, CalculateMedianOdd) {
46 // Test data
47 1 const double data[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
48 1 const int datalen = sizeof(data) / sizeof(data[0]);
49 // Perform the calculation
50
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 24 not taken.
1 double result = MathUtility::calculateMedian(data, datalen);
51 // Check the result
52
2/10
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 23 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 13 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 20 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 17 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 15 not taken.
1 EXPECT_DOUBLE_EQ(result, 3.0);
53 1 }
54
55 4 TEST_F(MathUtilityTest, CalculateMedianEven) {
56 // Test data
57 1 const double data[] = { 1.0, 2.0, 3.0, 4.0 };
58 1 const int datalen = sizeof(data) / sizeof(data[0]);
59 // Perform the calculation
60
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 24 not taken.
1 double result = MathUtility::calculateMedian(data, datalen);
61 // Check the result
62
2/10
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 23 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 13 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 20 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 17 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 15 not taken.
1 EXPECT_DOUBLE_EQ(result, 2.5);
63 1 }
64
65 4 TEST_F(MathUtilityTest, CompareDoubleLessTest) {
66 // Test data
67 1 const double val1 = 2.0;
68 1 const double val2 = 4.0;
69 // Perform the comparison
70
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 25 not taken.
1 int result = MathUtility::compareDouble(&val1, &val2);
71 // Check the result
72
2/10
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 15 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 13 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 21 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 18 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 16 not taken.
1 EXPECT_EQ(result, -1);
73 1 }
74
75 4 TEST_F(MathUtilityTest, CompareDoubleGreaterTest) {
76 // Test data
77 1 const double val1 = 4.0;
78 1 const double val2 = 2.0;
79 // Perform the comparison
80
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 25 not taken.
1 int result = MathUtility::compareDouble(&val1, &val2);
81 // Check the result
82
2/10
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 15 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 13 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 21 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 18 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 16 not taken.
1 EXPECT_EQ(result, 1);
83 1 }
84
85 4 TEST_F(MathUtilityTest, CompareDoubleEqualTest) {
86 // Test data
87 1 const double val1 = 3.0;
88 1 const double val2 = 3.0;
89 // Perform the comparison
90
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 25 not taken.
1 int result = MathUtility::compareDouble(&val1, &val2);
91 // Check the result
92
2/10
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 15 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 13 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 21 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 18 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 16 not taken.
1 EXPECT_EQ(result, 0);
93 1 }
94
95 4 TEST_F(MathUtilityTest, CalculateMinMaxTest_1) {
96 // Test data
97 1 const double data[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
98 1 const int datalen = sizeof(data) / sizeof(data[0]);
99 double min, max;
100 // Perform the calculation
101
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 44 not taken.
1 MathUtility::calculateMinMax(data, datalen, &min, &max);
102 // Check the result
103
2/10
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 34 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 13 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 31 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 28 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 26 not taken.
1 EXPECT_DOUBLE_EQ(min, 1.0);
104
2/10
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 43 not taken.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 24 taken 1 time.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 40 not taken.
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 37 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 35 not taken.
1 EXPECT_DOUBLE_EQ(max, 5.0);
105 1 }
106
107 4 TEST_F(MathUtilityTest, CalculateMinMaxTest_2) {
108 1 double data[] = { 3.14, 1.0, -2.5, 7.2, -5.0 };
109 double min, max;
110
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 44 not taken.
1 MathUtility::calculateMinMax(data, sizeof(data) / sizeof(data[0]), &min, &max);
111
2/10
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 34 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 13 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 31 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 28 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 26 not taken.
1 EXPECT_DOUBLE_EQ(min, -5.0);
112
2/10
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 43 not taken.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 24 taken 1 time.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 40 not taken.
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 37 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 35 not taken.
1 EXPECT_DOUBLE_EQ(max, 7.2);
113 1 }
114
115
116
117
118
119 /**
120 * @brief The main function of the test program.
121 *
122 * @param argc The number of command-line arguments.
123 * @param argv An array of command-line argument strings.
124 * @return int The exit status of the program.
125 */
126 9 int main(int argc, char **argv) {
127 #ifdef ENABLE_UTILITY_TEST
128 9 ::testing::InitGoogleTest(&argc, argv);
129 9 return RUN_ALL_TESTS();
130 #else
131 return 0;
132 #endif
133 }
134