Merge remote branch 'master'
[prop.git] / tests / test_gc20.cc
bloba2b5899b4ad11bb8ddd277b4faaf60467d86dd8b
1 #include <iostream.h>
2 #include <assert.h>
3 #include <AD/gc/gc.h>
4 #include <AD/gc/bgc.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 BGC heap1;
14 //BGC& heap1 = bgc;
15 BGC& heap2 = bgc;
17 class TREE : public GCObject {
18 static int tagcnt;
19 int tag;
20 int junk[JUNKSIZE];
21 public:
22 TREE * left_child;
23 TREE * right_child;
24 TREE(TREE * l, TREE * r) : left_child(l), right_child(r)
26 tag = 0;
27 tag = tagcnt;
28 tagcnt += 5*JUNKSIZE+17;
29 for (int i = 0; i < JUNKSIZE; i++) junk[i] = i + tag;
31 ~TREE() {}
32 void verify() const
33 { int i;
34 for (i = 0; i < JUNKSIZE; i++) assert(junk[i] == i+tag);
35 if (left_child) left_child->verify();
36 if (right_child) right_child->verify();
38 void trace(GC * gc)
40 left_child = (TREE*)gc->trace(left_child);
41 right_child = (TREE*)gc->trace(right_child);
43 int size() const;
46 int TREE::tagcnt = 0;
48 int TREE::size() const
50 int sz = 1;
51 if (left_child) sz += left_child->size();
52 if (right_child) sz += right_child->size();
53 return sz;
56 TREE * make_tree_1(int);
57 TREE * make_tree_2(int);
59 TREE * make_tree_1(int depth)
61 if(depth==0) return (TREE*) 0;
62 --depth;
63 TREE * l = make_tree_2(depth);
64 TREE * r = make_tree_2(depth);
65 return new (heap1) TREE(l,r);
69 TREE * make_tree_2(int depth)
71 if(depth==0) return (TREE*) 0;
72 --depth;
73 TREE * l = make_tree_1(depth);
74 TREE * r = make_tree_1(depth);
75 return new (heap2) TREE(l,r);
79 void do_something()
80 { TREE * t;
81 t = make_tree_1(DEPTH);
82 t->verify();
83 assert(t->size() == ((1 << DEPTH)-1));
86 int main()
88 cout << "Testing gc of trees\n";
89 for (int trial = 1; trial <= TRIALS; trial++) {
90 cout << "Trial " << trial << "\n" << flush;
91 do_something();
92 cout << "Trial " << trial << " has passed\n" << flush;
94 cout << "Final cleanup\n" << flush;
95 heap1.collect();
96 heap2.collect();
97 return 0;