GCC Code Coverage Report


Directory: ./
File: tasks/ivanova_p_marking_components_on_binary_image/data/image_generator.hpp
Date: 2026-06-04 20:25:32
Exec Total Coverage
Lines: 85 94 90.4%
Functions: 16 16 100.0%
Branches: 138 152 90.8%

Line Branch Exec Source
1 #pragma once
2
3 #include <cstddef>
4 #include <cstdint>
5 #include <fstream>
6 #include <ios>
7 #include <string>
8
9 #include "ivanova_p_marking_components_on_binary_image/common/include/common.hpp"
10
11 namespace ivanova_p_marking_components_on_binary_image {
12 // Функция для загрузки изображения из текстового файла
13 176 inline Image LoadImageFromTxt(const std::string &filename) {
14 176 Image img;
15
1/2
✓ Branch 1 taken 176 times.
✗ Branch 2 not taken.
176 std::ifstream file(filename);
16
17
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
176 if (!file.is_open()) {
18 img.width = 0;
19 img.height = 0;
20 return img;
21 }
22
23
2/4
✓ Branch 1 taken 176 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 176 times.
✗ Branch 5 not taken.
176 file >> img.width >> img.height;
24
25
2/4
✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 176 times.
176 if (img.width <= 0 || img.height <= 0) {
26 img.width = 0;
27 img.height = 0;
28 return img;
29 }
30
31
1/2
✓ Branch 1 taken 176 times.
✗ Branch 2 not taken.
176 img.data.resize(static_cast<size_t>(img.width) * static_cast<size_t>(img.height));
32
33
2/2
✓ Branch 0 taken 6204 times.
✓ Branch 1 taken 176 times.
6380 for (size_t i = 0; i < img.data.size(); ++i) {
34 6204 int pixel_value = 0;
35
2/4
✓ Branch 1 taken 6204 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6204 times.
6204 if (!(file >> pixel_value)) {
36 img.width = 0;
37 img.height = 0;
38 return img;
39 }
40 // Преобразуем в бинарное: если пиксель темный (< 128), то 1, иначе 0
41 // 0 - объект (черный), 255 - фон (белый)
42
2/2
✓ Branch 0 taken 4312 times.
✓ Branch 1 taken 1892 times.
10516 img.data[i] = (pixel_value < 128) ? 1 : 0;
43 }
44
45
1/2
✓ Branch 1 taken 176 times.
✗ Branch 2 not taken.
176 file.close();
46 return img;
47 176 }
48
49 // Вспомогательные функции для создания тестовых изображений
50 namespace test_image_helpers {
51
52 inline bool IsPixelInTestCase1(int xx, int yy, int width, int height) {
53
6/6
✓ Branch 0 taken 215600 times.
✓ Branch 1 taken 110000 times.
✓ Branch 2 taken 159544 times.
✓ Branch 3 taken 56056 times.
✓ Branch 4 taken 53900 times.
✓ Branch 5 taken 105644 times.
325600 return xx > width / 4 && xx < (3 * width) / 4 && yy > height / 4 && yy < (3 * height) / 4;
54 }
55
56 440000 inline bool IsPixelInTestCase2(int xx, int yy, int width, int height) {
57
8/8
✓ Branch 0 taken 382800 times.
✓ Branch 1 taken 57200 times.
✓ Branch 2 taken 105600 times.
✓ Branch 3 taken 277200 times.
✓ Branch 4 taken 91872 times.
✓ Branch 5 taken 13728 times.
✓ Branch 6 taken 66528 times.
✓ Branch 7 taken 25344 times.
440000 return (xx > width / 8 && xx < (3 * width) / 8 && yy > height / 8 && yy < (3 * height) / 8) ||
58
8/8
✓ Branch 0 taken 162800 times.
✓ Branch 1 taken 251856 times.
✓ Branch 2 taken 105600 times.
✓ Branch 3 taken 57200 times.
✓ Branch 4 taken 39072 times.
✓ Branch 5 taken 66528 times.
✓ Branch 6 taken 25344 times.
✓ Branch 7 taken 13728 times.
414656 (xx > (5 * width) / 8 && xx < (7 * width) / 8 && yy > (5 * height) / 8 && yy < (7 * height) / 8);
59 }
60
61 440000 inline bool IsPixelInTestCase3(int xx, int yy, int width, int height) {
62
6/6
✓ Branch 0 taken 83600 times.
✓ Branch 1 taken 308000 times.
✓ Branch 2 taken 74404 times.
✓ Branch 3 taken 9196 times.
✓ Branch 4 taken 58520 times.
✓ Branch 5 taken 15884 times.
391600 return (xx > width / 10 && xx < (3 * width) / 10 && yy > height / 10 && yy < (3 * height) / 10) ||
63
10/10
✓ Branch 0 taken 391600 times.
✓ Branch 1 taken 48400 times.
✓ Branch 2 taken 259600 times.
✓ Branch 3 taken 164516 times.
✓ Branch 4 taken 83600 times.
✓ Branch 5 taken 176000 times.
✓ Branch 6 taken 49324 times.
✓ Branch 7 taken 34276 times.
✓ Branch 8 taken 15884 times.
✓ Branch 9 taken 33440 times.
815716 (xx > (4 * width) / 10 && xx < (6 * width) / 10 && yy > (4 * height) / 10 && yy < (6 * height) / 10) ||
64
8/8
✓ Branch 0 taken 127600 times.
✓ Branch 1 taken 280632 times.
✓ Branch 2 taken 83600 times.
✓ Branch 3 taken 44000 times.
✓ Branch 4 taken 24244 times.
✓ Branch 5 taken 59356 times.
✓ Branch 6 taken 15884 times.
✓ Branch 7 taken 8360 times.
408232 (xx > (7 * width) / 10 && xx < (9 * width) / 10 && yy > (7 * height) / 10 && yy < (9 * height) / 10);
65 }
66
67 440000 inline bool IsPixelInTestCase4(int xx, int yy, int width, int height) {
68
6/6
✓ Branch 0 taken 272800 times.
✓ Branch 1 taken 17600 times.
✓ Branch 2 taken 26048 times.
✓ Branch 3 taken 9152 times.
✓ Branch 4 taken 8800 times.
✓ Branch 5 taken 17248 times.
308000 return (xx > width / 3 && xx < ((width / 3) + 5) && yy > height / 4 && yy < (3 * height) / 4) ||
69
8/8
✓ Branch 0 taken 290400 times.
✓ Branch 1 taken 149600 times.
✓ Branch 2 taken 145200 times.
✓ Branch 3 taken 277200 times.
✓ Branch 4 taken 17600 times.
✓ Branch 5 taken 127600 times.
✓ Branch 6 taken 273152 times.
✓ Branch 7 taken 149600 times.
730752 (xx > (2 * width) / 3 && xx < (((2 * width) / 3) + 5) && yy > height / 4 && yy < (3 * height) / 4) ||
70
6/6
✓ Branch 0 taken 145552 times.
✓ Branch 1 taken 127600 times.
✓ Branch 2 taken 73876 times.
✓ Branch 3 taken 71676 times.
✓ Branch 4 taken 3828 times.
✓ Branch 5 taken 70048 times.
273152 (xx > width / 3 && xx < (((2 * width) / 3) + 5) && yy > ((height / 2) - 2) && yy < ((height / 2) + 2));
71 }
72
73 440000 inline bool IsPixelInTestCase7(int xx, int yy, int width, int height) {
74
4/4
✓ Branch 0 taken 4400 times.
✓ Branch 1 taken 431200 times.
✓ Branch 2 taken 8712 times.
✓ Branch 3 taken 88 times.
440000 return (xx == width / 2 && yy == height / 4) || (xx == (3 * width) / 4 && yy == height / 4) ||
75
10/10
✓ Branch 0 taken 435600 times.
✓ Branch 1 taken 4400 times.
✓ Branch 2 taken 4400 times.
✓ Branch 3 taken 435512 times.
✓ Branch 4 taken 44 times.
✓ Branch 5 taken 4356 times.
✓ Branch 6 taken 4356 times.
✓ Branch 7 taken 435512 times.
✓ Branch 8 taken 44 times.
✓ Branch 9 taken 4312 times.
879912 (xx == width / 4 && yy == (3 * height) / 4) || (xx == (3 * width) / 4 && yy == (3 * height) / 4);
76 }
77
78 inline bool IsPixelInTestCase8(int xx, int yy, int width, int height) {
79 440000 int cell_width = width / 3;
80 440000 int cell_height = height / 3;
81 440000 int local_x = xx % cell_width;
82 440000 int local_y = yy % cell_height;
83
4/4
✓ Branch 0 taken 198000 times.
✓ Branch 1 taken 118800 times.
✓ Branch 2 taken 142560 times.
✓ Branch 3 taken 55440 times.
316800 return local_x > cell_width / 4 && local_x < (3 * cell_width) / 4 && local_y > cell_height / 4 &&
84
2/2
✓ Branch 0 taken 53460 times.
✓ Branch 1 taken 89100 times.
142560 local_y < (3 * cell_height) / 4;
85 }
86
87 } // namespace test_image_helpers
88
89 // Вспомогательные функции для каждого тестового случая
90 namespace test_case_generators {
91
92
2/2
✓ Branch 0 taken 325600 times.
✓ Branch 1 taken 114400 times.
440000 inline uint8_t GetPixelForTestCase1(int xx, int yy, int width, int height) {
93 440000 return test_image_helpers::IsPixelInTestCase1(xx, yy, width, height) ? 1 : 0;
94 }
95
96 440000 inline uint8_t GetPixelForTestCase2(int xx, int yy, int width, int height) {
97
2/2
✓ Branch 0 taken 389312 times.
✓ Branch 1 taken 50688 times.
440000 return test_image_helpers::IsPixelInTestCase2(xx, yy, width, height) ? 1 : 0;
98 }
99
100 440000 inline uint8_t GetPixelForTestCase3(int xx, int yy, int width, int height) {
101
2/2
✓ Branch 0 taken 392348 times.
✓ Branch 1 taken 47652 times.
440000 return test_image_helpers::IsPixelInTestCase3(xx, yy, width, height) ? 1 : 0;
102 }
103
104 440000 inline uint8_t GetPixelForTestCase4(int xx, int yy, int width, int height) {
105
2/2
✓ Branch 0 taken 418924 times.
✓ Branch 1 taken 21076 times.
440000 return test_image_helpers::IsPixelInTestCase4(xx, yy, width, height) ? 1 : 0;
106 }
107
108 560000 inline uint8_t GetPixelForTestCase5(int /*xx*/, int /*yy*/, int /*width*/, int /*height*/) {
109 560000 return 0;
110 }
111
112 440000 inline uint8_t GetPixelForTestCase6(int xx, int yy, int width, int height) {
113
4/4
✓ Branch 0 taken 4400 times.
✓ Branch 1 taken 435600 times.
✓ Branch 2 taken 4356 times.
✓ Branch 3 taken 44 times.
440000 return (xx == width / 2 && yy == height / 2) ? 1 : 0;
114 }
115
116 440000 inline uint8_t GetPixelForTestCase7(int xx, int yy, int width, int height) {
117
2/2
✓ Branch 0 taken 439824 times.
✓ Branch 1 taken 176 times.
440000 return test_image_helpers::IsPixelInTestCase7(xx, yy, width, height) ? 1 : 0;
118 }
119
120
2/2
✓ Branch 0 taken 316800 times.
✓ Branch 1 taken 123200 times.
440000 inline uint8_t GetPixelForTestCase8(int xx, int yy, int width, int height) {
121 440000 return test_image_helpers::IsPixelInTestCase8(xx, yy, width, height) ? 1 : 0;
122 }
123
124 440000 inline uint8_t GetPixelForTestCase9(int xx, int yy, int width, int height) {
125 (void)xx;
126 (void)width;
127
2/2
✓ Branch 0 taken 435600 times.
✓ Branch 1 taken 4400 times.
440000 return (yy == height / 2) ? 1 : 0;
128 }
129
130 440000 inline uint8_t GetPixelForTestCase10(int xx, int yy, int width, int height) {
131 (void)yy;
132 (void)height;
133
2/2
✓ Branch 0 taken 435600 times.
✓ Branch 1 taken 4400 times.
440000 return (xx == width / 2) ? 1 : 0;
134 }
135
136 } // namespace test_case_generators
137
138 // Вспомогательные функции для работы с кодированным test_case
139 // Формат: test_case = (размер << 16) | тип_теста
140 // Если размер не указан (старшие биты = 0), используется размер по умолчанию 500x500
141
142 inline int ExtractImageSize(int test_case) {
143 226 int size = (test_case >> 16) & 0xFFFF;
144
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 184 times.
✗ Branch 3 not taken.
226 return (size > 0) ? size : 500; // По умолчанию 500
145 }
146
147 inline int ExtractTestType(int test_case) {
148 452 return test_case & 0xFFFF;
149 }
150
151 inline int EncodeTestCase(int size, int test_type) {
152 226 return (size << 16) | test_type;
153 }
154
155 // Вспомогательная функция для создания тестовых изображений
156 452 inline Image CreateTestImage(int width, int height, int test_case) {
157 // Извлекаем реальный тип теста из закодированного значения
158 int test_type = ExtractTestType(test_case);
159
160 452 Image img;
161 452 img.width = width;
162 452 img.height = height;
163
1/2
✓ Branch 1 taken 452 times.
✗ Branch 2 not taken.
452 img.data.resize(static_cast<size_t>(width) * static_cast<size_t>(height));
164
165 // Выбираем функцию-генератор в зависимости от test_type
166 uint8_t (*generator)(int, int, int, int) = nullptr;
167
168
10/10
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 44 times.
✓ Branch 5 taken 44 times.
✓ Branch 6 taken 44 times.
✓ Branch 7 taken 44 times.
✓ Branch 8 taken 44 times.
✓ Branch 9 taken 44 times.
452 switch (test_type) {
169 case 1:
170 generator = test_case_generators::GetPixelForTestCase1;
171 break;
172 44 case 2:
173 generator = test_case_generators::GetPixelForTestCase2;
174 44 break;
175 44 case 3:
176 generator = test_case_generators::GetPixelForTestCase3;
177 44 break;
178 44 case 4:
179 generator = test_case_generators::GetPixelForTestCase4;
180 44 break;
181 case 5:
182 generator = test_case_generators::GetPixelForTestCase5;
183 break;
184 44 case 6:
185 generator = test_case_generators::GetPixelForTestCase6;
186 44 break;
187 44 case 7:
188 generator = test_case_generators::GetPixelForTestCase7;
189 44 break;
190 44 case 8:
191 generator = test_case_generators::GetPixelForTestCase8;
192 44 break;
193 44 case 9:
194 generator = test_case_generators::GetPixelForTestCase9;
195 44 break;
196 44 case 10:
197 generator = test_case_generators::GetPixelForTestCase10;
198 44 break;
199 default:
200 generator = test_case_generators::GetPixelForTestCase5; // фон по умолчанию
201 break;
202 }
203
204
2/2
✓ Branch 0 taken 45200 times.
✓ Branch 1 taken 452 times.
45652 for (int yy = 0; yy < height; ++yy) {
205
2/2
✓ Branch 0 taken 4520000 times.
✓ Branch 1 taken 45200 times.
4565200 for (int xx = 0; xx < width; ++xx) {
206 4520000 int idx = (yy * width) + xx;
207
1/2
✓ Branch 1 taken 4520000 times.
✗ Branch 2 not taken.
4520000 img.data[idx] = generator(xx, yy, width, height);
208 }
209 }
210
211 452 return img;
212 }
213 } // namespace ivanova_p_marking_components_on_binary_image
214