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.
27 /* test assertion variants */
29 test_assertions (void)
32 g_assert_cmpint (1, >, 0);
33 g_assert_cmphex (2, ==, 2);
34 g_assert_cmpfloat (3.3, !=, 7);
35 g_assert_cmpfloat (7, <=, 3 + 4);
37 g_assert_cmpstr ("foo", !=, "faa");
38 fuu
= g_strdup_printf ("f%s", "uu");
39 g_test_queue_free (fuu
);
40 g_assert_cmpstr ("foo", !=, fuu
);
41 g_assert_cmpstr ("fuu", ==, fuu
);
42 g_assert_cmpstr (NULL
, <, "");
43 g_assert_cmpstr (NULL
, ==, NULL
);
44 g_assert_cmpstr ("", >, NULL
);
45 g_assert_cmpstr ("foo", <, "fzz");
46 g_assert_cmpstr ("fzz", >, "faa");
47 g_assert_cmpstr ("fzz", ==, "fzz");
49 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
51 g_assert_cmpstr ("fzz", !=, "fzz");
53 g_test_trap_assert_failed ();
54 g_test_trap_assert_stderr ("*assertion failed*");
56 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
58 g_assert_cmpint (4, !=, 4);
60 g_test_trap_assert_failed ();
61 g_test_trap_assert_stderr ("*assertion failed*");
64 /* test g_test_timer* API */
70 g_assert_cmpfloat (g_test_timer_last(), ==, 0);
72 ttime
= g_test_timer_elapsed();
73 g_assert_cmpfloat (ttime
, >, 0);
74 g_assert_cmpfloat (g_test_timer_last(), ==, ttime
);
75 g_test_minimized_result (ttime
, "timer-test-time: %fsec", ttime
);
76 g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */
79 /* fork out for a failing test */
83 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
85 g_assert_not_reached();
87 g_test_trap_assert_failed();
88 g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
91 /* fork out to assert stdout and stderr patterns */
93 test_fork_patterns (void)
95 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT
| G_TEST_TRAP_SILENCE_STDERR
))
97 g_print ("some stdout text: somagic17\n");
98 g_printerr ("some stderr text: semagic43\n");
101 g_test_trap_assert_passed();
102 g_test_trap_assert_stdout ("*somagic17*");
103 g_test_trap_assert_stderr ("*semagic43*");
106 /* fork out for a timeout test */
108 test_fork_timeout (void)
110 /* allow child to run for only a fraction of a second */
111 if (g_test_trap_fork (0.11 * 1000000, 0))
113 /* loop and sleep forever */
115 g_usleep (1000 * 1000);
117 g_test_trap_assert_failed();
118 g_assert (g_test_trap_reached_timeout());
121 /* run a test with fixture setup and teardown */
128 fixturetest_setup (Fixturetest
*fix
,
129 gconstpointer test_data
)
131 g_assert (test_data
== (void*) 0xc0cac01a);
134 fix
->msg
= g_strdup_printf ("%d", fix
->prime
);
137 fixturetest_test (Fixturetest
*fix
,
138 gconstpointer test_data
)
140 guint prime
= g_spaced_primes_closest (fix
->seed
);
141 g_assert_cmpint (prime
, ==, fix
->prime
);
142 prime
= g_ascii_strtoull (fix
->msg
, NULL
, 0);
143 g_assert_cmpint (prime
, ==, fix
->prime
);
144 g_assert (test_data
== (void*) 0xc0cac01a);
147 fixturetest_teardown (Fixturetest
*fix
,
148 gconstpointer test_data
)
150 g_assert (test_data
== (void*) 0xc0cac01a);
155 int bit
, vint1
, vint2
, irange
;
156 long double vdouble
, drange
;
162 shared_rand_state
.bit
= g_test_rand_bit();
163 shared_rand_state
.vint1
= g_test_rand_int();
164 shared_rand_state
.vint2
= g_test_rand_int();
165 g_assert_cmpint (shared_rand_state
.vint1
, !=, shared_rand_state
.vint2
);
166 shared_rand_state
.irange
= g_test_rand_int_range (17, 35);
167 g_assert_cmpint (shared_rand_state
.irange
, >=, 17);
168 g_assert_cmpint (shared_rand_state
.irange
, <=, 35);
169 shared_rand_state
.vdouble
= g_test_rand_double();
170 shared_rand_state
.drange
= g_test_rand_double_range (-999, +17);
171 g_assert_cmpfloat (shared_rand_state
.drange
, >=, -999);
172 g_assert_cmpfloat (shared_rand_state
.drange
, <=, +17);
178 /* this test only works if run after test1.
179 * we do this to check that random number generators
180 * are reseeded upon fixture setup.
182 g_assert_cmpint (shared_rand_state
.bit
, ==, g_test_rand_bit());
183 g_assert_cmpint (shared_rand_state
.vint1
, ==, g_test_rand_int());
184 g_assert_cmpint (shared_rand_state
.vint2
, ==, g_test_rand_int());
185 g_assert_cmpint (shared_rand_state
.irange
, ==, g_test_rand_int_range (17, 35));
186 g_assert_cmpfloat (shared_rand_state
.vdouble
, ==, g_test_rand_double());
187 g_assert_cmpfloat (shared_rand_state
.drange
, ==, g_test_rand_double_range (-999, +17));
191 test_data_test (gconstpointer test_data
)
193 g_assert (test_data
== (void*) 0xc0c0baba);
197 test_random_conversions (void)
199 /* very simple conversion test using random numbers */
200 int vint
= g_test_rand_int();
201 char *err
, *str
= g_strdup_printf ("%d", vint
);
202 gint64 vint64
= g_ascii_strtoll (str
, &err
, 10);
203 g_assert_cmphex (vint
, ==, vint64
);
204 g_assert (!err
|| *err
== 0);
209 fatal_handler (const gchar
*log_domain
,
210 GLogLevelFlags log_level
,
211 const gchar
*message
,
218 test_fatal_log_handler (void)
220 g_test_log_set_fatal_handler (fatal_handler
, NULL
);
221 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
223 g_str_has_prefix (NULL
, "file://");
224 g_critical ("Test passing");
227 g_test_trap_assert_passed ();
229 g_test_log_set_fatal_handler (NULL
, NULL
);
230 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
231 g_error ("Test failing");
232 g_test_trap_assert_failed ();
234 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
235 g_str_has_prefix (NULL
, "file://");
236 g_test_trap_assert_failed ();
240 expected_messages_helper (void)
242 g_warning ("This is a %d warning", g_random_int ());
243 g_return_if_reached ();
247 test_expected_messages (void)
249 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
251 expected_messages_helper ();
254 g_test_trap_assert_failed ();
255 g_test_trap_assert_stderr ("*This is a * warning*");
256 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
258 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
260 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
261 "This is a * warning");
262 expected_messages_helper ();
265 g_test_trap_assert_failed ();
266 g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
267 g_test_trap_assert_stderr ("*should not be reached*");
269 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
271 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
273 expected_messages_helper ();
276 g_test_trap_assert_failed ();
277 g_test_trap_assert_stderr_unmatched ("*should not be reached*");
278 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
280 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
282 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
283 "This is a * warning");
284 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
285 "*should not be reached");
286 expected_messages_helper ();
287 g_test_assert_expected_messages ();
290 g_test_trap_assert_passed ();
291 g_test_trap_assert_stderr ("");
293 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
295 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
296 "This is a * warning");
297 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
298 "*should not be reached");
299 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
301 expected_messages_helper ();
302 /* If we don't assert, it won't notice the missing message */
305 g_test_trap_assert_passed ();
306 g_test_trap_assert_stderr ("");
308 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR
))
310 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
311 "This is a * warning");
312 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
313 "*should not be reached");
314 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
316 expected_messages_helper ();
317 g_test_assert_expected_messages ();
320 g_test_trap_assert_failed ();
321 g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
328 g_test_init (&argc
, &argv
, NULL
);
330 g_test_add_func ("/random-generator/rand-1", test_rand1
);
331 g_test_add_func ("/random-generator/rand-2", test_rand2
);
332 g_test_add_func ("/random-generator/random-conversions", test_random_conversions
);
333 g_test_add_func ("/misc/assertions", test_assertions
);
334 g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test
);
335 g_test_add ("/misc/primetoul", Fixturetest
, (void*) 0xc0cac01a, fixturetest_setup
, fixturetest_test
, fixturetest_teardown
);
337 g_test_add_func ("/misc/timer", test_timer
);
338 g_test_add_func ("/forking/fail assertion", test_fork_fail
);
339 g_test_add_func ("/forking/patterns", test_fork_patterns
);
341 g_test_add_func ("/forking/timeout", test_fork_timeout
);
342 g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler
);
343 g_test_add_func ("/misc/expected-messages", test_expected_messages
);