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.
28 /* test assertion variants */
30 test_assertions_bad_cmpstr (void)
32 g_assert_cmpstr ("fzz", !=, "fzz");
37 test_assertions_bad_cmpint (void)
39 g_assert_cmpint (4, !=, 4);
44 test_assertions (void)
47 g_assert_cmpint (1, >, 0);
48 g_assert_cmphex (2, ==, 2);
49 g_assert_cmpfloat (3.3, !=, 7);
50 g_assert_cmpfloat (7, <=, 3 + 4);
52 g_assert_cmpstr ("foo", !=, "faa");
53 fuu = g_strdup_printf ("f%s", "uu");
54 g_test_queue_free (fuu);
55 g_assert_cmpstr ("foo", !=, fuu);
56 g_assert_cmpstr ("fuu", ==, fuu);
57 g_assert_cmpstr (NULL, <, "");
58 g_assert_cmpstr (NULL, ==, NULL);
59 g_assert_cmpstr ("", >, NULL);
60 g_assert_cmpstr ("foo", <, "fzz");
61 g_assert_cmpstr ("fzz", >, "faa");
62 g_assert_cmpstr ("fzz", ==, "fzz");
64 g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpstr", 0, 0);
65 g_test_trap_assert_failed ();
66 g_test_trap_assert_stderr ("*assertion failed*");
68 g_test_trap_subprocess ("/misc/assertions/subprocess/bad_cmpint", 0, 0);
69 g_test_trap_assert_failed ();
70 g_test_trap_assert_stderr ("*assertion failed*");
73 /* test g_test_timer* API */
79 g_assert_cmpfloat (g_test_timer_last(), ==, 0);
81 ttime = g_test_timer_elapsed();
82 g_assert_cmpfloat (ttime, >, 0);
83 g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
84 g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
85 g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */
89 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
91 /* fork out for a failing test */
95 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
97 g_assert_not_reached();
99 g_test_trap_assert_failed();
100 g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
103 /* fork out to assert stdout and stderr patterns */
105 test_fork_patterns (void)
107 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
109 g_print ("some stdout text: somagic17\n");
110 g_printerr ("some stderr text: semagic43\n");
113 g_test_trap_assert_passed();
114 g_test_trap_assert_stdout ("*somagic17*");
115 g_test_trap_assert_stderr ("*semagic43*");
118 /* fork out for a timeout test */
120 test_fork_timeout (void)
122 /* allow child to run for only a fraction of a second */
123 if (g_test_trap_fork (0.11 * 1000000, 0))
125 /* loop and sleep forever */
127 g_usleep (1000 * 1000);
129 g_test_trap_assert_failed();
130 g_assert (g_test_trap_reached_timeout());
133 G_GNUC_END_IGNORE_DEPRECATIONS
134 #endif /* G_OS_UNIX */
137 test_subprocess_fail_child (void)
139 g_assert_not_reached ();
143 test_subprocess_fail (void)
145 g_test_trap_subprocess ("/trap_subprocess/fail/subprocess", 0, 0);
146 g_test_trap_assert_failed ();
147 g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail_child*should not be reached*");
151 test_subprocess_no_such_test_child (void)
153 g_test_trap_subprocess ("/trap_subprocess/this-test-does-not-exist", 0, 0);
154 g_assert_not_reached ();
158 test_subprocess_no_such_test (void)
160 g_test_trap_subprocess ("/trap_subprocess/no-such-test/subprocess", 0, 0);
161 g_test_trap_assert_failed ();
162 g_test_trap_assert_stderr ("*test does not exist*");
163 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
167 test_subprocess_patterns_child (void)
169 g_print ("some stdout text: somagic17\n");
170 g_printerr ("some stderr text: semagic43\n");
175 test_subprocess_patterns (void)
177 g_test_trap_subprocess ("/trap_subprocess/patterns/subprocess", 0, 0);
178 g_test_trap_assert_passed ();
179 g_test_trap_assert_stdout ("*somagic17*");
180 g_test_trap_assert_stderr ("*semagic43*");
184 test_subprocess_timeout_child (void)
186 /* loop and sleep forever */
188 g_usleep (1000 * 1000);
192 test_subprocess_timeout (void)
194 /* allow child to run for only a fraction of a second */
195 g_test_trap_subprocess ("/trap_subprocess/timeout/subprocess", 0.11 * 1000000, 0);
196 g_test_trap_assert_failed ();
197 g_assert (g_test_trap_reached_timeout ());
200 /* run a test with fixture setup and teardown */
207 fixturetest_setup (Fixturetest *fix,
208 gconstpointer test_data)
210 g_assert (test_data == (void*) 0xc0cac01a);
213 fix->msg = g_strdup_printf ("%d", fix->prime);
216 fixturetest_test (Fixturetest *fix,
217 gconstpointer test_data)
219 guint prime = g_spaced_primes_closest (fix->seed);
220 g_assert_cmpint (prime, ==, fix->prime);
221 prime = g_ascii_strtoull (fix->msg, NULL, 0);
222 g_assert_cmpint (prime, ==, fix->prime);
223 g_assert (test_data == (void*) 0xc0cac01a);
226 fixturetest_teardown (Fixturetest *fix,
227 gconstpointer test_data)
229 g_assert (test_data == (void*) 0xc0cac01a);
234 int bit, vint1, vint2, irange;
235 long double vdouble, drange;
241 shared_rand_state.bit = g_test_rand_bit();
242 shared_rand_state.vint1 = g_test_rand_int();
243 shared_rand_state.vint2 = g_test_rand_int();
244 g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
245 shared_rand_state.irange = g_test_rand_int_range (17, 35);
246 g_assert_cmpint (shared_rand_state.irange, >=, 17);
247 g_assert_cmpint (shared_rand_state.irange, <=, 35);
248 shared_rand_state.vdouble = g_test_rand_double();
249 shared_rand_state.drange = g_test_rand_double_range (-999, +17);
250 g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
251 g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
257 /* this test only works if run after test1.
258 * we do this to check that random number generators
259 * are reseeded upon fixture setup.
261 g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
262 g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
263 g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
264 g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
265 g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
266 g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
270 test_data_test (gconstpointer test_data)
272 g_assert (test_data == (void*) 0xc0c0baba);
276 test_random_conversions (void)
278 /* very simple conversion test using random numbers */
279 int vint = g_test_rand_int();
280 char *err, *str = g_strdup_printf ("%d", vint);
281 gint64 vint64 = g_ascii_strtoll (str, &err, 10);
282 g_assert_cmphex (vint, ==, vint64);
283 g_assert (!err || *err == 0);
288 fatal_handler (const gchar *log_domain,
289 GLogLevelFlags log_level,
290 const gchar *message,
297 test_fatal_log_handler_critical_pass (void)
299 g_test_log_set_fatal_handler (fatal_handler, NULL);
300 g_str_has_prefix (NULL, "file://");
301 g_critical ("Test passing");
306 test_fatal_log_handler_error_fail (void)
308 g_error ("Test failing");
313 test_fatal_log_handler_critical_fail (void)
315 g_str_has_prefix (NULL, "file://");
316 g_critical ("Test passing");
321 test_fatal_log_handler (void)
323 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-pass", 0, 0);
324 g_test_trap_assert_passed ();
325 g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
326 g_test_trap_assert_stderr ("*CRITICAL*Test passing*");
328 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/error-fail", 0, 0);
329 g_test_trap_assert_failed ();
330 g_test_trap_assert_stderr ("*ERROR*Test failing*");
332 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-fail", 0, 0);
333 g_test_trap_assert_failed ();
334 g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
335 g_test_trap_assert_stderr_unmatched ("*CRITICAL*Test passing*");
339 test_expected_messages_warning (void)
341 g_warning ("This is a %d warning", g_random_int ());
342 g_return_if_reached ();
346 test_expected_messages_expect_warning (void)
348 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
349 "This is a * warning");
350 test_expected_messages_warning ();
354 test_expected_messages_wrong_warning (void)
356 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
358 test_expected_messages_warning ();
362 test_expected_messages_expected (void)
364 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
365 "This is a * warning");
366 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
367 "*should not be reached");
369 test_expected_messages_warning ();
371 g_test_assert_expected_messages ();
376 test_expected_messages_null_domain (void)
378 g_test_expect_message (NULL, G_LOG_LEVEL_WARNING, "no domain");
379 g_log (NULL, G_LOG_LEVEL_WARNING, "no domain");
380 g_test_assert_expected_messages ();
384 test_expected_messages_expect_error (void)
386 /* make sure we can't try to expect a g_error() */
387 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*G_LOG_LEVEL_ERROR*");
388 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "this won't work");
389 g_test_assert_expected_messages ();
393 test_expected_messages_extra_warning (void)
395 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
396 "This is a * warning");
397 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
398 "*should not be reached");
399 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
402 test_expected_messages_warning ();
404 /* If we don't assert, it won't notice the missing message */
409 test_expected_messages_unexpected_extra_warning (void)
411 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
412 "This is a * warning");
413 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
414 "*should not be reached");
415 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
418 test_expected_messages_warning ();
420 g_test_assert_expected_messages ();
425 test_expected_messages (void)
427 g_test_trap_subprocess ("/misc/expected-messages/subprocess/warning", 0, 0);
428 g_test_trap_assert_failed ();
429 g_test_trap_assert_stderr ("*This is a * warning*");
430 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
432 g_test_trap_subprocess ("/misc/expected-messages/subprocess/expect-warning", 0, 0);
433 g_test_trap_assert_failed ();
434 g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
435 g_test_trap_assert_stderr ("*should not be reached*");
437 g_test_trap_subprocess ("/misc/expected-messages/subprocess/wrong-warning", 0, 0);
438 g_test_trap_assert_failed ();
439 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
440 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
442 g_test_trap_subprocess ("/misc/expected-messages/subprocess/expected", 0, 0);
443 g_test_trap_assert_passed ();
444 g_test_trap_assert_stderr ("");
446 g_test_trap_subprocess ("/misc/expected-messages/subprocess/null-domain", 0, 0);
447 g_test_trap_assert_passed ();
448 g_test_trap_assert_stderr ("");
450 g_test_trap_subprocess ("/misc/expected-messages/subprocess/extra-warning", 0, 0);
451 g_test_trap_assert_passed ();
452 g_test_trap_assert_stderr ("");
454 g_test_trap_subprocess ("/misc/expected-messages/subprocess/unexpected-extra-warning", 0, 0);
455 g_test_trap_assert_failed ();
456 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
460 test_expected_messages_debug (void)
462 g_test_expect_message ("Test", G_LOG_LEVEL_WARNING, "warning message");
463 g_log ("Test", G_LOG_LEVEL_DEBUG, "should be ignored");
464 g_log ("Test", G_LOG_LEVEL_WARNING, "warning message");
465 g_test_assert_expected_messages ();
467 g_test_expect_message ("Test", G_LOG_LEVEL_DEBUG, "debug message");
468 g_log ("Test", G_LOG_LEVEL_DEBUG, "debug message");
469 g_test_assert_expected_messages ();
473 test_dash_p_hidden (void)
475 if (!g_test_subprocess ())
476 g_assert_not_reached ();
478 g_print ("Test /misc/dash-p/subprocess/hidden ran\n");
482 test_dash_p_hidden_sub (void)
484 if (!g_test_subprocess ())
485 g_assert_not_reached ();
487 g_print ("Test /misc/dash-p/subprocess/hidden/sub ran\n");
490 /* The rest of the dash_p tests will get run by the toplevel test
491 * process, but they shouldn't do anything there.
495 test_dash_p_child (void)
497 if (!g_test_subprocess ())
500 g_print ("Test /misc/dash-p/child ran\n");
504 test_dash_p_child_sub (void)
506 if (!g_test_subprocess ())
509 g_print ("Test /misc/dash-p/child/sub ran\n");
513 test_dash_p_child_sub2 (void)
515 if (!g_test_subprocess ())
518 g_print ("Test /misc/dash-p/child/sub2 ran\n");
522 test_dash_p_child_sub_child (void)
524 if (!g_test_subprocess ())
527 g_print ("Test /misc/dash-p/child/subprocess ran\n");
533 g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden", 0, 0);
534 g_test_trap_assert_passed ();
535 g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden ran*");
536 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
537 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
538 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub/subprocess ran*");
539 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
541 g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden/sub", 0, 0);
542 g_test_trap_assert_passed ();
543 g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
544 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden ran*");
545 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
546 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/subprocess ran*");
547 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
549 g_test_trap_subprocess ("/misc/dash-p/child", 0, 0);
550 g_test_trap_assert_passed ();
551 g_test_trap_assert_stdout ("*Test /misc/dash-p/child ran*");
552 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
553 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub2 ran*");
554 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
555 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
557 g_test_trap_subprocess ("/misc/dash-p/child/sub", 0, 0);
558 g_test_trap_assert_passed ();
559 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
560 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child ran*");
561 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/sub2 ran*");
562 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
563 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
567 test_nonfatal_subprocess (void)
569 g_test_set_nonfatal_assertions ();
571 g_assert_cmpint (4, ==, 5);
573 g_print ("The End\n");
579 g_test_trap_subprocess ("/misc/nonfatal/subprocess", 0, 0);
580 g_test_trap_assert_failed ();
581 g_test_trap_assert_stderr ("*assertion failed*4 == 5*");
582 g_test_trap_assert_stdout ("*The End*");
589 g_test_init (&argc, &argv, NULL);
591 g_test_add_func ("/random-generator/rand-1", test_rand1);
592 g_test_add_func ("/random-generator/rand-2", test_rand2);
593 g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
594 g_test_add_func ("/misc/assertions", test_assertions);
595 g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr);
596 g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint);
597 g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
598 g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
600 g_test_add_func ("/misc/timer", test_timer);
603 g_test_add_func ("/forking/fail assertion", test_fork_fail);
604 g_test_add_func ("/forking/patterns", test_fork_patterns);
606 g_test_add_func ("/forking/timeout", test_fork_timeout);
609 g_test_add_func ("/trap_subprocess/fail", test_subprocess_fail);
610 g_test_add_func ("/trap_subprocess/fail/subprocess", test_subprocess_fail_child);
611 g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
612 g_test_add_func ("/trap_subprocess/no-such-test/subprocess", test_subprocess_no_such_test_child);
615 g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
616 g_test_add_func ("/trap_subprocess/timeout/subprocess", test_subprocess_timeout_child);
618 g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
619 g_test_add_func ("/trap_subprocess/patterns/subprocess", test_subprocess_patterns_child);
621 g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
622 g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-pass", test_fatal_log_handler_critical_pass);
623 g_test_add_func ("/misc/fatal-log-handler/subprocess/error-fail", test_fatal_log_handler_error_fail);
624 g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-fail", test_fatal_log_handler_critical_fail);
626 g_test_add_func ("/misc/expected-messages", test_expected_messages);
627 g_test_add_func ("/misc/expected-messages/subprocess/warning", test_expected_messages_warning);
628 g_test_add_func ("/misc/expected-messages/subprocess/expect-warning", test_expected_messages_expect_warning);
629 g_test_add_func ("/misc/expected-messages/subprocess/wrong-warning", test_expected_messages_wrong_warning);
630 g_test_add_func ("/misc/expected-messages/subprocess/expected", test_expected_messages_expected);
631 g_test_add_func ("/misc/expected-messages/subprocess/null-domain", test_expected_messages_null_domain);
632 g_test_add_func ("/misc/expected-messages/subprocess/extra-warning", test_expected_messages_extra_warning);
633 g_test_add_func ("/misc/expected-messages/subprocess/unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);
634 g_test_add_func ("/misc/expected-messages/expect-error", test_expected_messages_expect_error);
635 g_test_add_func ("/misc/expected-messages/skip-debug", test_expected_messages_debug);
637 g_test_add_func ("/misc/dash-p", test_dash_p);
638 g_test_add_func ("/misc/dash-p/child", test_dash_p_child);
639 g_test_add_func ("/misc/dash-p/child/sub", test_dash_p_child_sub);
640 g_test_add_func ("/misc/dash-p/child/sub/subprocess", test_dash_p_child_sub_child);
641 g_test_add_func ("/misc/dash-p/child/sub/subprocess/child", test_dash_p_child_sub_child);
642 g_test_add_func ("/misc/dash-p/child/sub2", test_dash_p_child_sub2);
643 g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden);
644 g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub);
646 g_test_add_func ("/misc/nonfatal", test_nonfatal);
647 g_test_add_func ("/misc/nonfatal/subprocess", test_nonfatal_subprocess);