1 /* GLib testing framework examples and tests
2 * Copyright (C) 2007 Tim Janik
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
21 #include <glib/gtestframework.h>
24 /* test assertion variants */
26 test_assertions (void)
28 g_assert_cmpint (1, >, 0);
29 g_assert_cmphex (2, ==, 2);
30 g_assert_cmpfloat (3.3, !=, 7);
31 g_assert_cmpfloat (7, <=, 3 + 4);
33 g_assert_cmpstr ("foo", !=, "faa");
34 gchar *fuu = g_strdup_printf ("f%s", "uu");
35 g_test_queue_free (fuu);
36 g_assert_cmpstr ("foo", !=, fuu);
37 g_assert_cmpstr ("fuu", ==, fuu);
38 g_assert_cmpstr (NULL, <, "");
39 g_assert_cmpstr (NULL, ==, NULL);
40 g_assert_cmpstr ("", >, NULL);
41 g_assert_cmpstr ("foo", <, "fzz");
42 g_assert_cmpstr ("fzz", >, "faa");
43 g_assert_cmpstr ("fzz", ==, "fzz");
46 /* fork out for a failing test */
50 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
52 g_assert_not_reached();
54 g_test_trap_assert_failed();
55 g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
58 /* fork out to assert stdout and stderr patterns */
60 test_fork_patterns (void)
62 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
64 g_print ("some stdout text: somagic17\n");
65 g_printerr ("some stderr text: semagic43\n");
68 g_test_trap_assert_passed();
69 g_test_trap_assert_stdout ("*somagic17*");
70 g_test_trap_assert_stderr ("*semagic43*");
73 /* fork out for a timeout test */
75 test_fork_timeout (void)
77 /* allow child to run for only a fraction of a second */
78 if (g_test_trap_fork (0.11 * 1000000, 0))
80 /* loop and sleep forever */
82 g_usleep (1000 * 1000);
84 g_test_trap_assert_failed();
85 g_assert (g_test_trap_reached_timeout());
88 /* run a test with fixture setup and teardown */
95 fixturetest_setup (Fixturetest *fix)
99 fix->msg = g_strdup_printf ("%d", fix->prime);
102 fixturetest_test (Fixturetest *fix)
104 guint prime = g_spaced_primes_closest (fix->seed);
105 g_assert_cmpint (prime, ==, fix->prime);
106 prime = g_ascii_strtoull (fix->msg, NULL, 0);
107 g_assert_cmpint (prime, ==, fix->prime);
110 fixturetest_teardown (Fixturetest *fix)
116 int bit, vint1, vint2, irange;
117 long double vdouble, drange;
123 shared_rand_state.bit = g_test_rand_bit();
124 shared_rand_state.vint1 = g_test_rand_int();
125 shared_rand_state.vint2 = g_test_rand_int();
126 g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
127 shared_rand_state.irange = g_test_rand_int_range (17, 35);
128 g_assert_cmpint (shared_rand_state.irange, >=, 17);
129 g_assert_cmpint (shared_rand_state.irange, <=, 35);
130 shared_rand_state.vdouble = g_test_rand_double();
131 shared_rand_state.drange = g_test_rand_double_range (-999, +17);
132 g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
133 g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
139 /* this test only works if run after test1.
140 * we do this to check that random number generators
141 * are reseeded upon fixture setup.
143 g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
144 g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
145 g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
146 g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
147 g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
148 g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
155 g_test_init (&argc, &argv, NULL);
157 g_test_add_func ("/random-generator/rand-1", test_rand1);
158 g_test_add_func ("/random-generator/rand-2", test_rand2);
159 g_test_add_func ("/misc/assertions", test_assertions);
160 g_test_add ("/misc/primetoul", Fixturetest, fixturetest_setup, fixturetest_test, fixturetest_teardown);
161 g_test_add_func ("/forking/fail assertion", test_fork_fail);
162 g_test_add_func ("/forking/patterns", test_fork_patterns);
163 g_test_add_func ("/forking/timeout", test_fork_timeout);