b9695d168e4e21955fcfcabfa0285fbdf59246d4
[platform/upstream/glib.git] / glib / tests / testing.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2007 Imendio AB
3  * Authors: Tim Janik
4  *
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.
8  *
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.
12  *
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.
21  */
22
23 #include <glib.h>
24
25 #include <stdlib.h>
26
27 /* test assertion variants */
28 static void
29 test_assertions_bad_cmpstr (void)
30 {
31   g_assert_cmpstr ("fzz", !=, "fzz");
32   exit (0);
33 }
34
35 static void
36 test_assertions_bad_cmpint (void)
37 {
38   g_assert_cmpint (4, !=, 4);
39   exit (0);
40 }
41
42 static void
43 test_assertions (void)
44 {
45   gchar *fuu;
46   g_assert_cmpint (1, >, 0);
47   g_assert_cmphex (2, ==, 2);
48   g_assert_cmpfloat (3.3, !=, 7);
49   g_assert_cmpfloat (7, <=, 3 + 4);
50   g_assert (TRUE);
51   g_assert_cmpstr ("foo", !=, "faa");
52   fuu = g_strdup_printf ("f%s", "uu");
53   g_test_queue_free (fuu);
54   g_assert_cmpstr ("foo", !=, fuu);
55   g_assert_cmpstr ("fuu", ==, fuu);
56   g_assert_cmpstr (NULL, <, "");
57   g_assert_cmpstr (NULL, ==, NULL);
58   g_assert_cmpstr ("", >, NULL);
59   g_assert_cmpstr ("foo", <, "fzz");
60   g_assert_cmpstr ("fzz", >, "faa");
61   g_assert_cmpstr ("fzz", ==, "fzz");
62
63   g_test_trap_subprocess ("/misc/assertions:bad_cmpstr",
64                           0, G_TEST_TRAP_SILENCE_STDERR);
65   g_test_trap_assert_failed ();
66   g_test_trap_assert_stderr ("*assertion failed*");
67
68   g_test_trap_subprocess ("/misc/assertions:bad_cmpint",
69                           0, G_TEST_TRAP_SILENCE_STDERR);
70   g_test_trap_assert_failed ();
71   g_test_trap_assert_stderr ("*assertion failed*");
72 }
73
74 /* test g_test_timer* API */
75 static void
76 test_timer (void)
77 {
78   double ttime;
79   g_test_timer_start();
80   g_assert_cmpfloat (g_test_timer_last(), ==, 0);
81   g_usleep (25 * 1000);
82   ttime = g_test_timer_elapsed();
83   g_assert_cmpfloat (ttime, >, 0);
84   g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
85   g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
86   g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */
87 }
88
89 #ifdef G_OS_UNIX
90 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
91
92 /* fork out for a failing test */
93 static void
94 test_fork_fail (void)
95 {
96   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
97     {
98       g_assert_not_reached();
99     }
100   g_test_trap_assert_failed();
101   g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
102 }
103
104 /* fork out to assert stdout and stderr patterns */
105 static void
106 test_fork_patterns (void)
107 {
108   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
109     {
110       g_print ("some stdout text: somagic17\n");
111       g_printerr ("some stderr text: semagic43\n");
112       exit (0);
113     }
114   g_test_trap_assert_passed();
115   g_test_trap_assert_stdout ("*somagic17*");
116   g_test_trap_assert_stderr ("*semagic43*");
117 }
118
119 /* fork out for a timeout test */
120 static void
121 test_fork_timeout (void)
122 {
123   /* allow child to run for only a fraction of a second */
124   if (g_test_trap_fork (0.11 * 1000000, 0))
125     {
126       /* loop and sleep forever */
127       while (TRUE)
128         g_usleep (1000 * 1000);
129     }
130   g_test_trap_assert_failed();
131   g_assert (g_test_trap_reached_timeout());
132 }
133
134 G_GNUC_END_IGNORE_DEPRECATIONS
135 #endif /* G_OS_UNIX */
136
137 static void
138 test_subprocess_fail_child (void)
139 {
140   g_assert_not_reached ();
141 }
142
143 static void
144 test_subprocess_fail (void)
145 {
146   g_test_trap_subprocess ("/subprocess/fail:child",
147                           0, G_TEST_TRAP_SILENCE_STDERR);
148   g_test_trap_assert_failed ();
149   g_test_trap_assert_stderr ("*ERROR*test_subprocess_fail_child*should not be reached*");
150 }
151
152 static void
153 test_subprocess_no_such_test_child (void)
154 {
155   g_test_trap_subprocess ("/subprocess/this-test-does-not-exist",
156                           0, G_TEST_TRAP_SILENCE_STDERR);
157   g_assert_not_reached ();
158 }
159
160 static void
161 test_subprocess_no_such_test (void)
162 {
163   g_test_trap_subprocess ("/subprocess/no-such-test:child",
164                           0, G_TEST_TRAP_SILENCE_STDERR);
165   g_test_trap_assert_failed ();
166   g_test_trap_assert_stderr ("*assertion failed*g_test_case_exists*");
167   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
168 }
169
170 static void
171 test_subprocess_patterns_child (void)
172 {
173   g_print ("some stdout text: somagic17\n");
174   g_printerr ("some stderr text: semagic43\n");
175   exit (0);
176 }
177
178 static void
179 test_subprocess_patterns (void)
180 {
181   g_test_trap_subprocess ("/subprocess/patterns:child",
182                           0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR);
183   g_test_trap_assert_passed ();
184   g_test_trap_assert_stdout ("*somagic17*");
185   g_test_trap_assert_stderr ("*semagic43*");
186 }
187
188 static void
189 test_subprocess_timeout_child (void)
190 {
191   /* loop and sleep forever */
192   while (TRUE)
193     g_usleep (1000 * 1000);
194 }
195
196 static void
197 test_subprocess_timeout (void)
198 {
199   /* allow child to run for only a fraction of a second */
200   g_test_trap_subprocess ("/subprocess/timeout:child",
201                           0.11 * 1000000, 0);
202   g_test_trap_assert_failed ();
203   g_assert (g_test_trap_reached_timeout ());
204 }
205
206 /* run a test with fixture setup and teardown */
207 typedef struct {
208   guint  seed;
209   guint  prime;
210   gchar *msg;
211 } Fixturetest;
212 static void
213 fixturetest_setup (Fixturetest  *fix,
214                    gconstpointer test_data)
215 {
216   g_assert (test_data == (void*) 0xc0cac01a);
217   fix->seed = 18;
218   fix->prime = 19;
219   fix->msg = g_strdup_printf ("%d", fix->prime);
220 }
221 static void
222 fixturetest_test (Fixturetest  *fix,
223                   gconstpointer test_data)
224 {
225   guint prime = g_spaced_primes_closest (fix->seed);
226   g_assert_cmpint (prime, ==, fix->prime);
227   prime = g_ascii_strtoull (fix->msg, NULL, 0);
228   g_assert_cmpint (prime, ==, fix->prime);
229   g_assert (test_data == (void*) 0xc0cac01a);
230 }
231 static void
232 fixturetest_teardown (Fixturetest  *fix,
233                       gconstpointer test_data)
234 {
235   g_assert (test_data == (void*) 0xc0cac01a);
236   g_free (fix->msg);
237 }
238
239 static struct {
240   int bit, vint1, vint2, irange;
241   long double vdouble, drange;
242 } shared_rand_state;
243
244 static void
245 test_rand1 (void)
246 {
247   shared_rand_state.bit = g_test_rand_bit();
248   shared_rand_state.vint1 = g_test_rand_int();
249   shared_rand_state.vint2 = g_test_rand_int();
250   g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
251   shared_rand_state.irange = g_test_rand_int_range (17, 35);
252   g_assert_cmpint (shared_rand_state.irange, >=, 17);
253   g_assert_cmpint (shared_rand_state.irange, <=, 35);
254   shared_rand_state.vdouble = g_test_rand_double();
255   shared_rand_state.drange = g_test_rand_double_range (-999, +17);
256   g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
257   g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
258 }
259
260 static void
261 test_rand2 (void)
262 {
263   /* this test only works if run after test1.
264    * we do this to check that random number generators
265    * are reseeded upon fixture setup.
266    */
267   g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
268   g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
269   g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
270   g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
271   g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
272   g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
273 }
274
275 static void
276 test_data_test (gconstpointer test_data)
277 {
278   g_assert (test_data == (void*) 0xc0c0baba);
279 }
280
281 static void
282 test_random_conversions (void)
283 {
284   /* very simple conversion test using random numbers */
285   int vint = g_test_rand_int();
286   char *err, *str = g_strdup_printf ("%d", vint);
287   gint64 vint64 = g_ascii_strtoll (str, &err, 10);
288   g_assert_cmphex (vint, ==, vint64);
289   g_assert (!err || *err == 0);
290   g_free (str);
291 }
292
293 static gboolean
294 fatal_handler (const gchar    *log_domain,
295                GLogLevelFlags  log_level,
296                const gchar    *message,
297                gpointer        user_data)
298 {
299   return FALSE;
300 }
301
302 static void
303 test_fatal_log_handler_critical_pass (void)
304 {
305   g_test_log_set_fatal_handler (fatal_handler, NULL);
306   g_str_has_prefix (NULL, "file://");
307   g_critical ("Test passing");
308   exit (0);
309 }
310
311 static void
312 test_fatal_log_handler_error_fail (void)
313 {
314   g_error ("Test failing");
315   exit (0);
316 }
317
318 static void
319 test_fatal_log_handler_critical_fail (void)
320 {
321   g_str_has_prefix (NULL, "file://");
322   g_critical ("Test passing");
323   exit (0);
324 }
325
326 static void
327 test_fatal_log_handler (void)
328 {
329   g_test_trap_subprocess ("/misc/fatal-log-handler:critical-pass",
330                           0, G_TEST_TRAP_SILENCE_STDERR);
331   g_test_trap_assert_passed ();
332   g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
333   g_test_trap_assert_stderr ("*CRITICAL*Test passing*");
334
335   g_test_trap_subprocess ("/misc/fatal-log-handler:error-fail",
336                           0, G_TEST_TRAP_SILENCE_STDERR);
337   g_test_trap_assert_failed ();
338   g_test_trap_assert_stderr ("*ERROR*Test failing*");
339
340   g_test_trap_subprocess ("/misc/fatal-log-handler:critical-fail",
341                           0, G_TEST_TRAP_SILENCE_STDERR);
342   g_test_trap_assert_failed ();
343   g_test_trap_assert_stderr ("*CRITICAL*g_str_has_prefix*");
344   g_test_trap_assert_stderr_unmatched ("*CRITICAL*Test passing*");
345 }
346
347 static void
348 test_expected_messages_warning (void)
349 {
350   g_warning ("This is a %d warning", g_random_int ());
351   g_return_if_reached ();
352 }
353
354 static void
355 test_expected_messages_expect_warning (void)
356 {
357   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
358                          "This is a * warning");
359   test_expected_messages_warning ();
360 }
361
362 static void
363 test_expected_messages_wrong_warning (void)
364 {
365   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
366                          "*should not be *");
367   test_expected_messages_warning ();
368 }
369
370 static void
371 test_expected_messages_expected (void)
372 {
373   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
374                          "This is a * warning");
375   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
376                          "*should not be reached");
377
378   test_expected_messages_warning ();
379
380   g_test_assert_expected_messages ();
381   exit (0);
382 }
383
384 static void
385 test_expected_messages_extra_warning (void)
386 {
387   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
388                          "This is a * warning");
389   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
390                          "*should not be reached");
391   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
392                          "nope");
393
394   test_expected_messages_warning ();
395
396   /* If we don't assert, it won't notice the missing message */
397   exit (0);
398 }
399
400 static void
401 test_expected_messages_unexpected_extra_warning (void)
402 {
403   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
404                          "This is a * warning");
405   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
406                          "*should not be reached");
407   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
408                          "nope");
409
410   test_expected_messages_warning ();
411
412   g_test_assert_expected_messages ();
413   exit (0);
414 }
415
416 static void
417 test_expected_messages (void)
418 {
419   g_test_trap_subprocess ("/misc/expected-messages:warning",
420                           0, G_TEST_TRAP_SILENCE_STDERR);
421   g_test_trap_assert_failed ();
422   g_test_trap_assert_stderr ("*This is a * warning*");
423   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
424
425   g_test_trap_subprocess ("/misc/expected-messages:expect-warning",
426                           0, G_TEST_TRAP_SILENCE_STDERR);
427   g_test_trap_assert_failed ();
428   g_test_trap_assert_stderr_unmatched ("*This is a * warning*");
429   g_test_trap_assert_stderr ("*should not be reached*");
430
431   g_test_trap_subprocess ("/misc/expected-messages:wrong-warning",
432                           0, G_TEST_TRAP_SILENCE_STDERR);
433   g_test_trap_assert_failed ();
434   g_test_trap_assert_stderr_unmatched ("*should not be reached*");
435   g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*should not be *WARNING*This is a * warning*");
436
437   g_test_trap_subprocess ("/misc/expected-messages:expected",
438                           0, G_TEST_TRAP_SILENCE_STDERR);
439   g_test_trap_assert_passed ();
440   g_test_trap_assert_stderr ("");
441
442   g_test_trap_subprocess ("/misc/expected-messages:extra-warning",
443                           0, G_TEST_TRAP_SILENCE_STDERR);
444   g_test_trap_assert_passed ();
445   g_test_trap_assert_stderr ("");
446
447   g_test_trap_subprocess ("/misc/expected-messages:unexpected-extra-warning",
448                           0, G_TEST_TRAP_SILENCE_STDERR);
449   g_test_trap_assert_failed ();
450   g_test_trap_assert_stderr ("*Did not see expected message CRITICAL*nope*");
451 }
452
453 int
454 main (int   argc,
455       char *argv[])
456 {
457   g_test_init (&argc, &argv, NULL);
458
459   g_test_add_func ("/random-generator/rand-1", test_rand1);
460   g_test_add_func ("/random-generator/rand-2", test_rand2);
461   g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
462   g_test_add_func ("/misc/assertions", test_assertions);
463   g_test_add_func ("/misc/assertions:bad_cmpstr", test_assertions_bad_cmpstr);
464   g_test_add_func ("/misc/assertions:bad_cmpint", test_assertions_bad_cmpint);
465   g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
466   g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
467   if (g_test_perf())
468     g_test_add_func ("/misc/timer", test_timer);
469
470 #ifdef G_OS_UNIX
471   g_test_add_func ("/forking/fail assertion", test_fork_fail);
472   g_test_add_func ("/forking/patterns", test_fork_patterns);
473   if (g_test_slow())
474     g_test_add_func ("/forking/timeout", test_fork_timeout);
475 #endif
476
477   g_test_add_func ("/subprocess/fail", test_subprocess_fail);
478   g_test_add_func ("/subprocess/fail:child", test_subprocess_fail_child);
479   g_test_add_func ("/subprocess/no-such-test", test_subprocess_no_such_test);
480   g_test_add_func ("/subprocess/no-such-test:child", test_subprocess_no_such_test_child);
481   if (g_test_slow ())
482     {
483       g_test_add_func ("/subprocess/timeout", test_subprocess_timeout);
484       g_test_add_func ("/subprocess/timeout:child", test_subprocess_timeout_child);
485     }
486   g_test_add_func ("/subprocess/patterns", test_subprocess_patterns);
487   g_test_add_func ("/subprocess/patterns:child", test_subprocess_patterns_child);
488
489   g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
490   g_test_add_func ("/misc/fatal-log-handler:critical-pass", test_fatal_log_handler_critical_pass);
491   g_test_add_func ("/misc/fatal-log-handler:error-fail", test_fatal_log_handler_error_fail);
492   g_test_add_func ("/misc/fatal-log-handler:critical-fail", test_fatal_log_handler_critical_fail);
493
494   g_test_add_func ("/misc/expected-messages", test_expected_messages);
495   g_test_add_func ("/misc/expected-messages:warning", test_expected_messages_warning);
496   g_test_add_func ("/misc/expected-messages:expect-warning", test_expected_messages_expect_warning);
497   g_test_add_func ("/misc/expected-messages:wrong-warning", test_expected_messages_wrong_warning);
498   g_test_add_func ("/misc/expected-messages:expected", test_expected_messages_expected);
499   g_test_add_func ("/misc/expected-messages:extra-warning", test_expected_messages_extra_warning);
500   g_test_add_func ("/misc/expected-messages:unexpected-extra-warning", test_expected_messages_unexpected_extra_warning);
501
502   return g_test_run();
503 }