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
|
program tarray12;
{$mode objfpc}
uses
SysUtils;
procedure PrintArray(a: array of LongInt);
var
i: LongInt;
begin
Writeln('Length: ', Length(a));
Write('Data: ');
for i := Low(a) to High(a) do begin
if i > Low(a) then
Write(', ');
Write(a[i]);
end;
Writeln;
end;
procedure CheckArray(aExpected, aGot: array of LongInt);
var
i: LongInt;
begin
if Length(aExpected) <> Length(aGot) then
Halt(1);
for i := Low(aExpected) to High(aExpected) do begin
if aExpected[i] <> aGot[i] then
Halt(2);
end;
end;
function InitArray(aCount: LongInt): specialize TArray<LongInt>;
var
i: LongInt;
begin
SetLength(Result, aCount);
for i := 0 to aCount - 1 do
Result[i] := i;
end;
type
TTest = class(TInterfacedObject, IInterface)
function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
end;
var
gRefCount: LongInt = 0;
function TTest._AddRef: LongInt; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
Result := inherited _AddRef;
gRefCount := Result;
end;
function TTest._Release: LongInt; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
Result := inherited _Release;
gRefCount := Result;
end;
function GetIntf: IInterface;
begin
Result := TTest.Create;
end;
procedure TestIntf;
procedure DoInsert(const aArg1: specialize TArray<IInterface>; var aArg2: specialize TArray<IInterface>);
begin
Insert(aArg1, aArg2, 0);
end;
var
ai1, ai2: specialize TArray<IInterface>;
intf: IInterface;
c: LongInt;
begin
intf := GetIntf;
SetLength(ai1, 1);
c := gRefCount;
ai1[0] := intf;
if c >= gRefCount then
Halt(3);
intf := Nil;
if c <> gRefCount then
Halt(4);
DoInsert(ai1, ai2);
if c >= gRefCount then
Halt(5);
ai1 := Nil;
if gRefCount = 0 then
Halt(6);
ai2 := Nil;
if gRefCount <> 0 then
Halt(7);
end;
var
t, t2: specialize TArray<LongInt>;
t3: array[0..2] of LongInt;
begin
t := Nil;
Insert([1, 3, 5], t, 0);
PrintArray(t);
CheckArray(t, [1, 3, 5]);
t := Nil;
t2 := Nil;
Insert(t2, t, 0);
PrintArray(t);
CheckArray(t, []);
t := Nil;
Insert([], t, 0);
PrintArray(t);
CheckArray(t, []);
t := InitArray(5);
Insert([], t, 0);
PrintArray(t);
CheckArray(t, [0, 1, 2, 3, 4]);
t := InitArray(5);
Insert([1, 3, 5], t, 2);
PrintArray(t);
CheckArray(t, [0, 1, 1, 3, 5, 2, 3, 4]);
t := InitArray(5);
Insert(5, t, 2);
PrintArray(t);
CheckArray(t, [0, 1, 5, 2, 3, 4]);
{ t := InitArray(5);
Insert([1, 3, 5] + [4, 6], t, 2);
PrintArray(t);
CheckArray(t, [0, 1, 1, 3, 5, 4, 6, 2, 3, 4]);}
t := InitArray(5);
Insert([1, 3, 5], t, -1);
PrintArray(t);
CheckArray(t, [1, 3, 5, 0, 1, 2, 3, 4]);
t := InitArray(5);
Insert([1, 3, 5], t, 5);
PrintArray(t);
CheckArray(t, [0, 1, 2, 3, 4, 1, 3, 5]);
t := InitArray(5);
Insert([1, 3, 5], t, 6);
PrintArray(t);
CheckArray(t, [0, 1, 2, 3, 4, 1, 3, 5]);
t2 := specialize TArray<LongInt>.Create(1, 3, 5);
t := InitArray(5);
Insert(t2, t, 1);
PrintArray(t);
CheckArray(t, [0, 1, 3, 5, 1, 2, 3, 4]);
{ support for static arrays is not Delphi compatible, but whatever :) }
t := InitArray(5);
t3[0] := 2;
t3[1] := 4;
t3[2] := 6;
Insert(t3, t, 2);
PrintArray(t);
CheckArray(t, [0, 1, 2, 4, 6, 2, 3, 4]);
TestIntf;
Writeln('Ok');
end.
|