1 /****************************************************************************
2 Copyright (c) 1994 by Xerox Corporation. All rights reserved.
4 THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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
16 usage: test_cpp number-of-iterations
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
22 A recommended value for number-of-iterations is 10, which will take a
23 few minutes to complete.
25 ***************************************************************************/
28 # include "private/config.h"
35 #define USE_STD_ALLOCATOR
36 #ifdef USE_STD_ALLOCATOR
37 # include "gc_allocator.h"
39 # include "new_gc_alloc.h"
41 # include "gc_alloc.h"
44 # include "private/gcconfig.h"
45 GC_API void GC_printf(const char *format, ...);
46 /* Use GC private output to reach the same log file. */
47 /* Don't include gc_priv.h, since that may include Windows system */
48 /* header files that don't take kindly to this context. */
53 #ifdef GC_NAME_CONFLICT
61 #define my_assert( e ) \
63 GC_printf( "Assertion failure in " __FILE__ ", line %d: " #e "\n", \
69 /* An uncollectable class. */
71 A( int iArg ): i( iArg ) {}
72 void Test( int iArg ) {
73 my_assert( i == iArg );}
77 class B: public gc, public A {public:
78 /* A collectable class. */
82 my_assert( deleting );}
83 static void Deleting( int on ) {
85 static int deleting;};
90 class C: public gc_cleanup, public A {public:
91 /* A collectable class with cleanup and virtual multiple inheritance. */
93 C( int levelArg ): A( levelArg ), level( levelArg ) {
96 left = new C( level - 1 );
97 right = new C( level - 1 );}
101 this->A::Test( level );
103 my_assert( level == 0 ?
104 left == 0 && right == 0 :
105 level == left->level + 1 && level == right->level + 1 );
109 my_assert( nFreed <= nAllocated && nFreed >= .8 * nAllocated );}
112 static int nAllocated;
118 int C::nAllocated = 0;
121 class D: public gc {public:
122 /* A collectable class with a static member function to be used as
123 an explicit clean-up function supplied to ::new. */
125 D( int iArg ): i( iArg ) {
127 static void CleanUp( void* obj, void* data ) {
130 my_assert( self->i == (int) (GC_word) data );}
132 my_assert( nFreed >= .8 * nAllocated );}
136 static int nAllocated;};
139 int D::nAllocated = 0;
142 class E: public gc_cleanup {public:
143 /* A collectable class with clean-up for use by F. */
151 static int nAllocated;};
154 int E::nAllocated = 0;
157 class F: public E {public:
158 /* A collectable class with clean-up, a base with clean-up, and a
159 member with clean-up. */
166 my_assert( nFreed >= .8 * nAllocated );
167 my_assert( 2 * nFreed == E::nFreed );}
171 static int nAllocated;};
174 int F::nAllocated = 0;
177 GC_word Disguise( void* p ) {
178 return ~ (GC_word) p;}
180 void* Undisguise( GC_word i ) {
185 int APIENTRY WinMain(
186 HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int cmdShow )
191 for (argc = 1; argc < sizeof( argv ) / sizeof( argv[ 0 ] ); argc++) {
192 argv[ argc ] = strtok( argc == 1 ? cmd : 0, " \t" );
193 if (0 == argv[ argc ]) break;}
199 int main( int argc, char* argv[] ) {
205 # if defined(MACOS) // MacOS
206 char* argv_[] = {"test_cpp", "10"}; // doesn't
207 argv = argv_; // have a
208 argc = sizeof(argv_)/sizeof(argv_[0]); // commandline
211 # ifdef USE_STD_ALLOCATOR
212 int *x = gc_allocator<int>().allocate(1);
213 int *xio = gc_allocator_ignore_off_page<int>().allocate(1);
214 int **xptr = traceable_allocator<int *>().allocate(1);
217 int *x = (int *)gc_alloc::allocate(sizeof(int));
219 int *x = (int *)alloc::allocate(sizeof(int));
223 # ifdef USE_STD_ALLOCATOR
227 if (argc != 2 || (0 >= (n = atoi( argv[ 1 ] )))) {
228 GC_printf( "usage: test_cpp number-of-iterations\nAssuming 10 iters\n" );
231 for (iters = 1; iters <= n; iters++) {
232 GC_printf( "Starting iteration %d\n", iters );
234 /* Allocate some uncollectable As and disguise their pointers.
235 Later we'll check to see if the objects are still there. We're
236 checking to make sure these objects really are uncollectable. */
239 for (i = 0; i < 1000; i++) {
240 as[ i ] = Disguise( new (NoGC) A( i ) );
241 bs[ i ] = Disguise( new (NoGC) B( i ) );}
243 /* Allocate a fair number of finalizable Cs, Ds, and Fs.
244 Later we'll check to make sure they've gone away. */
245 for (i = 0; i < 1000; i++) {
247 C c1( 2 ); /* stack allocation should work too */
248 D* d = ::new (USE_GC, D::CleanUp, (void*)(GC_word)i) D( i );
250 if (0 == i % 10) delete c;}
252 /* Allocate a very large number of collectable As and Bs and
253 drop the references to them immediately, forcing many
255 for (i = 0; i < 1000000; i++) {
256 A* a = new (USE_GC) A( i );
258 b = new (USE_GC) B( i );
263 # ifdef FINALIZE_ON_DEMAND
264 GC_invoke_finalizers();
268 /* Make sure the uncollectable As and Bs are still there. */
269 for (i = 0; i < 1000; i++) {
270 A* a = (A*) Undisguise( as[ i ] );
271 B* b = (B*) Undisguise( bs[ i ] );
278 # ifdef FINALIZE_ON_DEMAND
279 GC_invoke_finalizers();
284 /* Make sure most of the finalizable Cs, Ds, and Fs have
290 # ifdef USE_STD_ALLOCATOR
293 my_assert (29 == x[0]);
294 GC_printf( "The test appears to have succeeded.\n" );