Merge remote branch 'master'
[prop.git] / tests / test_gc7.cc
blob567a7ff0e958d79f9e22aaec9085f9763222852a
1 //////////////////////////////////////////////////////////////////////////////
2 // Testing the ability to trace cross heap pointers.
3 // We'll allocate a list in two different collectors.
4 // Garbage collection should preserve the shape of the list.
5 //////////////////////////////////////////////////////////////////////////////
7 #include <iostream.h>
8 #include <assert.h>
9 #include <AD/gc/gc.h>
10 #include <AD/gc/bgc.h>
11 #include <AD/gc/gcobject.h>
12 #include <AD/gc/gcheaps.h>
14 BGC heap1;
15 BGC& heap2 = bgc;
17 #define TRIALS 5 // number of times to test
18 #define LENGTH 100 // length of list to allocate
20 class LIST : public GCObject {
21 int tag;
22 int junk[500];
23 public:
24 LIST * next;
25 LIST(LIST * n) : next(n)
26 { for (int i = 0; i < 500; i++) junk[i] = i + tag; }
27 ~LIST() {}
28 void verify() const
29 { for (int i = 0; i < 500; i++) assert(junk[i] == i+tag);
30 if (next) next->verify();
32 void trace(GC * gc) { next = (LIST*)gc->trace(next); }
35 int length(LIST * l)
36 { int len;
37 for (len = 0; l; l = l->next)
38 len++;
39 return len;
43 void do_something()
44 { LIST * l = 0;
45 for (int i = 0; i < LENGTH; i++) {
46 l = new (heap1) LIST(l);
47 new (heap1) LIST(l); // some garbage for heap1
48 new (heap1) LIST(l); // some garbage for heap1
49 new (heap1) LIST(l); // some garbage for heap1
50 l = new (heap2) LIST(l);
51 new (heap2) LIST(l); // some garbage for heap2
52 new (heap2) LIST(l); // some garbage for heap2
53 new (heap2) LIST(l); // some garbage for heap2
55 l->verify();
56 assert(length(l) == LENGTH * 2);
59 int main()
61 cout << "Testing crossheap pointers\n";
62 for (int trial = 1; trial <= TRIALS; trial++) {
63 cout << "Trial " << trial << "\n" << flush;
64 do_something();
65 cout << "Trial " << trial << " has passed\n" << flush;
67 cout << "Now cleaning up\n";
68 heap1.collect();
69 heap2.collect();
70 return 0;