summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/units/system/tassignd.pp
blob: 0c0c50224163c80792ac4a47d23eae7ef7734e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{ Copyright (c) Carl Eric Codere            }
{ This program tests the assigned() routine }
{ Tested against Delphi 6 Personal Edition  }
{$ifdef fpc}
{$mode objfpc}
{$endif}

type

  tmyobject = object
    procedure myroutine(x: byte);
  end;

  tmyclass = class
    procedure myroutine(x: byte);
  end;


  tobjectmethod = procedure (x: byte) of object;
  tclassmethod = procedure (x: byte) of object;
  tproc = procedure (x: byte);


  type
    objpointer = packed record
      _method : pointer;
      _vmt : pointer;
    end;

var
  myobject : tmyobject;
  myclass : tmyclass;

  procedure fail;
   begin
     WriteLn('Failure!');
     halt(1);
   end;

  procedure mydummyproc(x: byte);
   begin
   end;

  function getpointer : pointer;
   begin
     getpointer := nil;
   end;

  function getprocpointer : tproc;
   begin
     getprocpointer:=@mydummyproc;
   end;

{$ifdef fpc}
  function getobjmethodpointer : tobjectmethod;
   begin
     getobjmethodpointer:=@myobject.myroutine;
   end;

  function getclamethodpointer : tclassmethod;
   begin
     getclamethodpointer:=@myclass.myroutine;
   end;
{$endif}

  procedure tmyclass.myroutine(x: byte);
   begin
   end;

  procedure tmyobject.myroutine(x: byte);
   begin
   end;

  { possible chocies (fixes branch only)  :
      LOC_REGISTER
      LOC_REFERENCE
    second branch handles this in a generic way
  }
var
  objmethod : tobjectmethod;
  clamethod : tclassmethod;
  proc : tproc;
  p : pointer;
  x: array[1..8] of integer;
  _result : boolean;
  ptrrecord : objpointer;
Begin
  myclass := tmyclass.create;
  Write('Assigned(pointer) tests...');
  _result := true;
  p:=@x;
  if not assigned(p) then
    _result := false;
  p:=nil;
  if assigned(p) then
    _result := false;
{$ifdef fpc}
  if assigned(getpointer) then
    _result := false;
{$endif}

  if _result then
    WriteLn('Success!')
  else
    fail;
  {*******************************************************}
  Write('Assigned(proc) tests...');
  _result := true;
  proc:=@mydummyproc;
  if not assigned(proc) then
    _result := false;
  proc:=nil;
{$ifdef fpc}
  if assigned(proc) then
    _result := false;
  if not assigned(getprocpointer) then
    _result := false;
{$endif}
  if _result then
    WriteLn('Success!')
  else
    fail;
  {*******************************************************}
  Write('Assigned(object method) tests...');
  _result := true;
{$ifdef fpc}
  objmethod:=@myobject.myroutine;
  if not assigned(objmethod) then
    _result := false;
{$endif}
  objmethod:=nil;
  if assigned(objmethod) then
    _result := false;
  { lets put the individual fields to nil
    This is a hack which should never occur
  }
  objmethod:={$ifdef fpc}@{$endif}myobject.myroutine;
  move(objmethod, ptrrecord, sizeof(ptrrecord));
  ptrrecord._vmt := nil;
  move(ptrrecord, objmethod, sizeof(ptrrecord));
  if not assigned(objmethod) then
    _result := false;

  objmethod:={$ifdef fpc}@{$endif}myobject.myroutine;
  move(objmethod, ptrrecord, sizeof(ptrrecord));
  ptrrecord._method := nil;
  move(ptrrecord, objmethod, sizeof(ptrrecord));
  if assigned(objmethod) then
    _result := false;

{$ifdef fpc}
  if not assigned(getobjmethodpointer) then
    _result := false;
{$endif}

  if _result then
    WriteLn('Success!')
  else
    fail;
  {*******************************************************}
  Write('Assigned(class method) tests...');
  _result := true;
{$ifdef fpc}
  clamethod:=@myclass.myroutine;
  if not assigned(clamethod) then
    _result := false;
{$endif}
  clamethod:=nil;
  if assigned(clamethod) then
    _result := false;
  { lets put the individual fields to nil
    This is a hack which should never occur
  }
  clamethod:={$ifdef fpc}@{$endif}myclass.myroutine;
  move(clamethod, ptrrecord, sizeof(ptrrecord));
  ptrrecord._vmt := nil;
  move(ptrrecord, clamethod, sizeof(ptrrecord));
  if not assigned(clamethod) then
    _result := false;

  clamethod:={$ifdef fpc}@{$endif}myclass.myroutine;
  move(clamethod, ptrrecord, sizeof(ptrrecord));
  ptrrecord._method := nil;
  move(ptrrecord, clamethod, sizeof(ptrrecord));
  if assigned(clamethod) then
    _result := false;

{$ifdef fpc}
  if not assigned(getclamethodpointer) then
    _result := false;
{$endif}

  if _result then
    WriteLn('Success!')
  else
    fail;

end.