with debug info
[prop.git] / tests / test_gc19.cc
blob21bc59fd7b4a79a2032ccff27fd05680d80bc00d
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 500 // number of times to test
9 #define DEPTH 4 // depth of the tree
10 #define SIZE 500
12 class TREE;
14 struct Junk : public GCObject {
15 const char * name;
16 TREE * tree;
17 int junk[SIZE];
18 Junk(TREE * t) : tree(t) {}
19 void trace (GC * gc);
22 class TREE : public GCObject {
23 TREE();
24 int tag;
25 int junk[SIZE];
26 Junk * junk2;
27 Junk * junk3;
28 Junk * junk4;
29 Junk * junk5;
30 public:
31 TREE * left;
32 TREE * right;
33 TREE(TREE *l , TREE*r)
34 : left(l), right(r),
35 junk2(new Junk(this)), junk3(new Junk(this)),
36 junk4(new Junk(this)), junk5(new Junk(this))
37 { for (int i = 0; i < SIZE; i++) junk[i] = i + tag; }
38 ~TREE() {}
39 void verify() const
40 { assert(junk2->tree == this);
41 for (int i = 0; i < SIZE; i++) assert(junk[i] == i+tag);
42 if (left) left->verify();
43 if (right) right->verify();
45 virtual void trace(GC * gc) {
46 left = (TREE*)gc->trace(left);
47 right = (TREE*)gc->trace(right);
48 junk2 = (Junk*)gc->trace(junk2);
49 junk3 = (Junk*)gc->trace(junk3);
50 junk4 = (Junk*)gc->trace(junk4);
51 junk5 = (Junk*)gc->trace(junk5);
53 int size() const;
56 void Junk::trace(GC * gc) { tree = (TREE*)gc->trace(tree); }
57 TREE * tree(TREE *l, TREE *r) { return new TREE(l,r); }
60 int TREE::size() const
62 int sz = 1;
63 if (left) sz += left->size();
64 if (right) sz += right->size();
65 return sz;
68 TREE * make_tree(int depth)
70 if(depth==0) return (TREE*) 0;
71 --depth;
72 TREE * t = tree(make_tree(depth), make_tree(depth));
73 return t;
77 void do_something()
78 { TREE * t;
79 t = make_tree(DEPTH);
80 t->verify();
81 assert(t->size() == ((1 << DEPTH)-1));
84 int main()
86 cout << "Testing gc of trees\n";
87 for (int trial = 1; trial <= TRIALS; trial++) {
88 cout << "Trial " << trial << "\n" << flush;
89 do_something();
90 cout << "Trial " << trial << " has passed\n" << flush;
92 return 0;