Skip to content

Commit a19dc9e

Browse files
committed
Merge branch 'feature/37-add-generic-array-reverse-fn' into develop
Fixes #37
2 parents a6d67b3 + bc5cf11 commit a19dc9e

File tree

5 files changed

+525
-84
lines changed

5 files changed

+525
-84
lines changed

Diff for: collection/623.dat

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
const Count: Integer;
2222
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>):
2323
Boolean; static;
24+
// Creates and returns a new array that is the reverse of the given array.
25+
class function Reverse<T>(const A: array of T): TArray<T>; static;
2426
end;
2527

2628
class function TArrayUtils.Equal<T>(const Left, Right: array of T;
@@ -60,6 +62,15 @@ begin
6062
Result := A[Pred(Length(A))];
6163
end;
6264

65+
class function TArrayUtils.Reverse<T>(const A: array of T): TArray<T>;
66+
var
67+
I: Integer;
68+
begin
69+
SetLength(Result, Length(A));
70+
for I := 0 to High(A) do
71+
Result[High(A)-I] := A[I];
72+
end;
73+
6374
class function TArrayUtils.SameStart<T>(const Left, Right: array of T;
6475
const Count: Integer;
6576
const EqualityComparer: Generics.Defaults.TEqualityComparison<T>): Boolean;

Diff for: collection/656.dat

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function ReverseByteArray(const A: array of Byte): TBytes;
2+
var
3+
I: Integer;
4+
begin
5+
SetLength(Result, Length(A));
6+
for I := 0 to High(A) do
7+
Result[High(A)-I] := A[I];
8+
end;

Diff for: collection/arrays.ini

+14-8
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,19 @@ Delphi5=N
363363
Delphi6=N
364364
Delphi7=N
365365
Delphi2005Win32=N
366-
Delphi2006Win32=Y
366+
Delphi2006Win32=N
367367
Delphi2007=N
368-
Delphi2009Win32=Y
369-
Delphi2010=Y
370368
DelphiXE=Y
371-
DelphiXE2=Y
372-
DelphiXE3=Y
373-
DelphiXE4=Y
374-
Delphi10S=Y
375-
FPC=N
369+
Delphi12A=Y
370+
371+
[ReverseByteArray]
372+
Kind=routine
373+
DisplayName=ReverseByteArray
374+
DescEx="<p>Returns a copy of a given byte array with the order of the bytes reversed.</p>"
375+
Depends=TBytes
376+
TestInfo=advanced
377+
AdvancedTest.Level=unit-tests
378+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Arrays"
379+
Snip=656.dat
380+
DelphiXE=Y
381+
Delphi12A=Y

Diff for: tests/Cat-Arrays/TestUArraysCatSnippets.pas

+95-40
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,30 @@
11
unit TestUArraysCatSnippets;
22

3-
{$UNDEF Generics}
4-
{$IFNDEF FPC}
5-
{$IFDEF CONDITIONALEXPRESSIONS}
6-
{$IF CompilerVersion >= 14.00}
7-
{$WARN SYMBOL_PLATFORM OFF}
8-
{$WARN SYMBOL_DEPRECATED OFF}
9-
{$WARN SYMBOL_LIBRARY OFF}
10-
{$IFEND}
11-
{$IF CompilerVersion >= 15.00}
12-
{$WARN UNSAFE_TYPE OFF}
13-
{$WARN UNSAFE_CAST OFF}
14-
{$WARN UNSAFE_CODE OFF}
15-
{$IFEND}
16-
{$IF CompilerVersion >= 20.00}
17-
{$WARN EXPLICIT_STRING_CAST OFF}
18-
{$WARN IMPLICIT_STRING_CAST OFF}
19-
{$DEFINE Generics}
20-
{$IFEND}
21-
{$ENDIF}
22-
{$ENDIF}
23-
243
interface
254

265
uses
276
TestFramework, UArraysCatSnippets;
287

298
type
309

31-
{$IFDEF Generics}
3210
TestTArrayUtils = class(TTestCase)
3311
strict private
3412
fSA0: TArray<string>;
3513
fSA1: TArray<string>;
3614
fSA2: TArray<string>;
15+
fSA2R: TArray<string>;
3716
fSAM: TArray<string>;
3817
fSAN: TArray<string>;
18+
fSAR: TArray<string>;
3919
fIA0: TArray<Integer>;
4020
fIA1: TArray<Integer>;
4121
fIA2: TArray<Integer>;
22+
fIA3: TArray<Integer>;
23+
fIA3R: TArray<Integer>;
4224
fIAM: TArray<Integer>;
4325
fIAN: TArray<Integer>;
4426
fIAP: TArray<Integer>;
27+
fIAR: TArray<Integer>;
4528
fIAX: TArray<Integer>;
4629
fIAY: TArray<Integer>;
4730
protected
@@ -52,30 +35,26 @@ TestTArrayUtils = class(TTestCase)
5235
procedure TestLast;
5336
procedure TestIndexOf;
5437
procedure TestEqual;
38+
// TestReverse must come after TestEqual since the test calls TArrayUtils.Equal<T>
39+
procedure TestReverse;
5540
procedure TestSameStart;
5641
end;
57-
{$ENDIF Generics}
5842

5943
TestArraysCatSnippets = class(TTestCase)
60-
44+
published
45+
procedure TestByteArraysEqual;
46+
// The following test must come after TestByteArraysEqual since the test calls it
47+
procedure TestReverseByteArray;
6148
end;
6249

6350
implementation
6451

6552
uses
66-
SysUtils
67-
{$IFDEF Generics}
68-
, Generics.Defaults
69-
{$ENDIF Generics}
70-
;
53+
SysUtils, Generics.Defaults;
7154

72-
{$IFDEF Generics}
7355
var
7456
IntegerCompareFn: TEqualityComparison<Integer>;
7557
StringCompareFn: TEqualityComparison<string>;
76-
{$ENDIF Generics}
77-
78-
{$IFDEF Generics}
7958

8059
{ TestTArrayUtils }
8160

@@ -84,13 +63,18 @@ procedure TestTArrayUtils.SetUp;
8463
fSA0 := TArray<string>.Create();
8564
fSA1 := TArray<string>.Create('foo');
8665
fSA2 := TArray<string>.Create('foo', 'bar');
66+
fSA2R := TArray<string>.Create('bar', 'foo');
8767
fSAM := TArray<string>.Create('a', 'stitch', 'in', 'time', 'saves', 'nine');
8868
fSAN := TArray<string>.Create('a', 'stitch', 'in', 'time', 'saves', 'nine');
69+
fSAR := TArray<string>.Create('nine', 'saves', 'time', 'in', 'stitch', 'a');
8970
fIA0 := TArray<Integer>.Create();
9071
fIA1 := TArray<Integer>.Create(42);
9172
fIA2 := TArray<Integer>.Create(42, 56);
73+
fIA3 := TArray<Integer>.Create(56, 42, 102);
74+
fIA3R := TArray<Integer>.Create(102, 42, 56);
9275
fIAM := TArray<Integer>.Create(1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37);
9376
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);
9478
fIAP := TArray<Integer>.Create(1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
9579
fIAX := TArray<Integer>.Create(1, 2, 3, 5, 4, 11, 13, 17, 19, 23, 29, 31);
9680
fIAY := TArray<Integer>.Create(0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37);
@@ -183,6 +167,25 @@ procedure TestTArrayUtils.TestLast;
183167
CheckEquals(37, TArrayUtils.Last<Integer>(fIAN), 'Test 6');
184168
end;
185169

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+
186189
procedure TestTArrayUtils.TestSameStart;
187190
begin
188191
CheckTrue(
@@ -229,11 +232,67 @@ procedure TestTArrayUtils.TestSameStart;
229232
);
230233
end;
231234

232-
{$ENDIF Generics}
235+
{ TestArraysCatSnippets }
233236

234-
initialization
237+
procedure TestArraysCatSnippets.TestByteArraysEqual;
238+
var
239+
A0L, A0R: TBytes;
240+
A1L, A1Req, A1Rneq: TBytes;
241+
ANL, ANReq, ANRneq1, ANRneq2, ANRneq3, ANRneq4: TBytes;
242+
AMR: TBytes;
243+
begin
244+
SetLength(A0L, 0);
245+
SetLength(A0R, 0);
246+
A1L := TBytes.Create(42);
247+
A1Req := TBytes.Create(42);
248+
A1Rneq := TBytes.Create(56);
249+
ANL := TBytes.Create(27,98,128,46,35,0,1);
250+
ANReq := TBytes.Create(27,98,128,46,35,0,1);
251+
ANRneq1 := TBytes.Create(27,98,128,46,35,0,7);
252+
ANRneq2 := TBytes.Create(26,98,128,46,35,0,1);
253+
ANRneq3 := TBytes.Create(27,98,67,46,35,0,1);
254+
ANRneq4 := TBytes.Create(27,17,67,46,35,12,1);
255+
AMR := TBytes.Create(27,98,128,35,0,1);
235256

236-
{$IFDEF Generics}
257+
CheckTrue(ByteArraysEqual(A0L, A0R), '#1');
258+
CheckTrue(ByteArraysEqual(A1L, A1Req), '#2');
259+
CheckFalse(ByteArraysEqual(A1L, A1Rneq), '#3');
260+
CheckTrue(ByteArraysEqual(ANL, ANReq), '#4');
261+
CheckFalse(ByteArraysEqual(A1L, ANRneq1), '#5');
262+
CheckFalse(ByteArraysEqual(A1L, ANRneq2), '#6');
263+
CheckFalse(ByteArraysEqual(A1L, ANRneq3), '#7');
264+
CheckFalse(ByteArraysEqual(A1L, ANRneq4), '#8');
265+
CheckFalse(ByteArraysEqual(A1L, AMR), '#9');
266+
CheckFalse(ByteArraysEqual(A0L, A1L), '#10');
267+
end;
268+
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+
295+
initialization
237296

238297
IntegerCompareFn := function (const Left, Right: Integer): Boolean
239298
begin
@@ -245,12 +304,8 @@ initialization
245304
Result := SameStr(Left, Right);
246305
end;
247306

248-
{$ENDIF Generics}
249-
250307
// Register any test cases with the test runner
251-
{$IFDEF Generics}
252308
RegisterTest(TestTArrayUtils.Suite);
253-
{$ENDIF Generics}
254309
RegisterTest(TestArraysCatSnippets.Suite);
255310

256311
end.

0 commit comments

Comments
 (0)