1 /* GLib testing framework examples and tests
2 * Copyright (C) 2007 Imendio AB
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.
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.
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.
27 /* test assertion variants */
29 test_assertions_bad_cmpstr (void)
31 g_assert_cmpstr ("fzz", !=, "fzz");
36 test_assertions_bad_cmpint (void)
38 g_assert_cmpint (4, !=, 4);
43 test_assertions (void)
46 g_assert_cmpint (1, >, 0);
47 g_assert_cmphex (2, ==, 2);
48 g_assert_cmpfloat (3.3, !=, 7);
49 g_assert_cmpfloat (7, <=, 3 + 4);
51 g_assert_cmpstr ("foo", !=, "faa");
52 fuu = g_strdup_printf ("f%s", "uu");
53 g_test_queue_free (fuu);
54 g_assert_cmpstr ("foo", !=, fuu);
55 g_assert_cmpstr ("fuu", ==, fuu);
56 g_assert_cmpstr (NULL, <, "");
57 g_assert_cmpstr (NULL, ==, NULL);
58 g_assert_cmpstr ("", >, NULL);
59 g_assert_cmpstr ("foo", <, "fzz");
60 g_assert_cmpstr ("fzz", >, "faa");
61 g_assert_cmpstr ("fzz", ==, "fzz");
63 g_test_trap_subprocess ("/misc/assertions:bad_cmpstr",
64 0, G_TEST_TRAP_SILENCE_STDERR);
65 g_test_trap_assert_failed ();
66 g_test_trap_assert_stderr ("*assertion failed*");
68 g_test_trap_subprocess ("/misc/assertions:bad_cmpint",
69 0, G_TEST_TRAP_SILENCE_STDERR);
70 g_test_trap_assert_failed ();
71 g_test_trap_assert_stderr ("*assertion failed*");
74 /* test g_test_timer* API */
80 g_assert_cmpfloat (g_test_timer_last(), ==, 0);
82 ttime = g_test_timer_elapsed();
83 g_assert_cmpfloat (ttime, >, 0);
84 g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
85 g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
86 g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */
90 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
92 /* fork out for a failing test */
96 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
98 g_assert_not_reached();
100 g_test_trap_assert_failed();
101 g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
104 /* fork out to assert stdout and stderr patterns */
106 test_fork_patterns (void)
108 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
110 g_print ("some stdout text: somagic17\n");
111 g_printerr ("some stderr text: semagic43\n");
114 g_test_trap_assert_passed();
115 g_test_trap_assert_stdout ("*somagic17*");
116 g_test_trap_assert_stderr ("*semagic43*");
119 /* fork out for a timeout test */
121 test_fork_timeout (void)
123 /* allow child to run for only a fraction of a second */
124 if (g_test_trap_fork (0.11 * 1000000, 0))
126 /* loop and sleep forever */
128 g_usleep (1000 * 1000);
130 g_test_trap_assert_failed();
131 g_assert (g_test_trap_reached_timeout());
134 G_GNUC_END_IGNORE_DEPRECATIONS
135 #endif /* G_OS_UNIX */
138 test_subprocess_fail_child (void)
140 g_assert_not_reached ();
144 test_subprocess_fail (void)
146 g_test_trap_subprocess ("/subprocess/fail:child",
147 0, G_TEST_TRAP_SILENCE_STDERR);
148 g_test_trap_assert_failed ();
149 g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail_child*should not be reached*");
153 test_subprocess_no_such_test_child (void)
155 g_test_trap_subprocess ("/subprocess/this-test-does-not-exist",
156 0, G_TEST_TRAP_SILENCE_STDERR);
157 g_assert_not_reached ();
161 test_subprocess_no_such_test (void)
163 g_test_trap_subprocess ("/subprocess/no-such-test:child",
164 0, G_TEST_TRAP_SILENCE_STDERR);
165 g_test_trap_assert_failed ();
166 g_test_trap_assert_stderr ("*assertion failed*g_test_case_exists*");
167 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
171 test_subprocess_patterns_child (void)
173 g_print ("some stdout text: somagic17\n");
174 g_printerr ("some stderr text: semagic43\n");
179 test_subprocess_patterns (void)
181 g_test_trap_subprocess ("/subprocess/patterns:child",
182 0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR);
183 g_test_trap_assert_passed ();
184 g_test_trap_assert_stdout ("*somagic17*");
185 g_test_trap_assert_stderr ("*semagic43*");
189 test_subprocess_timeout_child (void)
191 /* loop and sleep forever */
193 g_usleep (1000 * 1000);
197 test_subprocess_timeout (void)
199 /* allow child to run for only a fraction of a second */
200 g_test_trap_subprocess ("/subprocess/timeout:child",
202 g_test_trap_assert_failed ();
203 g_assert (g_test_trap_reached_timeout ());
206 /* run a test with fixture setup and teardown */
213 fixturetest_setup (Fixturetest *fix,
214 gconstpointer test_data)
216 g_assert (test_data == (void*) 0xc0cac01a);
219 fix->msg = g_strdup_printf ("%d", fix->prime);
222 fixturetest_test (Fixturetest *fix,
223 gconstpointer test_data)
225 guint prime = g_spaced_primes_closest (fix->seed);
226 g_assert_cmpint (prime, ==, fix->prime);
227 prime = g_ascii_strtoull (fix->msg, NULL, 0);
228 g_assert_cmpint (prime, ==, fix->prime);
229 g_assert (test_data == (void*) 0xc0cac01a);
232 fixturetest_teardown (Fixturetest *fix,
233 gconstpointer test_data)
235 g_assert (test_data == (void*) 0xc0cac01a);
240 int bit, vint1, vint2, irange;
241 long double vdouble, drange;
247 shared_rand_state.bit = g_test_rand_bit();
248 shared_rand_state.vint1 = g_test_rand_int();
249 shared_rand_state.vint2 = g_test_rand_int();
250 g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
251 shared_rand_state.irange = g_test_rand_int_range (17, 35);
252 g_assert_cmpint (shared_rand_state.irange, >=, 17);
253 g_assert_cmpint (shared_rand_state.irange, <=, 35);
254 shared_rand_state.vdouble = g_test_rand_double();
255 shared_rand_state.drange = g_test_rand_double_range (-999, +17);
256 g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
257 g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
263 /* this test only works if run after test1.
264 * we do this to check that random number generators
265 * are reseeded upon fixture setup.
267 g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
268 g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
269 g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
270 g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
271 g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
272 g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
276 test_data_test (gconstpointer test_data)
278 g_assert (test_data == (void*) 0xc0c0baba);
282 test_random_conversions (void)
284 /* very simple conversion test using random numbers */
285 int vint = g_test_rand_int();
286 char *err, *str = g_strdup_printf ("%d", vint);
287 gint64 vint64 = g_ascii_strtoll (str, &err, 10);
288 g_assert_cmphex (vint, ==, vint64);
289 g_assert (!err || *err == 0);
294 fatal_handler (const gchar *log_domain,
295 GLogLevelFlags log_level,
296 const gchar *message,
303 test_fatal_log_handler_critical_pass (void)
305 g_test_log_set_fatal_handler (fatal_handler, NULL);
306 g_str_has_prefix (NULL, "file://");
307 g_critical ("Test passing");
312 test_fatal_log_handler_error_fail (void)
314 g_error ("Test failing");
319 test_fatal_log_handler_critical_fail (void)
321 g_str_has_prefix (NULL, "file://");
322 g_critical ("Test passing");
327 test_fatal_log_handler (void)
329 g_test_trap_subprocess ("/misc/fatal-log-handler:critical-pass",
330 0, G_TEST_TRAP_SILENCE_STDERR);
331 g_test_trap_assert_passed ();
332 g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
333 g_test_trap_assert_stderr ("*CRITICAL*Test passing*");
335 g_test_trap_subprocess ("/misc/fatal-log-handler:error-fail",
336 0, G_TEST_TRAP_SILENCE_STDERR);
337 g_test_trap_assert_failed ();
338 g_test_trap_assert_stderr ("*ERROR*Test failing*");
340 g_test_trap_subprocess ("/misc/fatal-log-handler:critical-fail",
341 0, G_TEST_TRAP_SILENCE_STDERR);
342 g_test_trap_assert_failed ();
343 g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
344 g_test_trap_assert_stderr_unmatched ("*CRITICAL*Test passing*");
348 test_expected_messages_warning (void)
350 g_warning ("This is a %d warning", g_random_int ());
351 g_return_if_reached ();
355 test_expected_messages_expect_warning (void)
357 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
358 "This is a * warning");
359 test_expected_messages_warning ();
363 test_expected_messages_wrong_warning (void)
365 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
367 test_expected_messages_warning ();
371 test_expected_messages_expected (void)
373 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
374 "This is a * warning");
375 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
376 "*should not be reached");
378 test_expected_messages_warning ();
380 g_test_assert_expected_messages ();
385 test_expected_messages_extra_warning (void)
387 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
388 "This is a * warning");
389 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
390 "*should not be reached");
391 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
394 test_expected_messages_warning ();
396 /* If we don't assert, it won't notice the missing message */
401 test_expected_messages_unexpected_extra_warning (void)
403 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
404 "This is a * warning");
405 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
406 "*should not be reached");
407 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
410 test_expected_messages_warning ();
412 g_test_assert_expected_messages ();
417 test_expected_messages (void)
419 g_test_trap_subprocess ("/misc/expected-messages:warning",
420 0, G_TEST_TRAP_SILENCE_STDERR);
421 g_test_trap_assert_failed ();
422 g_test_trap_assert_stderr ("*This is a * warning*");
423 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
425 g_test_trap_subprocess ("/misc/expected-messages:expect-warning",
426 0, G_TEST_TRAP_SILENCE_STDERR);
427 g_test_trap_assert_failed ();
428 g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
429 g_test_trap_assert_stderr ("*should not be reached*");
431 g_test_trap_subprocess ("/misc/expected-messages:wrong-warning",
432 0, G_TEST_TRAP_SILENCE_STDERR);
433 g_test_trap_assert_failed ();
434 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
435 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
437 g_test_trap_subprocess ("/misc/expected-messages:expected",
438 0, G_TEST_TRAP_SILENCE_STDERR);
439 g_test_trap_assert_passed ();
440 g_test_trap_assert_stderr ("");
442 g_test_trap_subprocess ("/misc/expected-messages:extra-warning",
443 0, G_TEST_TRAP_SILENCE_STDERR);
444 g_test_trap_assert_passed ();
445 g_test_trap_assert_stderr ("");
447 g_test_trap_subprocess ("/misc/expected-messages:unexpected-extra-warning",
448 0, G_TEST_TRAP_SILENCE_STDERR);
449 g_test_trap_assert_failed ();
450 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
454 test_dash_p_colon (void)
456 if (!g_test_subprocess ())
457 g_assert_not_reached ();
459 g_print ("Test /misc/dash-p:colon ran\n");
462 /* The rest of the dash_p tests will get run by the toplevel test
463 * process, but they shouldn't do anything there.
466 test_dash_p_colon_sub (void)
468 if (!g_test_subprocess ())
471 g_print ("Test /misc/dash-p:colon/sub ran\n");
475 test_dash_p_colon_sub2 (void)
477 if (!g_test_subprocess ())
480 g_print ("Test /misc/dash-p:colon/sub2 ran\n");
484 test_dash_p_colon_sub_child (void)
486 if (!g_test_subprocess ())
489 g_print ("Test /misc/dash-p:colon/sub:child ran\n");
493 test_dash_p_slash (void)
495 if (!g_test_subprocess ())
498 g_print ("Test /misc/dash-p/slash ran\n");
502 test_dash_p_slash_sub (void)
504 if (!g_test_subprocess ())
507 g_print ("Test /misc/dash-p/slash/sub ran\n");
511 test_dash_p_slash_sub2 (void)
513 if (!g_test_subprocess ())
516 g_print ("Test /misc/dash-p/slash/sub2 ran\n");
520 test_dash_p_slash_sub_child (void)
522 if (!g_test_subprocess ())
525 g_print ("Test /misc/dash-p/slash/sub:child ran\n");
531 g_test_trap_subprocess ("/misc/dash-p:colon", 0,
532 G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR);
533 g_test_trap_assert_passed ();
534 g_test_trap_assert_stdout ("*Test /misc/dash-p:colon ran*");
535 g_test_trap_assert_stdout ("*Test /misc/dash-p:colon/sub ran*");
536 g_test_trap_assert_stdout ("*Test /misc/dash-p:colon/sub2 ran*");
537 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p:colon/sub:child ran*");
538 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/slash*");
540 g_test_trap_subprocess ("/misc/dash-p:colon/sub", 0,
541 G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR);
542 g_test_trap_assert_passed ();
543 g_test_trap_assert_stdout ("*Test /misc/dash-p:colon/sub ran*");
544 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p:colon ran*");
545 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p:colon/sub2 ran*");
546 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p:colon/sub:child ran*");
547 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/slash*");
549 g_test_trap_subprocess ("/misc/dash-p/slash", 0,
550 G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR);
551 g_test_trap_assert_passed ();
552 g_test_trap_assert_stdout ("*Test /misc/dash-p/slash ran*");
553 g_test_trap_assert_stdout ("*Test /misc/dash-p/slash/sub ran*");
554 g_test_trap_assert_stdout ("*Test /misc/dash-p/slash/sub2 ran*");
555 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/slash/sub:child ran*");
556 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p:colon*");
558 g_test_trap_subprocess ("/misc/dash-p/slash/sub", 0,
559 G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR);
560 g_test_trap_assert_passed ();
561 g_test_trap_assert_stdout ("*Test /misc/dash-p/slash/sub ran*");
562 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/slash ran*");
563 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/slash/sub2 ran*");
564 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/slash/sub:child ran*");
565 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p:colon*");
572 g_test_init (&argc, &argv, NULL);
574 g_test_add_func ("/random-generator/rand-1", test_rand1);
575 g_test_add_func ("/random-generator/rand-2", test_rand2);
576 g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
577 g_test_add_func ("/misc/assertions", test_assertions);
578 g_test_add_func ("/misc/assertions:bad_cmpstr", test_assertions_bad_cmpstr);
579 g_test_add_func ("/misc/assertions:bad_cmpint", test_assertions_bad_cmpint);
580 g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
581 g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
583 g_test_add_func ("/misc/timer", test_timer);
586 g_test_add_func ("/forking/fail assertion", test_fork_fail);
587 g_test_add_func ("/forking/patterns", test_fork_patterns);
589 g_test_add_func ("/forking/timeout", test_fork_timeout);
592 g_test_add_func ("/subprocess/fail", test_subprocess_fail);
593 g_test_add_func ("/subprocess/fail:child", test_subprocess_fail_child);
594 g_test_add_func ("/subprocess/no-such-test", test_subprocess_no_such_test);
595 g_test_add_func ("/subprocess/no-such-test:child", test_subprocess_no_such_test_child);
598 g_test_add_func ("/subprocess/timeout", test_subprocess_timeout);
599 g_test_add_func ("/subprocess/timeout:child", test_subprocess_timeout_child);
601 g_test_add_func ("/subprocess/patterns", test_subprocess_patterns);
602 g_test_add_func ("/subprocess/patterns:child", test_subprocess_patterns_child);
604 g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
605 g_test_add_func ("/misc/fatal-log-handler:critical-pass", test_fatal_log_handler_critical_pass);
606 g_test_add_func ("/misc/fatal-log-handler:error-fail", test_fatal_log_handler_error_fail);
607 g_test_add_func ("/misc/fatal-log-handler:critical-fail", test_fatal_log_handler_critical_fail);
609 g_test_add_func ("/misc/expected-messages", test_expected_messages);
610 g_test_add_func ("/misc/expected-messages:warning", test_expected_messages_warning);
611 g_test_add_func ("/misc/expected-messages:expect-warning", test_expected_messages_expect_warning);
612 g_test_add_func ("/misc/expected-messages:wrong-warning", test_expected_messages_wrong_warning);
613 g_test_add_func ("/misc/expected-messages:expected", test_expected_messages_expected);
614 g_test_add_func ("/misc/expected-messages:extra-warning", test_expected_messages_extra_warning);
615 g_test_add_func ("/misc/expected-messages:unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);
617 g_test_add_func ("/misc/dash-p", test_dash_p);
618 g_test_add_func ("/misc/dash-p:colon", test_dash_p_colon);
619 g_test_add_func ("/misc/dash-p:colon/sub", test_dash_p_colon_sub);
620 g_test_add_func ("/misc/dash-p:colon/sub:child", test_dash_p_colon_sub_child);
621 g_test_add_func ("/misc/dash-p:colon/sub2", test_dash_p_colon_sub2);
622 g_test_add_func ("/misc/dash-p/slash", test_dash_p_slash);
623 g_test_add_func ("/misc/dash-p/slash/sub", test_dash_p_slash_sub);
624 g_test_add_func ("/misc/dash-p/slash/sub:child", test_dash_p_slash_sub_child);
625 g_test_add_func ("/misc/dash-p/slash/sub2", test_dash_p_slash_sub2);