that match a given test path. (g_test_run_suite): run suite only if it
[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
23 /* test assertion variants */
24 static void
25 test_assertions (void)
26 {
27   g_assert_cmpint (1, >, 0);
28   g_assert_cmpint (2, ==, 2);
29   g_assert_cmpfloat (3.3, !=, 7);
30   g_assert_cmpfloat (7, <=, 3 + 4);
31   g_assert (TRUE);
32   g_assert_cmpstr ("foo", !=, "faa");
33   gchar *fuu = g_strdup_printf ("f%s", "uu");
34   g_test_queue_free (fuu);
35   g_assert_cmpstr ("foo", !=, fuu);
36   g_assert_cmpstr ("fuu", ==, fuu);
37   g_assert_cmpstr (NULL, <, "");
38   g_assert_cmpstr (NULL, ==, NULL);
39   g_assert_cmpstr ("", >, NULL);
40   g_assert_cmpstr ("foo", <, "fzz");
41   g_assert_cmpstr ("fzz", >, "faa");
42   g_assert_cmpstr ("fzz", ==, "fzz");
43 }
44
45 /* run a test with fixture setup and teardown */
46 typedef struct {
47   guint  seed;
48   guint  prime;
49   gchar *msg;
50 } Fixturetest;
51
52 static void
53 fixturetest_setup (Fixturetest *fix)
54 {
55   fix->seed = 18;
56   fix->prime = 19;
57   fix->msg = g_strdup_printf ("%d", fix->prime);
58 }
59
60 static void
61 fixturetest_test (Fixturetest *fix)
62 {
63   guint prime = g_spaced_primes_closest (fix->seed);
64   g_assert_cmpint (prime, ==, fix->prime);
65   prime = g_ascii_strtoull (fix->msg, NULL, 0);
66   g_assert_cmpint (prime, ==, fix->prime);
67 }
68
69 static void
70 fixturetest_teardown (Fixturetest *fix)
71 {
72   g_free (fix->msg);
73 }
74
75 int
76 main (int   argc,
77       char *argv[])
78 {
79   GTestCase *tc;
80   g_test_init (&argc, &argv, NULL);
81   GTestSuite *rootsuite = g_test_create_suite ("top");
82   GTestSuite *miscsuite = g_test_create_suite ("misc");
83   g_test_suite_add_suite (rootsuite, miscsuite);
84   GTestSuite *forksuite = g_test_create_suite ("fork");
85   g_test_suite_add_suite (rootsuite, forksuite);
86
87   tc = g_test_create_case ("assertions", 0, NULL, test_assertions, NULL);
88   g_test_suite_add (miscsuite, tc);
89   tc = g_test_create_case ("primetoul", sizeof (Fixturetest),
90                            (void(*)(void)) fixturetest_setup,
91                            (void(*)(void)) fixturetest_test,
92                            (void(*)(void)) fixturetest_teardown);
93   g_test_suite_add (miscsuite, tc);
94
95   return g_test_run_suite (rootsuite);
96 }