Debian Codegraph @

subprojects/gobject-introspection-tests/gimarshallingtestsextra.c

raw
1 /* gimarshallingtestsextra.c
2 *
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * SPDX-FileCopyrightText: 2016 Thibault Saunier <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <string.h>
21
22 #include "gimarshallingtestsextra.h"
23
24 void
25 gi_marshalling_tests_compare_two_gerrors_in_gvalue (GValue *v, GValue *v1)
26 {
27 GError *error, *error1;
28
29 g_assert_cmpstr (g_type_name (G_VALUE_TYPE (v)), ==,
30 g_type_name (G_TYPE_ERROR));
31 g_assert_cmpstr (g_type_name (G_VALUE_TYPE (v1)), ==,
32 g_type_name (G_TYPE_ERROR));
33
34 error = (GError *) g_value_get_boxed (v);
35 error1 = (GError *) g_value_get_boxed (v1);
36
37 g_assert_cmpint (error->domain, ==, error1->domain);
38 g_assert_cmpint (error->code, ==, error1->code);
39 g_assert_cmpstr (error->message, ==, error1->message);
40 }
41
42 /**
43 * gi_marshalling_tests_nullable_gerror:
44 * @error: (in) (nullable) (transfer full):
45 * an optional #GError.
46 *
47 * So we've got an API which takes a `GError *` as `(in)` argument,
48 * and we want this to be optional, so we added `(nullable)`.
49 *
50 * Returns: 1 or 0, depending if the error was set.
51 */
52 gboolean
53 gi_marshalling_tests_nullable_gerror (GError *error)
54 {
55 gboolean retval = error ? 1 : 0;
56 g_clear_error(&error);
57 return retval;
58 }
59
60 /**
61 * gi_marshalling_tests_ghashtable_enum_none_in:
62 * @hash_table: (element-type gint GIMarshallingTestsExtraEnum) (transfer none):
63 */
64 void
65 gi_marshalling_tests_ghashtable_enum_none_in (GHashTable *hash_table)
66 {
67 g_assert_cmpint (GPOINTER_TO_INT (g_hash_table_lookup (hash_table, GINT_TO_POINTER (1))), ==, GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE1);
68 g_assert_cmpint (GPOINTER_TO_INT (g_hash_table_lookup (hash_table, GINT_TO_POINTER (2))), ==, GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE2);
69 g_assert_cmpint (GPOINTER_TO_INT (g_hash_table_lookup (hash_table, GINT_TO_POINTER (3))), ==, GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE3);
70 }
71
72 /**
73 * gi_marshalling_tests_ghashtable_enum_none_return:
74 *
75 * Returns: (element-type gint GIMarshallingTestsExtraEnum) (transfer none):
76 */
77 GHashTable *
78 gi_marshalling_tests_ghashtable_enum_none_return (void)
79 {
80 static GHashTable *hash_table = NULL;
81
82 if (hash_table == NULL)
83 {
84 hash_table = g_hash_table_new (NULL, NULL);
85 g_hash_table_insert (hash_table, GINT_TO_POINTER (1), GINT_TO_POINTER (GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE1));
86 g_hash_table_insert (hash_table, GINT_TO_POINTER (2), GINT_TO_POINTER (GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE2));
87 g_hash_table_insert (hash_table, GINT_TO_POINTER (3), GINT_TO_POINTER (GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE3));
88 }
89
90 return hash_table;
91 }
92
93 /**
94 * gi_marshalling_tests_filename_copy:
95 * @path_in: (type filename) (nullable)
96 *
97 * Returns: (type filename) (nullable)
98 */
99 gchar *
100 gi_marshalling_tests_filename_copy (gchar *path_in)
101 {
102 return g_strdup (path_in);
103 }
104
105 /**
106 * gi_marshalling_tests_filename_to_glib_repr:
107 * @path_in: (type filename) (nullable)
108 *
109 * Returns: (array length=len) (element-type guint8)
110 */
111 gchar *
112 gi_marshalling_tests_filename_to_glib_repr (gchar *path_in, gsize *len)
113 {
114 *len = strlen (path_in);
115 return g_strdup (path_in);
116 }
117
118 /**
119 * gi_marshalling_tests_filename_exists:
120 * @path: (type filename)
121 */
122 gboolean
123 gi_marshalling_tests_filename_exists (gchar *path)
124 {
125 return g_file_test (path, G_FILE_TEST_EXISTS);
126 }
127
128 /**
129 * gi_marshalling_tests_enum_array_return_type:
130 * @n_members: (out): The number of members
131 *
132 * Returns: (array length=n_members) (transfer full): An array of enum values
133 */
134 GIMarshallingTestsExtraEnum *
135 gi_marshalling_tests_enum_array_return_type (gsize *n_members)
136 {
137 GIMarshallingTestsExtraEnum *res = g_new0 (GIMarshallingTestsExtraEnum, 3);
138
139 *n_members = 3;
140
141 res[0] = GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE1;
142 res[1] = GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE2;
143 res[2] = GI_MARSHALLING_TESTS_EXTRA_ENUM_VALUE3;
144
145 return res;
146 }
147
148 GType
149 gi_marshalling_tests_extra_flags_get_type (void)
150 {
151 static GType type = 0;
152 if (G_UNLIKELY (type == 0))
153 {
154 static const GFlagsValue values[] = {
155 { GI_MARSHALLING_TESTS_EXTRA_FLAGS_VALUE1,
156 "GI_MARSHALLING_TESTS_EXTRA_FLAGS_VALUE1", "value1" },
157 { GI_MARSHALLING_TESTS_EXTRA_FLAGS_VALUE2,
158 "GI_MARSHALLING_TESTS_EXTRA_FLAGS_VALUE2", "value2" },
159 { 0, NULL, NULL }
160 };
161 type = g_flags_register_static (
162 g_intern_static_string ("GIMarshallingTestsExtraFlags"), values);
163 }
164
165 return type;
166 }
167
168 /**
169 * gi_marshalling_tests_extra_flags_large_in:
170 */
171 void
172 gi_marshalling_tests_extra_flags_large_in (GIMarshallingTestsExtraFlags value)
173 {
174 g_assert_cmpint (value, ==, GI_MARSHALLING_TESTS_EXTRA_FLAGS_VALUE2);
175 }
176
177 /**
178 * gi_marshalling_tests_extra_utf8_full_return_invalid:
179 */
180 gchar *
181 gi_marshalling_tests_extra_utf8_full_return_invalid (void)
182 {
183 return g_strdup ("invalid utf8 \xff\xfe");
184 }
185
186 /**
187 * gi_marshalling_tests_extra_utf8_full_out_invalid:
188 * @utf8: (out) (transfer full):
189 */
190 void
191 gi_marshalling_tests_extra_utf8_full_out_invalid (gchar **utf8)
192 {
193 *utf8 = g_strdup ("invalid utf8 \xff\xfe");
194 }