Skip to content

Commit 6c35091

Browse files
committed
Add tests for 14 new Maths functions
Tests were added for each 7 overloaded versions of the following routines: * ArraySum * SumOfLogs
1 parent 8212ad2 commit 6c35091

File tree

2 files changed

+493
-0
lines changed

2 files changed

+493
-0
lines changed

tests/Cat-Maths/TestUMathsCatSnippets.pas

+230
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ TestMathsCatSnippets = class(TTestCase)
4343
procedure TestPowNZN;
4444
procedure TestPowNZZ;
4545
procedure TestPowN;
46+
procedure TestArraySum_Single;
47+
procedure TestArraySum_Double;
48+
procedure TestArraySum_Extended;
49+
procedure TestArraySum_Integer;
50+
procedure TestArraySum_Int64;
51+
procedure TestArraySum_Cardinal;
52+
procedure TestArraySum_UInt64;
53+
procedure TestSumOfLogs_Single;
54+
procedure TestSumOfLogs_Double;
55+
procedure TestSumOfLogs_Extended;
56+
procedure TestSumOfLogs_Integer;
57+
procedure TestSumOfLogs_Cardinal;
58+
procedure TestSumOfLogs_Int64;
59+
procedure TestSumOfLogs_UInt64;
4660
end;
4761

4862
implementation
@@ -61,6 +75,89 @@ implementation
6175
509, 521, 523, 541
6276
);
6377

78+
PosExtendedArray: array[0..4] of Extended = (
79+
1.234, 4256.12345, 7000000000.0, PI, 0.000006758493
80+
);
81+
82+
PosDoubleArray: array[0..4] of Double = (
83+
1.234, 4256.12345, 7000000000.0, PI, 0.000006758493
84+
);
85+
86+
PosSingleArray: array[0..4] of Single = (
87+
1.234, 4256.12345, 7000000000.0, PI, 0.000006758493
88+
);
89+
90+
LnPosExtendedArray: array[0..4] of Extended = (
91+
// Sourced by entering PosExtendedArray values into Windows Calculator
92+
0.21026092548319607136082943601527,
93+
8.3561140367098338229057764041835,
94+
22.669175986001724461267275835602,
95+
1.1447298858494001741553183317696,
96+
-11.904710621755846032803428450819
97+
);
98+
99+
LnPosDoubleArray: array[0..4] of Double = (
100+
// Sourced by entering PosExtendedArray values into Windows Calculator
101+
0.21026092548319607136082943601527,
102+
8.3561140367098338229057764041835,
103+
22.669175986001724461267275835602,
104+
1.1447298858494001741553183317696,
105+
-11.904710621755846032803428450819
106+
);
107+
108+
LnPosSingleArray: array[0..4] of Single = (
109+
// Sourced by entering PosExtendedArray values into Windows Calculator
110+
0.21026092548319607136082943601527,
111+
8.3561140367098338229057764041835,
112+
22.669175986001724461267275835602,
113+
1.1447298858494001741553183317696,
114+
-11.904710621755846032803428450819
115+
);
116+
117+
PosUInt64Array: array[0..3] of UInt64 = (
118+
1, 4256, 782937294729473, 18446744073709551615 {High(UInt64)}
119+
);
120+
121+
LnPosUInt64Array: array[0..3] of Extended = (
122+
0.0,
123+
8.3560850310214803122005407826219,
124+
34.294073725352958690377297588172,
125+
44.361419555836499802648645664699
126+
);
127+
128+
PosInt64Array: array[0..3] of UInt64 = (
129+
1, 4256, 782937294729473, 9223372036854775807 {High(Int64)}
130+
);
131+
132+
LnPosInt64Array: array[0..3] of Extended = (
133+
0.0,
134+
8.3560850310214803122005407826219,
135+
34.294073725352958690377297588172,
136+
43.668272375276554493177203434617
137+
);
138+
139+
PosCardinalArray: array[0..3] of Cardinal = (
140+
1, 4256, 30392847, 4294967295 {High(Cardinal)}
141+
);
142+
143+
LnPosCardinalArray: array[0..3] of Extended = (
144+
0.0,
145+
8.3560850310214803122005407826219,
146+
17.229717842637359251929684335162,
147+
22.180709777685419257670453203439
148+
);
149+
150+
PosIntegerArray: array[0..3] of Integer = (
151+
1, 4256, 738473, 2147483647 {High(Integer)}
152+
);
153+
154+
LnPosIntegerArray: array[0..3] of Extended = (
155+
0.0,
156+
8.3560850310214803122005407826219,
157+
13.512339819689414368459751325714,
158+
21.487562596892643304518036290109
159+
);
160+
64161
function RectWidth(const Rect: TRect): Integer;
65162
begin
66163
Result := Rect.Right - Rect.Left;
@@ -97,6 +194,62 @@ procedure TestMathsCatSnippets.StretchRect_B_Except;
97194
R1 := StretchRect(R0, 1234567890.0);
98195
end;
99196

197+
procedure TestMathsCatSnippets.TestArraySum_Cardinal;
198+
const
199+
A: array[0..3] of Cardinal = (12, 78, 0, 3);
200+
Expected: Cardinal = 93;
201+
begin
202+
CheckEquals(Expected, ArraySum(A));
203+
end;
204+
205+
procedure TestMathsCatSnippets.TestArraySum_Double;
206+
const
207+
A: array[0..3] of Double = (93849367.8695, 0.003, 10294834.9473984, -98374.8593847);
208+
Expected: Double = 104045827.9605137;
209+
begin
210+
CheckTrue(SameValue(Expected, ArraySum(A)));
211+
end;
212+
213+
procedure TestMathsCatSnippets.TestArraySum_Extended;
214+
const
215+
A: array[0..3] of Extended = (93849367.8695, 0.003, 10294834.9473984, -98374.8593847);
216+
Expected: Extended = 104045827.9605137;
217+
begin
218+
CheckTrue(SameValue(Expected, ArraySum(A)));
219+
end;
220+
221+
procedure TestMathsCatSnippets.TestArraySum_Int64;
222+
const
223+
A: array[0..3] of Int64 = (-3849374778, +3849374780, 0, -8);
224+
Expected: Int64 = -6;
225+
begin
226+
CheckEquals(Expected, ArraySum(A));
227+
end;
228+
229+
procedure TestMathsCatSnippets.TestArraySum_Integer;
230+
const
231+
A: array[0..3] of Integer = (-(MaxInt-5), MaxInt, 0, -671);
232+
Expected: Integer = -666;
233+
begin
234+
CheckEquals(Expected, ArraySum(A));
235+
end;
236+
237+
procedure TestMathsCatSnippets.TestArraySum_Single;
238+
const
239+
A: array[0..3] of Single = (93849367.8695, 0.003, 10294834.9473984, -98374.8593847);
240+
Expected: Single = 104045827.9605137;
241+
begin
242+
CheckTrue(SameValue(Expected, ArraySum(A)));
243+
end;
244+
245+
procedure TestMathsCatSnippets.TestArraySum_UInt64;
246+
const
247+
A: array[0..3] of UInt64 = (9223372036854775808, 2, 90, 100);
248+
Expected: UInt64 = 9223372036854776000;
249+
begin
250+
CheckEquals(Expected, ArraySum(A));
251+
end;
252+
100253
procedure TestMathsCatSnippets.TestDigitCount;
101254
begin
102255
CheckEquals(1, DigitCount(0), 'DigitCount(0)');
@@ -714,6 +867,83 @@ procedure TestMathsCatSnippets.TestStretchRect_B;
714867
CheckException(StretchRect_B_Except, EOverflow, 'Large scaling');
715868
end;
716869

870+
procedure TestMathsCatSnippets.TestSumOfLogs_Cardinal;
871+
var
872+
Res, Expected: Extended;
873+
BoolRes: Boolean;
874+
begin
875+
Expected := ArraySum(LnPosCardinalArray); // SumOfArray result cast to Extended
876+
Res := SumOfLogs(PosCardinalArray);
877+
BoolRes := SameValue(Expected, Res);
878+
CheckTrue(BoolRes, 'Normal');
879+
end;
880+
881+
procedure TestMathsCatSnippets.TestSumOfLogs_Double;
882+
var
883+
Res, Expected: Double;
884+
BoolRes: Boolean;
885+
begin
886+
Expected := ArraySum(LnPosDoubleArray);
887+
Res := SumOfLogs(PosDoubleArray);
888+
BoolRes := SameValue(Expected, Res);
889+
CheckTrue(BoolRes, 'SumOfLogs_Double');
890+
end;
891+
892+
procedure TestMathsCatSnippets.TestSumOfLogs_Extended;
893+
var
894+
Res, Expected: Extended;
895+
BoolRes: Boolean;
896+
begin
897+
Expected := ArraySum(LnPosExtendedArray);
898+
Res := SumOfLogs(PosExtendedArray);
899+
BoolRes := SameValue(Expected, Res);
900+
CheckTrue(BoolRes, 'SumOfLogs_Extended');
901+
end;
902+
903+
procedure TestMathsCatSnippets.TestSumOfLogs_Int64;
904+
var
905+
Res, Expected: Extended;
906+
BoolRes: Boolean;
907+
begin
908+
Expected := ArraySum(LnPosInt64Array); // SumOfArray result cast to Extended
909+
Res := SumOfLogs(PosInt64Array);
910+
BoolRes := SameValue(Expected, Res);
911+
CheckTrue(BoolRes, 'SumOfLogs_Int64');
912+
end;
913+
914+
procedure TestMathsCatSnippets.TestSumOfLogs_Integer;
915+
var
916+
Res, Expected: Extended;
917+
BoolRes: Boolean;
918+
begin
919+
Expected := ArraySum(LnPosIntegerArray); // SumOfArray result cast to Extended
920+
Res := SumOfLogs(PosIntegerArray);
921+
BoolRes := SameValue(Expected, Res);
922+
CheckTrue(BoolRes, 'SumOfLogs_Integer');
923+
end;
924+
925+
procedure TestMathsCatSnippets.TestSumOfLogs_Single;
926+
var
927+
Res, Expected: Single;
928+
BoolRes: Boolean;
929+
begin
930+
Expected := ArraySum(LnPosSingleArray);
931+
Res := SumOfLogs(PosSingleArray);
932+
BoolRes := SameValue(Expected, Res);
933+
CheckTrue(BoolRes, 'SumOfLogs_Single');
934+
end;
935+
936+
procedure TestMathsCatSnippets.TestSumOfLogs_UInt64;
937+
var
938+
Res, Expected: Extended;
939+
BoolRes: Boolean;
940+
begin
941+
Expected := ArraySum(LnPosUInt64Array); // SumOfArray result cast to Extended
942+
Res := SumOfLogs(PosUInt64Array);
943+
BoolRes := SameValue(Expected, Res);
944+
CheckTrue(BoolRes, 'SumOfLogs_UInt64');
945+
end;
946+
717947
initialization
718948
// Register any test cases with the test runner
719949
RegisterTest(TestMathsCatSnippets.Suite);

0 commit comments

Comments
 (0)