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_extra_warning (void)
378 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
379 "This is a * warning");
380 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
381 "*should not be reached");
382 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
385 test_expected_messages_warning ();
387 /* If we don't assert, it won't notice the missing message */
392 test_expected_messages_unexpected_extra_warning (void)
394 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
395 "This is a * warning");
396 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
397 "*should not be reached");
398 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
401 test_expected_messages_warning ();
403 g_test_assert_expected_messages ();
408 test_expected_messages (void)
410 g_test_trap_subprocess ("/misc/expected-messages/subprocess/warning", 0, 0);
411 g_test_trap_assert_failed ();
412 g_test_trap_assert_stderr ("*This is a * warning*");
413 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
415 g_test_trap_subprocess ("/misc/expected-messages/subprocess/expect-warning", 0, 0);
416 g_test_trap_assert_failed ();
417 g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
418 g_test_trap_assert_stderr ("*should not be reached*");
420 g_test_trap_subprocess ("/misc/expected-messages/subprocess/wrong-warning", 0, 0);
421 g_test_trap_assert_failed ();
422 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
423 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
425 g_test_trap_subprocess ("/misc/expected-messages/subprocess/expected", 0, 0);
426 g_test_trap_assert_passed ();
427 g_test_trap_assert_stderr ("");
429 g_test_trap_subprocess ("/misc/expected-messages/subprocess/extra-warning", 0, 0);
430 g_test_trap_assert_passed ();
431 g_test_trap_assert_stderr ("");
433 g_test_trap_subprocess ("/misc/expected-messages/subprocess/unexpected-extra-warning", 0, 0);
434 g_test_trap_assert_failed ();
435 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
439 test_dash_p_hidden (void)
441 if (!g_test_subprocess ())
442 g_assert_not_reached ();
444 g_print ("Test /misc/dash-p/subprocess/hidden ran\n");
448 test_dash_p_hidden_sub (void)
450 if (!g_test_subprocess ())
451 g_assert_not_reached ();
453 g_print ("Test /misc/dash-p/subprocess/hidden/sub ran\n");
456 /* The rest of the dash_p tests will get run by the toplevel test
457 * process, but they shouldn't do anything there.
461 test_dash_p_child (void)
463 if (!g_test_subprocess ())
466 g_print ("Test /misc/dash-p/child ran\n");
470 test_dash_p_child_sub (void)
472 if (!g_test_subprocess ())
475 g_print ("Test /misc/dash-p/child/sub ran\n");
479 test_dash_p_child_sub2 (void)
481 if (!g_test_subprocess ())
484 g_print ("Test /misc/dash-p/child/sub2 ran\n");
488 test_dash_p_child_sub_child (void)
490 if (!g_test_subprocess ())
493 g_print ("Test /misc/dash-p/child/subprocess ran\n");
499 g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden", 0, 0);
500 g_test_trap_assert_passed ();
501 g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden ran*");
502 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
503 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
504 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub/subprocess ran*");
505 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
507 g_test_trap_subprocess ("/misc/dash-p/subprocess/hidden/sub", 0, 0);
508 g_test_trap_assert_passed ();
509 g_test_trap_assert_stdout ("*Test /misc/dash-p/subprocess/hidden/sub ran*");
510 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden ran*");
511 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/sub2 ran*");
512 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden/subprocess ran*");
513 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child*");
515 g_test_trap_subprocess ("/misc/dash-p/child", 0, 0);
516 g_test_trap_assert_passed ();
517 g_test_trap_assert_stdout ("*Test /misc/dash-p/child ran*");
518 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
519 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub2 ran*");
520 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
521 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
523 g_test_trap_subprocess ("/misc/dash-p/child/sub", 0, 0);
524 g_test_trap_assert_passed ();
525 g_test_trap_assert_stdout ("*Test /misc/dash-p/child/sub ran*");
526 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child ran*");
527 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/sub2 ran*");
528 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/child/subprocess ran*");
529 g_test_trap_assert_stdout_unmatched ("*Test /misc/dash-p/subprocess/hidden*");
536 g_test_init (&argc, &argv, NULL);
538 g_test_add_func ("/random-generator/rand-1", test_rand1);
539 g_test_add_func ("/random-generator/rand-2", test_rand2);
540 g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
541 g_test_add_func ("/misc/assertions", test_assertions);
542 g_test_add_func ("/misc/assertions/subprocess/bad_cmpstr", test_assertions_bad_cmpstr);
543 g_test_add_func ("/misc/assertions/subprocess/bad_cmpint", test_assertions_bad_cmpint);
544 g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
545 g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
547 g_test_add_func ("/misc/timer", test_timer);
550 g_test_add_func ("/forking/fail assertion", test_fork_fail);
551 g_test_add_func ("/forking/patterns", test_fork_patterns);
553 g_test_add_func ("/forking/timeout", test_fork_timeout);
556 g_test_add_func ("/trap_subprocess/fail", test_subprocess_fail);
557 g_test_add_func ("/trap_subprocess/fail/subprocess", test_subprocess_fail_child);
558 g_test_add_func ("/trap_subprocess/no-such-test", test_subprocess_no_such_test);
559 g_test_add_func ("/trap_subprocess/no-such-test/subprocess", test_subprocess_no_such_test_child);
562 g_test_add_func ("/trap_subprocess/timeout", test_subprocess_timeout);
563 g_test_add_func ("/trap_subprocess/timeout/subprocess", test_subprocess_timeout_child);
565 g_test_add_func ("/trap_subprocess/patterns", test_subprocess_patterns);
566 g_test_add_func ("/trap_subprocess/patterns/subprocess", test_subprocess_patterns_child);
568 g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
569 g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-pass", test_fatal_log_handler_critical_pass);
570 g_test_add_func ("/misc/fatal-log-handler/subprocess/error-fail", test_fatal_log_handler_error_fail);
571 g_test_add_func ("/misc/fatal-log-handler/subprocess/critical-fail", test_fatal_log_handler_critical_fail);
573 g_test_add_func ("/misc/expected-messages", test_expected_messages);
574 g_test_add_func ("/misc/expected-messages/subprocess/warning", test_expected_messages_warning);
575 g_test_add_func ("/misc/expected-messages/subprocess/expect-warning", test_expected_messages_expect_warning);
576 g_test_add_func ("/misc/expected-messages/subprocess/wrong-warning", test_expected_messages_wrong_warning);
577 g_test_add_func ("/misc/expected-messages/subprocess/expected", test_expected_messages_expected);
578 g_test_add_func ("/misc/expected-messages/subprocess/extra-warning", test_expected_messages_extra_warning);
579 g_test_add_func ("/misc/expected-messages/subprocess/unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);
581 g_test_add_func ("/misc/dash-p", test_dash_p);
582 g_test_add_func ("/misc/dash-p/child", test_dash_p_child);
583 g_test_add_func ("/misc/dash-p/child/sub", test_dash_p_child_sub);
584 g_test_add_func ("/misc/dash-p/child/sub/subprocess", test_dash_p_child_sub_child);
585 g_test_add_func ("/misc/dash-p/child/sub/subprocess/child", test_dash_p_child_sub_child);
586 g_test_add_func ("/misc/dash-p/child/sub2", test_dash_p_child_sub2);
587 g_test_add_func ("/misc/dash-p/subprocess/hidden", test_dash_p_hidden);
588 g_test_add_func ("/misc/dash-p/subprocess/hidden/sub", test_dash_p_hidden_sub);