1 /* Unit tests for g_malloc
2 * Copyright (C) 2013 Red Hat, Inc.
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 * Author: Matthias Clasen
30 if (g_test_subprocess ())
33 g_mem_set_vtable (glib_mem_profiler_table);
35 p = g_realloc (p, 200);
39 p = g_try_malloc (2000);
40 p = g_try_realloc (p, 3000);
44 p = g_realloc (NULL, 0);
46 p = g_try_realloc (NULL, 0);
50 g_test_trap_subprocess (NULL, 0, 0);
51 g_test_trap_assert_passed ();
52 g_test_trap_assert_stdout ("*GLib Memory statistics*");
56 test_fallback_calloc (void)
58 if (g_test_subprocess ())
60 GMemVTable vtable = { malloc, realloc, free, NULL, NULL, NULL };
63 g_mem_set_vtable (&vtable);
68 g_test_trap_subprocess (NULL, 0, 0);
69 g_test_trap_assert_passed ();
73 test_incomplete_vtable (void)
75 if (g_test_subprocess ())
77 GMemVTable vtable = { malloc, realloc, NULL, NULL, NULL, NULL };
80 g_mem_set_vtable (&vtable);
85 g_test_trap_subprocess (NULL, 0, 0);
86 g_test_trap_assert_failed ();
87 g_test_trap_assert_stderr ("*lacks one of*");
91 test_double_vtable (void)
93 if (g_test_subprocess ())
95 GMemVTable vtable = { malloc, realloc, free, NULL, NULL, NULL };
97 g_mem_set_vtable (&vtable);
98 g_mem_set_vtable (&vtable);
101 g_test_trap_subprocess (NULL, 0, 0);
102 g_test_trap_assert_failed ();
103 g_test_trap_assert_stderr ("*can only be set once*");
107 main (int argc, char *argv[])
109 g_test_init (&argc, &argv, NULL);
111 g_test_add_func ("/malloc/incomplete-vtable", test_incomplete_vtable);
112 g_test_add_func ("/malloc/double-vtable", test_double_vtable);
113 g_test_add_func ("/malloc/fallback-calloc", test_fallback_calloc);
114 g_test_add_func ("/malloc/profiler", test_profiler);
116 return g_test_run ();