testing.c: added tests for g_assert_cmphex() and forked test traps.
[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 /* fork out for a failing test */
47 static void
48 test_fork_fail (void)
49 {
50   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
51     {
52       g_assert_not_reached();
53     }
54   g_test_trap_assert_failed();
55   g_test_trap_assert_stderr ("*ERROR*test_fork_fail*should not be reached*");
56 }
57
58 /* fork out to assert stdout and stderr patterns */
59 static void
60 test_fork_patterns (void)
61 {
62   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
63     {
64       g_print ("some stdout text: somagic17\n");
65       g_printerr ("some stderr text: semagic43\n");
66       exit (0);
67     }
68   g_test_trap_assert_passed();
69   g_test_trap_assert_stdout ("*somagic17*");
70   g_test_trap_assert_stderr ("*semagic43*");
71 }
72
73 /* fork out for a timeout test */
74 static void
75 test_fork_timeout (void)
76 {
77   /* allow child to run for only a fraction of a second */
78   if (g_test_trap_fork (0.11 * 1000000, 0))
79     {
80       /* loop and sleep forever */
81       while (TRUE)
82         g_usleep (1000 * 1000);
83     }
84   g_test_trap_assert_failed();
85   g_assert (g_test_trap_reached_timeout());
86 }
87
88 /* run a test with fixture setup and teardown */
89 typedef struct {
90   guint  seed;
91   guint  prime;
92   gchar *msg;
93 } Fixturetest;
94 static void
95 fixturetest_setup (Fixturetest *fix)
96 {
97   fix->seed = 18;
98   fix->prime = 19;
99   fix->msg = g_strdup_printf ("%d", fix->prime);
100 }
101 static void
102 fixturetest_test (Fixturetest *fix)
103 {
104   guint prime = g_spaced_primes_closest (fix->seed);
105   g_assert_cmpint (prime, ==, fix->prime);
106   prime = g_ascii_strtoull (fix->msg, NULL, 0);
107   g_assert_cmpint (prime, ==, fix->prime);
108 }
109 static void
110 fixturetest_teardown (Fixturetest *fix)
111 {
112   g_free (fix->msg);
113 }
114
115 int
116 main (int   argc,
117       char *argv[])
118 {
119   GTestCase *tc;
120   g_test_init (&argc, &argv, NULL);
121   GTestSuite *rootsuite = g_test_create_suite ("top");
122   GTestSuite *miscsuite = g_test_create_suite ("misc");
123   g_test_suite_add_suite (rootsuite, miscsuite);
124   GTestSuite *forksuite = g_test_create_suite ("fork");
125   g_test_suite_add_suite (rootsuite, forksuite);
126
127   tc = g_test_create_case ("fail assertion", 0, NULL, test_fork_fail, NULL);
128   g_test_suite_add (forksuite, tc);
129   tc = g_test_create_case ("patterns", 0, NULL, test_fork_patterns, NULL);
130   g_test_suite_add (forksuite, tc);
131   tc = g_test_create_case ("timeout", 0, NULL, test_fork_timeout, NULL);
132   g_test_suite_add (forksuite, tc);
133
134   tc = g_test_create_case ("assertions", 0, NULL, test_assertions, NULL);
135   g_test_suite_add (miscsuite, tc);
136   tc = g_test_create_case ("primetoul", sizeof (Fixturetest),
137                            (void(*)(void)) fixturetest_setup,
138                            (void(*)(void)) fixturetest_test,
139                            (void(*)(void)) fixturetest_teardown);
140   g_test_suite_add (miscsuite, tc);
141
142   return g_test_run_suite (rootsuite);
143 }