2020interface
2121
2222uses
23- TestFramework, MethodCallback;
23+ DUnitX.TestFramework,
24+ MethodCallback;
2425
2526implementation
2627
2728
2829type
30+ TTwoArgStdFunction = function (arg1, arg2: string): integer; stdcall;
31+ TThreeArgCDeclProcedure = procedure (arg1, arg2, arg3: string); cdecl;
32+
2933 TFourArgStdFunction = function(arg1, arg2, arg3, arg4: integer): integer; stdcall;
3034 TFiveArgCdeclFunction = function(arg1, arg2, arg3, arg4, arg5: integer): integer; cdecl;
3135
32- TTestObj = class (TObject)
36+ TMyFuncCallback = function(arg1, arg2: string): integer of object ; stdcall;
37+ TMyProcCallback = procedure (arg1, arg2, arg3: string) of object ; cdecl;
38+
39+
40+ TTestObj = class
3341 public
3442 Argument1: string;
3543 Argument2: string;
3644 Argument3: string;
37- function TwoArgStdFunction (arg1: string; arg2: string): integer; stdcall;
38- procedure ThreeArgCdeclProcedure (arg1: string; arg2: string; arg3: string); cdecl;
45+ function TwoArgStdFunction (arg1, arg2: string): integer; stdcall;
46+ procedure ThreeArgCdeclProcedure (arg1, arg2, arg3: string); cdecl;
3947 function FourArgStdFunction (arg1, arg2, arg3, arg4: integer): integer; stdcall;
4048 function FiveArgCdeclFunction (arg1, arg2, arg3, arg4, arg5: integer): integer; cdecl;
4149 end ;
4250
43- TMethodCallbackTest = class (TTestCase)
44- protected
51+ [TestFixture]
52+ TMethodCallbackTest = class
53+ private
4554 fTestObj: TTestObj;
46- procedure Setup ; override;
47- procedure Teardown ; override;
48- published
55+ public
56+ [SetupFixture]
57+ procedure SetupFixture ;
58+ [TearDownFixture]
59+ procedure Teardown ;
60+ [Test]
4961 procedure TestDeleteOnEmptyAllocator ;
62+ [Test]
5063 procedure TestCallBackStdCall ;
64+ [Test]
5165 procedure TestCallBackCDecl ;
66+ [Test]
5267 procedure TestOfObjectCallBackStdCall ;
68+ [Test]
5369 procedure TestOfObjectCallBackCDecl ;
70+ [Test]
5471 procedure TestDeleteCallBack ;
72+ [Test]
5573 procedure TestFourArgStdFunction ;
74+ [Test]
5675 procedure TestFiveArgCdeclFunction ;
76+ [Test]
5777 procedure TestMemoryMgmt ;
78+ [Test]
5879 procedure TestBug01 ;
5980 end ;
6081
61- TTwoArgStdFunction = function (arg1, arg2: string): integer; stdcall;
62- TThreeArgCDeclProcedure = procedure (arg1, arg2, arg3: string); cdecl;
63-
64- TMyFuncCallback = function(arg1, arg2: string): integer of object ; stdcall;
65- TMyProcCallback = procedure (arg1, arg2, arg3: string) of object ; cdecl;
66-
6782{ TTestObj }
6883
6984function TTestObj.FiveArgCdeclFunction (arg1, arg2, arg3, arg4,
@@ -95,26 +110,23 @@ function TTestObj.TwoArgStdFunction(arg1, arg2: string): integer;
95110
96111{ TMethodCallbackTest }
97112
98- procedure TMethodCallbackTest.Setup ;
113+ procedure TMethodCallbackTest.SetupFixture ;
99114begin
100- inherited ;
101115 fTestObj:=TTestObj.Create;
102116end ;
103117
104118procedure TMethodCallbackTest.Teardown ;
105119begin
106120 fTestObj.Free;
107121 FreeCallBacks;
108- inherited ;
109-
110122end ;
111123
112124procedure TMethodCallbackTest.TestBug01 ;
113125const
114126 AllocCount = { $IFDEF CPUX64} 51 { $ELSE} 90 { $ENDIF} ;
115127var
116128 i: integer;
117- ptr, ptr1, ptr2 : Pointer;
129+ ptr: Pointer;
118130begin
119131 {
120132I discovered a severe Bug in my memory manager code in MethodCallbacks.
@@ -142,19 +154,20 @@ procedure TMethodCallbackTest.TestBug01;
142154 // one ThreeArgDecl callback uses 33 bytes code + 4 bytes Next pointer = 37 bytes
143155 // we should be able to allocate 110 Callbacks per page
144156
145- CheckEquals(0 , CodeMemPageCount);
157+ FreeCallBacks;
158+ Assert.AreEqual(0 , CodeMemPageCount);
146159
147160 for i:=1 to AllocCount do
148161 ptr:=GetCallBack(fTestObj, @TTestObj.ThreeArgCdeclProcedure, 5 , ctCdecl);
149162
150163 // there should still be 1 page allocated
151- CheckEquals (1 , CodeMemPageCount);
164+ Assert.AreEqual (1 , CodeMemPageCount);
152165
153166 // get one callback more and we should have 2 pages
154167 ptr:=GetCallBack(fTestObj, @TTestObj.ThreeArgCdeclProcedure, 5 , ctCdecl);
155168 // getting CodeMemPageCount would crash as the next page pointer was overwritten by the
156169 // last allocation
157- CheckEquals (2 , CodeMemPageCount);
170+ Assert.AreEqual (2 , CodeMemPageCount);
158171
159172end ;
160173
@@ -170,9 +183,9 @@ procedure TMethodCallbackTest.TestCallBackCDecl;
170183
171184 proc(' first arg' , ' second arg' , ' third arg' );
172185
173- CheckEquals (' first arg' , fTestObj.Argument1);
174- CheckEquals (' second arg' , fTestObj.Argument2);
175- CheckEquals (' third arg' , fTestObj.Argument3);
186+ Assert.AreEqual (' first arg' , fTestObj.Argument1);
187+ Assert.AreEqual (' second arg' , fTestObj.Argument2);
188+ Assert.AreEqual (' third arg' , fTestObj.Argument3);
176189
177190end ;
178191
@@ -186,9 +199,9 @@ procedure TMethodCallbackTest.TestCallBackStdCall;
186199 // ---call method through pointer
187200 func:=TTwoArgStdFunction(ptr);
188201
189- CheckEquals (1 , func(' first arg' , ' second arg' ));
190- CheckEquals( ' first arg' , fTestObj.Argument1);
191- CheckEquals( ' second arg' , fTestObj.Argument2);
202+ Assert.AreEqual (1 , func(' first arg' , ' second arg' ));
203+ Assert.AreEqual(string( ' first arg' ) , fTestObj.Argument1);
204+ Assert.AreEqual(string( ' second arg' ) , fTestObj.Argument2);
192205end ;
193206
194207procedure TMethodCallbackTest.TestDeleteCallBack ;
@@ -215,6 +228,7 @@ procedure TMethodCallbackTest.TestDeleteCallBack;
215228 DeleteCallBack(ptr1);
216229 DeleteCallBack(ptr2);
217230 DeleteCallback(ptr3);
231+ Assert.Pass;
218232end ;
219233
220234procedure TMethodCallbackTest.TestDeleteOnEmptyAllocator ;
@@ -223,14 +237,15 @@ procedure TMethodCallbackTest.TestDeleteOnEmptyAllocator;
223237begin
224238 ptr1 := nil ;
225239 DeleteCallBack(ptr1);
240+ Assert.Pass();
226241end ;
227242
228243procedure TMethodCallbackTest.TestFiveArgCdeclFunction ;
229244Var
230245 CallBack : TFiveArgCdeclFunction;
231246begin
232247 CallBack := GetCallBack(fTestObj, @TTestObj.FiveArgCdeclFunction, 5 , ctCDECL);
233- CheckEquals (CallBack(1 ,2 ,3 ,4 ,5 ), 1 *4 +2 *5 +3 );
248+ Assert.AreEqual (CallBack(1 ,2 ,3 ,4 ,5 ), 1 *4 +2 *5 +3 );
234249 DeleteCallBack(@CallBack);
235250end ;
236251
@@ -239,7 +254,7 @@ procedure TMethodCallbackTest.TestFourArgStdFunction;
239254 CallBack : TFourArgStdFunction;
240255begin
241256 CallBack := GetCallBack(fTestObj, @TTestObj.FourArgStdFunction, 4 , ctSTDCALL);
242- CheckEquals (CallBack(1 ,2 ,3 ,4 ), 1 *3 +2 *4 );
257+ Assert.AreEqual (CallBack(1 ,2 ,3 ,4 ), 1 *3 +2 *4 );
243258 DeleteCallBack(@CallBack);
244259end ;
245260
@@ -255,39 +270,37 @@ procedure TMethodCallbackTest.TestMemoryMgmt;
255270 // one ThreeArgDecl callback uses 33 bytes code + 4 bytes Next pointer = 37 bytes
256271 // we should be able to allocate 110 Callbacks per page
257272
258- CheckEquals(0 , CodeMemPageCount);
273+ FreeCallBacks;
274+ Assert.AreEqual(0 , CodeMemPageCount);
259275
260276 for i:=1 to AllocCount do
261277 ptr:=GetCallBack(fTestObj, @TTestObj.ThreeArgCdeclProcedure, 3 , ctCdecl);
262278
263279 // there should still be 1 page allocated
264- CheckEquals (1 , CodeMemPageCount);
280+ Assert.AreEqual (1 , CodeMemPageCount);
265281
266282 // get one callback more and we should have 2 pages
267283 ptr:=GetCallBack(fTestObj, @TTestObj.ThreeArgCdeclProcedure, 3 , ctCdecl);
268- CheckEquals (2 , CodeMemPageCount);
284+ Assert.AreEqual (2 , CodeMemPageCount);
269285
270286 // get some more memory
271287 ptr1:=GetCallBack(fTestObj, @TTestObj.ThreeArgCdeclProcedure, 3 , ctCdecl);
272288 ptr2:=GetCallBack(fTestObj, @TTestObj.ThreeArgCdeclProcedure, 3 , ctCdecl);
273- CheckEquals (2 , CodeMemPageCount);
289+ Assert.AreEqual (2 , CodeMemPageCount);
274290
275291
276292 // now Free the callbacks on page 2
277293 DeleteCallBack(ptr1);
278- CheckEquals (2 , CodeMemPageCount);
294+ Assert.AreEqual (2 , CodeMemPageCount);
279295 DeleteCallBack(ptr);
280- CheckEquals (2 , CodeMemPageCount);
296+ Assert.AreEqual (2 , CodeMemPageCount);
281297 DeleteCallBack(ptr2);
282298 // page count should be back to 1
283- CheckEquals (1 , CodeMemPageCount);
299+ Assert.AreEqual (1 , CodeMemPageCount);
284300
285301 // allocate one more and page count should go up to 2 again
286302 ptr:=GetCallBack(fTestObj, @TTestObj.ThreeArgCdeclProcedure, 3 , ctCdecl);
287- CheckEquals(2 , CodeMemPageCount);
288-
289-
290-
303+ Assert.AreEqual(2 , CodeMemPageCount);
291304end ;
292305
293306procedure TMethodCallbackTest.TestOfObjectCallBackCDecl ;
@@ -304,9 +317,9 @@ procedure TMethodCallbackTest.TestOfObjectCallBackCDecl;
304317
305318 proc(' first arg' , ' second arg' , ' third arg' );
306319
307- CheckEquals (' first arg' , fTestObj.Argument1);
308- CheckEquals (' second arg' , fTestObj.Argument2);
309- CheckEquals (' third arg' , fTestObj.Argument3);
320+ Assert.AreEqual (' first arg' , fTestObj.Argument1);
321+ Assert.AreEqual (' second arg' , fTestObj.Argument2);
322+ Assert.AreEqual (' third arg' , fTestObj.Argument3);
310323
311324end ;
312325
@@ -322,13 +335,13 @@ procedure TMethodCallbackTest.TestOfObjectCallBackStdCall;
322335 // ---call method through pointer
323336 func:=TTwoArgStdFunction(ptr);
324337
325- CheckEquals (1 , func(' first arg' , ' second arg' ));
326- CheckEquals (' first arg' , fTestObj.Argument1);
327- CheckEquals (' second arg' , fTestObj.Argument2);
338+ Assert.AreEqual (1 , func(' first arg' , ' second arg' ));
339+ Assert.AreEqual (' first arg' , fTestObj.Argument1);
340+ Assert.AreEqual (' second arg' , fTestObj.Argument2);
328341end ;
329342
330343
331344initialization
332- TestFrameWork.RegisterTest (TMethodCallBackTest.Suite() );
345+ TDUnitX.RegisterTestFixture (TMethodCallBackTest);
333346
334347end .
0 commit comments