3ad12b896c1e32f833ac578e1e293fde60546031
[gcc.git] / libvtv / testsuite / libvtv.cc / virtfunc-test.cc
1 // { dg-do run }
2
3 /* This test script is part of GDB, the GNU debugger.
4
5 Copyright 1993, 1994, 1997, 1998, 1999, 2003, 2004,
6 Free Software Foundation, Inc.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 // Pls try the following program on virtual functions and try to do print on
23 // most of the code in main(). Almost none of them works !
24
25 //
26 // The inheritance structure is:
27 //
28 // V : VA VB
29 // A : (V)
30 // B : A
31 // D : AD (V)
32 // C : (V)
33 // E : B (V) D C
34 //
35
36 class VA
37 {
38 public:
39 int va;
40 };
41
42 class VB
43 {
44 public:
45 int vb;
46 int fvb();
47 virtual int vvb();
48 };
49
50 class V : public VA, public VB
51 {
52 public:
53 int f();
54 virtual int vv();
55 int w;
56 };
57
58 class A : virtual public V
59 {
60 public:
61 virtual int f();
62 private:
63 int a;
64 };
65
66 class B : public A
67 {
68 public:
69 int f();
70 private:
71 int b;
72 };
73
74 class C : public virtual V
75 {
76 public:
77 int c;
78 };
79
80 class AD
81 {
82 public:
83 virtual int vg() = 0;
84 };
85
86 class D : public AD, virtual public V
87 {
88 public:
89 static void s();
90 virtual int vg();
91 virtual int vd();
92 int fd();
93 int d;
94 };
95
96 class E : public B, virtual public V, public D, public C
97 {
98 public:
99 int f();
100 int vg();
101 int vv();
102 int e;
103 };
104
105 D dd;
106 D* ppd = &dd;
107 AD* pAd = &dd;
108
109 A a;
110 B b;
111 C c;
112 D d;
113 E e;
114 V v;
115 VB vb;
116
117
118 A* pAa = &a;
119 A* pAe = &e;
120
121 B* pBe = &e;
122
123 D* pDd = &d;
124 D* pDe = &e;
125
126 V* pVa = &a;
127 V* pVv = &v;
128 V* pVe = &e;
129 V* pVd = &d;
130
131 AD* pADe = &e;
132
133 E* pEe = &e;
134
135 VB* pVB = &vb;
136
137 void init()
138 {
139 a.vb = 1;
140 b.vb = 2;
141 c.vb = 3;
142 d.vb = 4;
143 e.vb = 5;
144 v.vb = 6;
145 vb.vb = 7;
146
147 d.d = 1;
148 e.d = 2;
149 }
150
151 extern "C" int printf(const char *, ...);
152
153 int all_count = 0;
154 int failed_count = 0;
155
156 #define TEST(EXPR, EXPECTED) \
157 ret = EXPR; \
158 if (ret != EXPECTED) {\
159 printf("Failed %s is %d, should be %d!\n", #EXPR, ret, EXPECTED); \
160 failed_count++; } \
161 all_count++;
162
163 int ret;
164
165 void test_calls()
166 {
167 TEST(pAe->f(), 20);
168 TEST(pAa->f(), 1);
169
170 TEST(pDe->vg(), 202);
171 TEST(pADe->vg(), 202);
172 TEST(pDd->vg(), 101);
173
174 TEST(pEe->vvb(), 411);
175
176 TEST(pVB->vvb(), 407);
177
178 TEST(pBe->vvb(), 411);
179 TEST(pDe->vvb(), 411);
180
181 TEST(pEe->vd(), 282);
182 TEST(pEe->fvb(), 311);
183
184 TEST(pEe->D::vg(), 102);
185 printf("Did %d tests, of which %d failed.\n", all_count, failed_count);
186 }
187 #ifdef usestubs
188 extern "C" {
189 void set_debug_traps();
190 void breakpoint();
191 };
192 #endif
193
194 int main()
195 {
196 #ifdef usestubs
197 set_debug_traps();
198 breakpoint();
199 #endif
200 init();
201
202 e.w = 7;
203 e.vb = 11;
204
205 test_calls();
206 return 0;
207
208 }
209
210 int A::f() {return 1;}
211 int B::f() {return 2;}
212 void D::s() {}
213 int E::f() {return 20;}
214 int D::vg() {return 100+d;}
215 int E::vg() {return 200+d;}
216 int V::f() {return 600+w;}
217 int V::vv() {return 400+w;}
218 int E::vv() {return 450+w;}
219 int D::fd() {return 250+d;}
220 int D::vd() {return 280+d;}
221 int VB::fvb() {return 300+vb;}
222 int VB::vvb() {return 400+vb;}