summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/cg/tclasize.pp
blob: cb94a89bd7966a3148789a15ce01057c3170b08b (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
{$ifdef fpc}{$mode objfpc}{$else}{$J+}{$endif}

type
   tbaseclass = class
     x : byte;
     class procedure virtual_class_method; virtual;
     class procedure call_virtual_class_method;
     class function getsize : longint;
     procedure check_size;
   end;

   tderivedclass = class(tbaseclass)
     y : byte;
     class procedure virtual_class_method; override;
   end;

const
  tbasecalled : boolean = false;
  tderivedcalled : boolean = false;
  has_error : boolean = false;
  expected_size_for_tbaseclass = sizeof(pointer) + sizeof(byte);
  expected_size_for_tderivedclass = sizeof(pointer) + 2*sizeof(byte);

var
  basesize : longint;
  derivedsize : longint;

class procedure tbaseclass.virtual_class_method;

begin
  Writeln('Calling tbase class class method');
  tbasecalled:=true;
  if sizeof(self)<>basesize then
    begin
      has_error:=true;
      Writeln('Error with sizeof');
    end;
end;

class function tbaseclass.getsize : longint;
begin
  getsize:=sizeof(self);
end;

procedure tbaseclass.check_size;
begin
  if sizeof(self)<>getsize then
    begin
      Writeln('Compiler creates garbage');
      has_error:=true;
    end;
end;

class procedure tbaseclass.call_virtual_class_method;
begin
  virtual_class_method;
  if getsize<>sizeof(self) then
    begin
      Writeln('Compiler creates garbage');
      has_error:=true;
    end;
end;

class procedure tderivedclass.virtual_class_method;

begin
  Writeln('Calling tderived class class method');
  tderivedcalled:=true;
  if sizeof(self)<>derivedsize then
    begin
      has_error:=true;
      Writeln('Error with sizeof');
    end;
end;

procedure reset_booleans;
begin
  tbasecalled:=false;
  tderivedcalled:=false;
end;

type
  tcl = class of tbaseclass;

var
  c1,cb : tbaseclass;
  cd : tderivedclass;
  cc : tcl;

begin
 cb:=tbaseclass.create;
 cd:=tderivedclass.create;
 c1:=tbaseclass.create;

 basesize:=sizeof(cb);
 Writeln('Sizeof(cb)=',basesize);
 if basesize<>sizeof(pointer) then
   Writeln('not the expected size : ',sizeof(pointer));
 Writeln('cb.InstanceSize=',Cb.InstanceSize);
 if cb.InstanceSize<>expected_size_for_tbaseclass then
   Writeln('not the expected size : ',expected_size_for_tbaseclass);
 Writeln('Tbaseclass.InstanceSize=',Tbaseclass.InstanceSize);
 if TBaseClass.InstanceSize<>expected_size_for_tbaseclass then
   Writeln('not the expected size : ',expected_size_for_tbaseclass);

 derivedsize:=sizeof(cd);
 Writeln('Sizeof(ct)=',derivedsize);
 if derivedsize<>sizeof(pointer) then
   Writeln('not the expected size : ',sizeof(pointer));

 cb.check_size;
 cd.check_size;

 tbaseclass.virtual_class_method;
 if not tbasecalled then
   has_error:=true;
 reset_booleans;

 tbaseclass.call_virtual_class_method;
 if not tbasecalled then
   has_error:=true;
 reset_booleans;

 tderivedclass.virtual_class_method;
 if not tderivedcalled then
   has_error:=true;
 reset_booleans;

 tderivedclass.call_virtual_class_method;
 if not tderivedcalled then
   has_error:=true;
 reset_booleans;

 c1.virtual_class_method;
 if not tbasecalled then
   has_error:=true;
 reset_booleans;

 c1.call_virtual_class_method;
 if not tbasecalled then
   has_error:=true;
 reset_booleans;

 c1.destroy;

 c1:=tderivedclass.create;

 c1.virtual_class_method;
 if not tderivedcalled then
   has_error:=true;
 reset_booleans;

 c1.call_virtual_class_method;
 if not tderivedcalled then
   has_error:=true;
 reset_booleans;

 c1.destroy;

 cc:=tbaseclass;

 cc.virtual_class_method;
 if not tbasecalled then
   has_error:=true;
 reset_booleans;

 cc.call_virtual_class_method;
 if not tbasecalled then
   has_error:=true;
 reset_booleans;

 cc:=tderivedclass;

 cc.virtual_class_method;
 if not tderivedcalled then
   has_error:=true;
 reset_booleans;

 cc.call_virtual_class_method;
 if not tderivedcalled then
   has_error:=true;
 reset_booleans;

 Writeln('Sizeof(cc)=',sizeof(cc));

 if has_error then
   begin
     Writeln('Error with class methods');
     halt(1);
   end;

end.