initial
[prop.git] / tests / test_gc21.cc
blob9b04c8fef45b85c2349c0f3277573adfeab82beb
1 #include <iostream.h>
2 #include <assert.h>
3 #include <AD/gc/gc.h>
4 #include <AD/gc/markswp.h>
5 #include <AD/gc/gcobject.h>
6 #include <AD/gc/gcheaps.h>
8 #define TRIALS 50 // number of times to test
9 #define DEPTH 6 // depth of the tree
10 #define JUNKSIZE 500
11 //#define JUNKSIZE 2000
13 MarkSweepGC heap1;
14 MarkSweepGC heap2;
16 class TREE : public GCObject {
17 static int tagcnt;
18 int tag;
19 int junk[JUNKSIZE];
20 public:
21 TREE * left_child;
22 TREE * right_child;
23 TREE(TREE * l, TREE * r) : left_child(l), right_child(r)
25 tag = 0;
26 tag = tagcnt;
27 tagcnt += 5*JUNKSIZE+17;
28 for (int i = 0; i < JUNKSIZE; i++) junk[i] = i + tag;
30 ~TREE() {}
31 void verify() const
32 { int i;
33 for (i = 0; i < JUNKSIZE; i++) assert(junk[i] == i+tag);
34 if (left_child) left_child->verify();
35 if (right_child) right_child->verify();
37 void trace(GC * gc)
39 left_child = (TREE*)gc->trace(left_child);
40 right_child = (TREE*)gc->trace(right_child);
42 int size() const;
45 int TREE::tagcnt = 0;
47 int TREE::size() const
49 int sz = 1;
50 if (left_child) sz += left_child->size();
51 if (right_child) sz += right_child->size();
52 return sz;
55 TREE * make_tree_1(int);
56 TREE * make_tree_2(int);
58 TREE * make_tree_1(int depth)
60 if(depth==0) return (TREE*) 0;
61 --depth;
62 TREE * l = make_tree_2(depth);
63 TREE * r = make_tree_2(depth);
64 return new (heap1) TREE(l,r);
68 TREE * make_tree_2(int depth)
70 if(depth==0) return (TREE*) 0;
71 --depth;
72 TREE * l = make_tree_1(depth);
73 TREE * r = make_tree_1(depth);
74 return new (heap2) TREE(l,r);
78 void do_something()
79 { TREE * t;
80 t = make_tree_1(DEPTH);
81 t->verify();
82 assert(t->size() == ((1 << DEPTH)-1));
85 int main()
87 cout << "Testing gc of trees\n";
88 for (int trial = 1; trial <= TRIALS; trial++) {
89 cout << "Trial " << trial << "\n" << flush;
90 do_something();
91 cout << "Trial " << trial << " has passed\n" << flush;
93 cout << "Final cleanup\n" << flush;
94 heap1.collect();
95 heap2.collect();
96 return 0;