2 * Copyright (C) 2010 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.
22 /* We test for errors in optimize-only definitions in gmem.h */
24 #pragma GCC optimize (1)
32 gsize a = G_MAXSIZE / 10 + 10;
37 /* "FAIL" here apparently means "fail to overflow"... */
38 #define CHECK_PASS(P) p = (P); g_assert (p == NULL);
39 #define CHECK_FAIL(P) p = (P); g_assert (p != NULL);
41 CHECK_PASS (g_try_malloc_n (a, a));
42 CHECK_PASS (g_try_malloc_n (a, b));
43 CHECK_PASS (g_try_malloc_n (b, a));
44 CHECK_FAIL (g_try_malloc_n (b, b));
47 CHECK_PASS (g_try_malloc0_n (a, a));
48 CHECK_PASS (g_try_malloc0_n (a, b));
49 CHECK_PASS (g_try_malloc0_n (b, a));
50 CHECK_FAIL (g_try_malloc0_n (b, b));
54 CHECK_PASS (g_try_realloc_n (q, a, a));
55 CHECK_PASS (g_try_realloc_n (q, a, b));
56 CHECK_PASS (g_try_realloc_n (q, b, a));
57 CHECK_FAIL (g_try_realloc_n (q, b, b));
60 CHECK_PASS (g_try_new (X, a));
61 CHECK_FAIL (g_try_new (X, b));
64 CHECK_PASS (g_try_new0 (X, a));
65 CHECK_FAIL (g_try_new0 (X, b));
69 CHECK_PASS (g_try_renew (X, q, a));
70 CHECK_FAIL (g_try_renew (X, q, b));
76 #define CHECK_FAIL(P) if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { p = (P); exit (0); } g_test_trap_assert_failed();
78 #define CHECK_PASS(P) do { \
79 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) \
86 g_test_trap_assert_passed(); \
89 CHECK_FAIL (g_malloc_n (a, a));
90 CHECK_FAIL (g_malloc_n (a, b));
91 CHECK_FAIL (g_malloc_n (b, a));
92 CHECK_PASS (g_malloc_n (b, b));
94 CHECK_FAIL (g_malloc0_n (a, a));
95 CHECK_FAIL (g_malloc0_n (a, b));
96 CHECK_FAIL (g_malloc0_n (b, a));
97 CHECK_PASS (g_malloc0_n (b, b));
100 CHECK_FAIL (g_realloc_n (q, a, a));
101 CHECK_FAIL (g_realloc_n (q, a, b));
102 CHECK_FAIL (g_realloc_n (q, b, a));
103 CHECK_PASS (g_realloc_n (q, b, b));
106 CHECK_FAIL (g_new (X, a));
107 CHECK_PASS (g_new (X, b));
109 CHECK_FAIL (g_new0 (X, a));
110 CHECK_PASS (g_new0 (X, b));
113 CHECK_FAIL (g_renew (X, q, a));
114 CHECK_PASS (g_renew (X, q, b));
125 g_test_bug ("615379");
127 g_assert_cmpint (sizeof (Empty), ==, 0);
129 if (g_test_trap_fork (0, 0))
133 empty = g_new0 (Empty, 1);
134 g_assert (empty == NULL);
137 g_test_trap_assert_passed ();
144 g_test_init (&argc, &argv, NULL);
146 g_test_bug_base ("http://bugzilla.gnome.org/");
148 g_test_add_func ("/mem/overflow", mem_overflow);
149 g_test_add_func ("/mem/empty-alloc", empty_alloc);