Implemented g_test_timer*().
[platform/upstream/glib.git] / glib / tests / testing.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2007 Tim Janik
3  *
4  * This work is provided "as is"; redistribution and modification
5  * in whole or in part, in any medium, physical or electronic is
6  * permitted without restriction.
7  *
8  * This work is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * In no event shall the authors or contributors be liable for any
13  * direct, indirect, incidental, special, exemplary, or consequential
14  * damages (including, but not limited to, procurement of substitute
15  * goods or services; loss of use, data, or profits; or business
16  * interruption) however caused and on any theory of liability, whether
17  * in contract, strict liability, or tort (including negligence or
18  * otherwise) arising in any way out of the use of this software, even
19  * if advised of the possibility of such damage.
20  */
21 #include <glib/gtestframework.h>
22 #include <stdlib.h>
23
24 /* test assertion variants */
25 static void
26 test_assertions (void)
27 {
28   g_assert_cmpint (1, >, 0);
29   g_assert_cmphex (2, ==, 2);
30   g_assert_cmpfloat (3.3, !=, 7);
31   g_assert_cmpfloat (7, <=, 3 + 4);
32   g_assert (TRUE);
33   g_assert_cmpstr ("foo", !=, "faa");
34   gchar *fuu = g_strdup_printf ("f%s", "uu");
35   g_test_queue_free (fuu);
36   g_assert_cmpstr ("foo", !=, fuu);
37   g_assert_cmpstr ("fuu", ==, fuu);
38   g_assert_cmpstr (NULL, <, "");
39   g_assert_cmpstr (NULL, ==, NULL);
40   g_assert_cmpstr ("", >, NULL);
41   g_assert_cmpstr ("foo", <, "fzz");
42   g_assert_cmpstr ("fzz", >, "faa");
43   g_assert_cmpstr ("fzz", ==, "fzz");
44 }
45
46 /* test g_test_timer* API */
47 static void
48 test_timer (void)
49 {
50   double ttime;
51   g_test_timer_start();
52   g_assert_cmpfloat (g_test_timer_last(), ==, 0);
53   g_usleep (25 * 1000);
54   ttime = g_test_timer_elapsed();
55   g_assert_cmpfloat (ttime, >, 0);
56   g_assert_cmpfloat (g_test_timer_last(), ==, ttime);
57 }
58
59 /* fork out for a failing test */
60 static void
61 test_fork_fail (void)
62 {
63   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
64     {
65       g_assert_not_reached();
66     }
67   g_test_trap_assert_failed();
68   g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
69 }
70
71 /* fork out to assert stdout and stderr patterns */
72 static void
73 test_fork_patterns (void)
74 {
75   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
76     {
77       g_print ("some stdout text: somagic17\n");
78       g_printerr ("some stderr text: semagic43\n");
79       exit (0);
80     }
81   g_test_trap_assert_passed();
82   g_test_trap_assert_stdout ("*somagic17*");
83   g_test_trap_assert_stderr ("*semagic43*");
84 }
85
86 /* fork out for a timeout test */
87 static void
88 test_fork_timeout (void)
89 {
90   /* allow child to run for only a fraction of a second */
91   if (g_test_trap_fork (0.11 * 1000000, 0))
92     {
93       /* loop and sleep forever */
94       while (TRUE)
95         g_usleep (1000 * 1000);
96     }
97   g_test_trap_assert_failed();
98   g_assert (g_test_trap_reached_timeout());
99 }
100
101 /* run a test with fixture setup and teardown */
102 typedef struct {
103   guint  seed;
104   guint  prime;
105   gchar *msg;
106 } Fixturetest;
107 static void
108 fixturetest_setup (Fixturetest *fix)
109 {
110   fix->seed = 18;
111   fix->prime = 19;
112   fix->msg = g_strdup_printf ("%d", fix->prime);
113 }
114 static void
115 fixturetest_test (Fixturetest *fix)
116 {
117   guint prime = g_spaced_primes_closest (fix->seed);
118   g_assert_cmpint (prime, ==, fix->prime);
119   prime = g_ascii_strtoull (fix->msg, NULL, 0);
120   g_assert_cmpint (prime, ==, fix->prime);
121 }
122 static void
123 fixturetest_teardown (Fixturetest *fix)
124 {
125   g_free (fix->msg);
126 }
127
128 static struct {
129   int bit, vint1, vint2, irange;
130   long double vdouble, drange;
131 } shared_rand_state;
132
133 static void
134 test_rand1 (void)
135 {
136   shared_rand_state.bit = g_test_rand_bit();
137   shared_rand_state.vint1 = g_test_rand_int();
138   shared_rand_state.vint2 = g_test_rand_int();
139   g_assert_cmpint (shared_rand_state.vint1, !=, shared_rand_state.vint2);
140   shared_rand_state.irange = g_test_rand_int_range (17, 35);
141   g_assert_cmpint (shared_rand_state.irange, >=, 17);
142   g_assert_cmpint (shared_rand_state.irange, <=, 35);
143   shared_rand_state.vdouble = g_test_rand_double();
144   shared_rand_state.drange = g_test_rand_double_range (-999, +17);
145   g_assert_cmpfloat (shared_rand_state.drange, >=, -999);
146   g_assert_cmpfloat (shared_rand_state.drange, <=, +17);
147 }
148
149 static void
150 test_rand2 (void)
151 {
152   /* this test only works if run after test1.
153    * we do this to check that random number generators
154    * are reseeded upon fixture setup.
155    */
156   g_assert_cmpint (shared_rand_state.bit, ==, g_test_rand_bit());
157   g_assert_cmpint (shared_rand_state.vint1, ==, g_test_rand_int());
158   g_assert_cmpint (shared_rand_state.vint2, ==, g_test_rand_int());
159   g_assert_cmpint (shared_rand_state.irange, ==, g_test_rand_int_range (17, 35));
160   g_assert_cmpfloat (shared_rand_state.vdouble, ==, g_test_rand_double());
161   g_assert_cmpfloat (shared_rand_state.drange, ==, g_test_rand_double_range (-999, +17));
162 }
163
164 int
165 main (int   argc,
166       char *argv[])
167 {
168   g_test_init (&argc, &argv, NULL);
169
170   g_test_add_func ("/random-generator/rand-1", test_rand1);
171   g_test_add_func ("/random-generator/rand-2", test_rand2);
172   g_test_add_func ("/misc/assertions", test_assertions);
173   g_test_add ("/misc/primetoul", Fixturetest, fixturetest_setup, fixturetest_test, fixturetest_teardown);
174   g_test_add_func ("/misc/timer", test_timer);
175   g_test_add_func ("/forking/fail assertion", test_fork_fail);
176   g_test_add_func ("/forking/patterns", test_fork_patterns);
177   g_test_add_func ("/forking/timeout", test_fork_timeout);
178
179   return g_test_run();
180 }