virtfunc-test.cc 3.4 KB

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