Menu

[r2014]: / trunk / Tests / Src / DUnit / TestUBaseObjects.pas  Maximize  Restore  History

Download this file

669 lines (581 with data), 19.2 kB

  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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
{
Delphi DUnit Test Case for the UBaseObjects Unit
------------------------------------------------
$Rev$
$Date$
}
unit TestUBaseObjects;
interface
uses
TestFramework, UBaseObjects, SysUtils;
type
// Test methods for class TNoConstructObject
TestTNoConstructObject = class(TTestCase)
public
procedure ConstructError;
published
procedure TestConstruct;
end;
// Test methods for class TNoPublicConstructObject
TestTNoPublicConstructObject = class(TTestCase)
strict private
type
TTestObject = class(TNoPublicConstructObject)
public
class function Instance: TTestObject;
end;
public
procedure ConstructError;
published
procedure TestContruct;
end;
// Test methods for class TNoPublicConstructIntfObject
TestTNoPublicConstructIntfObject = class(TTestCase)
strict private
type
TTestObject = class(TNoPublicConstructIntfObject, IInterface)
public
class function Instance: IInterface;
end;
public
procedure ConstructError;
published
procedure TestContruct;
end;
// Test methods for class TNonRefCountedObject
TestTNonRefCountedObject = class(TTestCase)
strict private
type
TTestObject = class(TNonRefCountedObject)
public
constructor Create;
destructor Destroy; override;
class var InstanceCount: Integer;
end;
published
procedure TestAll;
end;
// Test methods for class TAggregatedOrLoneObject
TestTAggregatedOrLoneObject = class(TTestCase)
strict private
type
ITest = interface(IInterface)
['{18CBCC27-A5DC-4BA6-BD30-DF16D270801D}']
function GetInstanceCount: Integer;
end;
IMain = interface(IInterface)
['{C9D1F469-D073-4BCF-B111-B605A7B1058F}']
function GetRefCount: Integer;
end;
TTestAggregated = class(TAggregatedOrLoneObject, ITest)
public
constructor Create(const Controller: IInterface); override;
destructor Destroy; override;
class var fInstanceCount: Integer;
function GetInstanceCount: Integer;
end;
TTestController = class(TInterfacedObject, IInterface, ITest, IMain)
strict private
fAgg: TTestAggregated;
public
constructor Create;
destructor Destroy; override;
property Agg: TTestAggregated read FAgg implements ITest;
class var fInstanceCount: Integer;
function GetRefCount: Integer;
end;
published
procedure TestStandAlone;
procedure TestAggregated;
end;
// Test methods for class TOwnedConditionalFreeObject
TestTOwnedConditionalFreeObject = class(TTestCase)
type
TTestObject = class(TOwnedConditionalFreeObject)
strict protected
procedure Finalize; override;
public
constructor Create(const AOwner: TObject);
class var InstanceCount: Integer;
class function Details: string;
end;
published
procedure TestFree;
end;
// Test methods for class TDelegatedConditionalFreeObject
TestTDelegatedConditionalFreeObject = class(TTestCase)
strict private
type
TTestObject = class(TDelegatedConditionalFreeObject)
strict protected
procedure Finalize; override;
public
constructor Create(const CanFreeFn: TFunc<TObject,Boolean>);
class var InstanceCount: Integer;
class function Details: string;
end;
published
procedure TestFree;
end;
// Test methods for class TControlledConditionalFreeObject
TestTControlledConditionalFreeObject = class(TTestCase)
public
type
TController = class(TNonRefCountedObject, IConditionalFreeController)
private
fObjectToFree: TObject;
public
constructor Create(AObjectToFree: TObject = nil);
function PermitDestruction(const Obj: TObject): Boolean;
property ObjectToFree: TObject read fObjectToFree write fObjectToFree;
end;
TTestObject = class(TControlledConditionalFreeObject)
strict protected
procedure Finalize; override;
public
constructor Create(AController: IConditionalFreeController = nil);
class var InstanceCount: Integer;
class function Details: string;
end;
published
procedure TestFreeObject;
procedure TestControllerChange;
procedure TestLock;
end;
implementation
uses
Windows;
{ TestTNoConstructObject }
procedure TestTNoConstructObject.ConstructError;
begin
TNoConstructObject.Create;
end;
procedure TestTNoConstructObject.TestConstruct;
begin
CheckException(ConstructError, ENoConstructException,
'Expected assertion failure');
end;
{ TestTNoPublicConstructObject }
procedure TestTNoPublicConstructObject.ConstructError;
begin
TTestObject.Create; // should cause assertion failure
end;
procedure TestTNoPublicConstructObject.TestContruct;
var
Obj: TTestObject;
begin
CheckException(ConstructError, ENoConstructException,
'Expected assertion failure calling constructor');
Obj := nil;
try
try
Obj := TTestObject.Instance;
except
Fail('Got an exception trying to create via InternalCreate');
end;
finally
Obj.Free;
end;
end;
{ TestTNoPublicConstructObject.TTestObject }
class function TestTNoPublicConstructObject.TTestObject.Instance: TTestObject;
begin
Result := inherited InternalCreate;
end;
{ TestTNoPublicConstructIntfObject }
procedure TestTNoPublicConstructIntfObject.ConstructError;
begin
TTestObject.Create; // should cause assertion failure
end;
procedure TestTNoPublicConstructIntfObject.TestContruct;
var
Obj: IInterface;
begin
CheckException(ConstructError, ENoConstructException,
'Expected assertion failure calling constructor');
try
Obj := TTestObject.Instance;
except
Fail('Got an exception trying to create via InternalCreate');
end;
end;
{ TestTNoPublicConstructIntfObject.TTestObject }
class function TestTNoPublicConstructIntfObject.TTestObject.Instance:
IInterface;
begin
Result := inherited InternalCreate;
end;
{ TestTNonRefCountedObject }
procedure TestTNonRefCountedObject.TestAll;
var
Obj: TTestObject;
begin
Obj := TTestObject.Create;
try
Check(Obj.InstanceCount = 1,
Format('Expected instance after construction of 1, got %d',
[Obj.InstanceCount]));
Check((Obj as IInterface)._Release = -1, '_Release didn''t return -1');
// with ref counted interface, we would now have freed object
Check(Obj.InstanceCount = 1,
Format('Expected instance after _Release of 1, got %d',
[Obj.InstanceCount]));
finally
Obj.Free;
end;
end;
{ TestTNonRefCountedObject.TTestObject }
constructor TestTNonRefCountedObject.TTestObject.Create;
begin
inherited;
Inc(InstanceCount);
end;
destructor TestTNonRefCountedObject.TTestObject.Destroy;
begin
Dec(InstanceCount);
inherited;
end;
{ TestTAggregatedOrLoneObject }
procedure TestTAggregatedOrLoneObject.TestAggregated;
var
ObjMain: IMain;
ObjTest: ITest;
begin
ObjMain := TTestController.Create;
Check(TTestController.fInstanceCount = 1,
'Expecting one instance of controller after construction');
Check(TTestAggregated.fInstanceCount = 1,
'Expecting one instance of aggregated object after construction');
Check(ObjMain.GetRefCount = 1,
Format('Expecting ref count of 1 after construction, got %d',
[ObjMain.GetRefCount]));
ObjTest := ObjMain as ITest;
Check(ObjMain.GetRefCount = 2,
Format('Expecting ref count of 2 after assigment of ITest, got %d',
[ObjMain.GetRefCount]));
ObjMain._Release;
Check(ObjMain.GetRefCount = 1,
Format('Expecting ref count of 1 after releasing IMain, got %d',
[ObjMain.GetRefCount]));
ObjTest._Release;
Check(TTestController.fInstanceCount = 0,
'Expecting controller to have been freed after releasing main object');
Check(TTestAggregated.fInstanceCount = 0,
'Expecting aggregated object to have been freed during destruction');
end;
procedure TestTAggregatedOrLoneObject.TestStandAlone;
var
Obj: TTestAggregated;
ObjI: ITest;
begin
Obj := TTestAggregated.Create; // no param if stand alone
ObjI := Obj;
Check(ObjI.GetInstanceCount = 1,
Format('Expected instance count of 1, got %d', [Obj.GetInstanceCount]));
Check(Supports(Obj, IInterface), 'Expected object to support IInterface');
Check(Supports(Obj, ITest), 'Expected object to support ITest');
ObjI := nil; // should have decrement instance count and freed this object
Check(TTestAggregated.fInstanceCount = 0,
Format('Expected instance count of 0, got %d',
[TTestAggregated.fInstanceCount]));
end;
{ TestTAggregatedOrLoneObject.TTestAggregated }
constructor TestTAggregatedOrLoneObject.TTestAggregated.Create(
const Controller: IInterface);
begin
inherited;
Inc(fInstanceCount);
end;
destructor TestTAggregatedOrLoneObject.TTestAggregated.Destroy;
begin
Dec(fInstanceCount);
inherited;
end;
function TestTAggregatedOrLoneObject.TTestAggregated.GetInstanceCount: Integer;
begin
Result := fInstanceCount;
end;
{ TestTAggregatedOrLoneObject.TTestController }
constructor TestTAggregatedOrLoneObject.TTestController.Create;
begin
inherited Create;
fAgg := TTestAggregated.Create(Self);
Inc(fInstanceCount);
end;
destructor TestTAggregatedOrLoneObject.TTestController.Destroy;
begin
fAgg.Free;
Dec(fInstanceCount);
inherited;
end;
function TestTAggregatedOrLoneObject.TTestController.GetRefCount: Integer;
begin
Result := RefCount;
end;
{ TestTOwnedConditionalFreeObject }
procedure TestTOwnedConditionalFreeObject.TestFree;
var
Obj: TTestObject;
begin
Obj := TTestObject.Create(Self);
Check(TTestObject.InstanceCount = 1,
Format('Expected Instance Count, got %d', [TTestObject.InstanceCount]));
Obj.Free; // should not have been allowed
Check(TTestObject.InstanceCount = 1,
Format('Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Owner := nil;
Obj.Free; // should have been allowed
Check(TTestObject.InstanceCount = 0,
Format('Expected Instance Count of 0, got %d',
[TTestObject.InstanceCount]));
end;
{ TestTOwnedConditionalFreeObject.TTestObject }
constructor TestTOwnedConditionalFreeObject.TTestObject.Create(
const AOwner: TObject);
begin
inherited;
Inc(InstanceCount);
end;
class function TestTOwnedConditionalFreeObject.TTestObject.Details: string;
begin
Result := Format('%s: instances = %d', [ClassName, InstanceCount]);
end;
procedure TestTOwnedConditionalFreeObject.TTestObject.Finalize;
begin
Dec(InstanceCount);
end;
{ TestTDelegatedConditionalFreeObject }
procedure TestTDelegatedConditionalFreeObject.TestFree;
var
Obj: TTestObject;
Allow: Boolean;
begin
Allow := False;
Obj := TTestObject.Create(
function(Sender: TObject): Boolean begin
Check(Obj = Sender, 'Expected Sender = Obj');
Result := Allow;
end
);
Check(TTestObject.InstanceCount = 1,
Format('Expected Instance Count, got %d', [TTestObject.InstanceCount]));
Obj.Free; // should not have been allowed
Check(TTestObject.InstanceCount = 1,
Format('Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Allow := True;
Obj.Free; // should have been allowed
Check(TTestObject.InstanceCount = 0,
Format('Expected Instance Count of 0, got %d',
[TTestObject.InstanceCount]));
end;
{ TestTDelegatedConditionalFreeObject.TTestObject }
constructor TestTDelegatedConditionalFreeObject.TTestObject.Create(
const CanFreeFn: TFunc<TObject,Boolean>);
begin
inherited;
Inc(InstanceCount);
end;
class function TestTDelegatedConditionalFreeObject.TTestObject.Details: string;
begin
Result := Format('%s: instances = %d', [ClassName, InstanceCount]);
end;
procedure TestTDelegatedConditionalFreeObject.TTestObject.Finalize;
begin
Dec(InstanceCount);
end;
{ TControlledConditionalFreeObject }
procedure TestTControlledConditionalFreeObject.TestControllerChange;
var
Controller1, Controller2: IConditionalFreeController;
Obj: TTestObject;
begin
Obj := TTestObject.Create(nil); // create Obj with no controller
Check(Obj.FreeController = nil, 'Obj.FreeController <> nil');
Controller1 := nil;
Controller2 := nil;
try
Controller1 := TController.Create;
Controller2 := TController.Create(Obj);
// In this test Obj should not be freed since it has no controller
Check(TTestObject.InstanceCount = 1,
Format('1: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Free;
Check(TTestObject.InstanceCount = 1,
Format('2: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
// In this test Obj should not be freed since Controller1.ObjectToFree = nil
Obj.FreeController := Controller1;
Check(Obj.FreeController = Controller1,
'Obj.FreeController <> Controller1');
Check(TTestObject.InstanceCount = 1,
Format('3: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Free;
Check(TTestObject.InstanceCount = 1,
Format('4: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
// In this test Obj should be freed since Controller2.ObjectToFree = nil
Obj.FreeController := Controller2;
Check(Obj.FreeController = Controller2,
'Obj.FreeController <> Controller2');
Check(TTestObject.InstanceCount = 1,
Format('5: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Free;
Check(TTestObject.InstanceCount = 0,
Format('6: Expected Instance Count of 0, got %d',
[TTestObject.InstanceCount]));
finally
if Assigned(Controller1) then (Controller1 as TController).Free;
if Assigned(Controller2) then (Controller2 as TController).Free;
end;
end;
procedure TestTControlledConditionalFreeObject.TestFreeObject;
var
ControllerObj: TController;
Controller: IConditionalFreeController;
Obj: TTestObject;
AnotherObj: TObject;
begin
AnotherObj := nil;
ControllerObj := TController.Create;
try
Controller := ControllerObj as IConditionalFreeController;
Check(ControllerObj.ObjectToFree = nil,
'ControllerObj.ObjectToFree <> nil');
Obj := TTestObject.Create(Controller);
AnotherObj := TObject.Create;
// In this test ControllerObj.ObjectToFree = nil, so Obj should not be freed
Check(TTestObject.InstanceCount = 1,
Format('1: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Free;
Check(TTestObject.InstanceCount = 1,
Format('2: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
// In this test ControllerObj.ObjectToFree = AnotherObj, which is not the
// same as Obj, so Obj should not be freed
ControllerObj.ObjectToFree := AnotherObj;
Check(TTestObject.InstanceCount = 1,
Format('3: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Free;
Check(TTestObject.InstanceCount = 1,
Format('4: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
// In this check Obj should be freed since ControllerObj.ObjectToFree = Obj
ControllerObj.ObjectToFree := Obj;
Check(TTestObject.InstanceCount = 1,
Format('5: Expected Instance Count of 1, got %d',
[TTestObject.InstanceCount]));
Obj.Free;
Check(TTestObject.InstanceCount = 0,
Format('6: Expected Instance Count of 0, got %d',
[TTestObject.InstanceCount]));
finally
AnotherObj.Free;
ControllerObj.Free;
end;
end;
procedure TestTControlledConditionalFreeObject.TestLock;
var
Controller1a, Controller1b,
Controller2a, Controller2b: IConditionalFreeController;
Obj1, Obj2: TTestObject;
begin
Obj1 := TTestObject.Create(nil); // create Obj with no controller
Check(Obj1.FreeController = nil, 'Obj.FreeController <> nil');
Obj2 := TTestObject.Create(nil); // create Obj with no controller
Check(Obj2.FreeController = nil, 'Obj.FreeController <> nil');
Controller1a := nil;
Controller1b := nil;
Controller2a := nil;
Controller2b := nil;
try
Controller1a := TController.Create(Obj1);
Controller1b := TController.Create(Obj1);
Controller2a := TController.Create(Obj2);
Controller2b := TController.Create(Obj2);
Obj1.FreeController := Controller1a;
Check(not Obj1.Locked, 'Expected Obj1.Locked = False');
Obj1.Lock;
Check(Obj1.Locked, 'Expected Obj1.Locked = True');
try
Obj1.FreeController := Controller1b;
Fail('Exception expected on assignment '
+ 'Obj1.FreeController := Controller1b');
except
on E: Exception do
Check(E is ELocked, 'ELocked exception expected');
end;
Check(Obj1.FreeController = Controller1a,
'Expected Obj1.FreeController = Controller1a');
Obj1.Free;
Assert(Obj2.FreeController = nil, 'Expected Obj2.FreeController = nil');
Obj2.Lock;
try
Obj2.FreeController := Controller2a;
except
Fail('Unexpected exception on assigment '
+ 'Obj2.FreeController := Controller2a');
end;
try
Obj2.FreeController := Controller2b;
Fail('Exception expected on assignment '
+ 'Obj2.FreeController := Controller2b');
except
on E: Exception do
Check(E is ELocked, 'ELocked exception expected');
end;
Check(Obj2.FreeController = Controller2a,
'Expected Obj2.FreeController = Controller2a');
Obj2.Free;
finally
if Assigned(Controller1a) then (Controller1a as TController).Free;
if Assigned(Controller1b) then (Controller1b as TController).Free;
if Assigned(Controller2a) then (Controller2a as TController).Free;
if Assigned(Controller2b) then (Controller2b as TController).Free;
end;
end;
{ TestTControlledConditionalFreeObject.TController }
constructor TestTControlledConditionalFreeObject.TController.Create(
AObjectToFree: TObject);
begin
inherited Create;
fObjectToFree := AObjectToFree;
end;
function TestTControlledConditionalFreeObject.TController.PermitDestruction(
const Obj: TObject): Boolean;
begin
Result := Obj = fObjectToFree;
end;
{ TestTControlledConditionalFreeObject.TTestObject }
constructor TestTControlledConditionalFreeObject.TTestObject.Create(
AController: IConditionalFreeController);
begin
inherited Create(AController);
Inc(InstanceCount);
end;
class function TestTControlledConditionalFreeObject.TTestObject.Details: string;
begin
Result := Format('%s: instances = %d', [ClassName, InstanceCount]);
end;
procedure TestTControlledConditionalFreeObject.TTestObject.Finalize;
begin
inherited;
Dec(InstanceCount);
end;
initialization
// Register any test cases with the test runner
RegisterTest(TestTNoConstructObject.Suite);
RegisterTest(TestTNoPublicConstructObject.Suite);
RegisterTest(TestTNoPublicConstructIntfObject.Suite);
RegisterTest(TestTNonRefCountedObject.Suite);
RegisterTest(TestTAggregatedOrLoneObject.Suite);
RegisterTest(TestTOwnedConditionalFreeObject.Suite);
RegisterTest(TestTDelegatedConditionalFreeObject.Suite);
RegisterTest(TestTControlledConditionalFreeObject.Suite);
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.