summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/cg/tobjsiz2.pp
blob: 817aaf8cb5e89640250645513c7d3b988ab325ea (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

{ Variation without virtual classes : no VMT }
{ here sizeof directly returns a constant value }


type
   pbaseclass = ^tbaseclass;
   pderivedclass = ^tderivedclass;

   tbaseclass = object
     x : byte;
     function getsize : longint; static;
     procedure check_size;
   end;

   tderivedclass = object(tbaseclass)
     y : byte;
   end;

const
  expected_size_for_tbaseclass = sizeof(byte);
  expected_size_for_tderivedclass = 2*sizeof(byte);

var
  basesize : longint;
  derivedsize : longint;

function tbaseclass.getsize : longint;
begin
  { self = pointer to VMT }
  getsize:=sizeof(self);
end;

procedure tbaseclass.check_size;
begin
  if getsize<>sizeof(pointer) then
    begin
      Writeln('Compiler creates garbage ',sizeof(self),'<>',sizeof(pointer));
      halt(1);
    end;
end;


var
  cb : tbaseclass;
  cd : tderivedclass;
  c1 : pbaseclass;
begin
 new(c1);

 basesize:=sizeof(cb);
 Writeln('Sizeof(cb)=',basesize);
 if basesize<>expected_size_for_tbaseclass then
   begin
     Writeln('not the expected size : ',expected_size_for_tbaseclass);
     halt(1);
   end;

 derivedsize:=sizeof(cd);
 Writeln('Sizeof(ct)=',derivedsize);
 if derivedsize<>expected_size_for_tderivedclass then
   begin
     Writeln('not the expected size : ',expected_size_for_tderivedclass);
     halt(1);
   end;

  cb.check_size;
end.