Debian Codegraph @

tests/code/testcases/testoldfpcbugs.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestOldFPCBugs" -*-
2 {
3 Copyright 2004-2021 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { A really old test unit, testing some FPC 1.0.x bugs (or, sometimes,
18 just some less-visible features that I wanted to make sure work right,
19 and they did). They are all fixed, since a long long time. }
20 unit TestOldFPCBugs;
21
22 interface
23
24 uses CastleTester;
25
26 type
27 TTestOldFPCBugs = class(TCastleTestCase)
28 published
29 procedure TestInherited;
30 procedure TestMethodPass;
31 procedure TestCompareMemBug;
32 procedure TestFormatIncompatibility;
33 procedure TestSizeOfObject;
34 procedure TestOthers;
35 procedure TestSwapEndian;
36 end;
37
38 implementation
39
40 uses SysUtils;
41
42 { TestInherited -------------------------------------------------------------- }
43
44 { sprawdzalem tylko czy freepascal na pewno dziala tak :
45 - wywolanie kontruktora/destruktora z wnetrza obiektu nie powoduje
46 zadnej alokacji/dealokacji pamieci na obiekt czy na jego VMT.
47 Wywoluje to tylko zawarty tam normalny kod.
48 - dopiero wywolanie konstrukt/destrukt z zewnatrz powoduje ze
49 najpierw przydzielana jest pamiec dla obiektu, jego VMT i czego tam
50 jeszcze trzeba, a POTEM wywolywany jest kod konstruktora obiektu
51 (w ktorym moze ale de facto nie musi byc wywolanie inherited -
52 to malo eleganckie pisac obiekty ktore nie wywoluja inherited
53 w konstrukt/destrukt, ale jesli wiesz ze kod w inherited konstrukt/destrukt
54 nie jest ci potrzebny to nie musisz wywolywac inherited)
55 analogicznie dla destruktora
56 }
57
58 type
59 TObj=class
60 a:integer;
61 constructor Create;
62 destructor Destroy; override;
63 end;
64
65 constructor TObj.Create;
66 begin
67 a:=42;
68 inherited;
69 end;
70
71 destructor TObj.Destroy;
72 var s:string;
73 begin
74 inherited;
75
76 {do some memory allocs/frees}
77 s:='blablabla';
78 if s<>'' then s:='popoty ' + s ;
79
80 {check value of a}
81 if a <> 42 then
82 raise Exception.Create('a <> 42');
83 end;
84
85 procedure TTestOldFPCBugs.TestInherited;
86 var obj:TObj;
87 begin
88 obj:=TObj.Create;
89 FreeAndNil(obj);
90 end;
91
92 { TestAbsProc ---------------------------------------------------------------- }
93
94 { fails in 1.0.6 (corrected in 1.0.7/10) }
95
96 { TestAbsProc needs not to be called, it will fail at compilation. }
97 procedure TestAbsProc(mm:TProcedure);
98 var m2:array[0..1]of Pointer absolute mm;
99 begin
100 { do something on "m2" variable }
101 if m2[0]=nil then Exit;
102 end;
103
104 { MethodPass ----------------------------------------------------------------- }
105
106 { fails in 1.0.10; reported to fpc-devel lists }
107
108 type
109 TProcOfObj=procedure of object;
110 PMethod = ^TMethod;
111
112 { we could write the procedure below as
113 procedure Proc(mm:TProcOfObj);
114 var m2:TMethod absolute mm;
115 begin
116 AssertTrue(m2.Code <> nil);
117 end;
118 but it would require TEST_ABSOLUTE_PROCEDURE to pass (and we want to
119 test these two things _separately_.)
120 }
121 procedure Proc(mm:TProcOfObj);
122 begin
123 if PMethod(@mm)^.Code = nil then
124 raise Exception.Create('PMethod(@mm)^.Code = nil');
125 end;
126
127 type
128 TObjMM=class
129 procedure MyMethod;
130 procedure CallProcWithMyMethod;
131 end;
132
133 procedure TObjMM.MyMethod; begin end;
134 procedure TObjMM.CallProcWithMyMethod; begin Proc(@MyMethod) end;
135
136 procedure TTestOldFPCBugs.TestMethodPass;
137 var o:TObjMM;
138 begin
139 o:=TObjMM.Create;
140 try
141 o.CallProcWithMyMethod;
142 finally o.Free end;
143 end;
144
145 { TestCallFuncOfObject ------------------------------------------------------- }
146
147 { fails in 1.1 (snapshot at 23.07.2003); submitted to bug form }
148
149 {$warnings off}
150 procedure TestCallFuncOfObject;
151 { this is a compile-time bug; do not call this procedure }
152 type
153 TFuncByObject = function(i:Integer):boolean of object;
154 var F:TFuncByObject;
155 begin
156 F(1);
157 end;
158 {$warnings on}
159
160 { TestCompareMemBug ---------------------------------------------------------- }
161
162 procedure TTestOldFPCBugs.TestCompareMemBug;
163 var b1,b2:array[0..1000]of byte;
164 begin
165 { if CompareMem(p1, p2, 0) would work good then values for p1 and p2
166 should be ignored. But, since there is a bug, they will not be ignored
167 so we have to provide valid pointers for p1 and p2 or we will get
168 AccessViolation. }
169 AssertTrue(CompareMem(@b1, @b2, 0));
170 end;
171
172 procedure TTestOldFPCBugs.TestFormatIncompatibility;
173 begin
174 AssertTrue(Format('%d %d %0:d %d', [0, 1, 2, 3]) = '0 1 0 1');
175 end;
176
177 { TestSizeOfObject ----------------------------------------------------------- }
178
179 { fails in some 1.0.7 versions and in 1.0.10
180 Reported to fpc-devel lists. }
181
182 const D = SizeOf(TObject);
183 procedure TTestOldFPCBugs.TestSizeOfObject;
184 begin
185 AssertTrue(D = SizeOf(Pointer));
186 end;
187
188 { TestOthers ----------------------------------------------------------------- }
189
190 { przeciazanie procedur gdy jedna wersja nie bierze zadnych arg.
191 To dziala tylko pod 1.1, 1.0.x musza byc odpowiednio zmodyfikowane
192 aby to dzialalo. (wiele mojego kodu wymaga zeby to dzialalo,
193 juz CastleUtils.) }
194 function TestProc(arg:integer):boolean; overload; begin result:=true end;
195 function TestProc :boolean; overload; begin result:=false end;
196
197 procedure TTestOldFPCBugs.TestOthers;
198 var b1,b2:array[0..1000]of byte;
199 begin
200 AssertTrue(not TestProc);
201 AssertTrue(TestProc(2));
202
203 AssertTrue(SizeOf(AnsiString) = SizeOf(Pointer));
204 AssertTrue(SizeOf(String) = SizeOf(Pointer));
205
206 { some set operations }
207 AssertTrue([0,1] = [0,1]);
208 AssertTrue([0] <= [0]);
209 AssertTrue([0] <= [0,1,2,3,4]);
210 AssertTrue(not ([0] <= []));
211 AssertTrue([0] >= []);
212
213 { test is CompareMem(..., ..., 0) bug fixed }
214 AssertTrue(CompareMem(@b1, @b2, 0));
215 { test is "Format incompatible with Delphi" bug fixed }
216 AssertTrue(Format('%d %d %0:d %d', [0, 1, 2, 3]) = '0 1 0 1');
217 end;
218
219 procedure TTestOldFPCBugs.TestSwapEndian;
220 const
221 A1: QWord = $0123456789ABCDEF;
222 var
223 A2: QWord;
224 begin
225 A2 := QWord($EFCDAB8967452301);
226 AssertTrue(SwapEndian(A1) = A2);
227 AssertTrue(SwapEndian(A2) = A1);
228 end;
229
230 initialization
231 RegisterTest(TTestOldFPCBugs);
232 end.