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 (void)
139 if (g_test_subprocess ())
141 g_assert_not_reached ();
145 g_test_trap_subprocess (NULL, 0, 0);
146 g_test_trap_assert_failed ();
147 g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail*should not be reached*");
151 test_subprocess_no_such_test (void)
153 if (g_test_subprocess ())
155 g_test_trap_subprocess ("/trap_subprocess/this-test-does-not-exist", 0, 0);
156 g_assert_not_reached ();
159 g_test_trap_subprocess (NULL, 0, 0);
160 g_test_trap_assert_failed ();
161 g_test_trap_assert_stderr ("*test does not exist*");
162 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
166 test_subprocess_patterns (void)
168 if (g_test_subprocess ())
170 g_print ("some stdout text: somagic17\n");
171 g_printerr ("some stderr text: semagic43\n");
174 g_test_trap_subprocess (NULL, 0, 0);
175 g_test_trap_assert_passed ();
176 g_test_trap_assert_stdout ("*somagic17*");
177 g_test_trap_assert_stderr ("*semagic43*");
181 test_subprocess_timeout (void)
183 if (g_test_subprocess ())
185 /* loop and sleep forever */
187 g_usleep (1000 * 1000);
190 /* allow child to run for only a fraction of a second */
191 g_test_trap_subprocess (NULL, 0.11 * 1000000, 0);
192 g_test_trap_assert_failed ();
193 g_assert (g_test_trap_reached_timeout ());
196 /* run a test with fixture setup and teardown */
203 fixturetest_setup (Fixturetest *fix,
204 gconstpointer test_data)
206 g_assert (test_data == (void*) 0xc0cac01a);
209 fix->msg = g_strdup_printf ("%d", fix->prime);
212 fixturetest_test (Fixturetest *fix,
213 gconstpointer test_data)
215 guint prime = g_spaced_primes_closest (fix->seed);
216 g_assert_cmpint (prime, ==, fix->prime);
217 prime = g_ascii_strtoull (fix->msg, NULL, 0);
218 g_assert_cmpint (prime, ==, fix->prime);
219 g_assert (test_data == (void*) 0xc0cac01a);
222 fixturetest_teardown (Fixturetest *fix,
223 gconstpointer test_data)
225 g_assert (test_data == (void*) 0xc0cac01a);
230 int bit, vint1, vint2, irange;
231 long double vdouble, drange;
237 shared_rand_state.bit = g_test_rand_bit();
238 shared_rand_state.vint1 = g_test_rand_int();
239 shared_rand_state.vint2 = g_test_rand_int();
240 g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
241 shared_rand_state.irange = g_test_rand_int_range (17, 35);
242 g_assert_cmpint (shared_rand_state.irange, >=, 17);
243 g_assert_cmpint (shared_rand_state.irange, <=, 35);
244 shared_rand_state.vdouble = g_test_rand_double();
245 shared_rand_state.drange = g_test_rand_double_range (-999, +17);
246 g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
247 g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
253 /* this test only works if run after test1.
254 * we do this to check that random number generators
255 * are reseeded upon fixture setup.
257 g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
258 g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
259 g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
260 g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
261 g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
262 g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
266 test_data_test (gconstpointer test_data)
268 g_assert (test_data == (void*) 0xc0c0baba);
272 test_random_conversions (void)
274 /* very simple conversion test using random numbers */
275 int vint = g_test_rand_int();
276 char *err, *str = g_strdup_printf ("%d", vint);
277 gint64 vint64 = g_ascii_strtoll (str, &err, 10);
278 g_assert_cmphex (vint, ==, vint64);
279 g_assert (!err || *err == 0);
284 fatal_handler (const gchar *log_domain,
285 GLogLevelFlags log_level,
286 const gchar *message,
293 test_fatal_log_handler_critical_pass (void)
295 g_test_log_set_fatal_handler (fatal_handler, NULL);
296 g_str_has_prefix (NULL, "file://");
297 g_critical ("Test passing");
302 test_fatal_log_handler_error_fail (void)
304 g_error ("Test failing");
309 test_fatal_log_handler_critical_fail (void)
311 g_str_has_prefix (NULL, "file://");
312 g_critical ("Test passing");
317 test_fatal_log_handler (void)
319 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-pass", 0, 0);
320 g_test_trap_assert_passed ();
321 g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
322 g_test_trap_assert_stderr ("*CRITICAL*Test passing*");
324 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/error-fail", 0, 0);
325 g_test_trap_assert_failed ();
326 g_test_trap_assert_stderr ("*ERROR*Test failing*");
328 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-fail", 0, 0);
329 g_test_trap_assert_failed ();
330 g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
331 g_test_trap_assert_stderr_unmatched ("*CRITICAL*Test passing*");
335 test_expected_messages_warning (void)
337 g_warning ("This is a %d warning", g_random_int ());
338 g_return_if_reached ();
342 test_expected_messages_expect_warning (void)
344 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
345 "This is a * warning");
346 test_expected_messages_warning ();
350 test_expected_messages_wrong_warning (void)
352 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
354 test_expected_messages_warning ();
358 test_expected_messages_expected (void)
360 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
361 "This is a * warning");
362 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
363 "*should not be reached");
365 test_expected_messages_warning ();
367 g_test_assert_expected_messages ();
372 test_expected_messages_null_domain (void)
374 g_test_expect_message (NULL, G_LOG_LEVEL_WARNING, "no domain");
375 g_log (NULL, G_LOG_LEVEL_WARNING, "no domain");
376 g_test_assert_expected_messages ();
380 test_expected_messages_expect_error (void)
382 /* make sure we can't try to expect a g_error() */
383 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*G_LOG_LEVEL_ERROR*");
384 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "this won't work");
385 g_test_assert_expected_messages ();
389 test_expected_messages_extra_warning (void)
391 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
392 "This is a * warning");
393 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
394 "*should not be reached");
395 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
398 test_expected_messages_warning ();
400 /* If we don't assert, it won't notice the missing message */
405 test_expected_messages_unexpected_extra_warning (void)
407 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
408 "This is a * warning");
409 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
410 "*should not be reached");
411 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
414 test_expected_messages_warning ();
416 g_test_assert_expected_messages ();
421 test_expected_messages (void)
423 g_test_trap_subprocess ("/misc/expected-messages/subprocess/warning", 0, 0);
424 g_test_trap_assert_failed ();
425 g_test_trap_assert_stderr ("*This is a * warning*");
426 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
428 g_test_trap_subprocess ("/misc/expected-messages/subprocess/expect-warning", 0, 0);
429 g_test_trap_assert_failed ();
430 g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
431 g_test_trap_assert_stderr ("*should not be reached*");
433 g_test_trap_subprocess ("/misc/expected-messages/subprocess/wrong-warning", 0, 0);
434 g_test_trap_assert_failed ();
435 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
436 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
438 g_test_trap_subprocess ("/misc/expected-messages/subprocess/expected", 0, 0);
439 g_test_trap_assert_passed ();
440 g_test_trap_assert_stderr ("");
442 g_test_trap_subprocess ("/misc/expected-messages/subprocess/null-domain", 0, 0);
443 g_test_trap_assert_passed ();
444 g_test_trap_assert_stderr ("");
446 g_test_trap_subprocess ("/misc/expected-messages/subprocess/extra-warning", 0, 0);
447 g_test_trap_assert_passed ();
448 g_test_trap_assert_stderr ("");
450 g_test_trap_subprocess ("/misc/expected-messages/subprocess/unexpected-extra-warning", 0, 0);
451 g_test_trap_assert_failed ();
452 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
456 test_expected_messages_debug (void)
458 g_test_expect_message ("Test", G_LOG_LEVEL_WARNING, "warning message");
459 g_log ("Test", G_LOG_LEVEL_DEBUG, "should be ignored");
460 g_log ("Test", G_LOG_LEVEL_WARNING, "warning message");
461 g_test_assert_expected_messages ();
463 g_test_expect_message ("Test", G_LOG_LEVEL_DEBUG, "debug message");
464 g_log ("Test", G_LOG_LEVEL_DEBUG, "debug message");
465 g_test_assert_expected_messages ();
469 test_dash_p_hidden (void)
471 if (!g_test_subprocess ())
472 g_assert_not_reached ();
474 g_print ("Test /misc/dash-p/subprocess/hidden ran\n");
478 test_dash_p_hidden_sub (void)
480 if (!g_test_subprocess ())
481 g_assert_not_reached ();
483 g_print ("Test /misc/dash-p/subprocess/hidden/sub ran\n");
486 /* The rest of the dash_p tests will get run by the toplevel test
487 * process, but they shouldn't do anything there.
491 test_dash_p_child (void)
493 if (!g_test_subprocess ())
496 g_print ("Test /misc/dash-p/child ran\n");
500 test_dash_p_child_sub (void)
502 if (!g_test_subprocess ())
505 g_print ("Test /misc/dash-p/child/sub ran\n");
509 test_dash_p_child_sub2 (void)
511 if (!g_test_subprocess ())
514 g_print ("Test /misc/dash-p/child/sub2 ran\n");
518 test_dash_p_child_sub_child (void)
520 if (!g_test_subprocess ())
523 g_print ("Test /misc/dash-p/child/subprocess ran\n");
529 g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden", 0, 0);
530 g_test_trap_assert_passed ();
531 g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden ran*");
532 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
533 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
534 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub/subprocess ran*");
535 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
537 g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden/sub", 0, 0);
538 g_test_trap_assert_passed ();
539 g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
540 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden ran*");
541 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
542 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/subprocess ran*");
543 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
545 g_test_trap_subprocess ("/misc/dash-p/child", 0, 0);
546 g_test_trap_assert_passed ();
547 g_test_trap_assert_stdout ("*Test /misc/dash-p/child ran*");
548 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
549 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub2 ran*");
550 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
551 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
553 g_test_trap_subprocess ("/misc/dash-p/child/sub", 0, 0);
554 g_test_trap_assert_passed ();
555 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
556 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child ran*");
557 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/sub2 ran*");
558 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
559 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
565 if (g_test_subprocess ())
567 g_test_set_nonfatal_assertions ();
568 g_assert_cmpint (4, ==, 5);
569 g_print ("The End\n");
572 g_test_trap_subprocess (NULL, 0, 0);
573 g_test_trap_assert_failed ();
574 g_test_trap_assert_stderr ("*assertion failed*4 == 5*");
575 g_test_trap_assert_stdout ("*The End*");
582 g_test_init (&argc, &argv, NULL);
584 g_test_add_func ("/random-generator/rand-1", test_rand1);
585 g_test_add_func ("/random-generator/rand-2", test_rand2);
586 g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
587 g_test_add_func ("/misc/assertions", test_assertions);
588 g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr);
589 g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint);
590 g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
591 g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
593 g_test_add_func ("/misc/timer", test_timer);
596 g_test_add_func ("/forking/fail assertion", test_fork_fail);
597 g_test_add_func ("/forking/patterns", test_fork_patterns);
599 g_test_add_func ("/forking/timeout", test_fork_timeout);
602 g_test_add_func ("/trap_subprocess/fail", test_subprocess_fail);
603 g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
605 g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
607 g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
609 g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
610 g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-pass", test_fatal_log_handler_critical_pass);
611 g_test_add_func ("/misc/fatal-log-handler/subprocess/error-fail", test_fatal_log_handler_error_fail);
612 g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-fail", test_fatal_log_handler_critical_fail);
614 g_test_add_func ("/misc/expected-messages", test_expected_messages);
615 g_test_add_func ("/misc/expected-messages/subprocess/warning", test_expected_messages_warning);
616 g_test_add_func ("/misc/expected-messages/subprocess/expect-warning", test_expected_messages_expect_warning);
617 g_test_add_func ("/misc/expected-messages/subprocess/wrong-warning", test_expected_messages_wrong_warning);
618 g_test_add_func ("/misc/expected-messages/subprocess/expected", test_expected_messages_expected);
619 g_test_add_func ("/misc/expected-messages/subprocess/null-domain", test_expected_messages_null_domain);
620 g_test_add_func ("/misc/expected-messages/subprocess/extra-warning", test_expected_messages_extra_warning);
621 g_test_add_func ("/misc/expected-messages/subprocess/unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);
622 g_test_add_func ("/misc/expected-messages/expect-error", test_expected_messages_expect_error);
623 g_test_add_func ("/misc/expected-messages/skip-debug", test_expected_messages_debug);
625 g_test_add_func ("/misc/dash-p", test_dash_p);
626 g_test_add_func ("/misc/dash-p/child", test_dash_p_child);
627 g_test_add_func ("/misc/dash-p/child/sub", test_dash_p_child_sub);
628 g_test_add_func ("/misc/dash-p/child/sub/subprocess", test_dash_p_child_sub_child);
629 g_test_add_func ("/misc/dash-p/child/sub/subprocess/child", test_dash_p_child_sub_child);
630 g_test_add_func ("/misc/dash-p/child/sub2", test_dash_p_child_sub2);
631 g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden);
632 g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub);
634 g_test_add_func ("/misc/nonfatal", test_nonfatal);