File: test-storage-compact-coll.c

package info (click to toggle)
sigscheme 0.8.3-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,672 kB
  • ctags: 7,108
  • sloc: lisp: 37,498; ansic: 30,947; sh: 9,257; makefile: 791; asm: 333; ruby: 288
file content (302 lines) | stat: -rw-r--r-- 10,738 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#line 1 "test-storage-compact.c"
/*===========================================================================
 *  Filename : test-storage-compact.c
 *  About    : storage layer tests specific to storage-compact
 *
 *  Copyright (C) 2005-2006 Kazuki Ohta <mover AT hct.zaq.ne.jp>
 *  Copyright (c) 2007-2008 SigScheme Project <uim-en AT googlegroups.com>
 *
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. Neither the name of authors nor the names of its contributors
 *     may be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
 *  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 *  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
===========================================================================*/

#include <sigscheme/config.h>
#if !SCM_USE_STORAGE_COMPACT
#define TST_EXCLUDE_THIS
#endif

#include "sscm-test.h"
#include "sigschemeinternal.h"
#include "utils.c"


#if SCM_USE_STORAGE_COMPACT

/* temporary workaround. see the comment of storage.c */
#if 1
#define SCM_CONS_INIT(obj, kar, kdr)                    \
    SCM_TYPESAFE_MACRO_VOID(SCM_ISAL_CONS_INIT,         \
                            (ScmObj, ScmObj, ScmObj),   \
                            ((obj), (kar), (kdr)))

#define SCM_SYMBOL_INIT(obj, nam, val)                  \
    SCM_TYPESAFE_MACRO_VOID(SCM_ISAL_SYMBOL_INIT,       \
                            (ScmObj, char*, ScmObj),    \
                            ((obj), (nam), (val)))
#endif

TST_CASE(tst_1, "tag-consistent?")
{
    ScmCell *cell;
    ScmObj obj;

    cell = malloc_aligned_8(sizeof(*cell));
    obj = (ScmObj)cell;
    SCM_SYMBOL_INIT(obj, NULL, SCM_NULL);
    TST_COND(SCM_CELL_MISCP(*cell), "cell-misc?");
    TST_COND(SCM_CELL_SYMBOLP(*cell), "cell-symbol?");
    TST_COND(SCM_SYMBOLP(obj), "init -> symbol?");
    TST_COND(SCM_TAG_CONSISTENTP(obj, *cell),
             "consistent? (ptag = misc, cell = misc)");
    SCM_PTAG_SET(obj, SCM_PTAG_CONS);
    TST_COND(CONSP(obj), "set ptag -> pair?");
    TST_COND(!SCM_TAG_CONSISTENTP(obj, *cell),
             "inconsistent? (ptag = pair, cell = misc)");
    SCM_PTAG_SET(obj, SCM_PTAG_CLOSURE);
    TST_COND(SCM_CLOSUREP(obj), "set ptag -> closure?");
    TST_COND(!SCM_TAG_CONSISTENTP(obj, *cell),
             "inconsistent? (ptag = closure, cell = misc)");
    /* Immediate objects pointing to misc cells are harmless. */

    obj = (ScmObj)cell;
    SCM_CONS_INIT(obj, SCM_TRUE, SCM_FALSE);
    TST_COND(!SCM_CELL_MISCP(*cell), "not cell-misc?");
    TST_COND(SCM_CONSP(obj), "init -> pair?");
    TST_COND(SCM_TAG_CONSISTENTP(obj, *cell),
             "consistent? (ptag = pair, cell = pair)");
    SCM_PTAG_SET(obj, SCM_PTAG_CLOSURE);
    TST_COND(SCM_CLOSUREP(obj), "set ptag -> closure?");
    /* Pair and closure have the same memory layout. */
    TST_COND(SCM_TAG_CONSISTENTP(obj, *cell),
             "consistent? (ptag = closure, cell = pair)");
    SCM_PTAG_SET(obj, SCM_PTAG_MISC);
    TST_COND(SCM_MISCP(obj), "set ptag -> misc?");
    TST_COND(!SCM_TAG_CONSISTENTP(obj, *cell),
             "consistent? (ptag = misc, cell = pair)");
    /* Immediate objects pointing to misc cells are harmless. */

    free(cell);
}


static scm_bool
cell_types_disjunct(ScmCell *cell)
{
    int tested_true = 0;
    tested_true += !!SCM_CELL_SYMBOLP(*cell);
    tested_true += !!SCM_CELL_STRINGP(*cell);
    tested_true += !!SCM_CELL_VECTORP(*cell);
    tested_true += !!SCM_CELL_PORTP(*cell);
    tested_true += !!SCM_CELL_CONTINUATIONP(*cell);
    return tested_true == 1;
}

TST_CASE(tst_2, "cell type predicates")
{
    ScmObj obj;

#if SCM_USE_VECTOR
    ScmObj *vec;
#endif

#define TYPE_TST(typ)                                           \
    TST_COND(SCM_CELL_##typ##P(*SCM_UNTAG_PTR(obj))             \
             && cell_types_disjunct(SCM_UNTAG_PTR(obj)),        \
             "CELL_" #typ "P()")

#if SCM_USE_VECTOR
    vec = malloc_aligned_8(sizeof(ScmObj) * 3);
    vec[0] = SCM_NULL;
    vec[1] = SCM_MAKE_INT(8);
    vec[2] = SCM_FALSE;
    obj = SCM_MAKE_VECTOR(vec, 3);
    TYPE_TST(VECTOR);
#endif

    obj = scm_p_current_input_port();
    TYPE_TST(PORT);

    obj = SCM_SYM_QUOTE;
    TYPE_TST(SYMBOL);
    obj = SCM_MAKE_SYMBOL(NULL, SCM_NULL);
    TYPE_TST(SYMBOL);

    {
        char str[] = "some string";
        char *p;
        p = aligned_dup(str, sizeof(str));
        obj = SCM_MAKE_STRING(p, sizeof(str)-1);
        TYPE_TST(STRING);
    }

    /* TODO: continuation */
}

SCM_MISC_DECLARE_TYPE(INTOBJ0, L1(3),
                      SCM_MISC_XTYPE(scm_intobj_t), SCM_MISC_XALIGN(0),
                      SCM_MISC_YTYPE(scm_intobj_t));
#define SCM_AS_INTOBJ0(o) (o)
#define SCM_INTOBJ0_PTR(o) SCM_MISC_PTR((o), INTOBJ0)

SCM_MISC_DECLARE_TYPE(INTOBJ1, L1(3),
                      SCM_MISC_XTYPE(scm_intobj_t), SCM_MISC_XALIGN(1),
                      SCM_MISC_YTYPE(scm_intobj_t));
#define SCM_AS_INTOBJ1(o) (o)
#define SCM_INTOBJ1_PTR(o) SCM_MISC_PTR((o), INTOBJ1)

SCM_MISC_DECLARE_TYPE(INTOBJ2, L1(3),
                      SCM_MISC_XTYPE(scm_intobj_t), SCM_MISC_XALIGN(2),
                      SCM_MISC_YTYPE(scm_intobj_t));
#define SCM_AS_INTOBJ2(o) (o)
#define SCM_INTOBJ2_PTR(o) SCM_MISC_PTR((o), INTOBJ2)

TST_CASE(tst_3, "misc obj fullsize S->X")
{
    ScmCell *heap;
    ScmObj o;

    heap = scm_malloc_aligned(SCM_DEFAULT_HEAP_SIZE * sizeof(ScmCell));

    TST_TN_FALSE(SCM_MISC_INTOBJ0_XDIRECTP);
    TST_TN_FALSE(SCM_MISC_INTOBJ0_XSHIFTP);
    TST_TN_TRUE (SCM_MISC_INTOBJ0_XSPLITP);
    TST_TN_EQ_INT(1, SCM_MISC_INTOBJ0_XSPILL);
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, -1, 0, INTOBJ0);
    TST_TN_EQ_INT(-1, SCM_MISC_X(o, INTOBJ0));
    TST_TN_EQ_INT(0,  SCM_MISC_Y(o, INTOBJ0));
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, 0, -1, INTOBJ0);
    TST_TN_EQ_INT(0,  SCM_MISC_X(o, INTOBJ0));
    TST_TN_EQ_INT(-1, SCM_MISC_Y(o, INTOBJ0));

    TST_TN_TRUE (SCM_MISC_INTOBJ1_XDIRECTP);
    TST_TN_FALSE(SCM_MISC_INTOBJ1_XSHIFTP);
    TST_TN_FALSE(SCM_MISC_INTOBJ1_XSPLITP);
    TST_TN_EQ_INT(0, SCM_MISC_INTOBJ1_XSPILL);
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, -1, 0, INTOBJ1);
    TST_TN_EQ_INT(-1, SCM_MISC_X(o, INTOBJ1));
    TST_TN_EQ_INT(0,  SCM_MISC_Y(o, INTOBJ1));
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, 0, -1, INTOBJ1);
    TST_TN_EQ_INT(0,  SCM_MISC_X(o, INTOBJ1));
    TST_TN_EQ_INT(-1, SCM_MISC_Y(o, INTOBJ1));

    TST_TN_TRUE (SCM_MISC_INTOBJ2_XDIRECTP);
    TST_TN_FALSE(SCM_MISC_INTOBJ2_XSHIFTP);
    TST_TN_FALSE(SCM_MISC_INTOBJ2_XSPLITP);
    TST_TN_EQ_INT(0, SCM_MISC_INTOBJ2_XSPILL);
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, -1, 0, INTOBJ2);
    TST_TN_EQ_INT(-1, SCM_MISC_X(o, INTOBJ2));
    TST_TN_EQ_INT(0,  SCM_MISC_Y(o, INTOBJ2));
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, 0, -1, INTOBJ2);
    TST_TN_EQ_INT(0,  SCM_MISC_X(o, INTOBJ2));
    TST_TN_EQ_INT(-1, SCM_MISC_Y(o, INTOBJ2));

    free(heap);
}

SCM_MISC_DECLARE_TYPE(SHORT0, L1(3),
                      SCM_MISC_XTYPE(int16_t), SCM_MISC_XALIGN(0),
                      SCM_MISC_YTYPE(int16_t));
#define SCM_AS_SHORT0(o) (o)
#define SCM_SHORT0_PTR(o) SCM_MISC_PTR((o), SHORT0)

SCM_MISC_DECLARE_TYPE(SHORT1, L1(3),
                      SCM_MISC_XTYPE(int16_t), SCM_MISC_XALIGN(1),
                      SCM_MISC_YTYPE(int16_t));
#define SCM_AS_SHORT1(o) (o)
#define SCM_SHORT1_PTR(o) SCM_MISC_PTR((o), SHORT1)

SCM_MISC_DECLARE_TYPE(SHORT2, L1(3),
                      SCM_MISC_XTYPE(int16_t), SCM_MISC_XALIGN(2),
                      SCM_MISC_YTYPE(int16_t));
#define SCM_AS_SHORT2(o) (o)
#define SCM_SHORT2_PTR(o) SCM_MISC_PTR((o), SHORT2)

TST_CASE(tst_4, "misc obj 16-bit S->X")
{
    ScmCell *heap;
    ScmObj o;

    heap = scm_malloc_aligned(SCM_DEFAULT_HEAP_SIZE * sizeof(ScmCell));

    TST_TN_FALSE(SCM_MISC_SHORT0_XDIRECTP);
    TST_TN_TRUE (SCM_MISC_SHORT0_XSHIFTP);
    TST_TN_FALSE(SCM_MISC_SHORT0_XSPLITP);
    TST_TN_EQ_INT(1, SCM_MISC_SHORT0_XSPILL);
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, -1, 0, SHORT0);
    TST_TN_EQ_INT(-1, SCM_MISC_X(o, SHORT0));
    TST_TN_EQ_INT(0,  SCM_MISC_Y(o, SHORT0));
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, 0, -1, SHORT0);
    TST_TN_EQ_INT(0,  SCM_MISC_X(o, SHORT0));
    TST_TN_EQ_INT(-1, SCM_MISC_Y(o, SHORT0));

    TST_TN_TRUE (SCM_MISC_SHORT1_XDIRECTP);
    TST_TN_FALSE(SCM_MISC_SHORT1_XSHIFTP);
    TST_TN_FALSE(SCM_MISC_SHORT1_XSPLITP);
    TST_TN_EQ_INT(0, SCM_MISC_SHORT1_XSPILL);
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, -1, 0, SHORT1);
    TST_TN_EQ_INT(-1, SCM_MISC_X(o, SHORT1));
    TST_TN_EQ_INT(0,  SCM_MISC_Y(o, SHORT1));
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, 0, -1, SHORT1);
    TST_TN_EQ_INT(0,  SCM_MISC_X(o, SHORT1));
    TST_TN_EQ_INT(-1, SCM_MISC_Y(o, SHORT1));

    TST_TN_TRUE (SCM_MISC_SHORT2_XDIRECTP);
    TST_TN_FALSE(SCM_MISC_SHORT2_XSHIFTP);
    TST_TN_FALSE(SCM_MISC_SHORT2_XSPLITP);
    TST_TN_EQ_INT(0, SCM_MISC_SHORT2_XSPILL);
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, -1, 0, SHORT2);
    TST_TN_EQ_INT(-1, SCM_MISC_X(o, SHORT2));
    TST_TN_EQ_INT(0,  SCM_MISC_Y(o, SHORT2));
    o = (ScmObj)&heap[0];
    SCM_MISC_INIT(o, 0, -1, SHORT2);
    TST_TN_EQ_INT(0,  SCM_MISC_X(o, SHORT2));
    TST_TN_EQ_INT(-1, SCM_MISC_Y(o, SHORT2));

    free(heap);
}


/* TODO: add tests for the GC algorithm (perhaps by #include'ing a
 * part of storage-gc.c extracted with sed -n '/^gc_mark/,/^}/ p' ) */
#endif /* SCM_USE_STORAGE_COMPACT */
TST_LIST_BEGIN()
    TST_REGISTER(tst_1)
    TST_REGISTER(tst_2)
    TST_REGISTER(tst_3)
    TST_REGISTER(tst_4)
TST_LIST_END()