| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestFGL" -*- |
| 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 |
{ Test FGL unit. } |
| 18 |
|
| 19 |
unit TestFGL; |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses |
| 24 |
Classes, SysUtils, FGL, |
| 25 |
CastleTester; |
| 26 |
|
| 27 |
type |
| 28 |
TTestFGL = class(TCastleTestCase) |
| 29 |
procedure TestObjectsList; |
| 30 |
end; |
| 31 |
|
| 32 |
implementation |
| 33 |
|
| 34 |
uses CastleUtils, CastleClassUtils; |
| 35 |
|
| 36 |
type |
| 37 |
TItem = class |
| 38 |
Str: string; |
| 39 |
end; |
| 40 |
|
| 41 |
TItemList = class(specialize TFPGObjectList<TItem>) |
| 42 |
{ For each item of the list, delete all it's duplicates. } |
| 43 |
procedure DeleteDuplicates; |
| 44 |
procedure AddList(Source: TItemList); |
| 45 |
end; |
| 46 |
|
| 47 |
procedure TItemList.DeleteDuplicates; |
| 48 |
|
| 49 |
function IndexOf(Item: TItem; StartIndex: Integer): Integer; |
| 50 |
begin |
| 51 |
for result := StartIndex to Count - 1 do |
| 52 |
if Item = Items[result] then exit; |
| 53 |
result := -1; |
| 54 |
end; |
| 55 |
|
| 56 |
{ Set to @nil (never freeing) given item on TFPGObjectList. } |
| 57 |
procedure FPGObjectList_NilItem(List: TFPSList; I: Integer); |
| 58 |
begin |
| 59 |
{ Do not set the list item by normal List.Items, as it will cause |
| 60 |
freeing the item, once http://bugs.freepascal.org/view.php?id=19854 |
| 61 |
is fixed (it is fixed in FPC > 2.6.0). } |
| 62 |
PPointer(List.List)[I] := nil; |
| 63 |
end; |
| 64 |
|
| 65 |
var |
| 66 |
I, Index: integer; |
| 67 |
begin |
| 68 |
I := 0; |
| 69 |
while I < Count do |
| 70 |
begin |
| 71 |
Index := I + 1; |
| 72 |
repeat |
| 73 |
Index := IndexOf(Items[I], Index); |
| 74 |
if Index = -1 then Break; |
| 75 |
// even in case the list has FreeObjects, this needs to nil *without freeing* |
| 76 |
if FreeObjects then |
| 77 |
FPGObjectList_NilItem(Self, Index) |
| 78 |
else |
| 79 |
// we could use FPGObjectList_NilItem(Self, Index) here too, |
| 80 |
// but we can also make non-hacky assignment to nil. |
| 81 |
Items[Index] := nil; |
| 82 |
Delete(Index); |
| 83 |
until false; |
| 84 |
|
| 85 |
Inc(I); |
| 86 |
end; |
| 87 |
end; |
| 88 |
|
| 89 |
procedure TItemList.AddList(Source: TItemList); |
| 90 |
var |
| 91 |
OldCount: Integer; |
| 92 |
begin |
| 93 |
OldCount := Count; |
| 94 |
Count := Count + Source.Count; |
| 95 |
if Source.Count <> 0 then |
| 96 |
System.Move(Source.List^[0], List^[OldCount], SizeOf(Pointer) * Source.Count); |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TTestFGL.TestObjectsList; |
| 100 |
var ol, ol2: TItemList; |
| 101 |
begin |
| 102 |
ol := TItemList.Create(false); |
| 103 |
try ol.Add(TItem.Create); ol[0].Str := 'foo'; ol[0].Free; |
| 104 |
finally ol.Free end; |
| 105 |
|
| 106 |
ol := TItemList.Create(false); |
| 107 |
try ol.Add(TItem($454545)); ol.Delete(0); |
| 108 |
finally ol.Free end; |
| 109 |
|
| 110 |
ol := TItemList.Create(true); |
| 111 |
try ol.Add(TItem.Create); ol.Clear; |
| 112 |
finally ol.Free end; |
| 113 |
|
| 114 |
ol := TItemList.Create(true); |
| 115 |
try ol.Add(nil); ol.Clear; |
| 116 |
finally ol.Free end; |
| 117 |
|
| 118 |
ol := TItemList.Create(true); |
| 119 |
try |
| 120 |
ol.Add(TItem.Create); ol.Last.Str := 'first item'; |
| 121 |
|
| 122 |
ol2 := TItemList.Create(false); |
| 123 |
try |
| 124 |
ol2.Add(TItem.Create); ol2.Last.Str := 'one'; |
| 125 |
ol2.Add(TItem.Create); ol2.Last.Str := 'two'; |
| 126 |
ol.AddList(ol2); |
| 127 |
finally ol2.Free end; |
| 128 |
ol.Clear; |
| 129 |
finally ol.Free end; |
| 130 |
|
| 131 |
ol := TItemList.Create(true); |
| 132 |
try |
| 133 |
ol.Add(TItem.Create); ol.Last.Str := 'first item'; |
| 134 |
|
| 135 |
ol2 := TItemList.Create(false); |
| 136 |
try |
| 137 |
ol2.Add(TItem.Create); ol2.Last.Str := 'one'; |
| 138 |
ol2.Add(TItem.Create); ol2.Last.Str := 'two'; |
| 139 |
ol.AddList(ol2); |
| 140 |
ol.AddList(ol2); |
| 141 |
finally ol2.Free end; |
| 142 |
|
| 143 |
AssertTrue(ol.Count = 5); |
| 144 |
AssertTrue(ol[0].Str = 'first item'); |
| 145 |
AssertTrue(ol[1].Str = 'one'); |
| 146 |
AssertTrue(ol[2].Str = 'two'); |
| 147 |
AssertTrue(ol[3].Str = 'one'); |
| 148 |
AssertTrue(ol[4].Str = 'two'); |
| 149 |
|
| 150 |
AssertTrue(ol[1] = ol[3]); { (1 i 3) i (2 i 4) to te same obiekty. } |
| 151 |
AssertTrue(ol[2] = ol[4]); |
| 152 |
|
| 153 |
{ delete dups, preventing Clear from Freeing two times the same reference } |
| 154 |
ol.DeleteDuplicates; |
| 155 |
ol.Clear; |
| 156 |
finally ol.Free end; |
| 157 |
end; |
| 158 |
|
| 159 |
initialization |
| 160 |
{ For some reason, testing FGL with FPC 3.3.1 fails with: |
| 161 |
|
| 162 |
Marked memory at $00007F65B6276C60 invalid |
| 163 |
Wrong signature $2071BAA5 instead of 243D6DCB |
| 164 |
$00000000004CB2C0 |
| 165 |
|
| 166 |
FPC rev 40000, Linux/x86_64. |
| 167 |
Also: FPC rev 41505, Windows/x86_64. |
| 168 |
Once the backtrace pointed to DEREF, line 1028 of fgl.pp . |
| 169 |
|
| 170 |
TODO: submit FPC bug. |
| 171 |
For now, do not do this test with newer FPCs. |
| 172 |
We don't use FGL in CGE now anyway. |
| 173 |
} |
| 174 |
{$ifdef VER3_0} |
| 175 |
RegisterTest(TTestFGL); |
| 176 |
{$endif} |
| 177 |
end. |