Merge branch 'makefiles'
[prop.git] / tests / test_gc14.cc
blobc0669d28cdc01409121f37021767d2d80e7844f7
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/gcobject.h>
11 #include <AD/gc/gcheaps.h>
12 #include <AD/gc/markswp.h>
14 MarkSweepGC heap1, heap2;
16 #define TRIALS 5 // number of times to test
17 #define LENGTH 100 // length of list to allocate
19 class LIST : public GCObject {
20 int tag;
21 int junk[500];
22 public:
23 LIST * next;
24 LIST(LIST * n) : next(n)
25 { for (int i = 0; i < 500; i++) junk[i] = i + tag; }
26 ~LIST() {}
27 void verify() const
28 { for (int i = 0; i < 500; i++) assert(junk[i] == i+tag);
29 if (next) next->verify();
31 void trace(GC * gc) { next = (LIST*)gc->trace(next); }
34 int length(LIST * l)
35 { int len;
36 for (len = 0; l; l = l->next)
37 len++;
38 return len;
42 void do_something()
43 { LIST * l = 0;
44 for (int i = 0; i < LENGTH; i++) {
45 l = new (heap1) LIST(l);
46 new (heap1) LIST(l); // some garbage for heap1
47 new (heap1) LIST(l); // some garbage for heap1
48 new (heap1) LIST(l); // some garbage for heap1
49 l = new (heap2) LIST(l);
50 new (heap2) LIST(l); // some garbage for heap2
51 new (heap2) LIST(l); // some garbage for heap2
52 new (heap2) LIST(l); // some garbage for heap2
54 l->verify();
55 assert(length(l) == LENGTH * 2);
58 int main()
60 cout << "Testing crossheap pointers\n";
61 for (int trial = 1; trial <= TRIALS; trial++) {
62 cout << "Trial " << trial << "\n" << flush;
63 do_something();
64 cout << "Trial " << trial << " has passed\n" << flush;
66 cout << "Now cleaning up\n";
67 heap1.collect();
68 heap2.collect();
69 return 0;