~ubuntu-branches/ubuntu/hardy/sigscheme/hardy-proposed

« back to all changes in this revision

Viewing changes to test-c/test-gc-coll.c

  • Committer: Bazaar Package Importer
  • Author(s): NIIBE Yutaka
  • Date: 2007-01-29 15:31:24 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070129153124-j5fcqyrwcfbczma7
Tags: 0.7.4-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#line 1 "test-gc.c"
 
2
/*===========================================================================
 
3
 *  Filename : test-gc.c
 
4
 *  About    : garbage collector test
 
5
 *
 
6
 *  Copyright (C) 2006 Jun Inoue <jun.lambda@gmail.com>
 
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
#include <sigscheme/config.h>
 
37
#if !(SCM_USE_STORAGE_COMPACT || SCM_USE_STORAGE_FATTY)
 
38
#define TST_EXCLUDE_THIS
 
39
#endif
 
40
 
 
41
#include "sscm-test.h"
 
42
#include "sigscheme.h"
 
43
#include "sigschemeinternal.h"
 
44
#define HEAP_SIZE    SCM_DEFAULT_HEAP_SIZE
 
45
 
 
46
 
 
47
void saturate_heap(size_t heap_size);
 
48
ScmObj alloc (TST_PARAMS_DECL);
 
49
void verify (ScmObj obj, TST_PARAMS_DECL);
 
50
 
 
51
void
 
52
saturate_heap (size_t size)
 
53
{
 
54
    /* We overallocate objects to force the GC to kick in.  This is
 
55
     * problematic if the new objects occupy cells that were
 
56
     * inadvertently freed due to a bug in GC.  To guard against that
 
57
     * possibility, we allocate closures (which the caller doesn't use
 
58
     * by convention) so that type predicates that the caller will do
 
59
     * on its objects won't accidentally succeed. */
 
60
    while (size--)
 
61
        SCM_MAKE_CLOSURE(SCM_NULL, SCM_NULL);
 
62
}
 
63
 
 
64
 
 
65
ScmObj
 
66
alloc (TST_PARAMS_DECL)
 
67
{
 
68
    return CONS(LIST_1(MAKE_INT(1)), scm_intern("aaa"));
 
69
}
 
70
 
 
71
#define TST(cond) TST_COND(cond, #cond)
 
72
void
 
73
verify (ScmObj obj, TST_PARAMS_DECL)
 
74
{
 
75
    TST_ASSERT(TST(CONSP(obj)));
 
76
    TST_ASSERT(TST(SYMBOLP(CDR(obj))));
 
77
    obj = CAR(obj);
 
78
    TST_ASSERT(TST(CONSP(obj)));
 
79
    TST_ASSERT(TST(NULLP(CDR(obj))));
 
80
    TST_ASSERT(TST(INTP(CAR(obj))));
 
81
    TST_ASSERT(TST_COND(EQVP(CAR(obj), MAKE_INT(1)),
 
82
                        "integer=? after GC"));
 
83
}
 
84
 
 
85
#define ALLOC()   alloc(TST_PARAMS)
 
86
#define VERIFY(o) verify((o), TST_PARAMS)
 
87
 
 
88
#define TEST(o)                                                           \
 
89
    do {                                                                  \
 
90
        /* We run it twice to see if the contents of the stack makes a */ \
 
91
        /* difference. */                                                 \
 
92
        saturate_heap (HEAP_SIZE);                                        \
 
93
        VERIFY(o);                                                        \
 
94
        saturate_heap (HEAP_SIZE);                                        \
 
95
        VERIFY(o);                                                        \
 
96
    } while (0)
 
97
 
 
98
TST_CASE(tst_1, "GC mark stack")
 
99
{
 
100
    ScmObj obj;
 
101
 
 
102
    obj = ALLOC ();
 
103
    TEST (obj);
 
104
}
 
105
 
 
106
 
 
107
TST_CASE(tst_2, "GC mark protected var")
 
108
{
 
109
    ScmObj *var;
 
110
 
 
111
    var = malloc (sizeof (*var));
 
112
    scm_gc_protect (var);
 
113
 
 
114
    *var = ALLOC ();
 
115
    TEST (*var);
 
116
 
 
117
    scm_gc_unprotect (var);
 
118
    free (var);
 
119
}
 
120
 
 
121
volatile scm_intobj_t false_ptr;
 
122
 
 
123
TST_CASE(tst_3, "GC pointer predicate false positives")
 
124
{
 
125
    ScmObj obj;
 
126
    size_t i;
 
127
 
 
128
    obj = ALLOC ();
 
129
    false_ptr = (scm_intobj_t)obj;
 
130
    for (i = 0; i < sizeof (ScmCell); i++) {
 
131
        ++false_ptr;
 
132
        TEST(obj);
 
133
    }
 
134
}
 
135
TST_LIST_BEGIN()
 
136
    TST_REGISTER(tst_1)
 
137
    TST_REGISTER(tst_2)
 
138
    TST_REGISTER(tst_3)
 
139
TST_LIST_END()