1 /* GLib testing framework examples and tests
2 * Copyright (C) 2007 Tim Janik
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.
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.
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.
21 #include <glib/gtestframework.h>
23 /* test assertion variants */
25 test_assertions (void)
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);
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");
45 /* run a test with fixture setup and teardown */
53 fixturetest_setup (Fixturetest *fix)
57 fix->msg = g_strdup_printf ("%d", fix->prime);
61 fixturetest_test (Fixturetest *fix)
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);
70 fixturetest_teardown (Fixturetest *fix)
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);
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);
95 return g_test_run_suite (rootsuite);