GCC Code Coverage Report


Directory: ./
File: tasks/shilin_n_monte_carlo_integration/common/include/common.hpp
Date: 2026-04-02 17:12:27
Exec Total Coverage
Lines: 43 45 95.6%
Functions: 5 5 100.0%
Branches: 40 44 90.9%

Line Branch Exec Source
1 #pragma once
2
3 #include <cmath>
4 #include <cstddef>
5 #include <cstdint>
6 #include <string>
7 #include <tuple>
8 #include <vector>
9
10 #include "task/include/task.hpp"
11
12 namespace shilin_n_monte_carlo_integration {
13
14 enum class FuncType : std::uint8_t {
15 kConstant = 0,
16 kLinear = 1,
17 kProduct = 2,
18 kSumSquares = 3,
19 kSinProduct = 4,
20 };
21
22 class IntegrandFunction {
23 public:
24 6720000 static double Evaluate(FuncType func_type, const std::vector<double> &point) {
25
5/6
✓ Branch 0 taken 2560000 times.
✓ Branch 1 taken 2400000 times.
✓ Branch 2 taken 160000 times.
✓ Branch 3 taken 800000 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 800000 times.
6720000 switch (func_type) {
26 case FuncType::kConstant:
27 return 1.0;
28 2560000 case FuncType::kLinear: {
29 double sum = 0.0;
30
2/2
✓ Branch 0 taken 6560000 times.
✓ Branch 1 taken 2560000 times.
9120000 for (double x : point) {
31 6560000 sum += x;
32 }
33 return sum;
34 }
35 2400000 case FuncType::kProduct: {
36 double prod = 1.0;
37
2/2
✓ Branch 0 taken 6400000 times.
✓ Branch 1 taken 2400000 times.
8800000 for (double x : point) {
38 6400000 prod *= x;
39 }
40 return prod;
41 }
42 160000 case FuncType::kSumSquares: {
43 double sum = 0.0;
44
2/2
✓ Branch 0 taken 160000 times.
✓ Branch 1 taken 160000 times.
320000 for (double x : point) {
45 160000 sum += x * x;
46 }
47 return sum;
48 }
49 800000 case FuncType::kSinProduct: {
50 double prod = 1.0;
51
2/2
✓ Branch 0 taken 1600000 times.
✓ Branch 1 taken 800000 times.
2400000 for (double x : point) {
52 1600000 prod *= std::sin(x);
53 }
54 return prod;
55 }
56 default:
57 return 0.0;
58 }
59 }
60
61
5/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
128 static double AnalyticalIntegral(FuncType func_type, const std::vector<double> &lower,
62 const std::vector<double> &upper) {
63 size_t dim = lower.size();
64
5/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
128 switch (func_type) {
65 case FuncType::kConstant:
66 return ComputeVolume(lower, upper);
67 48 case FuncType::kLinear:
68 48 return ComputeLinearIntegral(lower, upper, dim);
69 case FuncType::kProduct:
70 return ComputeProductIntegral(lower, upper, dim);
71 16 case FuncType::kSumSquares:
72 16 return ComputeSumSquaresIntegral(lower, upper, dim);
73 16 case FuncType::kSinProduct:
74 16 return ComputeSinProductIntegral(lower, upper, dim);
75 default:
76 return 0.0;
77 }
78 }
79
80 private:
81 static double ComputeVolume(const std::vector<double> &lower, const std::vector<double> &upper) {
82 double vol = 1.0;
83
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (size_t i = 0; i < lower.size(); ++i) {
84 32 vol *= (upper[i] - lower[i]);
85 }
86 return vol;
87 }
88
89 static double VolumeExcludingDim(const std::vector<double> &lower, const std::vector<double> &upper, size_t exclude) {
90 double product = 1.0;
91
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 96 times.
352 for (size_t j = 0; j < lower.size(); ++j) {
92
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 96 times.
240 if (j != exclude) {
93 128 product *= (upper[j] - lower[j]);
94 }
95 }
96 return product;
97 }
98
99 48 static double ComputeLinearIntegral(const std::vector<double> &lower, const std::vector<double> &upper, size_t dim) {
100 double result = 0.0;
101
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
144 for (size_t i = 0; i < dim; ++i) {
102 96 double term = (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0;
103 96 result += term * VolumeExcludingDim(lower, upper, i);
104 }
105 48 return result;
106 }
107
108 static double ComputeProductIntegral(const std::vector<double> &lower, const std::vector<double> &upper, size_t dim) {
109 double prod = 1.0;
110
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 32 times.
112 for (size_t i = 0; i < dim; ++i) {
111 80 prod *= (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0;
112 }
113 return prod;
114 }
115
116 16 static double ComputeSumSquaresIntegral(const std::vector<double> &lower, const std::vector<double> &upper,
117 size_t dim) {
118 double result = 0.0;
119
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 for (size_t i = 0; i < dim; ++i) {
120 16 double term = (upper[i] * upper[i] * upper[i] - lower[i] * lower[i] * lower[i]) / 3.0;
121 16 result += term * VolumeExcludingDim(lower, upper, i);
122 }
123 16 return result;
124 }
125
126 16 static double ComputeSinProductIntegral(const std::vector<double> &lower, const std::vector<double> &upper,
127 size_t dim) {
128 double prod = 1.0;
129
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (size_t i = 0; i < dim; ++i) {
130 32 prod *= (-std::cos(upper[i]) + std::cos(lower[i]));
131 }
132 16 return prod;
133 }
134 };
135
136 using InType = std::tuple<std::vector<double>, std::vector<double>, int, FuncType>;
137 using OutType = double;
138 using TestType = std::tuple<InType, std::string>;
139 using BaseTask = ppc::task::Task<InType, OutType>;
140
141 } // namespace shilin_n_monte_carlo_integration
142