nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* GLib testing framework examples and tests |
2 | * Copyright (C) 2007 Imendio AB |
||
3 | * Authors: Tim Janik |
||
4 | * |
||
5 | * This work is provided "as is"; redistribution and modification |
||
6 | * in whole or in part, in any medium, physical or electronic is |
||
7 | * permitted without restriction. |
||
8 | * |
||
9 | * This work is distributed in the hope that it will be useful, |
||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
12 | * |
||
13 | * In no event shall the authors or contributors be liable for any |
||
14 | * direct, indirect, incidental, special, exemplary, or consequential |
||
15 | * damages (including, but not limited to, procurement of substitute |
||
16 | * goods or services; loss of use, data, or profits; or business |
||
17 | * interruption) however caused and on any theory of liability, whether |
||
18 | * in contract, strict liability, or tort (including negligence or |
||
19 | * otherwise) arising in any way out of the use of this software, even |
||
20 | * if advised of the possibility of such damage. |
||
21 | */ |
||
22 | |||
23 | /* We want to distinguish between messages originating from libglib |
||
24 | * and messages originating from this program. |
||
25 | */ |
||
26 | #undef G_LOG_DOMAIN |
||
27 | #define G_LOG_DOMAIN "testing" |
||
28 | |||
29 | #include <glib.h> |
||
30 | |||
31 | #include <stdlib.h> |
||
32 | #include <string.h> |
||
33 | |||
34 | /* test assertion variants */ |
||
35 | static void |
||
36 | test_assertions_bad_cmpstr (void) |
||
37 | { |
||
38 | g_assert_cmpstr ("fzz", !=, "fzz"); |
||
39 | exit (0); |
||
40 | } |
||
41 | |||
42 | static void |
||
43 | test_assertions_bad_cmpint (void) |
||
44 | { |
||
45 | g_assert_cmpint (4, !=, 4); |
||
46 | exit (0); |
||
47 | } |
||
48 | |||
49 | static void |
||
50 | test_assertions_bad_cmpmem_len (void) |
||
51 | { |
||
52 | g_assert_cmpmem ("foo", 3, "foot", 4); |
||
53 | exit (0); |
||
54 | } |
||
55 | |||
56 | static void |
||
57 | test_assertions_bad_cmpmem_data (void) |
||
58 | { |
||
59 | g_assert_cmpmem ("foo", 3, "fzz", 3); |
||
60 | exit (0); |
||
61 | } |
||
62 | |||
63 | static void |
||
64 | test_assertions (void) |
||
65 | { |
||
66 | gchar *fuu; |
||
67 | g_assert_cmpint (1, >, 0); |
||
68 | g_assert_cmphex (2, ==, 2); |
||
69 | g_assert_cmpfloat (3.3, !=, 7); |
||
70 | g_assert_cmpfloat (7, <=, 3 + 4); |
||
71 | g_assert (TRUE); |
||
72 | g_assert_cmpstr ("foo", !=, "faa"); |
||
73 | fuu = g_strdup_printf ("f%s", "uu"); |
||
74 | g_test_queue_free (fuu); |
||
75 | g_assert_cmpstr ("foo", !=, fuu); |
||
76 | g_assert_cmpstr ("fuu", ==, fuu); |
||
77 | g_assert_cmpstr (NULL, <, ""); |
||
78 | g_assert_cmpstr (NULL, ==, NULL); |
||
79 | g_assert_cmpstr ("", >, NULL); |
||
80 | g_assert_cmpstr ("foo", <, "fzz"); |
||
81 | g_assert_cmpstr ("fzz", >, "faa"); |
||
82 | g_assert_cmpstr ("fzz", ==, "fzz"); |
||
83 | g_assert_cmpmem ("foo", 3, "foot", 3); |
||
84 | |||
85 | g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstr", 0, 0); |
||
86 | g_test_trap_assert_failed (); |
||
87 | g_test_trap_assert_stderr ("*assertion failed*"); |
||
88 | |||
89 | g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpint", 0, 0); |
||
90 | g_test_trap_assert_failed (); |
||
91 | g_test_trap_assert_stderr ("*assertion failed*"); |
||
92 | |||
93 | g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpmem_len", 0, 0); |
||
94 | g_test_trap_assert_failed (); |
||
95 | g_test_trap_assert_stderr ("*assertion failed*len*"); |
||
96 | |||
97 | g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpmem_data", 0, 0); |
||
98 | g_test_trap_assert_failed (); |
||
99 | g_test_trap_assert_stderr ("*assertion failed*"); |
||
100 | g_test_trap_assert_stderr_unmatched ("*assertion failed*len*"); |
||
101 | } |
||
102 | |||
103 | /* test g_test_timer* API */ |
||
104 | static void |
||
105 | test_timer (void) |
||
106 | { |
||
107 | double ttime; |
||
108 | g_test_timer_start(); |
||
109 | g_assert_cmpfloat (g_test_timer_last(), ==, 0); |
||
110 | g_usleep (25 * 1000); |
||
111 | ttime = g_test_timer_elapsed(); |
||
112 | g_assert_cmpfloat (ttime, >, 0); |
||
113 | g_assert_cmpfloat (g_test_timer_last(), ==, ttime); |
||
114 | g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime); |
||
115 | g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */ |
||
116 | } |
||
117 | |||
118 | #ifdef G_OS_UNIX |
||
119 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
||
120 | |||
121 | /* fork out for a failing test */ |
||
122 | static void |
||
123 | test_fork_fail (void) |
||
124 | { |
||
125 | if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) |
||
126 | { |
||
127 | g_assert_not_reached(); |
||
128 | } |
||
129 | g_test_trap_assert_failed(); |
||
130 | g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*"); |
||
131 | } |
||
132 | |||
133 | /* fork out to assert stdout and stderr patterns */ |
||
134 | static void |
||
135 | test_fork_patterns (void) |
||
136 | { |
||
137 | if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) |
||
138 | { |
||
139 | g_print ("some stdout text: somagic17\n"); |
||
140 | g_printerr ("some stderr text: semagic43\n"); |
||
141 | exit (0); |
||
142 | } |
||
143 | g_test_trap_assert_passed(); |
||
144 | g_test_trap_assert_stdout ("*somagic17*"); |
||
145 | g_test_trap_assert_stderr ("*semagic43*"); |
||
146 | } |
||
147 | |||
148 | /* fork out for a timeout test */ |
||
149 | static void |
||
150 | test_fork_timeout (void) |
||
151 | { |
||
152 | /* allow child to run for only a fraction of a second */ |
||
153 | if (g_test_trap_fork (0.11 * 1000000, 0)) |
||
154 | { |
||
155 | /* loop and sleep forever */ |
||
156 | while (TRUE) |
||
157 | g_usleep (1000 * 1000); |
||
158 | } |
||
159 | g_test_trap_assert_failed(); |
||
160 | g_assert (g_test_trap_reached_timeout()); |
||
161 | } |
||
162 | |||
163 | G_GNUC_END_IGNORE_DEPRECATIONS |
||
164 | #endif /* G_OS_UNIX */ |
||
165 | |||
166 | static void |
||
167 | test_subprocess_fail (void) |
||
168 | { |
||
169 | if (g_test_subprocess ()) |
||
170 | { |
||
171 | g_assert_not_reached (); |
||
172 | return; |
||
173 | } |
||
174 | |||
175 | g_test_trap_subprocess (NULL, 0, 0); |
||
176 | g_test_trap_assert_failed (); |
||
177 | g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail*should not be reached*"); |
||
178 | } |
||
179 | |||
180 | static void |
||
181 | test_subprocess_no_such_test (void) |
||
182 | { |
||
183 | if (g_test_subprocess ()) |
||
184 | { |
||
185 | g_test_trap_subprocess ("/trap_subprocess/this-test-does-not-exist", 0, 0); |
||
186 | g_assert_not_reached (); |
||
187 | return; |
||
188 | } |
||
189 | g_test_trap_subprocess (NULL, 0, 0); |
||
190 | g_test_trap_assert_failed (); |
||
191 | g_test_trap_assert_stderr ("*test does not exist*"); |
||
192 | g_test_trap_assert_stderr_unmatched ("*should not be reached*"); |
||
193 | } |
||
194 | |||
195 | static void |
||
196 | test_subprocess_patterns (void) |
||
197 | { |
||
198 | if (g_test_subprocess ()) |
||
199 | { |
||
200 | g_print ("some stdout text: somagic17\n"); |
||
201 | g_printerr ("some stderr text: semagic43\n"); |
||
202 | exit (0); |
||
203 | } |
||
204 | g_test_trap_subprocess (NULL, 0, 0); |
||
205 | g_test_trap_assert_passed (); |
||
206 | g_test_trap_assert_stdout ("*somagic17*"); |
||
207 | g_test_trap_assert_stderr ("*semagic43*"); |
||
208 | } |
||
209 | |||
210 | static void |
||
211 | test_subprocess_timeout (void) |
||
212 | { |
||
213 | if (g_test_subprocess ()) |
||
214 | { |
||
215 | /* loop and sleep forever */ |
||
216 | while (TRUE) |
||
217 | g_usleep (1000 * 1000); |
||
218 | return; |
||
219 | } |
||
220 | /* allow child to run for only a fraction of a second */ |
||
221 | g_test_trap_subprocess (NULL, 0.11 * 1000000, 0); |
||
222 | g_test_trap_assert_failed (); |
||
223 | g_assert (g_test_trap_reached_timeout ()); |
||
224 | } |
||
225 | |||
226 | /* run a test with fixture setup and teardown */ |
||
227 | typedef struct { |
||
228 | guint seed; |
||
229 | guint prime; |
||
230 | gchar *msg; |
||
231 | } Fixturetest; |
||
232 | static void |
||
233 | fixturetest_setup (Fixturetest *fix, |
||
234 | gconstpointer test_data) |
||
235 | { |
||
236 | g_assert (test_data == (void*) 0xc0cac01a); |
||
237 | fix->seed = 18; |
||
238 | fix->prime = 19; |
||
239 | fix->msg = g_strdup_printf ("%d", fix->prime); |
||
240 | } |
||
241 | static void |
||
242 | fixturetest_test (Fixturetest *fix, |
||
243 | gconstpointer test_data) |
||
244 | { |
||
245 | guint prime = g_spaced_primes_closest (fix->seed); |
||
246 | g_assert_cmpint (prime, ==, fix->prime); |
||
247 | prime = g_ascii_strtoull (fix->msg, NULL, 0); |
||
248 | g_assert_cmpint (prime, ==, fix->prime); |
||
249 | g_assert (test_data == (void*) 0xc0cac01a); |
||
250 | } |
||
251 | static void |
||
252 | fixturetest_teardown (Fixturetest *fix, |
||
253 | gconstpointer test_data) |
||
254 | { |
||
255 | g_assert (test_data == (void*) 0xc0cac01a); |
||
256 | g_free (fix->msg); |
||
257 | } |
||
258 | |||
259 | static struct { |
||
260 | int bit, vint1, vint2, irange; |
||
261 | long double vdouble, drange; |
||
262 | } shared_rand_state; |
||
263 | |||
264 | static void |
||
265 | test_rand1 (void) |
||
266 | { |
||
267 | shared_rand_state.bit = g_test_rand_bit(); |
||
268 | shared_rand_state.vint1 = g_test_rand_int(); |
||
269 | shared_rand_state.vint2 = g_test_rand_int(); |
||
270 | g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2); |
||
271 | shared_rand_state.irange = g_test_rand_int_range (17, 35); |
||
272 | g_assert_cmpint (shared_rand_state.irange, >=, 17); |
||
273 | g_assert_cmpint (shared_rand_state.irange, <=, 35); |
||
274 | shared_rand_state.vdouble = g_test_rand_double(); |
||
275 | shared_rand_state.drange = g_test_rand_double_range (-999, +17); |
||
276 | g_assert_cmpfloat (shared_rand_state.drange, >=, -999); |
||
277 | g_assert_cmpfloat (shared_rand_state.drange, <=, +17); |
||
278 | } |
||
279 | |||
280 | static void |
||
281 | test_rand2 (void) |
||
282 | { |
||
283 | /* this test only works if run after test1. |
||
284 | * we do this to check that random number generators |
||
285 | * are reseeded upon fixture setup. |
||
286 | */ |
||
287 | g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit()); |
||
288 | g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int()); |
||
289 | g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int()); |
||
290 | g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35)); |
||
291 | g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double()); |
||
292 | g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17)); |
||
293 | } |
||
294 | |||
295 | static void |
||
296 | test_data_test (gconstpointer test_data) |
||
297 | { |
||
298 | g_assert (test_data == (void*) 0xc0c0baba); |
||
299 | } |
||
300 | |||
301 | static void |
||
302 | test_random_conversions (void) |
||
303 | { |
||
304 | /* very simple conversion test using random numbers */ |
||
305 | int vint = g_test_rand_int(); |
||
306 | char *err, *str = g_strdup_printf ("%d", vint); |
||
307 | gint64 vint64 = g_ascii_strtoll (str, &err, 10); |
||
308 | g_assert_cmphex (vint, ==, vint64); |
||
309 | g_assert (!err || *err == 0); |
||
310 | g_free (str); |
||
311 | } |
||
312 | |||
313 | static gboolean |
||
314 | fatal_handler (const gchar *log_domain, |
||
315 | GLogLevelFlags log_level, |
||
316 | const gchar *message, |
||
317 | gpointer user_data) |
||
318 | { |
||
319 | return FALSE; |
||
320 | } |
||
321 | |||
322 | static void |
||
323 | test_fatal_log_handler_critical_pass (void) |
||
324 | { |
||
325 | g_test_log_set_fatal_handler (fatal_handler, NULL); |
||
326 | g_str_has_prefix (NULL, "file://"); |
||
327 | g_critical ("Test passing"); |
||
328 | exit (0); |
||
329 | } |
||
330 | |||
331 | static void |
||
332 | test_fatal_log_handler_error_fail (void) |
||
333 | { |
||
334 | g_error ("Test failing"); |
||
335 | exit (0); |
||
336 | } |
||
337 | |||
338 | static void |
||
339 | test_fatal_log_handler_critical_fail (void) |
||
340 | { |
||
341 | g_str_has_prefix (NULL, "file://"); |
||
342 | g_critical ("Test passing"); |
||
343 | exit (0); |
||
344 | } |
||
345 | |||
346 | static void |
||
347 | test_fatal_log_handler (void) |
||
348 | { |
||
349 | g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-pass", 0, 0); |
||
350 | g_test_trap_assert_passed (); |
||
351 | g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*"); |
||
352 | g_test_trap_assert_stderr ("*CRITICAL*Test passing*"); |
||
353 | |||
354 | g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/error-fail", 0, 0); |
||
355 | g_test_trap_assert_failed (); |
||
356 | g_test_trap_assert_stderr ("*ERROR*Test failing*"); |
||
357 | |||
358 | g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-fail", 0, 0); |
||
359 | g_test_trap_assert_failed (); |
||
360 | g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*"); |
||
361 | g_test_trap_assert_stderr_unmatched ("*CRITICAL*Test passing*"); |
||
362 | } |
||
363 | |||
364 | static void |
||
365 | test_expected_messages_warning (void) |
||
366 | { |
||
367 | g_warning ("This is a %d warning", g_random_int ()); |
||
368 | g_return_if_reached (); |
||
369 | } |
||
370 | |||
371 | static void |
||
372 | test_expected_messages_expect_warning (void) |
||
373 | { |
||
374 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, |
||
375 | "This is a * warning"); |
||
376 | test_expected_messages_warning (); |
||
377 | } |
||
378 | |||
379 | static void |
||
380 | test_expected_messages_wrong_warning (void) |
||
381 | { |
||
382 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, |
||
383 | "*should not be *"); |
||
384 | test_expected_messages_warning (); |
||
385 | } |
||
386 | |||
387 | static void |
||
388 | test_expected_messages_expected (void) |
||
389 | { |
||
390 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, |
||
391 | "This is a * warning"); |
||
392 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, |
||
393 | "*should not be reached"); |
||
394 | |||
395 | test_expected_messages_warning (); |
||
396 | |||
397 | g_test_assert_expected_messages (); |
||
398 | exit (0); |
||
399 | } |
||
400 | |||
401 | static void |
||
402 | test_expected_messages_null_domain (void) |
||
403 | { |
||
404 | g_test_expect_message (NULL, G_LOG_LEVEL_WARNING, "no domain"); |
||
405 | g_log (NULL, G_LOG_LEVEL_WARNING, "no domain"); |
||
406 | g_test_assert_expected_messages (); |
||
407 | } |
||
408 | |||
409 | static void |
||
410 | test_expected_messages_expect_error (void) |
||
411 | { |
||
412 | /* make sure we can't try to expect a g_error() */ |
||
413 | g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "*G_LOG_LEVEL_ERROR*"); |
||
414 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "this won't work"); |
||
415 | g_test_assert_expected_messages (); |
||
416 | } |
||
417 | |||
418 | static void |
||
419 | test_expected_messages_extra_warning (void) |
||
420 | { |
||
421 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, |
||
422 | "This is a * warning"); |
||
423 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, |
||
424 | "*should not be reached"); |
||
425 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, |
||
426 | "nope"); |
||
427 | |||
428 | test_expected_messages_warning (); |
||
429 | |||
430 | /* If we don't assert, it won't notice the missing message */ |
||
431 | exit (0); |
||
432 | } |
||
433 | |||
434 | static void |
||
435 | test_expected_messages_unexpected_extra_warning (void) |
||
436 | { |
||
437 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, |
||
438 | "This is a * warning"); |
||
439 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, |
||
440 | "*should not be reached"); |
||
441 | g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, |
||
442 | "nope"); |
||
443 | |||
444 | test_expected_messages_warning (); |
||
445 | |||
446 | g_test_assert_expected_messages (); |
||
447 | exit (0); |
||
448 | } |
||
449 | |||
450 | static void |
||
451 | test_expected_messages (void) |
||
452 | { |
||
453 | g_test_trap_subprocess ("/misc/expected-messages/subprocess/warning", 0, 0); |
||
454 | g_test_trap_assert_failed (); |
||
455 | g_test_trap_assert_stderr ("*This is a * warning*"); |
||
456 | g_test_trap_assert_stderr_unmatched ("*should not be reached*"); |
||
457 | |||
458 | g_test_trap_subprocess ("/misc/expected-messages/subprocess/expect-warning", 0, 0); |
||
459 | g_test_trap_assert_failed (); |
||
460 | g_test_trap_assert_stderr_unmatched ("*This is a * warning*"); |
||
461 | g_test_trap_assert_stderr ("*should not be reached*"); |
||
462 | |||
463 | g_test_trap_subprocess ("/misc/expected-messages/subprocess/wrong-warning", 0, 0); |
||
464 | g_test_trap_assert_failed (); |
||
465 | g_test_trap_assert_stderr_unmatched ("*should not be reached*"); |
||
466 | g_test_trap_assert_stderr ("*GLib-CRITICAL*Did not see expected message testing-CRITICAL*should not be *WARNING*This is a * warning*"); |
||
467 | |||
468 | g_test_trap_subprocess ("/misc/expected-messages/subprocess/expected", 0, 0); |
||
469 | g_test_trap_assert_passed (); |
||
470 | g_test_trap_assert_stderr (""); |
||
471 | |||
472 | g_test_trap_subprocess ("/misc/expected-messages/subprocess/null-domain", 0, 0); |
||
473 | g_test_trap_assert_passed (); |
||
474 | g_test_trap_assert_stderr (""); |
||
475 | |||
476 | g_test_trap_subprocess ("/misc/expected-messages/subprocess/extra-warning", 0, 0); |
||
477 | g_test_trap_assert_passed (); |
||
478 | g_test_trap_assert_stderr (""); |
||
479 | |||
480 | g_test_trap_subprocess ("/misc/expected-messages/subprocess/unexpected-extra-warning", 0, 0); |
||
481 | g_test_trap_assert_failed (); |
||
482 | g_test_trap_assert_stderr ("*GLib:ERROR*Did not see expected message testing-CRITICAL*nope*"); |
||
483 | } |
||
484 | |||
485 | static void |
||
486 | test_expected_messages_debug (void) |
||
487 | { |
||
488 | g_test_expect_message ("Test", G_LOG_LEVEL_WARNING, "warning message"); |
||
489 | g_log ("Test", G_LOG_LEVEL_DEBUG, "should be ignored"); |
||
490 | g_log ("Test", G_LOG_LEVEL_WARNING, "warning message"); |
||
491 | g_test_assert_expected_messages (); |
||
492 | |||
493 | g_test_expect_message ("Test", G_LOG_LEVEL_DEBUG, "debug message"); |
||
494 | g_log ("Test", G_LOG_LEVEL_DEBUG, "debug message"); |
||
495 | g_test_assert_expected_messages (); |
||
496 | } |
||
497 | |||
498 | static void |
||
499 | test_dash_p_hidden (void) |
||
500 | { |
||
501 | if (!g_test_subprocess ()) |
||
502 | g_assert_not_reached (); |
||
503 | |||
504 | g_print ("Test /misc/dash-p/subprocess/hidden ran\n"); |
||
505 | } |
||
506 | |||
507 | static void |
||
508 | test_dash_p_hidden_sub (void) |
||
509 | { |
||
510 | if (!g_test_subprocess ()) |
||
511 | g_assert_not_reached (); |
||
512 | |||
513 | g_print ("Test /misc/dash-p/subprocess/hidden/sub ran\n"); |
||
514 | } |
||
515 | |||
516 | /* The rest of the dash_p tests will get run by the toplevel test |
||
517 | * process, but they shouldn't do anything there. |
||
518 | */ |
||
519 | |||
520 | static void |
||
521 | test_dash_p_child (void) |
||
522 | { |
||
523 | if (!g_test_subprocess ()) |
||
524 | return; |
||
525 | |||
526 | g_print ("Test /misc/dash-p/child ran\n"); |
||
527 | } |
||
528 | |||
529 | static void |
||
530 | test_dash_p_child_sub (void) |
||
531 | { |
||
532 | if (!g_test_subprocess ()) |
||
533 | return; |
||
534 | |||
535 | g_print ("Test /misc/dash-p/child/sub ran\n"); |
||
536 | } |
||
537 | |||
538 | static void |
||
539 | test_dash_p_child_sub2 (void) |
||
540 | { |
||
541 | if (!g_test_subprocess ()) |
||
542 | return; |
||
543 | |||
544 | g_print ("Test /misc/dash-p/child/sub2 ran\n"); |
||
545 | } |
||
546 | |||
547 | static void |
||
548 | test_dash_p_child_sub_child (void) |
||
549 | { |
||
550 | if (!g_test_subprocess ()) |
||
551 | return; |
||
552 | |||
553 | g_print ("Test /misc/dash-p/child/subprocess ran\n"); |
||
554 | } |
||
555 | |||
556 | static void |
||
557 | test_dash_p (void) |
||
558 | { |
||
559 | g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden", 0, 0); |
||
560 | g_test_trap_assert_passed (); |
||
561 | g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden ran*"); |
||
562 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub ran*"); |
||
563 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*"); |
||
564 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub/subprocess ran*"); |
||
565 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*"); |
||
566 | |||
567 | g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden/sub", 0, 0); |
||
568 | g_test_trap_assert_passed (); |
||
569 | g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden/sub ran*"); |
||
570 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden ran*"); |
||
571 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*"); |
||
572 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/subprocess ran*"); |
||
573 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*"); |
||
574 | |||
575 | g_test_trap_subprocess ("/misc/dash-p/child", 0, 0); |
||
576 | g_test_trap_assert_passed (); |
||
577 | g_test_trap_assert_stdout ("*Test /misc/dash-p/child ran*"); |
||
578 | g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*"); |
||
579 | g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub2 ran*"); |
||
580 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*"); |
||
581 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*"); |
||
582 | |||
583 | g_test_trap_subprocess ("/misc/dash-p/child/sub", 0, 0); |
||
584 | g_test_trap_assert_passed (); |
||
585 | g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*"); |
||
586 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child ran*"); |
||
587 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/sub2 ran*"); |
||
588 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*"); |
||
589 | g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*"); |
||
590 | } |
||
591 | |||
592 | static void |
||
593 | test_nonfatal (void) |
||
594 | { |
||
595 | if (g_test_subprocess ()) |
||
596 | { |
||
597 | g_test_set_nonfatal_assertions (); |
||
598 | g_assert_cmpint (4, ==, 5); |
||
599 | g_print ("The End\n"); |
||
600 | return; |
||
601 | } |
||
602 | g_test_trap_subprocess (NULL, 0, 0); |
||
603 | g_test_trap_assert_failed (); |
||
604 | g_test_trap_assert_stderr ("*assertion failed*4 == 5*"); |
||
605 | g_test_trap_assert_stdout ("*The End*"); |
||
606 | } |
||
607 | |||
608 | static void |
||
609 | test_skip (void) |
||
610 | { |
||
611 | g_test_skip ("Skipped should count as passed, not failed"); |
||
612 | } |
||
613 | |||
614 | static void |
||
615 | test_pass (void) |
||
616 | { |
||
617 | } |
||
618 | |||
619 | static void |
||
620 | test_fail (void) |
||
621 | { |
||
622 | if (g_test_subprocess ()) |
||
623 | { |
||
624 | g_test_fail (); |
||
625 | g_assert (g_test_failed ()); |
||
626 | return; |
||
627 | } |
||
628 | g_test_trap_subprocess (NULL, 0, 0); |
||
629 | g_test_trap_assert_failed (); |
||
630 | } |
||
631 | |||
632 | static void |
||
633 | test_incomplete (void) |
||
634 | { |
||
635 | if (g_test_subprocess ()) |
||
636 | { |
||
637 | g_test_incomplete ("not done"); |
||
638 | g_assert (g_test_failed ()); |
||
639 | return; |
||
640 | } |
||
641 | g_test_trap_subprocess (NULL, 0, 0); |
||
642 | g_test_trap_assert_failed (); |
||
643 | } |
||
644 | |||
645 | static void |
||
646 | test_subprocess_timed_out (void) |
||
647 | { |
||
648 | if (g_test_subprocess ()) |
||
649 | { |
||
650 | g_usleep (1000000); |
||
651 | return; |
||
652 | } |
||
653 | g_test_trap_subprocess (NULL, 50000, 0); |
||
654 | g_assert (g_test_trap_reached_timeout ()); |
||
655 | } |
||
656 | |||
657 | static const char *argv0; |
||
658 | |||
659 | static void |
||
660 | test_skip_all (void) |
||
661 | { |
||
662 | GPtrArray *argv; |
||
663 | GError *error = NULL; |
||
664 | int status; |
||
665 | |||
666 | argv = g_ptr_array_new (); |
||
667 | g_ptr_array_add (argv, (char *) argv0); |
||
668 | g_ptr_array_add (argv, "--GTestSubprocess"); |
||
669 | g_ptr_array_add (argv, "-p"); |
||
670 | g_ptr_array_add (argv, "/misc/skip"); |
||
671 | g_ptr_array_add (argv, NULL); |
||
672 | |||
673 | g_spawn_sync (NULL, (char **) argv->pdata, NULL, |
||
674 | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, |
||
675 | NULL, NULL, NULL, NULL, &status, |
||
676 | &error); |
||
677 | g_assert_no_error (error); |
||
678 | |||
679 | g_spawn_check_exit_status (status, &error); |
||
680 | g_assert_error (error, G_SPAWN_EXIT_ERROR, 77); |
||
681 | g_clear_error (&error); |
||
682 | |||
683 | g_ptr_array_set_size (argv, 0); |
||
684 | g_ptr_array_add (argv, (char *) argv0); |
||
685 | g_ptr_array_add (argv, "--GTestSubprocess"); |
||
686 | g_ptr_array_add (argv, "-p"); |
||
687 | g_ptr_array_add (argv, "/misc/skip"); |
||
688 | g_ptr_array_add (argv, "-p"); |
||
689 | g_ptr_array_add (argv, "/misc/skip-all/subprocess/skip1"); |
||
690 | g_ptr_array_add (argv, "-p"); |
||
691 | g_ptr_array_add (argv, "/misc/skip-all/subprocess/skip2"); |
||
692 | g_ptr_array_add (argv, NULL); |
||
693 | |||
694 | g_spawn_sync (NULL, (char **) argv->pdata, NULL, |
||
695 | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, |
||
696 | NULL, NULL, NULL, NULL, &status, |
||
697 | &error); |
||
698 | g_assert_no_error (error); |
||
699 | |||
700 | g_spawn_check_exit_status (status, &error); |
||
701 | g_assert_error (error, G_SPAWN_EXIT_ERROR, 77); |
||
702 | g_clear_error (&error); |
||
703 | |||
704 | g_ptr_array_set_size (argv, 0); |
||
705 | g_ptr_array_add (argv, (char *) argv0); |
||
706 | g_ptr_array_add (argv, "--GTestSubprocess"); |
||
707 | g_ptr_array_add (argv, "-p"); |
||
708 | g_ptr_array_add (argv, "/misc/skip"); |
||
709 | g_ptr_array_add (argv, "-p"); |
||
710 | g_ptr_array_add (argv, "/misc/skip-all/subprocess/pass"); |
||
711 | g_ptr_array_add (argv, "-p"); |
||
712 | g_ptr_array_add (argv, "/misc/skip-all/subprocess/skip1"); |
||
713 | g_ptr_array_add (argv, NULL); |
||
714 | |||
715 | g_spawn_sync (NULL, (char **) argv->pdata, NULL, |
||
716 | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, |
||
717 | NULL, NULL, NULL, NULL, &status, |
||
718 | &error); |
||
719 | g_assert_no_error (error); |
||
720 | |||
721 | g_spawn_check_exit_status (status, &error); |
||
722 | g_assert_no_error (error); |
||
723 | |||
724 | g_ptr_array_unref (argv); |
||
725 | } |
||
726 | |||
727 | int |
||
728 | main (int argc, |
||
729 | char *argv[]) |
||
730 | { |
||
731 | argv0 = argv[0]; |
||
732 | |||
733 | g_test_init (&argc, &argv, NULL); |
||
734 | |||
735 | g_test_add_func ("/random-generator/rand-1", test_rand1); |
||
736 | g_test_add_func ("/random-generator/rand-2", test_rand2); |
||
737 | g_test_add_func ("/random-generator/random-conversions", test_random_conversions); |
||
738 | g_test_add_func ("/misc/assertions", test_assertions); |
||
739 | g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr); |
||
740 | g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint); |
||
741 | g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_len", test_assertions_bad_cmpmem_len); |
||
742 | g_test_add_func ("/misc/assertions/subprocess/bad_cmpmem_data", test_assertions_bad_cmpmem_data); |
||
743 | g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test); |
||
744 | g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown); |
||
745 | if (g_test_perf()) |
||
746 | g_test_add_func ("/misc/timer", test_timer); |
||
747 | |||
748 | #ifdef G_OS_UNIX |
||
749 | g_test_add_func ("/forking/fail assertion", test_fork_fail); |
||
750 | g_test_add_func ("/forking/patterns", test_fork_patterns); |
||
751 | if (g_test_slow()) |
||
752 | g_test_add_func ("/forking/timeout", test_fork_timeout); |
||
753 | #endif |
||
754 | |||
755 | g_test_add_func ("/trap_subprocess/fail", test_subprocess_fail); |
||
756 | g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test); |
||
757 | if (g_test_slow ()) |
||
758 | g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout); |
||
759 | |||
760 | g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns); |
||
761 | |||
762 | g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler); |
||
763 | g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-pass", test_fatal_log_handler_critical_pass); |
||
764 | g_test_add_func ("/misc/fatal-log-handler/subprocess/error-fail", test_fatal_log_handler_error_fail); |
||
765 | g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-fail", test_fatal_log_handler_critical_fail); |
||
766 | |||
767 | g_test_add_func ("/misc/expected-messages", test_expected_messages); |
||
768 | g_test_add_func ("/misc/expected-messages/subprocess/warning", test_expected_messages_warning); |
||
769 | g_test_add_func ("/misc/expected-messages/subprocess/expect-warning", test_expected_messages_expect_warning); |
||
770 | g_test_add_func ("/misc/expected-messages/subprocess/wrong-warning", test_expected_messages_wrong_warning); |
||
771 | g_test_add_func ("/misc/expected-messages/subprocess/expected", test_expected_messages_expected); |
||
772 | g_test_add_func ("/misc/expected-messages/subprocess/null-domain", test_expected_messages_null_domain); |
||
773 | g_test_add_func ("/misc/expected-messages/subprocess/extra-warning", test_expected_messages_extra_warning); |
||
774 | g_test_add_func ("/misc/expected-messages/subprocess/unexpected-extra-warning", test_expected_messages_unexpected_extra_warning); |
||
775 | g_test_add_func ("/misc/expected-messages/expect-error", test_expected_messages_expect_error); |
||
776 | g_test_add_func ("/misc/expected-messages/skip-debug", test_expected_messages_debug); |
||
777 | |||
778 | g_test_add_func ("/misc/dash-p", test_dash_p); |
||
779 | g_test_add_func ("/misc/dash-p/child", test_dash_p_child); |
||
780 | g_test_add_func ("/misc/dash-p/child/sub", test_dash_p_child_sub); |
||
781 | g_test_add_func ("/misc/dash-p/child/sub/subprocess", test_dash_p_child_sub_child); |
||
782 | g_test_add_func ("/misc/dash-p/child/sub/subprocess/child", test_dash_p_child_sub_child); |
||
783 | g_test_add_func ("/misc/dash-p/child/sub2", test_dash_p_child_sub2); |
||
784 | g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden); |
||
785 | g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub); |
||
786 | |||
787 | g_test_add_func ("/misc/nonfatal", test_nonfatal); |
||
788 | |||
789 | g_test_add_func ("/misc/skip", test_skip); |
||
790 | g_test_add_func ("/misc/skip-all", test_skip_all); |
||
791 | g_test_add_func ("/misc/skip-all/subprocess/skip1", test_skip); |
||
792 | g_test_add_func ("/misc/skip-all/subprocess/skip2", test_skip); |
||
793 | g_test_add_func ("/misc/skip-all/subprocess/pass", test_pass); |
||
794 | g_test_add_func ("/misc/fail", test_fail); |
||
795 | g_test_add_func ("/misc/incomplete", test_incomplete); |
||
796 | g_test_add_func ("/misc/timeout", test_subprocess_timed_out); |
||
797 | |||
798 | return g_test_run(); |
||
799 | } |