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.
22 #include <glib/gtestutils.h>
25 /* test assertion variants */
27 test_assertions (void)
30 g_assert_cmpint (1, >, 0);
31 g_assert_cmphex (2, ==, 2);
32 g_assert_cmpfloat (3.3, !=, 7);
33 g_assert_cmpfloat (7, <=, 3 + 4);
35 g_assert_cmpstr ("foo", !=, "faa");
36 fuu = g_strdup_printf ("f%s", "uu");
37 g_test_queue_free (fuu);
38 g_assert_cmpstr ("foo", !=, fuu);
39 g_assert_cmpstr ("fuu", ==, fuu);
40 g_assert_cmpstr (NULL, <, "");
41 g_assert_cmpstr (NULL, ==, NULL);
42 g_assert_cmpstr ("", >, NULL);
43 g_assert_cmpstr ("foo", <, "fzz");
44 g_assert_cmpstr ("fzz", >, "faa");
45 g_assert_cmpstr ("fzz", ==, "fzz");
48 /* test g_test_timer* API */
54 g_assert_cmpfloat (g_test_timer_last(), ==, 0);
56 ttime = g_test_timer_elapsed();
57 g_assert_cmpfloat (ttime, >, 0);
58 g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
59 g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
60 g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); // simple API test
63 /* fork out for a failing test */
67 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
69 g_assert_not_reached();
71 g_test_trap_assert_failed();
72 g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
75 /* fork out to assert stdout and stderr patterns */
77 test_fork_patterns (void)
79 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
81 g_print ("some stdout text: somagic17\n");
82 g_printerr ("some stderr text: semagic43\n");
85 g_test_trap_assert_passed();
86 g_test_trap_assert_stdout ("*somagic17*");
87 g_test_trap_assert_stderr ("*semagic43*");
90 /* fork out for a timeout test */
92 test_fork_timeout (void)
94 /* allow child to run for only a fraction of a second */
95 if (g_test_trap_fork (0.11 * 1000000, 0))
97 /* loop and sleep forever */
99 g_usleep (1000 * 1000);
101 g_test_trap_assert_failed();
102 g_assert (g_test_trap_reached_timeout());
105 /* run a test with fixture setup and teardown */
112 fixturetest_setup (Fixturetest *fix,
113 gconstpointer test_data)
115 g_assert (test_data == (void*) 0xc0cac01a);
118 fix->msg = g_strdup_printf ("%d", fix->prime);
121 fixturetest_test (Fixturetest *fix,
122 gconstpointer test_data)
124 guint prime = g_spaced_primes_closest (fix->seed);
125 g_assert_cmpint (prime, ==, fix->prime);
126 prime = g_ascii_strtoull (fix->msg, NULL, 0);
127 g_assert_cmpint (prime, ==, fix->prime);
128 g_assert (test_data == (void*) 0xc0cac01a);
131 fixturetest_teardown (Fixturetest *fix,
132 gconstpointer test_data)
134 g_assert (test_data == (void*) 0xc0cac01a);
139 int bit, vint1, vint2, irange;
140 long double vdouble, drange;
146 shared_rand_state.bit = g_test_rand_bit();
147 shared_rand_state.vint1 = g_test_rand_int();
148 shared_rand_state.vint2 = g_test_rand_int();
149 g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
150 shared_rand_state.irange = g_test_rand_int_range (17, 35);
151 g_assert_cmpint (shared_rand_state.irange, >=, 17);
152 g_assert_cmpint (shared_rand_state.irange, <=, 35);
153 shared_rand_state.vdouble = g_test_rand_double();
154 shared_rand_state.drange = g_test_rand_double_range (-999, +17);
155 g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
156 g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
162 /* this test only works if run after test1.
163 * we do this to check that random number generators
164 * are reseeded upon fixture setup.
166 g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
167 g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
168 g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
169 g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
170 g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
171 g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
175 test_data_test (gconstpointer test_data)
177 g_assert (test_data == (void*) 0xc0c0baba);
184 g_test_init (&argc, &argv, NULL);
186 g_test_add_func ("/random-generator/rand-1", test_rand1);
187 g_test_add_func ("/random-generator/rand-2", test_rand2);
188 g_test_add_func ("/misc/assertions", test_assertions);
189 g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
190 g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
192 g_test_add_func ("/misc/timer", test_timer);
193 g_test_add_func ("/forking/fail assertion", test_fork_fail);
194 g_test_add_func ("/forking/patterns", test_fork_patterns);
196 g_test_add_func ("/forking/timeout", test_fork_timeout);