tclasize.pp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. {$ifdef fpc}{$mode objfpc}{$else}{$J+}{$endif}
  2. type
  3. tbaseclass = class
  4. x : byte;
  5. class procedure virtual_class_method; virtual;
  6. class procedure call_virtual_class_method;
  7. class function getsize : longint;
  8. procedure check_size;
  9. end;
  10. tderivedclass = class(tbaseclass)
  11. y : byte;
  12. class procedure virtual_class_method; override;
  13. end;
  14. const
  15. tbasecalled : boolean = false;
  16. tderivedcalled : boolean = false;
  17. has_error : boolean = false;
  18. expected_size_for_tbaseclass = sizeof(pointer) + sizeof(byte);
  19. expected_size_for_tderivedclass = sizeof(pointer) + 2*sizeof(byte);
  20. var
  21. basesize : longint;
  22. derivedsize : longint;
  23. class procedure tbaseclass.virtual_class_method;
  24. begin
  25. Writeln('Calling tbase class class method');
  26. tbasecalled:=true;
  27. if sizeof(self)<>basesize then
  28. begin
  29. has_error:=true;
  30. Writeln('Error with sizeof');
  31. end;
  32. end;
  33. class function tbaseclass.getsize : longint;
  34. begin
  35. getsize:=sizeof(self);
  36. end;
  37. procedure tbaseclass.check_size;
  38. begin
  39. if sizeof(self)<>getsize then
  40. begin
  41. Writeln('Compiler creates garbage');
  42. has_error:=true;
  43. end;
  44. end;
  45. class procedure tbaseclass.call_virtual_class_method;
  46. begin
  47. virtual_class_method;
  48. if getsize<>sizeof(self) then
  49. begin
  50. Writeln('Compiler creates garbage');
  51. has_error:=true;
  52. end;
  53. end;
  54. class procedure tderivedclass.virtual_class_method;
  55. begin
  56. Writeln('Calling tderived class class method');
  57. tderivedcalled:=true;
  58. if sizeof(self)<>derivedsize then
  59. begin
  60. has_error:=true;
  61. Writeln('Error with sizeof');
  62. end;
  63. end;
  64. procedure reset_booleans;
  65. begin
  66. tbasecalled:=false;
  67. tderivedcalled:=false;
  68. end;
  69. type
  70. tcl = class of tbaseclass;
  71. var
  72. c1,cb : tbaseclass;
  73. cd : tderivedclass;
  74. cc : tcl;
  75. begin
  76. cb:=tbaseclass.create;
  77. cd:=tderivedclass.create;
  78. c1:=tbaseclass.create;
  79. basesize:=sizeof(cb);
  80. Writeln('Sizeof(cb)=',basesize);
  81. if basesize<>sizeof(pointer) then
  82. Writeln('not the expected size : ',sizeof(pointer));
  83. Writeln('cb.InstanceSize=',Cb.InstanceSize);
  84. if cb.InstanceSize<>expected_size_for_tbaseclass then
  85. Writeln('not the expected size : ',expected_size_for_tbaseclass);
  86. Writeln('Tbaseclass.InstanceSize=',Tbaseclass.InstanceSize);
  87. if TBaseClass.InstanceSize<>expected_size_for_tbaseclass then
  88. Writeln('not the expected size : ',expected_size_for_tbaseclass);
  89. derivedsize:=sizeof(cd);
  90. Writeln('Sizeof(ct)=',derivedsize);
  91. if derivedsize<>sizeof(pointer) then
  92. Writeln('not the expected size : ',sizeof(pointer));
  93. cb.check_size;
  94. cd.check_size;
  95. tbaseclass.virtual_class_method;
  96. if not tbasecalled then
  97. has_error:=true;
  98. reset_booleans;
  99. tbaseclass.call_virtual_class_method;
  100. if not tbasecalled then
  101. has_error:=true;
  102. reset_booleans;
  103. tderivedclass.virtual_class_method;
  104. if not tderivedcalled then
  105. has_error:=true;
  106. reset_booleans;
  107. tderivedclass.call_virtual_class_method;
  108. if not tderivedcalled then
  109. has_error:=true;
  110. reset_booleans;
  111. c1.virtual_class_method;
  112. if not tbasecalled then
  113. has_error:=true;
  114. reset_booleans;
  115. c1.call_virtual_class_method;
  116. if not tbasecalled then
  117. has_error:=true;
  118. reset_booleans;
  119. c1.destroy;
  120. c1:=tderivedclass.create;
  121. c1.virtual_class_method;
  122. if not tderivedcalled then
  123. has_error:=true;
  124. reset_booleans;
  125. c1.call_virtual_class_method;
  126. if not tderivedcalled then
  127. has_error:=true;
  128. reset_booleans;
  129. c1.destroy;
  130. cc:=tbaseclass;
  131. cc.virtual_class_method;
  132. if not tbasecalled then
  133. has_error:=true;
  134. reset_booleans;
  135. cc.call_virtual_class_method;
  136. if not tbasecalled then
  137. has_error:=true;
  138. reset_booleans;
  139. cc:=tderivedclass;
  140. cc.virtual_class_method;
  141. if not tderivedcalled then
  142. has_error:=true;
  143. reset_booleans;
  144. cc.call_virtual_class_method;
  145. if not tderivedcalled then
  146. has_error:=true;
  147. reset_booleans;
  148. Writeln('Sizeof(cc)=',sizeof(cc));
  149. if has_error then
  150. begin
  151. Writeln('Error with class methods');
  152. halt(1);
  153. end;
  154. end.