Merge remote-tracking branch 'gvdb/master'
[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_undefined ())
50     {
51       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
52         {
53           g_assert_cmpstr ("fzz", !=, "fzz");
54         }
55       g_test_trap_assert_failed ();
56       g_test_trap_assert_stderr ("*assertion failed*");
57
58       if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
59         {
60           g_assert_cmpint (4, !=, 4);
61         }
62       g_test_trap_assert_failed ();
63       g_test_trap_assert_stderr ("*assertion failed*");
64     }
65 }
66
67 /* test g_test_timer* API */
68 static void
69 test_timer (void)
70 {
71   double ttime;
72   g_test_timer_start();
73   g_assert_cmpfloat (g_test_timer_last(), ==, 0);
74   g_usleep (25 * 1000);
75   ttime = g_test_timer_elapsed();
76   g_assert_cmpfloat (ttime, >, 0);
77   g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
78   g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
79   g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); /* simple API test */
80 }
81
82 /* fork out for a failing test */
83 static void
84 test_fork_fail (void)
85 {
86   if (!g_test_undefined ())
87     return;
88
89   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
90     {
91       g_assert_not_reached();
92     }
93   g_test_trap_assert_failed();
94   g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
95 }
96
97 /* fork out to assert stdout and stderr patterns */
98 static void
99 test_fork_patterns (void)
100 {
101   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
102     {
103       g_print ("some stdout text: somagic17\n");
104       g_printerr ("some stderr text: semagic43\n");
105       exit (0);
106     }
107   g_test_trap_assert_passed();
108   g_test_trap_assert_stdout ("*somagic17*");
109   g_test_trap_assert_stderr ("*semagic43*");
110 }
111
112 /* fork out for a timeout test */
113 static void
114 test_fork_timeout (void)
115 {
116   if (!g_test_undefined ())
117     return;
118
119   /* allow child to run for only a fraction of a second */
120   if (g_test_trap_fork (0.11 * 1000000, 0))
121     {
122       /* loop and sleep forever */
123       while (TRUE)
124         g_usleep (1000 * 1000);
125     }
126   g_test_trap_assert_failed();
127   g_assert (g_test_trap_reached_timeout());
128 }
129
130 /* run a test with fixture setup and teardown */
131 typedef struct {
132   guint  seed;
133   guint  prime;
134   gchar *msg;
135 } Fixturetest;
136 static void
137 fixturetest_setup (Fixturetest  *fix,
138                    gconstpointer test_data)
139 {
140   g_assert (test_data == (void*) 0xc0cac01a);
141   fix->seed = 18;
142   fix->prime = 19;
143   fix->msg = g_strdup_printf ("%d", fix->prime);
144 }
145 static void
146 fixturetest_test (Fixturetest  *fix,
147                   gconstpointer test_data)
148 {
149   guint prime = g_spaced_primes_closest (fix->seed);
150   g_assert_cmpint (prime, ==, fix->prime);
151   prime = g_ascii_strtoull (fix->msg, NULL, 0);
152   g_assert_cmpint (prime, ==, fix->prime);
153   g_assert (test_data == (void*) 0xc0cac01a);
154 }
155 static void
156 fixturetest_teardown (Fixturetest  *fix,
157                       gconstpointer test_data)
158 {
159   g_assert (test_data == (void*) 0xc0cac01a);
160   g_free (fix->msg);
161 }
162
163 static struct {
164   int bit, vint1, vint2, irange;
165   long double vdouble, drange;
166 } shared_rand_state;
167
168 static void
169 test_rand1 (void)
170 {
171   shared_rand_state.bit = g_test_rand_bit();
172   shared_rand_state.vint1 = g_test_rand_int();
173   shared_rand_state.vint2 = g_test_rand_int();
174   g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
175   shared_rand_state.irange = g_test_rand_int_range (17, 35);
176   g_assert_cmpint (shared_rand_state.irange, >=, 17);
177   g_assert_cmpint (shared_rand_state.irange, <=, 35);
178   shared_rand_state.vdouble = g_test_rand_double();
179   shared_rand_state.drange = g_test_rand_double_range (-999, +17);
180   g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
181   g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
182 }
183
184 static void
185 test_rand2 (void)
186 {
187   /* this test only works if run after test1.
188    * we do this to check that random number generators
189    * are reseeded upon fixture setup.
190    */
191   g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
192   g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
193   g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
194   g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
195   g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
196   g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
197 }
198
199 static void
200 test_data_test (gconstpointer test_data)
201 {
202   g_assert (test_data == (void*) 0xc0c0baba);
203 }
204
205 static void
206 test_random_conversions (void)
207 {
208   /* very simple conversion test using random numbers */
209   int vint = g_test_rand_int();
210   char *err, *str = g_strdup_printf ("%d", vint);
211   gint64 vint64 = g_ascii_strtoll (str, &err, 10);
212   g_assert_cmphex (vint, ==, vint64);
213   g_assert (!err || *err == 0);
214   g_free (str);
215 }
216
217 static gboolean
218 fatal_handler (const gchar    *log_domain,
219                GLogLevelFlags  log_level,
220                const gchar    *message,
221                gpointer        user_data)
222 {
223   return FALSE;
224 }
225
226 static void
227 test_fatal_log_handler (void)
228 {
229   if (!g_test_undefined ())
230     return;
231
232   g_test_log_set_fatal_handler (fatal_handler, NULL);
233   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
234     {
235       g_str_has_prefix (NULL, "file://");
236       g_critical ("Test passing");
237       exit (0);
238     }
239   g_test_trap_assert_passed ();
240
241   g_test_log_set_fatal_handler (NULL, NULL);
242   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
243     g_error ("Test failing");
244   g_test_trap_assert_failed ();
245
246   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
247     g_str_has_prefix (NULL, "file://");
248   g_test_trap_assert_failed ();
249 }
250
251 int
252 main (int   argc,
253       char *argv[])
254 {
255   g_test_init (&argc, &argv, NULL);
256
257   g_test_add_func ("/random-generator/rand-1", test_rand1);
258   g_test_add_func ("/random-generator/rand-2", test_rand2);
259   g_test_add_func ("/random-generator/random-conversions", test_random_conversions);
260   g_test_add_func ("/misc/assertions", test_assertions);
261   g_test_add_data_func ("/misc/test-data", (void*) 0xc0c0baba, test_data_test);
262   g_test_add ("/misc/primetoul", Fixturetest, (void*) 0xc0cac01a, fixturetest_setup, fixturetest_test, fixturetest_teardown);
263   if (g_test_perf())
264     g_test_add_func ("/misc/timer", test_timer);
265   g_test_add_func ("/forking/fail assertion", test_fork_fail);
266   g_test_add_func ("/forking/patterns", test_fork_patterns);
267   if (g_test_slow())
268     g_test_add_func ("/forking/timeout", test_fork_timeout);
269   g_test_add_func ("/misc/fatal-log-handler", test_fatal_log_handler);
270
271   return g_test_run();
272 }