home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / SYSTEM / GC / TEST_CPP.CC < prev    next >
C/C++ Source or Header  |  1994-12-20  |  5KB  |  169 lines

  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.  
  14. usage: test_gc_c++ number-of-iterations
  15.  
  16. This program tries to test the specific C++ functionality provided by
  17. gc_c++.h that isn't tested by the more general test routines of the
  18. collector.
  19.  
  20. A recommended value for number-of-iterations is 10, which will take a
  21. few minutes to complete.
  22.  
  23. ***************************************************************************/
  24. /* Boehm, December 20, 1994 7:27 pm PST */
  25.  
  26. #include "gc_cpp.h"
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30.  
  31. class A {public:
  32.     /* An uncollectable class. */
  33.  
  34.     A( int iArg ): i( iArg ) {}
  35.     void Test( int iArg ) {
  36.         assert( i == iArg );} 
  37.     int i;};
  38.  
  39.  
  40. class B: public gc, public A {public:
  41.     /* A collectable class. */
  42.  
  43.     B( int j ): A( j ) {}
  44.     ~B() {
  45.         assert( deleting );}
  46.     static void Deleting( int on ) {
  47.         deleting = on;}
  48.     static int deleting;};
  49.  
  50. int B::deleting = 0;
  51.  
  52.  
  53. class C: public gc_cleanup, public A {public:
  54.     /* A collectable class with cleanup and virtual multiple inheritance. */
  55.  
  56.     C( int levelArg ): A( levelArg ), level( levelArg ) {
  57.         nAllocated++;
  58.         if (level > 0) {
  59.             left = new C( level - 1 );
  60.             right = new C( level - 1 );}
  61.         else {
  62.             left = right = 0;}}
  63.     ~C() {
  64.         this->A::Test( level );
  65.         nFreed++;
  66.         assert( level == 0 ? 
  67.                    left == 0 && right == 0 :
  68.                    level == left->level + 1 && level == right->level + 1 );
  69.         left = right = 0;
  70.         level = -123456;}
  71.     static void Test() {
  72.         assert( nFreed <= nAllocated && nFreed >= .8 * nAllocated );}
  73.  
  74.     static int nFreed;
  75.     static int nAllocated;
  76.     int level;
  77.     C* left;
  78.     C* right;};
  79.  
  80. int C::nFreed = 0;
  81. int C::nAllocated = 0;
  82.  
  83.  
  84. class D: public gc {public:
  85.     /* A collectable class with a static member function to be used as
  86.     an explicit clean-up function supplied to ::new. */
  87.  
  88.     D( int iArg ): i( iArg ) {
  89.         nAllocated++;}
  90.     static void CleanUp( void* obj, void* data ) {
  91.         D* self = (D*) obj;
  92.         nFreed++;
  93.         assert( self->i == (int) data );}
  94.     static void Test() {
  95.         assert( nFreed >= .8 * nAllocated );}
  96.        
  97.     int i;
  98.     static int nFreed;
  99.     static int nAllocated;};
  100.  
  101. int D::nFreed = 0;
  102. int D::nAllocated = 0;
  103.  
  104.  
  105. long Disguise( void* p ) {
  106.     return ~ (long) p;}
  107.  
  108. void* Undisguise( long i ) {
  109.     return (void*) ~ i;}
  110.  
  111.  
  112. int main( int argc, char* argv[] ) {
  113.     int i, iters, n;
  114.  
  115.     if (argc != 2 || (0 >= (n = atoi( argv[ 1 ] )))) {
  116.         fprintf( stderr, "usage: test_gc_c++ number-of-iterations\n" );
  117.         exit( 1 );}
  118.         
  119.     for (iters = 1; iters <= n; iters++) {
  120.         printf( "Starting iteration %d\n", iters );
  121.  
  122.             /* Allocate some uncollectable As and disguise their pointers.
  123.             Later we'll check to see if the objects are still there.  We're
  124.             checking to make sure these objects really are uncollectable. */
  125.         long as[ 1000 ];
  126.         long bs[ 1000 ];
  127.         for (i = 0; i < 1000; i++) {
  128.             as[ i ] = Disguise( new (NoGC) A( i ) );
  129.             bs[ i ] = Disguise( new (NoGC) B( i ) );}
  130.  
  131.             /* Allocate a fair number of finalizable Cs and Ds.  Later we'll
  132.             check to make sure they've gone away. */
  133.         for (i = 0; i < 1000; i++) {
  134.             C* c = new C( 2 );
  135.             D* d = ::new (GC, D::CleanUp, (void*) i) D( i );
  136.             if (0 == i % 10) delete c;}
  137.  
  138.             /* Allocate a very large number of collectable As and Bs and
  139.             drop the references to them immediately, forcing many
  140.             collections. */
  141.         for (i = 0; i < 1000000; i++) {
  142.             A* a = new (GC) A( i );
  143.             B* b = new B( i );
  144.             b = new (GC) B( i );
  145.             if (0 == i % 10) {
  146.                 B::Deleting( 1 );
  147.                 delete b;
  148.                 B::Deleting( 0 );}}
  149.  
  150.             /* Make sure the uncollectable As and Bs are still there. */
  151.         for (i = 0; i < 1000; i++) {
  152.             A* a = (A*) Undisguise( as[ i ] );
  153.             B* b = (B*) Undisguise( bs[ i ] );
  154.             a->Test( i );
  155.             delete a;
  156.             b->Test( i );
  157.             B::Deleting( 1 );
  158.             delete b;
  159.             B::Deleting( 0 );}
  160.  
  161.             /* Make sure most of the finalizable Cs and Ds have gone away. */
  162.         C::Test();
  163.         D::Test();}
  164.  
  165.     printf( "The test appears to have succeeded.\n" );
  166.     return( 0 );}
  167.     
  168.  
  169.