tassignd.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. { Copyright (c) Carl Eric Codere }
  2. { This program tests the assigned() routine }
  3. { Tested against Delphi 6 Personal Edition }
  4. {$ifdef fpc}
  5. {$mode objfpc}
  6. {$endif}
  7. type
  8. tmyobject = object
  9. procedure myroutine(x: byte);
  10. end;
  11. tmyclass = class
  12. procedure myroutine(x: byte);
  13. end;
  14. tobjectmethod = procedure (x: byte) of object;
  15. tclassmethod = procedure (x: byte) of object;
  16. tproc = procedure (x: byte);
  17. type
  18. objpointer = packed record
  19. _method : pointer;
  20. _vmt : pointer;
  21. end;
  22. var
  23. myobject : tmyobject;
  24. myclass : tmyclass;
  25. procedure fail;
  26. begin
  27. WriteLn('Failure!');
  28. halt(1);
  29. end;
  30. procedure mydummyproc(x: byte);
  31. begin
  32. end;
  33. function getpointer : pointer;
  34. begin
  35. getpointer := nil;
  36. end;
  37. function getprocpointer : tproc;
  38. begin
  39. getprocpointer:=@mydummyproc;
  40. end;
  41. {$ifdef fpc}
  42. function getobjmethodpointer : tobjectmethod;
  43. begin
  44. getobjmethodpointer:[email protected];
  45. end;
  46. function getclamethodpointer : tclassmethod;
  47. begin
  48. getclamethodpointer:[email protected];
  49. end;
  50. {$endif}
  51. procedure tmyclass.myroutine(x: byte);
  52. begin
  53. end;
  54. procedure tmyobject.myroutine(x: byte);
  55. begin
  56. end;
  57. { possible chocies (fixes branch only) :
  58. LOC_REGISTER
  59. LOC_REFERENCE
  60. second branch handles this in a generic way
  61. }
  62. var
  63. objmethod : tobjectmethod;
  64. clamethod : tclassmethod;
  65. proc : tproc;
  66. p : pointer;
  67. x: array[1..8] of integer;
  68. _result : boolean;
  69. ptrrecord : objpointer;
  70. Begin
  71. myclass := tmyclass.create;
  72. Write('Assigned(pointer) tests...');
  73. _result := true;
  74. p:=@x;
  75. if not assigned(p) then
  76. _result := false;
  77. p:=nil;
  78. if assigned(p) then
  79. _result := false;
  80. {$ifdef fpc}
  81. if assigned(getpointer) then
  82. _result := false;
  83. {$endif}
  84. if _result then
  85. WriteLn('Success!')
  86. else
  87. fail;
  88. {*******************************************************}
  89. Write('Assigned(proc) tests...');
  90. _result := true;
  91. proc:=@mydummyproc;
  92. if not assigned(proc) then
  93. _result := false;
  94. proc:=nil;
  95. {$ifdef fpc}
  96. if assigned(proc) then
  97. _result := false;
  98. if not assigned(getprocpointer) then
  99. _result := false;
  100. {$endif}
  101. if _result then
  102. WriteLn('Success!')
  103. else
  104. fail;
  105. {*******************************************************}
  106. Write('Assigned(object method) tests...');
  107. _result := true;
  108. {$ifdef fpc}
  109. objmethod:[email protected];
  110. if not assigned(objmethod) then
  111. _result := false;
  112. {$endif}
  113. objmethod:=nil;
  114. if assigned(objmethod) then
  115. _result := false;
  116. { lets put the individual fields to nil
  117. This is a hack which should never occur
  118. }
  119. objmethod:={$ifdef fpc}@{$endif}myobject.myroutine;
  120. move(objmethod, ptrrecord, sizeof(ptrrecord));
  121. ptrrecord._vmt := nil;
  122. move(ptrrecord, objmethod, sizeof(ptrrecord));
  123. if not assigned(objmethod) then
  124. _result := false;
  125. objmethod:={$ifdef fpc}@{$endif}myobject.myroutine;
  126. move(objmethod, ptrrecord, sizeof(ptrrecord));
  127. ptrrecord._method := nil;
  128. move(ptrrecord, objmethod, sizeof(ptrrecord));
  129. if assigned(objmethod) then
  130. _result := false;
  131. {$ifdef fpc}
  132. if not assigned(getobjmethodpointer) then
  133. _result := false;
  134. {$endif}
  135. if _result then
  136. WriteLn('Success!')
  137. else
  138. fail;
  139. {*******************************************************}
  140. Write('Assigned(class method) tests...');
  141. _result := true;
  142. {$ifdef fpc}
  143. clamethod:[email protected];
  144. if not assigned(clamethod) then
  145. _result := false;
  146. {$endif}
  147. clamethod:=nil;
  148. if assigned(clamethod) then
  149. _result := false;
  150. { lets put the individual fields to nil
  151. This is a hack which should never occur
  152. }
  153. clamethod:={$ifdef fpc}@{$endif}myclass.myroutine;
  154. move(clamethod, ptrrecord, sizeof(ptrrecord));
  155. ptrrecord._vmt := nil;
  156. move(ptrrecord, clamethod, sizeof(ptrrecord));
  157. if not assigned(clamethod) then
  158. _result := false;
  159. clamethod:={$ifdef fpc}@{$endif}myclass.myroutine;
  160. move(clamethod, ptrrecord, sizeof(ptrrecord));
  161. ptrrecord._method := nil;
  162. move(ptrrecord, clamethod, sizeof(ptrrecord));
  163. if assigned(clamethod) then
  164. _result := false;
  165. {$ifdef fpc}
  166. if not assigned(getclamethodpointer) then
  167. _result := false;
  168. {$endif}
  169. if _result then
  170. WriteLn('Success!')
  171. else
  172. fail;
  173. end.