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 */
88 /* fork out for a failing test */
92 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
94 g_assert_not_reached();
96 g_test_trap_assert_failed();
97 g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
100 /* fork out to assert stdout and stderr patterns */
102 test_fork_patterns (void)
104 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
106 g_print ("some stdout text: somagic17\n");
107 g_printerr ("some stderr text: semagic43\n");
110 g_test_trap_assert_passed();
111 g_test_trap_assert_stdout ("*somagic17*");
112 g_test_trap_assert_stderr ("*semagic43*");
115 /* fork out for a timeout test */
117 test_fork_timeout (void)
119 /* allow child to run for only a fraction of a second */
120 if (g_test_trap_fork (0.11 * 1000000, 0))
122 /* loop and sleep forever */
124 g_usleep (1000 * 1000);
126 g_test_trap_assert_failed();
127 g_assert (g_test_trap_reached_timeout());
131 test_subprocess_fail_child (void)
133 g_assert_not_reached ();
137 test_subprocess_fail (void)
139 g_test_trap_subprocess ("/trap_subprocess/fail/subprocess", 0, 0);
140 g_test_trap_assert_failed ();
141 g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail_child*should not be reached*");
145 test_subprocess_no_such_test_child (void)
147 g_test_trap_subprocess ("/trap_subprocess/this-test-does-not-exist", 0, 0);
148 g_assert_not_reached ();
152 test_subprocess_no_such_test (void)
154 g_test_trap_subprocess ("/trap_subprocess/no-such-test/subprocess", 0, 0);
155 g_test_trap_assert_failed ();
156 g_test_trap_assert_stderr ("*test does not exist*");
157 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
161 test_subprocess_patterns_child (void)
163 g_print ("some stdout text: somagic17\n");
164 g_printerr ("some stderr text: semagic43\n");
169 test_subprocess_patterns (void)
171 g_test_trap_subprocess ("/trap_subprocess/patterns/subprocess", 0, 0);
172 g_test_trap_assert_passed ();
173 g_test_trap_assert_stdout ("*somagic17*");
174 g_test_trap_assert_stderr ("*semagic43*");
178 test_subprocess_timeout_child (void)
180 /* loop and sleep forever */
182 g_usleep (1000 * 1000);
186 test_subprocess_timeout (void)
188 /* allow child to run for only a fraction of a second */
189 g_test_trap_subprocess ("/trap_subprocess/timeout/subprocess", 0.11 * 1000000, 0);
190 g_test_trap_assert_failed ();
191 g_assert (g_test_trap_reached_timeout ());
194 /* run a test with fixture setup and teardown */
201 fixturetest_setup (Fixturetest *fix,
202 gconstpointer test_data)
204 g_assert (test_data == (void*) 0xc0cac01a);
207 fix->msg = g_strdup_printf ("%d", fix->prime);
210 fixturetest_test (Fixturetest *fix,
211 gconstpointer test_data)
213 guint prime = g_spaced_primes_closest (fix->seed);
214 g_assert_cmpint (prime, ==, fix->prime);
215 prime = g_ascii_strtoull (fix->msg, NULL, 0);
216 g_assert_cmpint (prime, ==, fix->prime);
217 g_assert (test_data == (void*) 0xc0cac01a);
220 fixturetest_teardown (Fixturetest *fix,
221 gconstpointer test_data)
223 g_assert (test_data == (void*) 0xc0cac01a);
228 int bit, vint1, vint2, irange;
229 long double vdouble, drange;
235 shared_rand_state.bit = g_test_rand_bit();
236 shared_rand_state.vint1 = g_test_rand_int();
237 shared_rand_state.vint2 = g_test_rand_int();
238 g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
239 shared_rand_state.irange = g_test_rand_int_range (17, 35);
240 g_assert_cmpint (shared_rand_state.irange, >=, 17);
241 g_assert_cmpint (shared_rand_state.irange, <=, 35);
242 shared_rand_state.vdouble = g_test_rand_double();
243 shared_rand_state.drange = g_test_rand_double_range (-999, +17);
244 g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
245 g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
251 /* this test only works if run after test1.
252 * we do this to check that random number generators
253 * are reseeded upon fixture setup.
255 g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
256 g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
257 g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
258 g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
259 g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
260 g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
264 test_data_test (gconstpointer test_data)
266 g_assert (test_data == (void*) 0xc0c0baba);
270 test_random_conversions (void)
272 /* very simple conversion test using random numbers */
273 int vint = g_test_rand_int();
274 char *err, *str = g_strdup_printf ("%d", vint);
275 gint64 vint64 = g_ascii_strtoll (str, &err, 10);
276 g_assert_cmphex (vint, ==, vint64);
277 g_assert (!err || *err == 0);
282 fatal_handler (const gchar *log_domain,
283 GLogLevelFlags log_level,
284 const gchar *message,
291 test_fatal_log_handler_critical_pass (void)
293 g_test_log_set_fatal_handler (fatal_handler, NULL);
294 g_str_has_prefix (NULL, "file://");
295 g_critical ("Test passing");
300 test_fatal_log_handler_error_fail (void)
302 g_error ("Test failing");
307 test_fatal_log_handler_critical_fail (void)
309 g_str_has_prefix (NULL, "file://");
310 g_critical ("Test passing");
315 test_fatal_log_handler (void)
317 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-pass", 0, 0);
318 g_test_trap_assert_passed ();
319 g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
320 g_test_trap_assert_stderr ("*CRITICAL*Test passing*");
322 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/error-fail", 0, 0);
323 g_test_trap_assert_failed ();
324 g_test_trap_assert_stderr ("*ERROR*Test failing*");
326 g_test_trap_subprocess ("/misc/fatal-log-handler/subprocess/critical-fail", 0, 0);
327 g_test_trap_assert_failed ();
328 g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
329 g_test_trap_assert_stderr_unmatched ("*CRITICAL*Test passing*");
333 test_expected_messages_warning (void)
335 g_warning ("This is a %d warning", g_random_int ());
336 g_return_if_reached ();
340 test_expected_messages_expect_warning (void)
342 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
343 "This is a * warning");
344 test_expected_messages_warning ();
348 test_expected_messages_wrong_warning (void)
350 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
352 test_expected_messages_warning ();
356 test_expected_messages_expected (void)
358 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
359 "This is a * warning");
360 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
361 "*should not be reached");
363 test_expected_messages_warning ();
365 g_test_assert_expected_messages ();
370 test_expected_messages_extra_warning (void)
372 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
373 "This is a * warning");
374 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
375 "*should not be reached");
376 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
379 test_expected_messages_warning ();
381 /* If we don't assert, it won't notice the missing message */
386 test_expected_messages_unexpected_extra_warning (void)
388 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
389 "This is a * warning");
390 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
391 "*should not be reached");
392 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
395 test_expected_messages_warning ();
397 g_test_assert_expected_messages ();
402 test_expected_messages (void)
404 g_test_trap_subprocess ("/misc/expected-messages/subprocess/warning", 0, 0);
405 g_test_trap_assert_failed ();
406 g_test_trap_assert_stderr ("*This is a * warning*");
407 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
409 g_test_trap_subprocess ("/misc/expected-messages/subprocess/expect-warning", 0, 0);
410 g_test_trap_assert_failed ();
411 g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
412 g_test_trap_assert_stderr ("*should not be reached*");
414 g_test_trap_subprocess ("/misc/expected-messages/subprocess/wrong-warning", 0, 0);
415 g_test_trap_assert_failed ();
416 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
417 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
419 g_test_trap_subprocess ("/misc/expected-messages/subprocess/expected", 0, 0);
420 g_test_trap_assert_passed ();
421 g_test_trap_assert_stderr ("");
423 g_test_trap_subprocess ("/misc/expected-messages/subprocess/extra-warning", 0, 0);
424 g_test_trap_assert_passed ();
425 g_test_trap_assert_stderr ("");
427 g_test_trap_subprocess ("/misc/expected-messages/subprocess/unexpected-extra-warning", 0, 0);
428 g_test_trap_assert_failed ();
429 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
433 test_dash_p_hidden (void)
435 if (!g_test_subprocess ())
436 g_assert_not_reached ();
438 g_print ("Test /misc/dash-p/subprocess/hidden ran\n");
442 test_dash_p_hidden_sub (void)
444 if (!g_test_subprocess ())
445 g_assert_not_reached ();
447 g_print ("Test /misc/dash-p/subprocess/hidden/sub ran\n");
450 /* The rest of the dash_p tests will get run by the toplevel test
451 * process, but they shouldn't do anything there.
455 test_dash_p_child (void)
457 if (!g_test_subprocess ())
460 g_print ("Test /misc/dash-p/child ran\n");
464 test_dash_p_child_sub (void)
466 if (!g_test_subprocess ())
469 g_print ("Test /misc/dash-p/child/sub ran\n");
473 test_dash_p_child_sub2 (void)
475 if (!g_test_subprocess ())
478 g_print ("Test /misc/dash-p/child/sub2 ran\n");
482 test_dash_p_child_sub_child (void)
484 if (!g_test_subprocess ())
487 g_print ("Test /misc/dash-p/child/subprocess ran\n");
493 g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden", 0, 0);
494 g_test_trap_assert_passed ();
495 g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden ran*");
496 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
497 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
498 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub/subprocess ran*");
499 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
501 g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden/sub", 0, 0);
502 g_test_trap_assert_passed ();
503 g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
504 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden ran*");
505 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
506 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/subprocess ran*");
507 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
509 g_test_trap_subprocess ("/misc/dash-p/child", 0, 0);
510 g_test_trap_assert_passed ();
511 g_test_trap_assert_stdout ("*Test /misc/dash-p/child ran*");
512 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
513 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub2 ran*");
514 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
515 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
517 g_test_trap_subprocess ("/misc/dash-p/child/sub", 0, 0);
518 g_test_trap_assert_passed ();
519 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
520 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child ran*");
521 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/sub2 ran*");
522 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
523 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
530 g_test_init (&argc, &argv, NULL);
532 g_test_add_func ("/random-generator/rand-1", test_rand1);
533 g_test_add_func ("/random-generator/rand-2", test_rand2);
534 g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
535 g_test_add_func ("/misc/assertions", test_assertions);
536 g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr);
537 g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint);
538 g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
539 g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
541 g_test_add_func ("/misc/timer", test_timer);
544 g_test_add_func ("/forking/fail assertion", test_fork_fail);
545 g_test_add_func ("/forking/patterns", test_fork_patterns);
547 g_test_add_func ("/forking/timeout", test_fork_timeout);
550 g_test_add_func ("/trap_subprocess/fail", test_subprocess_fail);
551 g_test_add_func ("/trap_subprocess/fail/subprocess", test_subprocess_fail_child);
552 g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
553 g_test_add_func ("/trap_subprocess/no-such-test/subprocess", test_subprocess_no_such_test_child);
556 g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
557 g_test_add_func ("/trap_subprocess/timeout/subprocess", test_subprocess_timeout_child);
559 g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
560 g_test_add_func ("/trap_subprocess/patterns/subprocess", test_subprocess_patterns_child);
562 g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
563 g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-pass", test_fatal_log_handler_critical_pass);
564 g_test_add_func ("/misc/fatal-log-handler/subprocess/error-fail", test_fatal_log_handler_error_fail);
565 g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-fail", test_fatal_log_handler_critical_fail);
567 g_test_add_func ("/misc/expected-messages", test_expected_messages);
568 g_test_add_func ("/misc/expected-messages/subprocess/warning", test_expected_messages_warning);
569 g_test_add_func ("/misc/expected-messages/subprocess/expect-warning", test_expected_messages_expect_warning);
570 g_test_add_func ("/misc/expected-messages/subprocess/wrong-warning", test_expected_messages_wrong_warning);
571 g_test_add_func ("/misc/expected-messages/subprocess/expected", test_expected_messages_expected);
572 g_test_add_func ("/misc/expected-messages/subprocess/extra-warning", test_expected_messages_extra_warning);
573 g_test_add_func ("/misc/expected-messages/subprocess/unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);
575 g_test_add_func ("/misc/dash-p", test_dash_p);
576 g_test_add_func ("/misc/dash-p/child", test_dash_p_child);
577 g_test_add_func ("/misc/dash-p/child/sub", test_dash_p_child_sub);
578 g_test_add_func ("/misc/dash-p/child/sub/subprocess", test_dash_p_child_sub_child);
579 g_test_add_func ("/misc/dash-p/child/sub/subprocess/child", test_dash_p_child_sub_child);
580 g_test_add_func ("/misc/dash-p/child/sub2", test_dash_p_child_sub2);
581 g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden);
582 g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub);