Skip to content

Commit bc5cf11

Browse files
committed
Add tests for new Arrays routine and method.
Added tests for new TArrayUtils.Reverse<T> method and ReverseByteArray routine. Regenerated UArraysCatSnippets unit using CodeSnip. The generated unit contains up to date versions of all snippets in the Arrays category. The unit was used as generated with no changes.
1 parent 4c7eb54 commit bc5cf11

File tree

2 files changed

+456
-36
lines changed

2 files changed

+456
-36
lines changed

tests/Cat-Arrays/TestUArraysCatSnippets.pas

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ TestTArrayUtils = class(TTestCase)
1212
fSA0: TArray<string>;
1313
fSA1: TArray<string>;
1414
fSA2: TArray<string>;
15+
fSA2R: TArray<string>;
1516
fSAM: TArray<string>;
1617
fSAN: TArray<string>;
18+
fSAR: TArray<string>;
1719
fIA0: TArray<Integer>;
1820
fIA1: TArray<Integer>;
1921
fIA2: TArray<Integer>;
22+
fIA3: TArray<Integer>;
23+
fIA3R: TArray<Integer>;
2024
fIAM: TArray<Integer>;
2125
fIAN: TArray<Integer>;
2226
fIAP: TArray<Integer>;
27+
fIAR: TArray<Integer>;
2328
fIAX: TArray<Integer>;
2429
fIAY: TArray<Integer>;
2530
protected
@@ -30,12 +35,16 @@ TestTArrayUtils = class(TTestCase)
3035
procedure TestLast;
3136
procedure TestIndexOf;
3237
procedure TestEqual;
38+
// TestReverse must come after TestEqual since the test calls TArrayUtils.Equal<T>
39+
procedure TestReverse;
3340
procedure TestSameStart;
3441
end;
3542

3643
TestArraysCatSnippets = class(TTestCase)
3744
published
3845
procedure TestByteArraysEqual;
46+
// The following test must come after TestByteArraysEqual since the test calls it
47+
procedure TestReverseByteArray;
3948
end;
4049

4150
implementation
@@ -54,13 +63,18 @@ procedure TestTArrayUtils.SetUp;
5463
fSA0 := TArray<string>.Create();
5564
fSA1 := TArray<string>.Create('foo');
5665
fSA2 := TArray<string>.Create('foo', 'bar');
66+
fSA2R := TArray<string>.Create('bar', 'foo');
5767
fSAM := TArray<string>.Create('a', 'stitch', 'in', 'time', 'saves', 'nine');
5868
fSAN := TArray<string>.Create('a', 'stitch', 'in', 'time', 'saves', 'nine');
69+
fSAR := TArray<string>.Create('nine', 'saves', 'time', 'in', 'stitch', 'a');
5970
fIA0 := TArray<Integer>.Create();
6071
fIA1 := TArray<Integer>.Create(42);
6172
fIA2 := TArray<Integer>.Create(42, 56);
73+
fIA3 := TArray<Integer>.Create(56, 42, 102);
74+
fIA3R := TArray<Integer>.Create(102, 42, 56);
6275
fIAM := TArray<Integer>.Create(1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37);
6376
fIAN := TArray<Integer>.Create(1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37);
77+
fIAR := TArray<Integer>.Create(37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1);
6478
fIAP := TArray<Integer>.Create(1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
6579
fIAX := TArray<Integer>.Create(1, 2, 3, 5, 4, 11, 13, 17, 19, 23, 29, 31);
6680
fIAY := TArray<Integer>.Create(0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37);
@@ -153,6 +167,25 @@ procedure TestTArrayUtils.TestLast;
153167
CheckEquals(37, TArrayUtils.Last<Integer>(fIAN), 'Test 6');
154168
end;
155169

170+
procedure TestTArrayUtils.TestReverse;
171+
var
172+
RS: TArray<string>;
173+
RI: TArray<Integer>;
174+
begin
175+
RS := TArrayUtils.Reverse<string>(fSAM);
176+
CheckTrue(TArrayUtils.Equal<string>(fSAR, RS, StringCompareFn), 'Test 1');
177+
RI := TArrayUtils.Reverse<Integer>(fIAM);
178+
CheckTrue(TArrayUtils.Equal<Integer>(fIAR, RI, IntegerCompareFn), 'Test 2');
179+
RS := TArrayUtils.Reverse<string>(fSA2);
180+
CheckTrue(TArrayUtils.Equal<string>(fSA2R, RS, StringCompareFn), 'Test 3');
181+
RI := TArrayUtils.Reverse<Integer>(fIA0);
182+
CheckTrue(TArrayUtils.Equal<Integer>(fIA0, RI, IntegerCompareFn), 'Test 4');
183+
RI := TArrayUtils.Reverse<Integer>(fIA1);
184+
CheckTrue(TArrayUtils.Equal<Integer>(fIA1, RI, IntegerCompareFn), 'Test 5');
185+
RI := TArrayUtils.Reverse<Integer>(fIA3);
186+
CheckTrue(TArrayUtils.Equal<Integer>(fIA3R, RI, IntegerCompareFn), 'Test 6');
187+
end;
188+
156189
procedure TestTArrayUtils.TestSameStart;
157190
begin
158191
CheckTrue(
@@ -233,6 +266,32 @@ procedure TestArraysCatSnippets.TestByteArraysEqual;
233266
CheckFalse(ByteArraysEqual(A0L, A1L), '#10');
234267
end;
235268

269+
procedure TestArraysCatSnippets.TestReverseByteArray;
270+
var
271+
A0, A1, A2, A6, A7, A4Sym, A5Sym: TBytes;
272+
R0, R1, R2, R6, R7: TBytes;
273+
begin
274+
SetLength(A0, 0);
275+
SetLength(R0, 0);
276+
A1 := TBytes.Create(42);
277+
R1 := TBytes.Create(42);
278+
A2 := TBytes.Create(42,56);
279+
R2 := TBytes.Create(56,42);
280+
A6 := TBytes.Create(1, 1, 2, 3, 5, 8);
281+
R6 := TBytes.Create(8, 5, 3, 2, 1, 1);
282+
A7 := TBytes.Create(0, 1, 1, 2, 3, 5, 8);
283+
R7 := TBytes.Create(8, 5, 3, 2, 1, 1, 0);
284+
A4Sym := TBytes.Create(3, 5, 5, 3);
285+
A5Sym := TBytes.Create(3, 5, 8, 5, 3);
286+
CheckTrue(ByteArraysEqual(R0, ReverseByteArray(A0)), '#0');
287+
CheckTrue(ByteArraysEqual(R1, ReverseByteArray(A1)), '#1');
288+
CheckTrue(ByteArraysEqual(R2, ReverseByteArray(A2)), '#2');
289+
CheckTrue(ByteArraysEqual(R6, ReverseByteArray(A6)), '#6');
290+
CheckTrue(ByteArraysEqual(R7, ReverseByteArray(A7)), '#7');
291+
CheckTrue(ByteArraysEqual(A4Sym, ReverseByteArray(A4Sym)), '#4 sym');
292+
CheckTrue(ByteArraysEqual(A5Sym, ReverseByteArray(A5Sym)), '#5 sym');
293+
end;
294+
236295
initialization
237296

238297
IntegerCompareFn := function (const Left, Right: Integer): Boolean

0 commit comments

Comments
 (0)