Deprecate and drop support for memory vtables
[platform/upstream/glib.git] / glib / tests / malloc.c
1 /* Unit tests for g_malloc
2  * Copyright (C) 2013 Red Hat, Inc.
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  * Author: Matthias Clasen
22  */
23
24 #include "glib.h"
25 #include <stdlib.h>
26
27 static void
28 test_profiler (void)
29 {
30   if (g_test_subprocess ())
31     {
32       gpointer p;
33       g_mem_set_vtable (glib_mem_profiler_table);
34       p = g_malloc (100);
35       p = g_realloc (p, 200);
36       g_free (p);
37       p = g_malloc0 (1000);
38       g_free (p);
39       p = g_try_malloc (2000);
40       p = g_try_realloc (p, 3000);
41       g_free (p);
42       p = g_malloc (0);
43       p = g_malloc0 (0);
44       p = g_realloc (NULL, 0);
45       p = g_try_malloc (0);
46       p = g_try_realloc (NULL, 0);
47       g_mem_profile ();
48       exit (0);
49     }
50   g_test_trap_subprocess (NULL, 0, 0);
51   g_test_trap_assert_passed ();
52   g_test_trap_assert_stdout ("*GLib Memory statistics*");
53 }
54
55 static void
56 test_fallback_calloc (void)
57 {
58   if (g_test_subprocess ())
59     {
60       GMemVTable vtable = { malloc, realloc, free, NULL, NULL, NULL };
61       gpointer p;
62
63       g_mem_set_vtable (&vtable);
64       p = g_malloc0 (1000);
65       g_free (p);
66       exit (0);
67     }
68   g_test_trap_subprocess (NULL, 0, 0);
69   g_test_trap_assert_passed ();
70 }
71
72 static void
73 test_incomplete_vtable (void)
74 {
75   if (g_test_subprocess ())
76     {
77       GMemVTable vtable = { malloc, realloc, NULL, NULL, NULL, NULL };
78       gpointer p;
79
80       g_mem_set_vtable (&vtable);
81       p = g_malloc0 (1000);
82       g_free (p);
83       exit (0);
84     }
85   g_test_trap_subprocess (NULL, 0, 0);
86   g_test_trap_assert_failed ();
87   g_test_trap_assert_stderr ("*lacks one of*");
88 }
89
90 static void
91 test_double_vtable (void)
92 {
93   if (g_test_subprocess ())
94     {
95       GMemVTable vtable = { malloc, realloc, free, NULL, NULL, NULL };
96
97       g_mem_set_vtable (&vtable);
98       g_mem_set_vtable (&vtable);
99       exit (0);
100     }
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*");
104 }
105
106 int
107 main (int argc, char *argv[])
108 {
109   g_test_init (&argc, &argv, NULL);
110
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);
115
116   return g_test_run ();
117 }