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