Debian Codegraph @

pencil/test/integration/pencil_diagram_maker_test.c

raw
1 /* File: pencil_diagram_maker_test.c; Copyright and License: see below */
2
3 #include "pencil_diagram_maker_test.h"
4 #include "pencil_layouter.h"
5 #include "pencil_diagram_maker.h"
6 #include "geometry/geometry_rectangle.h"
7 #include "set/data_visible_set.h"
8 #include "set/data_profile_part.h"
9 #include "test_data/test_data_setup.h"
10 #include "test_data/test_data_evaluation.h"
11 #include "u8/u8_trace.h"
12 #include "test_fixture.h"
13 #include "test_expect.h"
14 #include "test_environment_assert.h"
15 #include "test_case_result.h"
16
17 static test_fixture_t * set_up();
18 static void tear_down( test_fixture_t *fix );
19 static test_case_result_t render_good_cases_no_output( test_fixture_t *fix );
20 static test_case_result_t render_good_cases( test_fixture_t *fix );
21 static test_case_result_t render_challenging_cases( test_fixture_t *fix );
22 /*
23 * There is no value in rendering too full diagrams
24 static test_case_result_t render_edge_cases( test_fixture_t *fix );
25 */
26
27 test_suite_t pencil_diagram_maker_test_get_suite(void)
28 {
29 test_suite_t result;
30 test_suite_init( &result,
31 "pencil_diagram_maker_test_get_suite",
32 TEST_CATEGORY_INTEGRATION | TEST_CATEGORY_CONTINUOUS,
33 &set_up,
34 &tear_down
35 );
36 test_suite_add_test_case( &result, "render_good_cases_no_output", &render_good_cases_no_output );
37 const test_category_t ON_QUEST = TEST_CATEGORY_INTEGRATION | TEST_CATEGORY_QUEST;
38 test_suite_add_special_test_case( &result, "render_good_cases", ON_QUEST, &render_good_cases );
39 test_suite_add_special_test_case( &result, "render_challenging_cases", ON_QUEST, &render_challenging_cases );
40 /*
41 test_suite_add_special_test_case( &result, "render_edge_cases", ON_QUEST, &render_edge_cases );
42 */
43 return result;
44 }
45
46 struct test_fixture_struct {
47 data_visible_set_t data_set;
48 data_profile_part_t profile;
49 pencil_diagram_maker_t painter;
50 cairo_surface_t *surface;
51 cairo_t *cr;
52 geometry_rectangle_t diagram_bounds;
53 };
54 typedef struct test_fixture_struct test_fixture_t; /* double declaration as reminder */
55 static test_fixture_t test_fixture;
56
57 static test_fixture_t * set_up()
58 {
59 test_fixture_t *fix = &test_fixture;
60 data_profile_part_init( &((*fix).profile) );
61 data_visible_set_init( &((*fix).data_set) );
62 pencil_diagram_maker_init( &((*fix).painter), &((*fix).data_set), &((*fix).profile) );
63 geometry_rectangle_init( &((*fix).diagram_bounds), 0.0, 0.0, 640.0, 480.0 );
64 const uint32_t width = (uint32_t) geometry_rectangle_get_width( &((*fix).diagram_bounds) );
65 const uint32_t height = (uint32_t) geometry_rectangle_get_height( &((*fix).diagram_bounds) );
66 (*fix).surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, width, height );
67 TEST_ENVIRONMENT_ASSERT( CAIRO_STATUS_SUCCESS == cairo_surface_status( (*fix).surface ) );
68 (*fix).cr = cairo_create( (*fix).surface );
69 TEST_ENVIRONMENT_ASSERT( CAIRO_STATUS_SUCCESS == cairo_status( (*fix).cr ) );
70 return fix;
71 }
72
73 static void tear_down( test_fixture_t *fix )
74 {
75 assert( fix != NULL );
76 cairo_destroy ( (*fix).cr );
77 cairo_surface_finish ( (*fix).surface );
78 cairo_surface_destroy ( (*fix).surface );
79 geometry_rectangle_destroy( &((*fix).diagram_bounds) );
80 pencil_diagram_maker_destroy( &((*fix).painter) );
81 data_visible_set_destroy( &((*fix).data_set) );
82 data_profile_part_destroy( &((*fix).profile) );
83 }
84
85 static void draw_background( const geometry_rectangle_t *diagram_bounds, cairo_t *cr )
86 {
87 U8_TRACE_BEGIN();
88 /* draw paper */
89 cairo_set_source_rgba( cr, 1.0, 1.0, 1.0, 1.0 );
90 cairo_rectangle ( cr,
91 geometry_rectangle_get_left( diagram_bounds ),
92 geometry_rectangle_get_top( diagram_bounds ),
93 geometry_rectangle_get_width( diagram_bounds ),
94 geometry_rectangle_get_height( diagram_bounds )
95 );
96 cairo_fill (cr);
97 U8_TRACE_END();
98 }
99
100 static void render_to_file( cairo_surface_t *surface,
101 const test_data_setup_t *ts_case_setup,
102 data_stat_t *render_stats )
103 {
104 U8_TRACE_BEGIN();
105 /* create filename */
106 char filename_buf[48]="";
107 utf8stringbuf_t filename = UTF8STRINGBUF(filename_buf);
108 utf8stringbuf_append_str( &filename, "test_pencil" );
109
110 const unsigned int variant = test_data_setup_get_variant( ts_case_setup );
111 switch ( test_data_setup_get_mode( ts_case_setup ) )
112 {
113 default:
114 case TEST_DATA_SETUP_MODE_GOOD_CASES:
115 {
116 utf8stringbuf_append_str( &filename, "_good" );
117 }
118 break;
119
120 case TEST_DATA_SETUP_MODE_CHALLENGING_CASES:
121 {
122 utf8stringbuf_append_str( &filename, "_cllg" );
123 }
124 break;
125
126 case TEST_DATA_SETUP_MODE_EDGE_CASES:
127 {
128 utf8stringbuf_append_str( &filename, "_edge" );
129 }
130 break;
131 }
132 utf8stringbuf_append_str( &filename, "_" );
133 utf8stringbuf_append_int( &filename, data_stat_get_series_count( render_stats, DATA_STAT_SERIES_ERROR ) );
134 utf8stringbuf_append_str( &filename, "e_" );
135 utf8stringbuf_append_int( &filename, data_stat_get_series_count( render_stats, DATA_STAT_SERIES_WARNING ) );
136 utf8stringbuf_append_str( &filename, "w_" );
137 utf8stringbuf_append_int( &filename, data_stat_get_series_count( render_stats, DATA_STAT_SERIES_EXPORTED ) );
138 utf8stringbuf_append_str( &filename, "ok_" );
139 utf8stringbuf_append_int( &filename, variant );
140 utf8stringbuf_append_str( &filename, ".png" );
141 U8_TRACE_INFO_STR("filename:", utf8stringbuf_get_string( &filename ));
142
143 /* finish surface */
144 const cairo_status_t png_result
145 = cairo_surface_write_to_png ( surface, utf8stringbuf_get_string( &filename ) );
146 TEST_ENVIRONMENT_ASSERT( CAIRO_STATUS_SUCCESS == png_result );
147 U8_TRACE_END();
148 }
149
150 static test_case_result_t render_good_cases_no_output( test_fixture_t *fix )
151 {
152 assert( fix != NULL );
153 test_data_setup_t ts_setup;
154 test_data_setup_init( &ts_setup, TEST_DATA_SETUP_MODE_GOOD_CASES );
155 for ( ; test_data_setup_is_valid_variant( &ts_setup ); test_data_setup_next_variant( &ts_setup ) )
156 {
157 /* setup */
158 test_data_setup_get_variant_data( &ts_setup, &((*fix).data_set) );
159 draw_background( &((*fix).diagram_bounds), (*fix).cr );
160
161 /* perform test: draw diagram */
162 data_id_t void_id;
163 data_id_init_void( &void_id );
164 data_small_set_t void_set;
165 data_small_set_init( &void_set );
166 data_stat_t layout_stats;
167 data_stat_init( &layout_stats );
168 pencil_diagram_maker_define_grid( &((*fix).painter), (*fix).diagram_bounds, (*fix).cr );
169 pencil_diagram_maker_layout_elements( &((*fix).painter), NULL, (*fix).cr );
170 layout_visible_set_get_statistics( pencil_diagram_maker_get_layout_data_const( &((*fix).painter) ),
171 &layout_stats
172 );
173 pencil_diagram_maker_draw ( &((*fix).painter),
174 void_id,
175 void_id,
176 &void_set,
177 (*fix).cr
178 );
179
180 /* check result, no output to png files */
181 data_stat_destroy( &layout_stats );
182 }
183 test_data_setup_destroy( &ts_setup );
184 return TEST_CASE_RESULT_OK;
185 }
186
187 static test_case_result_t render_good_cases( test_fixture_t *fix )
188 {
189 assert( fix != NULL );
190 test_data_setup_t ts_setup;
191 test_data_setup_init( &ts_setup, TEST_DATA_SETUP_MODE_GOOD_CASES );
192 test_data_evaluation_t ts_eval;
193 test_data_evaluation_init( &ts_eval );
194 for ( ; test_data_setup_is_valid_variant( &ts_setup ); test_data_setup_next_variant( &ts_setup ) )
195 {
196 /* setup */
197 test_data_setup_get_variant_data( &ts_setup, &((*fix).data_set) );
198 draw_background( &((*fix).diagram_bounds), (*fix).cr );
199
200 /* perform test: draw diagram */
201 data_id_t void_id;
202 data_id_init_void( &void_id );
203 data_small_set_t void_set;
204 data_small_set_init( &void_set );
205 data_stat_t layout_stats;
206 data_stat_init( &layout_stats );
207 pencil_diagram_maker_define_grid( &((*fix).painter), (*fix).diagram_bounds, (*fix).cr );
208 pencil_diagram_maker_layout_elements( &((*fix).painter), NULL, (*fix).cr );
209 test_data_evaluation_analyze( &ts_eval,
210 pencil_diagram_maker_get_layout_data_const( &((*fix).painter) ),
211 &layout_stats,
212 pencil_diagram_maker_test_draw_rects_callback,
213 (*fix).cr /* the user data of type void* */
214 );
215 pencil_diagram_maker_draw ( &((*fix).painter),
216 void_id,
217 void_id,
218 &void_set,
219 (*fix).cr
220 );
221
222 /* check result */
223 render_to_file( (*fix).surface, &ts_setup, &layout_stats );
224 data_stat_destroy( &layout_stats );
225 }
226 test_data_evaluation_destroy( &ts_eval );
227 test_data_setup_destroy( &ts_setup );
228 return TEST_CASE_RESULT_OK;
229 }
230
231 static test_case_result_t render_challenging_cases( test_fixture_t *fix )
232 {
233 assert( fix != NULL );
234 test_data_setup_t ts_setup;
235 test_data_setup_init( &ts_setup, TEST_DATA_SETUP_MODE_CHALLENGING_CASES );
236 test_data_evaluation_t ts_eval;
237 test_data_evaluation_init( &ts_eval );
238 for ( ; test_data_setup_is_valid_variant( &ts_setup ); test_data_setup_next_variant( &ts_setup ) )
239 {
240 /* setup */
241 test_data_setup_get_variant_data( &ts_setup, &((*fix).data_set) );
242 draw_background( &((*fix).diagram_bounds), (*fix).cr );
243
244 /* perform test: draw diagram */
245 data_id_t void_id;
246 data_id_init_void( &void_id );
247 data_small_set_t void_set;
248 data_small_set_init( &void_set );
249 data_stat_t layout_stats;
250 data_stat_init( &layout_stats );
251 pencil_diagram_maker_define_grid( &((*fix).painter), (*fix).diagram_bounds, (*fix).cr );
252 pencil_diagram_maker_layout_elements( &((*fix).painter), NULL, (*fix).cr );
253 test_data_evaluation_analyze( &ts_eval,
254 pencil_diagram_maker_get_layout_data_const( &((*fix).painter) ),
255 &layout_stats,
256 pencil_diagram_maker_test_draw_rects_callback,
257 (*fix).cr /* the user data of type void* */
258 );
259 pencil_diagram_maker_draw ( &((*fix).painter),
260 void_id,
261 void_id,
262 &void_set,
263 (*fix).cr
264 );
265
266 /* check result */
267 render_to_file( (*fix).surface, &ts_setup, &layout_stats );
268 data_stat_destroy( &layout_stats );
269 }
270 test_data_evaluation_destroy( &ts_eval );
271 test_data_setup_destroy( &ts_setup );
272 return TEST_CASE_RESULT_OK;
273 }
274
275 /*
276 static test_case_result_t render_edge_cases( test_fixture_t *fix )
277 {
278 assert( fix != NULL );
279 test_data_setup_t ts_setup;
280 test_data_setup_init( &ts_setup, TEST_DATA_SETUP_MODE_EDGE_CASES );
281 test_data_evaluation_t ts_eval;
282 test_data_evaluation_init( &ts_eval );
283 for ( ; test_data_setup_is_valid_variant( &ts_setup ); test_data_setup_next_variant( &ts_setup ) )
284 {
285 / * setup * /
286 test_data_setup_get_variant_data( &ts_setup, &((*fix).data_set) );
287 draw_background( &((*fix).diagram_bounds), (*fix).cr );
288
289 / * perform test: draw diagram * /
290 data_id_t void_id;
291 data_id_init_void( &void_id );
292 data_small_set_t void_set;
293 data_small_set_init( &void_set );
294 data_stat_t layout_stats;
295 data_stat_init( &layout_stats );
296 pencil_diagram_maker_define_grid( &((*fix).painter), (*fix).diagram_bounds, (*fix).cr );
297 pencil_diagram_maker_layout_elements( &((*fix).painter), NULL, (*fix).cr );
298 test_data_evaluation_analyze( &ts_eval,
299 pencil_diagram_maker_get_layout_data_const( &((*fix).painter) ),
300 &layout_stats,
301 pencil_diagram_maker_test_draw_rects_callback,
302 (*fix).cr / * the user data of type void* * /
303 );
304 pencil_diagram_maker_draw ( &((*fix).painter),
305 void_id,
306 void_id,
307 &void_set,
308 (*fix).cr
309 );
310
311 / * check result * /
312 render_to_file( (*fix).surface, &ts_setup, &layout_stats );
313 data_stat_destroy( &layout_stats );
314 }
315 test_data_evaluation_destroy( &ts_eval );
316 test_data_setup_destroy( &ts_setup );
317 return TEST_CASE_RESULT_OK;
318 }
319 */
320
321 void pencil_diagram_maker_test_draw_rects_callback ( void *data,
322 const geometry_rectangle_t *rect_a,
323 const geometry_rectangle_t *rect_b )
324 {
325 assert( NULL != data );
326 assert( NULL != rect_a );
327 assert( NULL != rect_b );
328 cairo_t *cr = data;
329 cairo_set_source_rgba( cr, 1.0, 0.6, 0.65, 1.0 );
330 cairo_rectangle( cr,
331 geometry_rectangle_get_left ( rect_a ),
332 geometry_rectangle_get_top ( rect_a ),
333 geometry_rectangle_get_width ( rect_a ),
334 geometry_rectangle_get_height ( rect_a )
335 );
336 cairo_rectangle( cr,
337 geometry_rectangle_get_left ( rect_b ),
338 geometry_rectangle_get_top ( rect_b ),
339 geometry_rectangle_get_width ( rect_b ),
340 geometry_rectangle_get_height ( rect_b )
341 );
342 cairo_stroke( cr );
343
344 geometry_rectangle_t isect;
345 const int err_no_intersect = geometry_rectangle_init_by_intersect( &isect, rect_a, rect_b );
346 if ( err_no_intersect == 0 )
347 {
348 geometry_rectangle_enlarge( &isect, 6.0, 6.0 );
349 geometry_rectangle_shift( &isect, -3.0, -3.0 );
350 cairo_rectangle( cr,
351 geometry_rectangle_get_left ( &isect ),
352 geometry_rectangle_get_top ( &isect ),
353 geometry_rectangle_get_width ( &isect ),
354 geometry_rectangle_get_height ( &isect )
355 );
356 cairo_fill( cr );
357 geometry_rectangle_destroy( &isect );
358 }
359 }
360
361
362 /*
363 * Copyright 2021-2026 Andreas Warnke
364 *
365 * Licensed under the Apache License, Version 2.0 (the "License");
366 * you may not use this file except in compliance with the License.
367 * You may obtain a copy of the License at
368 *
369 * http://www.apache.org/licenses/LICENSE-2.0
370 *
371 * Unless required by applicable law or agreed to in writing, software
372 * distributed under the License is distributed on an "AS IS" BASIS,
373 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
374 * See the License for the specific language governing permissions and
375 * limitations under the License.
376 */