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