Rune - Content-locking work 1/2
[rune.git] / tests / reftype.d
1 #!/usr/local/bin/rune -x
2 #
3 #       Demonstrate reference types
4
5 limport "sys";
6 import "stdio";
7
8 alias stdio.File *stdout = stdio.stdout;
9
10 class Fubar {
11         int a = 1;
12         int b = 2;
13         int c = 3;
14         int addme(Fubar @this) {
15                 stdio.stdout->show("addme-Fubar:", this->a, this->b, this->c);
16                 return(this->a + this->b + this->c);
17         }
18         method int addme2() {
19                 return(this.a + this.b + this.c);
20         }
21         destructor method void killme() {
22                 stdio.stdout->show("DESTRUCTED");
23         }
24 }
25
26 subclass Fubar as SubFubar {
27         refine int a;
28         refine int addme(Fubar @this) {
29                 return(this->a + this->b + this->c + 1);
30         }
31         refine method int addme2() {
32                 return(this.a + this.b + this.c + 1);
33         }
34 }
35
36 subclass Fubar as FuA5 {
37         refine int a = 5;
38         int x = 99;
39 }
40 subclass Fubar as FuA6 {
41         refine int a = 6;
42         int x = 99;
43         int y = 99;
44 }
45
46 Fubar @base;
47
48 void
49 addList(Fubar @ptr)
50 {
51         base = ptr;
52         stdout->show("value-std", base->addme(base));
53         stdout->show("value-method", base->addme2());
54 }
55
56 # switch main and altmain to test simple referencing, else we do the
57 # more complex test.
58 #
59 int
60 altmain(int ac, char **av)
61 {
62         int x;
63         Fubar *s1;
64         s1.new();
65         s1->addme(s1);
66         s1 = NULL;
67         return(1);
68 }
69
70 int
71 main(int ac, char **av)
72 {
73         Fubar @fu;
74         FuA5 fua5;
75         FuA6 fua6;
76         Fubar *s1;
77         SubFubar *s2;
78         int i;
79
80         s1.new();
81         s2.new();
82         s1->a = 1;
83         s2->b = 1;
84         stdout->show("s1 direct-value-std (should be 6):", s1->addme(s1));
85         stdout->show("s1 direct-value-method (should be 6):", s1->addme2());
86         stdout->show("s2 direct-value-std (should be 5):", s2->addme(s2));
87         stdout->show("s2 direct-value-method (should be 5):", s2->addme2());
88
89         # XXX addList calls will crash on s1->addme (unimplemented method
90         # extraction for now, remove this comment when this is implemented)
91         #
92         addList(s1);
93         addList(s2);
94
95         fu = &fua5;
96         stdout->show("fua5 (should be 5):", fu->a);
97         fu = &fua6;
98         stdout->show("fua6 (should be 6):", fu->a);
99         fu = &fua5;
100         stdout->show("fua5 (should be 5):", fu->a);
101 }