Debian Codegraph @

subprojects/libglnx/tests/testing-helper.c

raw
1 /*
2 * Based on glib/tests/testing-helper.c from GLib
3 *
4 * Copyright 2018-2022 Collabora Ltd.
5 * Copyright 2019 Руслан Ижбулатов
6 * Copyright 2018-2022 Endless OS Foundation LLC
7 *
8 * SPDX-License-Identifier: LGPL-2.1-or-later
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General
21 * Public License along with this library; if not, see
22 * <http://www.gnu.org/licenses/>.
23 */
24
25 #include "libglnx-config.h"
26 #include "libglnx.h"
27
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include <locale.h>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 static const char *null = NULL;
36 static const char *nonnull = "not null";
37
38 static void
39 test_pass (void)
40 {
41 }
42
43 static void
44 test_messages (void)
45 {
46 g_test_message ("This message has multiple lines.\n"
47 "In older GLib, it would corrupt TAP output.\n"
48 "That's why libglnx provides a wrapper.\n");
49 }
50
51 static void
52 test_assertion_failure_true (void)
53 {
54 g_assert_true (null != NULL);
55 }
56
57 static void
58 test_assertion_failure_false (void)
59 {
60 g_assert_false (null == NULL);
61 }
62
63 static void
64 test_assertion_failure_nonnull (void)
65 {
66 g_assert_nonnull (null);
67 }
68
69 static void
70 test_assertion_failure_null (void)
71 {
72 g_assert_null (nonnull);
73 }
74
75 static void
76 test_assertion_failure_mem_null_nonnull (void)
77 {
78 g_assert_cmpmem (null, 0, nonnull, strlen (nonnull));
79 }
80
81 static void
82 test_assertion_failure_mem_nonnull_null (void)
83 {
84 g_assert_cmpmem (nonnull, strlen (nonnull), null, 0);
85 }
86
87 static void
88 test_assertion_failure_mem_len (void)
89 {
90 g_assert_cmpmem (nonnull, strlen (nonnull), nonnull, 0);
91 }
92
93 static void
94 test_assertion_failure_mem_cmp (void)
95 {
96 g_assert_cmpmem (nonnull, 4, nonnull + 4, 4);
97 }
98
99 static void
100 test_assertion_failure_cmpfloat_with_epsilon (void)
101 {
102 g_assert_cmpfloat_with_epsilon (1.0, 1.5, 0.001);
103 }
104
105 static void
106 test_assertion_failure_cmpvariant (void)
107 {
108 g_autoptr(GVariant) a = g_variant_ref_sink (g_variant_new ("i", 42));
109 g_autoptr(GVariant) b = g_variant_ref_sink (g_variant_new ("u", 42));
110
111 g_assert_cmpvariant (a, b);
112 }
113
114 static void
115 test_assertion_failure_errno (void)
116 {
117 g_assert_no_errno (mkdir ("/", 0755));
118 }
119
120 static void
121 test_assertion_failure_cmpstrv_null_nonnull (void)
122 {
123 const char * const b[] = { NULL };
124
125 g_assert_cmpstrv (NULL, b);
126 }
127
128 static void
129 test_assertion_failure_cmpstrv_nonnull_null (void)
130 {
131 const char * const a[] = { NULL };
132
133 g_assert_cmpstrv (a, NULL);
134 }
135
136 static void
137 test_assertion_failure_cmpstrv_len (void)
138 {
139 const char * const a[] = { "one", NULL };
140 const char * const b[] = { NULL };
141
142 g_assert_cmpstrv (a, b);
143 }
144
145 static void
146 test_assertion_failure_cmpstrv_cmp (void)
147 {
148 const char * const a[] = { "one", "two", NULL };
149 const char * const b[] = { "one", "three", NULL };
150
151 g_assert_cmpstrv (a, b);
152 }
153
154 static void
155 test_skip (void)
156 {
157 g_test_skip ("not enough tea");
158 }
159
160 static void
161 test_skip_printf (void)
162 {
163 const char *beverage = "coffee";
164
165 g_test_skip_printf ("not enough %s", beverage);
166 }
167
168 static void
169 test_fail (void)
170 {
171 g_test_fail ();
172 }
173
174 static void
175 test_fail_printf (void)
176 {
177 g_test_fail_printf ("this test intentionally left failing");
178 }
179
180 static void
181 test_incomplete (void)
182 {
183 g_test_incomplete ("mind reading not implemented yet");
184 }
185
186 static void
187 test_incomplete_printf (void)
188 {
189 const char *operation = "telekinesis";
190
191 g_test_incomplete_printf ("%s not implemented yet", operation);
192 }
193
194 static void
195 test_summary (void)
196 {
197 g_test_summary ("Tests that g_test_summary() works with TAP, by outputting a "
198 "known summary message in testing-helper, and checking for "
199 "it in the TAP output later.");
200 }
201
202 int
203 main (int argc,
204 char *argv[])
205 {
206 char *argv1;
207
208 setlocale (LC_ALL, "");
209
210 #ifdef G_OS_WIN32
211 /* Windows opens std streams in text mode, with \r\n EOLs.
212 * Sometimes it's easier to force a switch to binary mode than
213 * to account for extra \r in testcases.
214 */
215 setmode (fileno (stdout), O_BINARY);
216 #endif
217
218 g_return_val_if_fail (argc > 1, 1);
219 argv1 = argv[1];
220
221 if (argc > 2)
222 memmove (&argv[1], &argv[2], (argc - 2) * sizeof (char *));
223
224 argc -= 1;
225 argv[argc] = NULL;
226
227 if (g_strcmp0 (argv1, "init-null-argv0") == 0)
228 {
229 int test_argc = 0;
230 char *test_argva[1] = { NULL };
231 char **test_argv = test_argva;
232
233 /* Test that `g_test_init()` can handle being called with an empty argv
234 * and argc == 0. While this isn’t recommended, it is possible for another
235 * process to use execve() to call a gtest process this way, so we’d
236 * better handle it gracefully.
237 *
238 * This test can’t be run after `g_test_init()` has been called normally,
239 * as it isn’t allowed to be called more than once in a process. */
240 g_test_init (&test_argc, &test_argv, NULL);
241
242 return 0;
243 }
244
245 g_test_init (&argc, &argv, NULL);
246 g_test_disable_crash_reporting ();
247 #if GLIB_CHECK_VERSION(2, 38, 0)
248 g_test_set_nonfatal_assertions ();
249 #endif
250
251 if (g_strcmp0 (argv1, "pass") == 0)
252 {
253 g_test_add_func ("/pass", test_pass);
254 }
255 else if (g_strcmp0 (argv1, "messages") == 0)
256 {
257 g_test_add_func ("/messages", test_messages);
258 }
259 else if (g_strcmp0 (argv1, "skip") == 0)
260 {
261 g_test_add_func ("/skip", test_skip);
262 }
263 else if (g_strcmp0 (argv1, "skip-printf") == 0)
264 {
265 g_test_add_func ("/skip-printf", test_skip_printf);
266 }
267 else if (g_strcmp0 (argv1, "incomplete") == 0)
268 {
269 g_test_add_func ("/incomplete", test_incomplete);
270 }
271 else if (g_strcmp0 (argv1, "incomplete-printf") == 0)
272 {
273 g_test_add_func ("/incomplete-printf", test_incomplete_printf);
274 }
275 else if (g_strcmp0 (argv1, "fail") == 0)
276 {
277 g_test_add_func ("/fail", test_fail);
278 }
279 else if (g_strcmp0 (argv1, "fail-printf") == 0)
280 {
281 g_test_add_func ("/fail-printf", test_fail_printf);
282 }
283 else if (g_strcmp0 (argv1, "all-non-failures") == 0)
284 {
285 g_test_add_func ("/pass", test_pass);
286 g_test_add_func ("/skip", test_skip);
287 g_test_add_func ("/incomplete", test_incomplete);
288 }
289 else if (g_strcmp0 (argv1, "all") == 0)
290 {
291 g_test_add_func ("/pass", test_pass);
292 g_test_add_func ("/skip", test_skip);
293 g_test_add_func ("/incomplete", test_incomplete);
294 g_test_add_func ("/fail", test_fail);
295 }
296 else if (g_strcmp0 (argv1, "skip-options") == 0)
297 {
298 /* The caller is expected to skip some of these with
299 * -p/-r, -s/-x and/or --GTestSkipCount */
300 g_test_add_func ("/a", test_pass);
301 g_test_add_func ("/b", test_pass);
302 g_test_add_func ("/b/a", test_pass);
303 g_test_add_func ("/b/b", test_pass);
304 g_test_add_func ("/b/b/a", test_pass);
305 g_test_add_func ("/prefix/a", test_pass);
306 g_test_add_func ("/prefix/b/b", test_pass);
307 g_test_add_func ("/prefix-long/a", test_pass);
308 g_test_add_func ("/c/a", test_pass);
309 g_test_add_func ("/d/a", test_pass);
310 }
311 else if (g_strcmp0 (argv1, "summary") == 0)
312 {
313 g_test_add_func ("/summary", test_summary);
314 }
315 else if (g_strcmp0 (argv1, "assertion-failures") == 0)
316 {
317 /* Use -p to select a specific one of these */
318 #define T(x) g_test_add_func ("/assertion-failure/" #x, test_assertion_failure_ ## x)
319 T (true);
320 T (false);
321 T (nonnull);
322 T (null);
323 T (mem_null_nonnull);
324 T (mem_nonnull_null);
325 T (mem_len);
326 T (mem_cmp);
327 T (cmpfloat_with_epsilon);
328 T (cmpvariant);
329 T (errno);
330 T (cmpstrv_null_nonnull);
331 T (cmpstrv_nonnull_null);
332 T (cmpstrv_len);
333 T (cmpstrv_cmp);
334 #undef T
335 }
336 else
337 {
338 g_assert_not_reached ();
339 }
340
341 return g_test_run ();
342 }