Debian Codegraph @

subprojects/gobject-introspection-tests/regressextra.c

raw
1 /*
2 SPDX-License-Identifier: LGPL-2.1-or-later
3 SPDX-FileCopyrightText: 2018 PyGObject contributors
4 */
5
6 #include <string.h>
7
8 #ifndef GI_TEST_DISABLE_CAIRO
9 #include <cairo.h>
10 #endif /* GI_TEST_DISABLE_CAIRO */
11 #include <glib-object.h>
12
13 #include "regress.h"
14 #include "regressextra.h"
15
16 struct _RegressTestBoxedCWrapper
17 {
18 RegressTestBoxedC *cptr;
19 };
20
21 RegressTestBoxedCWrapper *
22 regress_test_boxed_c_wrapper_new (void)
23 {
24 RegressTestBoxedCWrapper *boxed;
25 boxed = g_slice_new (RegressTestBoxedCWrapper);
26 boxed->cptr = regress_test_boxed_c_new ();
27 return boxed;
28 }
29
30 RegressTestBoxedCWrapper *
31 regress_test_boxed_c_wrapper_copy (RegressTestBoxedCWrapper *self)
32 {
33 RegressTestBoxedCWrapper *ret_boxed;
34 ret_boxed = g_slice_new (RegressTestBoxedCWrapper);
35 ret_boxed->cptr = g_boxed_copy (regress_test_boxed_c_get_type (), self->cptr);
36 return ret_boxed;
37 }
38
39 static void
40 regress_test_boxed_c_wrapper_free (RegressTestBoxedCWrapper *boxed)
41 {
42 g_boxed_free (regress_test_boxed_c_get_type (), boxed->cptr);
43 g_slice_free (RegressTestBoxedCWrapper, boxed);
44 }
45
46 G_DEFINE_BOXED_TYPE (RegressTestBoxedCWrapper,
47 regress_test_boxed_c_wrapper,
48 regress_test_boxed_c_wrapper_copy,
49 regress_test_boxed_c_wrapper_free);
50
51 /**
52 * regress_test_boxed_c_wrapper_get
53 * @self: a #RegressTestBoxedCWrapper objects
54 *
55 * Returns: (transfer none): associated #RegressTestBoxedC
56 **/
57 RegressTestBoxedC *
58 regress_test_boxed_c_wrapper_get (RegressTestBoxedCWrapper *self)
59 {
60 return self->cptr;
61 }
62
63 /**
64 * regress_test_array_of_non_utf8_strings
65 * Returns: (transfer full) (allow-none) (array zero-terminated=1): Array of strings
66 */
67 gchar **
68 regress_test_array_of_non_utf8_strings (void)
69 {
70 char **ret = g_new (char *, 2);
71 ret[0] = g_strdup ("Andr\351 Lur\347at");
72 ret[1] = NULL;
73 return ret;
74 }
75
76 /**
77 * regress_test_array_fixed_boxed_none_out
78 * @objs: (out) (array fixed-size=2) (transfer none): An array of #RegressTestBoxedC
79 **/
80 void
81 regress_test_array_fixed_boxed_none_out (RegressTestBoxedC ***objs)
82 {
83 static RegressTestBoxedC **arr;
84
85 if (arr == NULL)
86 {
87 arr = g_new0 (RegressTestBoxedC *, 3);
88 arr[0] = regress_test_boxed_c_new ();
89 arr[1] = regress_test_boxed_c_new ();
90 }
91
92 *objs = arr;
93 }
94
95 /**
96 * regress_test_gvalue_out_boxed:
97 * @value: (out) (transfer full): the output gvalue
98 * @init: (in): the initialisation value
99 **/
100 void
101 regress_test_gvalue_out_boxed (GValue *value, int init)
102 {
103 RegressTestBoxed rtb;
104 GValue v = G_VALUE_INIT;
105
106 memset (&rtb, 0, sizeof (rtb));
107 rtb.some_int8 = init;
108 g_value_init (&v, REGRESS_TEST_TYPE_BOXED);
109 g_value_set_boxed (&v, &rtb);
110 *value = v;
111 }
112
113 /**
114 * regress_test_glist_boxed_none_return
115 * Return value: (element-type RegressTestBoxedC) (transfer none):
116 **/
117 GList *
118 regress_test_glist_boxed_none_return (guint count)
119 {
120 static GList *list = NULL;
121 if (!list)
122 {
123 while (count > 0)
124 {
125 list = g_list_prepend (list, regress_test_boxed_c_new ());
126 count--;
127 }
128 }
129
130 return list;
131 }
132
133 /**
134 * regress_test_glist_boxed_full_return
135 * Return value: (element-type RegressTestBoxedC) (transfer full):
136 **/
137 GList *
138 regress_test_glist_boxed_full_return (guint count)
139 {
140 GList *list = NULL;
141 while (count > 0)
142 {
143 list = g_list_prepend (list, regress_test_boxed_c_new ());
144 count--;
145 }
146 return list;
147 }
148
149 /**
150 * regress_test_array_of_fundamental_objects_in
151 * @list: (array length=len) (element-type RegressTestFundamentalObject): An array of #RegressTestFundamentalObject
152 * @len: length of the list
153 **/
154 gboolean
155 regress_test_array_of_fundamental_objects_in (RegressTestFundamentalObject **list, gsize len)
156 {
157 gsize i;
158
159 for (i = 0; i < len; i++)
160 {
161 if (!REGRESS_TEST_IS_FUNDAMENTAL_OBJECT (list[i]))
162 {
163 return FALSE;
164 }
165 }
166 return TRUE;
167 }
168
169 /**
170 * regress_test_array_of_fundamental_objects_out
171 * @len: (out): length of the list
172 * Returns: (array length=len) (transfer full): An array of #RegressTestFundamentalObject
173 **/
174 RegressTestFundamentalObject **
175 regress_test_array_of_fundamental_objects_out (gsize *len)
176 {
177 RegressTestFundamentalObject **objs;
178 int i;
179
180 objs = g_new (RegressTestFundamentalObject *, 2);
181
182 for (i = 0; i < 2; i++)
183 {
184 objs[i] = (RegressTestFundamentalObject *) regress_test_fundamental_sub_object_new ("foo");
185 }
186 *len = 2;
187 return objs;
188 }
189
190 /**
191 * regress_test_fundamental_argument_in
192 * @obj: (transfer full): A #RegressTestFundamentalObject
193 **/
194 gboolean
195 regress_test_fundamental_argument_in (RegressTestFundamentalObject *obj)
196 {
197 gboolean retval = REGRESS_TEST_IS_FUNDAMENTAL_OBJECT (obj);
198 regress_test_fundamental_object_unref(obj);
199 return retval;
200 }
201
202 /**
203 * regress_test_fundamental_argument_out
204 * @obj: (transfer none): A #RegressTestFundamentalObject
205 * Returns: (transfer none): Same #RegressTestFundamentalObject
206 **/
207 RegressTestFundamentalObject *
208 regress_test_fundamental_argument_out (RegressTestFundamentalObject *obj)
209 {
210 return obj;
211 }
212
213 #ifndef GI_TEST_DISABLE_CAIRO
214
215 /**
216 * regress_test_cairo_context_none_return:
217 *
218 * Returns: (transfer none):
219 */
220 cairo_t *
221 regress_test_cairo_context_none_return (void)
222 {
223 static cairo_t *cr;
224
225 if (cr == NULL)
226 {
227 cairo_surface_t *surface;
228 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
229 cr = cairo_create (surface);
230 cairo_surface_destroy (surface);
231 }
232
233 return cr;
234 }
235
236 /**
237 * regress_test_cairo_context_full_in:
238 * @context: (transfer full):
239 */
240 void
241 regress_test_cairo_context_full_in (cairo_t *context)
242 {
243 cairo_destroy (context);
244 }
245
246 /**
247 * regress_test_cairo_path_full_return:
248 *
249 * Returns: (transfer full):
250 */
251 cairo_path_t *
252 regress_test_cairo_path_full_return (void)
253 {
254 cairo_t *cr = regress_test_cairo_context_none_return ();
255
256 return cairo_copy_path (cr);
257 }
258
259 /**
260 * regress_test_cairo_path_none_in:
261 * @path: (transfer none):
262 */
263 void
264 regress_test_cairo_path_none_in (cairo_path_t *path)
265 {
266 cairo_t *cr = regress_test_cairo_context_full_return ();
267 cairo_append_path (cr, path);
268 g_assert (cairo_status (cr) == CAIRO_STATUS_SUCCESS);
269 cairo_destroy (cr);
270 }
271
272 /**
273 * regress_test_cairo_path_full_in_full_return:
274 * @path: (transfer full):
275 *
276 * Returns: (transfer full):
277 */
278 cairo_path_t *
279 regress_test_cairo_path_full_in_full_return (cairo_path_t *path)
280 {
281 return path;
282 }
283
284 /**
285 * regress_test_cairo_pattern_full_in:
286 * @pattern: (transfer full):
287 */
288 void
289 regress_test_cairo_pattern_full_in (cairo_pattern_t *pattern)
290 {
291 cairo_pattern_destroy (pattern);
292 }
293
294 /**
295 * regress_test_cairo_pattern_none_in:
296 * @pattern: (transfer none):
297 */
298 void
299 regress_test_cairo_pattern_none_in (cairo_pattern_t *pattern)
300 {
301 cairo_t *cr = regress_test_cairo_context_full_return ();
302 cairo_set_source (cr, pattern);
303 g_assert (cairo_status (cr) == CAIRO_STATUS_SUCCESS);
304 cairo_destroy (cr);
305 }
306
307 /**
308 * regress_test_cairo_pattern_none_return:
309 *
310 * Returns: (transfer none):
311 */
312 cairo_pattern_t *
313 regress_test_cairo_pattern_none_return (void)
314 {
315 static cairo_pattern_t *pattern;
316
317 if (pattern == NULL)
318 {
319 pattern = cairo_pattern_create_rgb (0.1, 0.2, 0.3);
320 }
321
322 return pattern;
323 }
324
325 /**
326 * regress_test_cairo_pattern_full_return:
327 *
328 * Returns: (transfer full):
329 */
330 cairo_pattern_t *
331 regress_test_cairo_pattern_full_return (void)
332 {
333 cairo_pattern_t *pattern = cairo_pattern_create_rgb (0.5, 0.6, 0.7);
334 return pattern;
335 }
336
337 /**
338 * regress_test_cairo_region_full_in:
339 * @region: (transfer full):
340 */
341 void
342 regress_test_cairo_region_full_in (cairo_region_t *region)
343 {
344 cairo_region_destroy (region);
345 }
346
347 /**
348 * regress_test_cairo_surface_full_in:
349 * @surface: (transfer full):
350 */
351 void
352 regress_test_cairo_surface_full_in (cairo_surface_t *surface)
353 {
354 g_assert (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32);
355 g_assert (cairo_image_surface_get_width (surface) == 10);
356 g_assert (cairo_image_surface_get_height (surface) == 10);
357 cairo_surface_destroy (surface);
358 }
359
360 /**
361 * regress_test_cairo_font_options_full_return:
362 *
363 * Returns: (transfer full):
364 */
365 cairo_font_options_t *
366 regress_test_cairo_font_options_full_return (void)
367 {
368 return cairo_font_options_create ();
369 }
370
371 /**
372 * regress_test_cairo_font_options_none_return:
373 *
374 * Returns: (transfer none):
375 */
376 cairo_font_options_t *
377 regress_test_cairo_font_options_none_return (void)
378 {
379 static cairo_font_options_t *options;
380
381 if (options == NULL)
382 options = cairo_font_options_create ();
383
384 return options;
385 }
386
387 /**
388 * regress_test_cairo_font_options_full_in:
389 * @options: (transfer full):
390 */
391 void
392 regress_test_cairo_font_options_full_in (cairo_font_options_t *options)
393 {
394 cairo_font_options_destroy (options);
395 }
396
397 /**
398 * regress_test_cairo_font_options_none_in:
399 * @options: (transfer none):
400 */
401 void
402 regress_test_cairo_font_options_none_in (cairo_font_options_t *options G_GNUC_UNUSED)
403 {
404 }
405
406 /**
407 * regress_test_cairo_font_face_full_return:
408 * @cairo: (transfer none):
409 *
410 * Returns: (transfer full):
411 */
412 cairo_font_face_t *
413 regress_test_cairo_font_face_full_return (cairo_t *cairo)
414 {
415 cairo_font_face_t *font_face = cairo_get_font_face (cairo);
416
417 return cairo_font_face_reference (font_face);
418 }
419
420 /**
421 * regress_test_cairo_scaled_font_full_return:
422 * @cairo: (transfer none):
423 *
424 * Returns: (transfer full):
425 */
426 cairo_scaled_font_t *
427 regress_test_cairo_scaled_font_full_return (cairo_t *cairo)
428 {
429 cairo_scaled_font_t *scaled_font = cairo_get_scaled_font (cairo);
430
431 return cairo_scaled_font_reference (scaled_font);
432 }
433
434 /**
435 * regress_test_cairo_matrix_none_in:
436 * @matrix: (transfer none):
437 */
438 void
439 regress_test_cairo_matrix_none_in (const cairo_matrix_t *matrix)
440 {
441 cairo_matrix_t m = *matrix;
442 g_assert (m.x0 == 0);
443 g_assert (m.y0 == 0);
444 g_assert (m.xx == 1);
445 g_assert (m.xy == 0);
446 g_assert (m.yy == 1);
447 g_assert (m.yx == 0);
448 }
449
450 /**
451 * regress_test_cairo_matrix_none_return:
452 * Returns: (transfer none):
453 */
454 cairo_matrix_t *
455 regress_test_cairo_matrix_none_return (void)
456 {
457 static cairo_matrix_t matrix;
458 cairo_matrix_init_identity (&matrix);
459 return &matrix;
460 }
461
462 /**
463 * regress_test_cairo_matrix_out_caller_allocates:
464 * @matrix: (out):
465 */
466 void
467 regress_test_cairo_matrix_out_caller_allocates (cairo_matrix_t *matrix)
468 {
469 cairo_matrix_t m;
470 cairo_matrix_init_identity (&m);
471 *matrix = m;
472 }
473
474 #endif
475
476 G_DEFINE_TYPE (RegressTestAction, regress_test_action, G_TYPE_INITIALLY_UNOWNED)
477
478 enum
479 {
480 SIGNAL_0,
481 ACTION_SIGNAL,
482 ACTION2_SIGNAL,
483 LAST_SIGNAL
484 };
485
486 static guint regress_test_action_signals[LAST_SIGNAL] = { 0 };
487
488 static RegressTestAction *
489 regress_test_action_do_action (RegressTestAction *self G_GNUC_UNUSED)
490 {
491 RegressTestAction *ret = g_object_new (regress_test_action_get_type (), NULL);
492
493 return ret;
494 }
495
496 static RegressTestAction *
497 regress_test_action_do_action2 (RegressTestAction *self G_GNUC_UNUSED)
498 {
499 return NULL;
500 }
501
502 static void
503 regress_test_action_init (RegressTestAction *self G_GNUC_UNUSED)
504 {
505 }
506
507 static void
508 regress_test_action_class_init (RegressTestActionClass *klass)
509 {
510 /**
511 * RegressTestAction::action:
512 *
513 * An action signal.
514 *
515 * Returns: (transfer full): another #RegressTestAction
516 */
517 regress_test_action_signals[ACTION_SIGNAL] =
518 g_signal_new_class_handler ("action",
519 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
520 G_CALLBACK (regress_test_action_do_action), NULL, NULL,
521 NULL, regress_test_action_get_type (), 0);
522
523 /**
524 * RegressTestAction::action2:
525 *
526 * Another action signal.
527 *
528 * Returns: (transfer full): another #RegressTestAction
529 */
530 regress_test_action_signals[ACTION2_SIGNAL] =
531 g_signal_new_class_handler ("action2",
532 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
533 G_CALLBACK (regress_test_action_do_action2), NULL, NULL,
534 NULL, regress_test_action_get_type (), 0);
535 }
536
537 /*
538 * RegressBitmask
539 *
540 * Mimic a primitive, fundamental type.
541 */
542
543 static void
544 regress_value_init_bitmask (GValue *value)
545 {
546 value->data[0].v_uint64 = 0;
547 }
548
549 static void
550 regress_value_copy_bitmask (const GValue *src_value, GValue *dest_value)
551 {
552 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
553 }
554
555 static void
556 _value_transform_uint64_bitmask (const GValue *src_value, GValue *dest_value)
557 {
558 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
559 }
560
561 static void
562 _value_transform_bitmask_uint64 (const GValue *src_value, GValue *dest_value)
563 {
564 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
565 }
566
567 static const GTypeValueTable _regress_bitmask_value_table = {
568 regress_value_init_bitmask,
569 NULL,
570 regress_value_copy_bitmask,
571 NULL,
572 (char *) NULL,
573 NULL,
574 (char *) NULL,
575 NULL
576 };
577
578 GType
579 regress_bitmask_get_type (void)
580 {
581 static GType regress_bitmask_type = 0;
582
583 if (g_once_init_enter (&regress_bitmask_type))
584 {
585 GTypeInfo _info = { 0, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, &_regress_bitmask_value_table };
586 GTypeFundamentalInfo _finfo = { 0 };
587 GType _type = g_type_register_fundamental (
588 g_type_fundamental_next (),
589 "RegressBitmask", &_info, &_finfo, 0);
590
591 g_once_init_leave (&regress_bitmask_type, _type);
592
593 g_value_register_transform_func (
594 REGRESS_TYPE_BITMASK, G_TYPE_UINT64,
595 _value_transform_bitmask_uint64);
596 g_value_register_transform_func (
597 G_TYPE_UINT64, REGRESS_TYPE_BITMASK,
598 _value_transform_uint64_bitmask);
599 }
600
601 return regress_bitmask_type;
602 }