~ubuntu-branches/ubuntu/hardy/uim/hardy

« back to all changes in this revision

Viewing changes to sigscheme/test-c/test-gc-protect-stack-coll.c

  • Committer: Bazaar Package Importer
  • Author(s): Masahito Omote
  • Date: 2007-04-21 03:46:09 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070421034609-gpcurkutp8vaysqj
Tags: 1:1.4.1-3
* Switch to dh_gtkmodules for the gtk 2.10 transition (Closes:
  #419318)
  - debian/control: Add ${misc:Depends} and remove libgtk2.0-bin on
    uim-gtk2.0.
  - debian/uim-gtk2.0.post{inst,rm}: Removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#line 1 "test-gc-protect-stack.c"
 
2
/*===========================================================================
 
3
 *  Filename : test-gc-protect-stack.c
 
4
 *  About    : unit test for scm_gc_protect_stack()
 
5
 *
 
6
 *  Copyright (C) 2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
 
7
 *  Copyright (c) 2007 SigScheme Project <uim AT freedesktop.org>
 
8
 *
 
9
 *  All rights reserved.
 
10
 *
 
11
 *  Redistribution and use in source and binary forms, with or without
 
12
 *  modification, are permitted provided that the following conditions
 
13
 *  are met:
 
14
 *
 
15
 *  1. Redistributions of source code must retain the above copyright
 
16
 *     notice, this list of conditions and the following disclaimer.
 
17
 *  2. Redistributions in binary form must reproduce the above copyright
 
18
 *     notice, this list of conditions and the following disclaimer in the
 
19
 *     documentation and/or other materials provided with the distribution.
 
20
 *  3. Neither the name of authors nor the names of its contributors
 
21
 *     may be used to endorse or promote products derived from this software
 
22
 *     without specific prior written permission.
 
23
 *
 
24
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
 
25
 *  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
26
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
27
 *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 
28
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
29
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
30
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
31
 *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
32
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
33
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
34
 *  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
35
===========================================================================*/
 
36
 
 
37
#if 1
 
38
/* Since introduction of libgcroots to SigScheme, the GC protection interfaces
 
39
 * are withdrawn. These tests does not make sense now. -- YamaKen 2006-12-23 */
 
40
#define TST_EXCLUDE_THIS
 
41
#endif
 
42
 
 
43
#define TST_HAVE_MAIN 1
 
44
#include "sscm-test.h"
 
45
 
 
46
#ifndef TST_EXCLUDE_THIS
 
47
 
 
48
#include <stddef.h>
 
49
#include <sigscheme/sigscheme.h>
 
50
 
 
51
#define SCMOBJ_ALIGNEDP(ptr) (!((uintptr_t)(ptr) % sizeof(ScmObj)))
 
52
 
 
53
#define TEST_STACK_START(protected, actual)                                  \
 
54
    if (stack_dir == STACK_GROWS_DOWNWARDS) {                                \
 
55
        TST_TN_TRUE(actual <= protected);                                    \
 
56
    } else {                                                                 \
 
57
        TST_TN_TRUE(actual >= protected);                                    \
 
58
    }
 
59
 
 
60
enum stack_growth_dir {
 
61
    STACK_GROWS_DOWNWARDS,
 
62
    STACK_GROWS_UPWARDS
 
63
};
 
64
 
 
65
static enum stack_growth_dir stack_dir;
 
66
static void *volatile stack_start_protected;
 
67
static void *volatile stack_start_actual;
 
68
static void (*volatile fvv_internal)(void);
 
69
static int  (*volatile fiv_internal)(void);
 
70
static void (*volatile fvi_internal)(int);
 
71
static int  (*volatile fii_internal)(int);
 
72
static ScmObj *(*volatile fspsp_internal)(ScmObj *dummy);
 
73
static ScmObj *(*volatile fspsp2_internal)(ScmObj *dummy);
 
74
 
 
75
static enum stack_growth_dir probe_stack_growth_dir(void);
 
76
static enum stack_growth_dir probe_stack_growth_dir2(void *upper_frame);
 
77
static void fvv(void);
 
78
static int  fiv(void);
 
79
static void fvi(int dummy);
 
80
static int  fii(int dummy);
 
81
static ScmObj *fspsp(ScmObj *dummy);
 
82
static ScmObj *fspsp2(ScmObj *dummy);
 
83
 
 
84
 
 
85
static enum stack_growth_dir
 
86
probe_stack_growth_dir(void)
 
87
{
 
88
    int stack_start;
 
89
 
 
90
    return probe_stack_growth_dir2(&stack_start);
 
91
}
 
92
 
 
93
static enum stack_growth_dir
 
94
probe_stack_growth_dir2(void *upper_frame)
 
95
{
 
96
    int stack_start;
 
97
 
 
98
    if ((void *)&stack_start < upper_frame)
 
99
        return STACK_GROWS_DOWNWARDS;
 
100
    else
 
101
        return STACK_GROWS_UPWARDS;
 
102
}
 
103
 
 
104
static void
 
105
fvv(void)
 
106
{
 
107
    ScmObj stack_start;
 
108
 
 
109
    stack_start_actual = &stack_start;
 
110
}
 
111
 
 
112
static int
 
113
fiv(void)
 
114
{
 
115
    ScmObj stack_start;
 
116
 
 
117
    stack_start_actual = &stack_start;
 
118
    return 0;
 
119
}
 
120
 
 
121
static void
 
122
fvi(int dummy)
 
123
{
 
124
    ScmObj stack_start;
 
125
 
 
126
    stack_start_actual = &stack_start;
 
127
}
 
128
 
 
129
static int
 
130
fii(int dummy)
 
131
{
 
132
    ScmObj stack_start;
 
133
 
 
134
    stack_start_actual = &stack_start;
 
135
    return 0;
 
136
}
 
137
 
 
138
static ScmObj *
 
139
fspsp(ScmObj *dummy)
 
140
{
 
141
    ScmObj stack_start;
 
142
 
 
143
    stack_start_actual = &stack_start;
 
144
    return dummy;
 
145
}
 
146
 
 
147
/* simulates scm_gc_protect_stack_internal() */
 
148
static ScmObj *
 
149
fspsp2(ScmObj *designated_stack_start)
 
150
{
 
151
    ScmObj stack_start;
 
152
 
 
153
    if (!designated_stack_start)
 
154
        designated_stack_start = &stack_start;
 
155
 
 
156
    stack_start_actual = designated_stack_start;
 
157
 
 
158
    /* enabling this fragment on --enable-debug configuration offsets stack
 
159
     * pointer */
 
160
#if 1
 
161
    SCM_ASSERT(SCMOBJ_ALIGNEDP(stack_start_actual));
 
162
#endif
 
163
 
 
164
    /* may intentionally be an invalidated local address */
 
165
    return designated_stack_start;
 
166
}
 
167
 
 
168
int
 
169
main(int argc, char **argv)
 
170
{
 
171
    tst_suite_info suite = TST_DEFAULT_SUITE_SETUP;
 
172
 
 
173
    scm_initialize(NULL);
 
174
 
 
175
    stack_dir = probe_stack_growth_dir();
 
176
    fvv_internal = fvv;
 
177
    fiv_internal = fiv;
 
178
    fvi_internal = fvi;
 
179
    fii_internal = fii;
 
180
    fspsp_internal = fspsp;
 
181
    fspsp2_internal = fspsp2;
 
182
 
 
183
    tst_main(&suite);
 
184
 
 
185
    scm_finalize();
 
186
 
 
187
    TST_DEFAULT_SUITE_CLEANUP(suite);
 
188
    return !!suite.stats.fail;
 
189
}
 
190
 
 
191
TST_CASE(tst_1, "void (*)(void)")
 
192
{
 
193
    stack_start_protected = scm_gc_current_stack();
 
194
    scm_gc_protect_stack(stack_start_protected);
 
195
    (*fvv_internal)();
 
196
    scm_gc_unprotect_stack(stack_start_protected);
 
197
 
 
198
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
199
}
 
200
 
 
201
TST_CASE(tst_2, "int (*)(void)")
 
202
{
 
203
    stack_start_protected = scm_gc_current_stack();
 
204
    scm_gc_protect_stack(stack_start_protected);
 
205
    (*fiv_internal)();
 
206
    scm_gc_unprotect_stack(stack_start_protected);
 
207
 
 
208
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
209
}
 
210
 
 
211
TST_CASE(tst_3, "void (*)(int)")
 
212
{
 
213
    stack_start_protected = scm_gc_current_stack();
 
214
    scm_gc_protect_stack(stack_start_protected);
 
215
    (*fvi_internal)(0);
 
216
    scm_gc_unprotect_stack(stack_start_protected);
 
217
 
 
218
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
219
}
 
220
 
 
221
TST_CASE(tst_4, "int (*)(int)")
 
222
{
 
223
    stack_start_protected = scm_gc_current_stack();
 
224
    scm_gc_protect_stack(stack_start_protected);
 
225
    (*fii_internal)(0);
 
226
    scm_gc_unprotect_stack(stack_start_protected);
 
227
 
 
228
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
229
}
 
230
 
 
231
TST_CASE(tst_5, "ScmObj *(*)(ScmObj *)")
 
232
{
 
233
    stack_start_protected = scm_gc_current_stack();
 
234
    scm_gc_protect_stack(stack_start_protected);
 
235
    (*fspsp_internal)(NULL);
 
236
    scm_gc_unprotect_stack(stack_start_protected);
 
237
 
 
238
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
239
}
 
240
 
 
241
TST_CASE(tst_6, "ScmObj *(*)(ScmObj *) (2)")
 
242
{
 
243
    stack_start_protected = scm_gc_current_stack();
 
244
    scm_gc_protect_stack(stack_start_protected);
 
245
    (*fspsp2_internal)(NULL);
 
246
    scm_gc_unprotect_stack(stack_start_protected);
 
247
 
 
248
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
249
}
 
250
 
 
251
#endif /* TST_EXCLUDE_THIS */
 
252
TST_LIST_BEGIN()
 
253
    TST_REGISTER(tst_1)
 
254
    TST_REGISTER(tst_2)
 
255
    TST_REGISTER(tst_3)
 
256
    TST_REGISTER(tst_4)
 
257
    TST_REGISTER(tst_5)
 
258
    TST_REGISTER(tst_6)
 
259
TST_LIST_END()