implemented Setup.hs to build boehm cpp libs and install them;
[hs-boehmgc.git] / gc-7.2 / tests / test_cpp.cc
1 /****************************************************************************
2 Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
3
4 THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
6
7 Permission is hereby granted to use or copy this program for any
8 purpose, provided the above notices are retained on all copies.
9 Permission to modify the code and to distribute modified code is
10 granted, provided the above notices are retained, and a notice that
11 the code was modified is included with the above copyright notice.
12 ****************************************************************************
13 Last modified on Mon Jul 10 21:06:03 PDT 1995 by ellis
14      modified on December 20, 1994 7:27 pm PST by boehm
15
16 usage: test_cpp number-of-iterations
17
18 This program tries to test the specific C++ functionality provided by
19 gc_c++.h that isn't tested by the more general test routines of the
20 collector.
21
22 A recommended value for number-of-iterations is 10, which will take a
23 few minutes to complete.
24
25 ***************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "private/config.h"
29 #endif
30
31 #undef GC_BUILD
32
33 #include "gc_cpp.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #define USE_STD_ALLOCATOR
40
41 #ifdef USE_STD_ALLOCATOR
42 #   include "gc_allocator.h"
43 #else
44 #   include "new_gc_alloc.h"
45 #endif
46
47 extern "C" {
48 # include "private/gcconfig.h"
49
50 # ifndef GC_API_PRIV
51 #   define GC_API_PRIV GC_API
52 # endif
53   GC_API_PRIV void GC_printf(const char * format, ...);
54   /* Use GC private output to reach the same log file.  */
55   /* Don't include gc_priv.h, since that may include Windows system     */
56   /* header files that don't take kindly to this context.               */
57 }
58
59 #ifdef MSWIN32
60 # include <windows.h>
61 #endif
62
63 #ifdef GC_NAME_CONFLICT
64 # define USE_GC UseGC
65   struct foo * GC;
66 #else
67 # define USE_GC GC
68 #endif
69
70 #define my_assert( e ) \
71     if (! (e)) { \
72         GC_printf( "Assertion failure in " __FILE__ ", line %d: " #e "\n", \
73                     __LINE__ ); \
74         exit( 1 ); }
75
76
77 class A {public:
78     /* An uncollectable class. */
79
80     A( int iArg ): i( iArg ) {}
81     void Test( int iArg ) {
82         my_assert( i == iArg );}
83     int i;};
84
85
86 class B: public gc, public A {public:
87     /* A collectable class. */
88
89     B( int j ): A( j ) {}
90     ~B() {
91         my_assert( deleting );}
92     static void Deleting( int on ) {
93         deleting = on;}
94     static int deleting;};
95
96 int B::deleting = 0;
97
98
99 class C: public gc_cleanup, public A {public:
100     /* A collectable class with cleanup and virtual multiple inheritance. */
101
102     C( int levelArg ): A( levelArg ), level( levelArg ) {
103         nAllocated++;
104         if (level > 0) {
105             left = new C( level - 1 );
106             right = new C( level - 1 );}
107         else {
108             left = right = 0;}}
109     ~C() {
110         this->A::Test( level );
111         nFreed++;
112         my_assert( level == 0 ?
113                    left == 0 && right == 0 :
114                    level == left->level + 1 && level == right->level + 1 );
115         left = right = 0;
116         level = -123456;}
117     static void Test() {
118         my_assert( nFreed <= nAllocated && nFreed >= .8 * nAllocated );}
119
120     static int nFreed;
121     static int nAllocated;
122     int level;
123     C* left;
124     C* right;};
125
126 int C::nFreed = 0;
127 int C::nAllocated = 0;
128
129
130 class D: public gc {public:
131     /* A collectable class with a static member function to be used as
132     an explicit clean-up function supplied to ::new. */
133
134     D( int iArg ): i( iArg ) {
135         nAllocated++;}
136     static void CleanUp( void* obj, void* data ) {
137         D* self = (D*) obj;
138         nFreed++;
139         my_assert( self->i == (int) (GC_word) data );}
140     static void Test() {
141         my_assert( nFreed >= .8 * nAllocated );}
142
143     int i;
144     static int nFreed;
145     static int nAllocated;};
146
147 int D::nFreed = 0;
148 int D::nAllocated = 0;
149
150
151 class E: public gc_cleanup {public:
152     /* A collectable class with clean-up for use by F. */
153
154     E() {
155         nAllocated++;}
156     ~E() {
157         nFreed++;}
158
159     static int nFreed;
160     static int nAllocated;};
161
162 int E::nFreed = 0;
163 int E::nAllocated = 0;
164
165
166 class F: public E {public:
167     /* A collectable class with clean-up, a base with clean-up, and a
168     member with clean-up. */
169
170     F() {
171         nAllocated++;}
172     ~F() {
173         nFreed++;}
174     static void Test() {
175         my_assert( nFreed >= .8 * nAllocated );
176         my_assert( 2 * nFreed == E::nFreed );}
177
178     E e;
179     static int nFreed;
180     static int nAllocated;};
181
182 int F::nFreed = 0;
183 int F::nAllocated = 0;
184
185
186 GC_word Disguise( void* p ) {
187     return ~ (GC_word) p;}
188
189 void* Undisguise( GC_word i ) {
190     return (void*) ~ i;}
191
192 #ifdef MSWIN32
193 int APIENTRY WinMain(
194     HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int cmdShow )
195 {
196     int argc = 0;
197     char* argv[ 3 ];
198
199     if (cmd != 0)
200       for (argc = 1; argc < (int)(sizeof(argv) / sizeof(argv[0])); argc++) {
201         argv[ argc ] = strtok( argc == 1 ? cmd : 0, " \t" );
202         if (0 == argv[ argc ]) break;}
203 #elif defined(MACOS)
204   int main() {
205     char* argv_[] = {"test_cpp", "10"}; // MacOS doesn't have a commandline
206     argv = argv_;
207     argc = sizeof(argv_)/sizeof(argv_[0]);
208 #else
209   int main( int argc, char* argv[] ) {
210 #endif
211
212     GC_set_all_interior_pointers(1);
213                         /* needed due to C++ multiple inheritance used  */
214
215     GC_INIT();
216
217     int i, iters, n;
218 #   ifdef USE_STD_ALLOCATOR
219       int *x = gc_allocator<int>().allocate(1);
220       int *xio;
221       xio = gc_allocator_ignore_off_page<int>().allocate(1);
222       int **xptr = traceable_allocator<int *>().allocate(1);
223 #   else
224       int *x = (int *)gc_alloc::allocate(sizeof(int));
225 #   endif
226     *x = 29;
227 #   ifdef USE_STD_ALLOCATOR
228       *xptr = x;
229       x = 0;
230 #   endif
231     if (argc != 2 || (0 >= (n = atoi( argv[ 1 ] )))) {
232         GC_printf( "usage: test_cpp number-of-iterations\nAssuming 10 iters\n" );
233         n = 10;}
234
235     for (iters = 1; iters <= n; iters++) {
236         GC_printf( "Starting iteration %d\n", iters );
237
238             /* Allocate some uncollectable As and disguise their pointers.
239             Later we'll check to see if the objects are still there.  We're
240             checking to make sure these objects really are uncollectable. */
241         GC_word as[ 1000 ];
242         GC_word bs[ 1000 ];
243         for (i = 0; i < 1000; i++) {
244             as[ i ] = Disguise( new (NoGC ) A( i ) );
245             bs[ i ] = Disguise( new (NoGC) B( i ) );}
246
247             /* Allocate a fair number of finalizable Cs, Ds, and Fs.
248             Later we'll check to make sure they've gone away. */
249         for (i = 0; i < 1000; i++) {
250             C* c = new C( 2 );
251             C c1( 2 );           /* stack allocation should work too */
252             D* d;
253             F* f;
254             d = ::new (USE_GC, D::CleanUp, (void*)(GC_word)i) D( i );
255             f = new F;
256             if (0 == i % 10) delete c;}
257
258             /* Allocate a very large number of collectable As and Bs and
259             drop the references to them immediately, forcing many
260             collections. */
261         for (i = 0; i < 1000000; i++) {
262             A* a;
263             a = new (USE_GC) A( i );
264             B* b = new B( i );
265             b = new (USE_GC) B( i );
266             if (0 == i % 10) {
267                 B::Deleting( 1 );
268                 delete b;
269                 B::Deleting( 0 );}
270 #           ifdef FINALIZE_ON_DEMAND
271               GC_invoke_finalizers();
272 #           endif
273             }
274
275             /* Make sure the uncollectable As and Bs are still there. */
276         for (i = 0; i < 1000; i++) {
277             A* a = (A*) Undisguise( as[ i ] );
278             B* b = (B*) Undisguise( bs[ i ] );
279             a->Test( i );
280             delete a;
281             b->Test( i );
282             B::Deleting( 1 );
283             delete b;
284             B::Deleting( 0 );
285 #           ifdef FINALIZE_ON_DEMAND
286                  GC_invoke_finalizers();
287 #           endif
288             }
289
290             /* Make sure most of the finalizable Cs, Ds, and Fs have
291             gone away. */
292         C::Test();
293         D::Test();
294         F::Test();}
295
296 #   ifdef USE_STD_ALLOCATOR
297       x = *xptr;
298 #   endif
299     my_assert (29 == x[0]);
300     GC_printf( "The test appears to have succeeded.\n" );
301     return( 0 );
302 }
303