initialize automake variables EXTRA_DIST and TEST_PROGS for unconditional
[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 #include <glib/gtestutils.h>
23 #include <stdlib.h>
24
25 /* test assertion variants */
26 static void
27 test_assertions (void)
28 {
29   g_assert_cmpint (1, >, 0);
30   g_assert_cmphex (2, ==, 2);
31   g_assert_cmpfloat (3.3, !=, 7);
32   g_assert_cmpfloat (7, <=, 3 + 4);
33   g_assert (TRUE);
34   g_assert_cmpstr ("foo", !=, "faa");
35   gchar *fuu = g_strdup_printf ("f%s", "uu");
36   g_test_queue_free (fuu);
37   g_assert_cmpstr ("foo", !=, fuu);
38   g_assert_cmpstr ("fuu", ==, fuu);
39   g_assert_cmpstr (NULL, <, "");
40   g_assert_cmpstr (NULL, ==, NULL);
41   g_assert_cmpstr ("", >, NULL);
42   g_assert_cmpstr ("foo", <, "fzz");
43   g_assert_cmpstr ("fzz", >, "faa");
44   g_assert_cmpstr ("fzz", ==, "fzz");
45 }
46
47 /* test g_test_timer* API */
48 static void
49 test_timer (void)
50 {
51   double ttime;
52   g_test_timer_start();
53   g_assert_cmpfloat (g_test_timer_last(), ==, 0);
54   g_usleep (25 * 1000);
55   ttime = g_test_timer_elapsed();
56   g_assert_cmpfloat (ttime, >, 0);
57   g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
58   g_test_minimized_result (ttime, "timer-test-time: %fsec", ttime);
59   g_test_maximized_result (5, "bogus-quantity: %ddummies", 5); // simple API test
60 }
61
62 /* fork out for a failing test */
63 static void
64 test_fork_fail (void)
65 {
66   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
67     {
68       g_assert_not_reached();
69     }
70   g_test_trap_assert_failed();
71   g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
72 }
73
74 /* fork out to assert stdout and stderr patterns */
75 static void
76 test_fork_patterns (void)
77 {
78   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
79     {
80       g_print ("some stdout text: somagic17\n");
81       g_printerr ("some stderr text: semagic43\n");
82       exit (0);
83     }
84   g_test_trap_assert_passed();
85   g_test_trap_assert_stdout ("*somagic17*");
86   g_test_trap_assert_stderr ("*semagic43*");
87 }
88
89 /* fork out for a timeout test */
90 static void
91 test_fork_timeout (void)
92 {
93   /* allow child to run for only a fraction of a second */
94   if (g_test_trap_fork (0.11 * 1000000, 0))
95     {
96       /* loop and sleep forever */
97       while (TRUE)
98         g_usleep (1000 * 1000);
99     }
100   g_test_trap_assert_failed();
101   g_assert (g_test_trap_reached_timeout());
102 }
103
104 /* run a test with fixture setup and teardown */
105 typedef struct {
106   guint  seed;
107   guint  prime;
108   gchar *msg;
109 } Fixturetest;
110 static void
111 fixturetest_setup (Fixturetest *fix)
112 {
113   fix->seed = 18;
114   fix->prime = 19;
115   fix->msg = g_strdup_printf ("%d", fix->prime);
116 }
117 static void
118 fixturetest_test (Fixturetest *fix)
119 {
120   guint prime = g_spaced_primes_closest (fix->seed);
121   g_assert_cmpint (prime, ==, fix->prime);
122   prime = g_ascii_strtoull (fix->msg, NULL, 0);
123   g_assert_cmpint (prime, ==, fix->prime);
124 }
125 static void
126 fixturetest_teardown (Fixturetest *fix)
127 {
128   g_free (fix->msg);
129 }
130
131 static struct {
132   int bit, vint1, vint2, irange;
133   long double vdouble, drange;
134 } shared_rand_state;
135
136 static void
137 test_rand1 (void)
138 {
139   shared_rand_state.bit = g_test_rand_bit();
140   shared_rand_state.vint1 = g_test_rand_int();
141   shared_rand_state.vint2 = g_test_rand_int();
142   g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
143   shared_rand_state.irange = g_test_rand_int_range (17, 35);
144   g_assert_cmpint (shared_rand_state.irange, >=, 17);
145   g_assert_cmpint (shared_rand_state.irange, <=, 35);
146   shared_rand_state.vdouble = g_test_rand_double();
147   shared_rand_state.drange = g_test_rand_double_range (-999, +17);
148   g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
149   g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
150 }
151
152 static void
153 test_rand2 (void)
154 {
155   /* this test only works if run after test1.
156    * we do this to check that random number generators
157    * are reseeded upon fixture setup.
158    */
159   g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
160   g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
161   g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
162   g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
163   g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
164   g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
165 }
166
167 int
168 main (int   argc,
169       char *argv[])
170 {
171   g_test_init (&argc, &argv, NULL);
172
173   g_test_add_func ("/random-generator/rand-1", test_rand1);
174   g_test_add_func ("/random-generator/rand-2", test_rand2);
175   g_test_add_func ("/misc/assertions", test_assertions);
176   g_test_add ("/misc/primetoul", Fixturetest, fixturetest_setup, fixturetest_test, fixturetest_teardown);
177   if (g_test_perf())
178     g_test_add_func ("/misc/timer", test_timer);
179   g_test_add_func ("/forking/fail assertion", test_fork_fail);
180   g_test_add_func ("/forking/patterns", test_fork_patterns);
181   if (g_test_slow())
182     g_test_add_func ("/forking/timeout", test_fork_timeout);
183
184   return g_test_run();
185 }