more typename fixes
[prop.git] / tests / test_gc22.cc
blob6d589c8edc075e19a2a052b720e73a24ce04a02b
1 #include <iostream.h>
2 #include <assert.h>
3 #include <AD/gc/gc.h>
4 #include <AD/gc/markswp.h>
5 #include <AD/gc/bgc.h>
6 #include <AD/gc/gcobject.h>
7 #include <AD/gc/gcheaps.h>
9 #define TRIALS 50 // number of times to test
10 #define DEPTH 6 // depth of the tree
11 #define JUNKSIZE 500
12 //#define JUNKSIZE 2000
14 MarkSweepGC heap1;
15 BGC heap2;
16 BGC& heap3 = bgc;
18 class TREE : public GCObject {
19 static int tagcnt;
20 int tag;
21 int junk[JUNKSIZE];
22 public:
23 TREE * left_child;
24 TREE * right_child;
25 TREE(TREE * l, TREE * r) : left_child(l), right_child(r)
27 tag = 0;
28 tag = tagcnt;
29 tagcnt += 5*JUNKSIZE+17;
30 for (int i = 0; i < JUNKSIZE; i++) junk[i] = i + tag;
32 ~TREE() {}
33 void verify() const
34 { int i;
35 for (i = 0; i < JUNKSIZE; i++) assert(junk[i] == i+tag);
36 if (left_child) left_child->verify();
37 if (right_child) right_child->verify();
39 void trace(GC * gc)
41 left_child = (TREE*)gc->trace(left_child);
42 right_child = (TREE*)gc->trace(right_child);
44 int size() const;
47 int TREE::tagcnt = 0;
49 int TREE::size() const
51 int sz = 1;
52 if (left_child) sz += left_child->size();
53 if (right_child) sz += right_child->size();
54 return sz;
57 TREE * make_tree_1(int);
58 TREE * make_tree_2(int);
59 TREE * make_tree_3(int);
61 TREE * make_tree_1(int depth)
63 if(depth==0) return (TREE*) 0;
64 --depth;
65 TREE * l = make_tree_2(depth);
66 TREE * r = make_tree_2(depth);
67 return new (heap1) TREE(l,r);
71 TREE * make_tree_2(int depth)
73 if(depth==0) return (TREE*) 0;
74 --depth;
75 TREE * l = make_tree_3(depth);
76 TREE * r = make_tree_3(depth);
77 return new (heap2) TREE(l,r);
80 TREE * make_tree_3(int depth)
82 if(depth==0) return (TREE*) 0;
83 --depth;
84 TREE * l = make_tree_1(depth);
85 TREE * r = make_tree_1(depth);
86 return new (heap3) TREE(l,r);
89 void do_something()
90 { TREE * t;
91 t = make_tree_1(DEPTH);
92 t->verify();
93 assert(t->size() == ((1 << DEPTH)-1));
96 int main()
98 cout << "Testing gc of trees\n";
99 for (int trial = 1; trial <= TRIALS; trial++) {
100 cout << "Trial " << trial << "\n" << flush;
101 do_something();
102 cout << "Trial " << trial << " has passed\n" << flush;
104 cout << "Final cleanup\n" << flush;
105 heap1.collect();
106 heap2.collect();
107 heap3.collect();
108 return 0;