-
Notifications
You must be signed in to change notification settings - Fork 3
/
uTestMapReduce.pas
135 lines (107 loc) · 4.05 KB
/
uTestMapReduce.pas
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
unit uTestMapReduce;
interface
uses
DUnitX.TestFramework, System.SysUtils, RegularExpressions, GBEArray;
type
[TestFixture]
TestMapReduce = class
private
function convertStringToArrayInt(aString : string):TArray<integer>;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
[Test]
[TestCase('Test 1 MAP OK','1;2;3;53;-2,2;4;6;106;-4')]
[TestCase('Test 2 MAP OK','32;12;-43;68;89;13;103;23,64;24;-86;136;178;26;206;46')]
procedure TestMapInteger(aValue, expectedResult : string);
[Test]
[TestCase('Test 3 MAP K0 (valeur)','1;2;3;53;-2,2;4;6;106;4')]
[TestCase('Test 4 MAP K0 (longueur)','1;2;3;53,2;4;6;106;4')]
procedure TestMapIntegerKO(aValue, expectedResult : string);
[Test]
[TestCase('Test 5 REDUCE OK','5;8;3,16')]
[TestCase('Test 6 REDUCE OK','1;2;3;53;-2,57')]
procedure TestReduceInteger(aValue, expectedResult : string);
[Test]
[TestCase('Test 7 EXTRACT OK','1;2;3;4;5;6;7;8;9;10,4;5;6;7;8;9')]
procedure TestExtractInteger(aValue, expectedResult : string);
end;
implementation
uses System.Generics.Collections, System.Generics.Defaults, System.Types;
function TestMapReduce.convertStringToArrayInt(aString: string): TArray<integer>;
begin
var sep := ';';
var tabEntree := aString.Split(sep);
SetLength(result, length(tabEntree));
for var i := 0 to length(tabEntree)-1 do begin
result[i] := StrToIntdef(tabEntree[i],0);
end;
end;
procedure TestMapReduce.Setup;
begin
end;
procedure TestMapReduce.TearDown;
begin
end;
procedure TestMapReduce.TestMapInteger(aValue, expectedResult : string);
begin
var tabEntreeInt := convertStringToArrayInt(aValue);
var tabExpectedResultInt := convertStringToArrayInt(expectedResult);
var resultat := TGBEArray<integer>.Create(tabEntreeInt)
.Map<integer>(function(value: integer): integer begin result := value * 2; end)
.ToArray;
var isOK := length(resultat) = length(tabExpectedResultInt);
if isOK then begin
for var i := 0 to length(resultat)-1 do begin
isOK := resultat[i] = tabExpectedResultInt[i];
if not(isOk) then break;
end;
end;
assert.IsTrue(isOK);
end;
procedure TestMapReduce.TestMapIntegerKO(aValue, expectedResult : string);
begin
var tabEntreeInt := convertStringToArrayInt(aValue);
var tabExpectedResultInt := convertStringToArrayInt(expectedResult);
var resultat := TGBEArray<integer>.Create(tabEntreeInt)
.Map<integer>(function(value: integer): integer begin result := value * 2; end)
.ToArray;
var isOK := length(resultat) = length(tabExpectedResultInt);
if isOK then begin
for var i := 0 to length(resultat)-1 do begin
isOK := resultat[i] = tabExpectedResultInt[i];
if not(isOk) then break;
end;
end;
assert.IsFalse(isOK);
end;
procedure TestMapReduce.TestReduceInteger(aValue, expectedResult: string);
begin
var tabEntreeInt := convertStringToArrayInt(aValue);
var expectedResultInt := strtointdef(expectedResult, -1);
var resultat := TGBEArray<integer>.Create(tabEntreeInt)
.Reduce<integer>(function(valeurPrec, valeur: integer): integer
begin
result := valeurPrec + valeur;
end, 0);
var isOK := resultat = expectedResultInt;
assert.IsTrue(isOk);
end;
procedure TestMapReduce.TestExtractInteger(aValue, expectedResult: string);
begin
var tabEntreeInt := convertStringToArrayInt(aValue);
var resultat := TGBEArray<integer>.Create(tabEntreeInt)
.Extract(3,8);
var s := '';
for var i := 0 to length(resultat.toArray) -1 do begin
if s.IsEmpty then s := resultat.toArray[i].ToString
else s := s + ';' + resultat.toArray[i].ToString;
end;
var isOK := s = expectedResult;
assert.IsTrue(isOk);
end;
initialization
TDUnitX.RegisterTestFixture(TestMapReduce);
end.