Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / glib / tests / slice.c
blobb0ad3da66142f8e1ec19086326c67712c340b96c
1 #include <string.h>
2 #include <glib.h>
4 /* We test deprecated functionality here */
5 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
7 #ifdef G_ENABLE_DEBUG
8 static void
9 test_slice_nodebug (void)
11 const gchar *oldval;
13 oldval = g_getenv ("G_SLICE");
14 g_unsetenv ("G_SLICE");
16 if (g_test_subprocess ())
18 gpointer p, q;
20 p = g_slice_alloc (237);
21 q = g_slice_alloc (259);
22 g_slice_free1 (237, p);
23 g_slice_free1 (259, q);
25 g_slice_debug_tree_statistics ();
26 return;
28 g_test_trap_subprocess (NULL, 1000000, 0);
29 g_test_trap_assert_passed ();
30 g_test_trap_assert_stderr ("*GSlice: MemChecker: root=NULL*");
32 if (oldval)
33 g_setenv ("G_SLICE", oldval, TRUE);
36 static void
37 test_slice_debug (void)
39 const gchar *oldval;
41 oldval = g_getenv ("G_SLICE");
42 g_setenv ("G_SLICE", "debug-blocks:always-malloc", TRUE);
44 if (g_test_subprocess ())
46 gpointer p, q;
48 p = g_slice_alloc (237);
49 q = g_slice_alloc (259);
50 g_slice_free1 (237, p);
51 g_slice_free1 (259, q);
53 g_slice_debug_tree_statistics ();
54 return;
56 g_test_trap_subprocess (NULL, 1000000, 0);
57 g_test_trap_assert_passed ();
58 g_test_trap_assert_stderr ("*GSlice: MemChecker: * trunks, * branches, * old branches*");
60 if (oldval)
61 g_setenv ("G_SLICE", oldval, TRUE);
62 else
63 g_unsetenv ("G_SLICE");
65 #endif
67 static void
68 test_slice_copy (void)
70 const gchar *block = "0123456789ABCDEF";
71 gpointer p;
73 p = g_slice_copy (12, block);
74 g_assert (memcmp (p, block, 12) == 0);
75 g_slice_free1 (12, p);
78 typedef struct {
79 gint int1;
80 gint int2;
81 gchar byte;
82 gpointer next;
83 gint64 more;
84 } TestStruct;
86 static void
87 test_chain (void)
89 TestStruct *ts, *head;
91 head = ts = g_slice_new (TestStruct);
92 ts->next = g_slice_new (TestStruct);
93 ts = ts->next;
94 ts->next = g_slice_new (TestStruct);
95 ts = ts->next;
96 ts->next = NULL;
98 g_slice_free_chain (TestStruct, head, next);
101 static gpointer chunks[4096][30];
103 static gpointer
104 thread_allocate (gpointer data)
106 gint i;
107 gint b;
108 gint size;
109 gpointer p;
110 volatile gpointer *loc;
112 for (i = 0; i < 10000; i++)
114 b = g_random_int_range (0, 30);
115 size = g_random_int_range (0, 4096);
116 loc = &(chunks[size][b]);
118 p = g_atomic_pointer_get (loc);
119 if (p == NULL)
121 p = g_slice_alloc (size + 1);
122 if (!g_atomic_pointer_compare_and_exchange (loc, NULL, p))
123 g_slice_free1 (size + 1, p);
125 else
127 if (g_atomic_pointer_compare_and_exchange (loc, p, NULL))
128 g_slice_free1 (size + 1, p);
132 return NULL;
135 static void
136 test_allocate (void)
138 GThread *threads[30];
139 gint size;
140 gint i;
142 for (i = 0; i < 30; i++)
143 for (size = 1; size <= 4096; size++)
144 chunks[size - 1][i] = NULL;
146 for (i = 0; i < G_N_ELEMENTS(threads); i++)
147 threads[i] = g_thread_create (thread_allocate, NULL, TRUE, NULL);
149 for (i = 0; i < G_N_ELEMENTS(threads); i++)
150 g_thread_join (threads[i]);
154 main (int argc, char **argv)
156 g_test_init (&argc, &argv, NULL);
158 #ifdef G_ENABLE_DEBUG
159 g_test_add_func ("/slice/nodebug", test_slice_nodebug);
160 g_test_add_func ("/slice/debug", test_slice_debug);
161 #endif
162 g_test_add_func ("/slice/copy", test_slice_copy);
163 g_test_add_func ("/slice/chain", test_chain);
164 g_test_add_func ("/slice/allocate", test_allocate);
166 return g_test_run ();