Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e9d317

Browse files
Add tests for 8 new Maths routines
Added unit tests for each routine to TestUMathsCatSnippets unit. Regenerated UMathsCatSnippets unit using CodeSnip that contains the 8 new routines being tested.
1 parent 273051d commit 7e9d317

File tree

2 files changed

+617
-2
lines changed

2 files changed

+617
-2
lines changed

‎tests/Cat-Maths/TestUMathsCatSnippets.pas

Lines changed: 352 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,22 @@ TestMathsCatSnippets = class(TTestCase)
2424
procedure TestDigitPowerSum_EOverflow;
2525
procedure TestDigitPowerSum_EArgumentException;
2626
procedure TestLSE_EArgumentException;
27-
function EqualArrays(const Left, Right: TBytes): Boolean;
27+
procedure TestMinMaxOfArray_Integer_ExceptEmpty;
28+
procedure TestMinMaxOfArray_Double_ExceptEmpty;
29+
procedure TestNormaliseByWeight_Cardinal_ExceptEmpty;
30+
procedure TestNormaliseByWeight_Cardinal_ExceptZero;
31+
procedure TestNormaliseByWeight_Double_ExceptEmpty;
32+
procedure TestNormaliseByWeight_Double_ExceptNegative;
33+
procedure TestNormaliseByWeight_Double_ExceptZero;
34+
procedure TestRescaleRange_Integer_ExceptEmpty;
35+
procedure TestRescaleRange_Integer_ExceptAllValuesSame;
36+
procedure TestRescaleRange_Double_ExceptEmpty;
37+
procedure TestRescaleRange_Double_ExceptAllValuesSame;
38+
procedure TestRangeOf_Double_ExceptEmpty;
39+
procedure TestRangeOf_Integer_ExceptEmpty;
40+
function EqualArrays(const Left, Right: TBytes): Boolean; overload;
41+
function EqualArrays(const Left, Right: array of Double;
42+
Fudge: Double = 0.0): Boolean; overload;
2843
function ReverseArray(const A: TBytes): TBytes;
2944
published
3045
procedure TestDigitCount;
@@ -88,6 +103,14 @@ TestMathsCatSnippets = class(TTestCase)
88103
procedure TestSoftMax;
89104
procedure TestMedian_Integer;
90105
procedure TestMedian_Double;
106+
procedure TestMinMaxOfArray_Integer; // required by Rescale & RangeOf
107+
procedure TestMinMaxOfArray_Double; // required by Rescale & RangeOf
108+
procedure TestNormaliseByWeight_Cardinal;
109+
procedure TestNormaliseByWeight_Double;
110+
procedure TestRescaleRange_Integer;
111+
procedure TestRescaleRange_Double;
112+
procedure TestRangeOf_Integer;
113+
procedure TestRangeOf_Double;
91114
end;
92115

93116
implementation
@@ -210,6 +233,19 @@ function TestMathsCatSnippets.EqualArrays(const Left, Right: TBytes): Boolean;
210233
Exit(False);
211234
end;
212235

236+
function TestMathsCatSnippets.EqualArrays(const Left,
237+
Right: array of Double; Fudge: Double = 0.0): Boolean;
238+
var
239+
Idx: Integer;
240+
begin
241+
Result := True;
242+
if Length(Left) <> Length(Right) then
243+
Exit(False);
244+
for Idx := Low(Left) to High(Left) do
245+
if not SameValue(Left[Idx], Right[Idx], Fudge) then
246+
Exit(False);
247+
end;
248+
213249
function TestMathsCatSnippets.ReverseArray(const A: TBytes): TBytes;
214250
var
215251
I: Integer;
@@ -1015,6 +1051,90 @@ procedure TestMathsCatSnippets.TestMid_Single;
10151051
Check(SameValue(E, Mid(E, E, D)), 'Mid(E, E, D)');
10161052
end;
10171053

1054+
procedure TestMathsCatSnippets.TestMinMaxOfArray_Double;
1055+
const
1056+
A1: array[1..1] of Double = (436.57);
1057+
Min1 = 436.57; Max1 = 436.57;
1058+
A2: array[1..2] of Double = (-123.45, 170.05);
1059+
Min2 = -123.45; Max2 = 170.05;
1060+
A5: array[1..5] of Double = (1.234, 4256.12345, 7000000000.0, PI, 0.000006758493);
1061+
Min5 = 0.000006758493; Max5 = 7000000000.0;
1062+
A6: array[1..6] of Double = (4883.937382, 37473.0, 235.00001, -99.9282, 42.32654, 56.986382);
1063+
Min6 = -99.9282; Max6 = 37473.0;
1064+
A7: array[1..7] of Double = (938298.0837, 729837.3627, 80001.34, 79876.46372, 67012.1234, 38983.12, 3500.93937);
1065+
Min7 = 3500.93937; Max7 = 938298.0837;
1066+
var
1067+
MinVal, MaxVal: Double;
1068+
begin
1069+
MinMaxOfArray(A1, MinVal, MaxVal);
1070+
CheckTrue(SameValue(Min1, MinVal), '1 min');
1071+
CheckTrue(SameValue(Max1, MaxVal), '1 max');
1072+
MinMaxOfArray(A2, MinVal, MaxVal);
1073+
CheckTrue(SameValue(Min2, MinVal), '2 min');
1074+
CheckTrue(SameValue(Max2, MaxVal), '2 max');
1075+
MinMaxOfArray(A5, MinVal, MaxVal);
1076+
CheckTrue(SameValue(Min5, MinVal), '5 min');
1077+
CheckTrue(SameValue(Max5, MaxVal), '5 max');
1078+
MinMaxOfArray(A6, MinVal, MaxVal);
1079+
CheckTrue(SameValue(Min6, MinVal), '6 min');
1080+
CheckTrue(SameValue(Max6, MaxVal), '6 max');
1081+
MinMaxOfArray(A7, MinVal, MaxVal);
1082+
CheckTrue(SameValue(Min7, MinVal), '7 min');
1083+
CheckTrue(SameValue(Max7, MaxVal), '7 max');
1084+
CheckException(TestMinMaxOfArray_Double_ExceptEmpty, EArgumentException, 'Empty exception');
1085+
end;
1086+
1087+
procedure TestMathsCatSnippets.TestMinMaxOfArray_Double_ExceptEmpty;
1088+
var
1089+
A: array of Double;
1090+
Min, Max: Double;
1091+
begin
1092+
SetLength(A, 0);
1093+
MinMaxOfArray(A, Min, Max);
1094+
end;
1095+
1096+
procedure TestMathsCatSnippets.TestMinMaxOfArray_Integer;
1097+
const
1098+
A1: array[1..1] of Integer = (4);
1099+
Min1 = 4; Max1 = 4;
1100+
A2: array[1..2] of Integer = (-6, 1);
1101+
Min2 = -6; Max2 = 1;
1102+
A5: array[1..5] of Integer = (1, 3, 5, 7, 9);
1103+
Min5 = 1; Max5 = 9;
1104+
A6: array[1..6] of Integer = (4883, 37473, 235, -99, 42, 56);
1105+
Min6 = -99; Max6 = 37473;
1106+
A7: array[1..7] of Integer = (77, 66, 55, 44, 33, 22, 11);
1107+
Min7 = 11; Max7 = 77;
1108+
var
1109+
MinVal, MaxVal: Integer;
1110+
begin
1111+
MinMaxOfArray(A1, MinVal, MaxVal);
1112+
CheckEquals(Min1, MinVal, '1 min');
1113+
CheckEquals(Max1, MaxVal, '1 max');
1114+
MinMaxOfArray(A2, MinVal, MaxVal);
1115+
CheckEquals(Min2, MinVal, '2 min');
1116+
CheckEquals(Max2, MaxVal, '2 max');
1117+
MinMaxOfArray(A5, MinVal, MaxVal);
1118+
CheckEquals(Min5, MinVal, '5 min');
1119+
CheckEquals(Max5, MaxVal, '5 max');
1120+
MinMaxOfArray(A6, MinVal, MaxVal);
1121+
CheckEquals(Min6, MinVal, '6 min');
1122+
CheckEquals(Max6, MaxVal, '6 max');
1123+
MinMaxOfArray(A7, MinVal, MaxVal);
1124+
CheckEquals(Min7, MinVal, '7 min');
1125+
CheckEquals(Max7, MaxVal, '7 max');
1126+
CheckException(TestMinMaxOfArray_Integer_ExceptEmpty, EArgumentException, 'Empty exception');
1127+
end;
1128+
1129+
procedure TestMathsCatSnippets.TestMinMaxOfArray_Integer_ExceptEmpty;
1130+
var
1131+
A: array of Integer;
1132+
Min, Max: Integer;
1133+
begin
1134+
SetLength(A, 0);
1135+
MinMaxOfArray(A, Min, Max);
1136+
end;
1137+
10181138
procedure TestMathsCatSnippets.TestMinOfArray_Double;
10191139
var
10201140
A: TDoubleDynArray;
@@ -1115,6 +1235,100 @@ procedure TestMathsCatSnippets.TestMinOfArray_Single;
11151235
Check(SameValue(N, MinOfArray(A)), 'Test 5');
11161236
end;
11171237

1238+
procedure TestMathsCatSnippets.TestNormaliseByWeight_Cardinal;
1239+
const
1240+
A1: array[1..1] of Cardinal = (6);
1241+
E1: array[1..1] of Double = (1.0);
1242+
A2Eq: array[1..2] of Cardinal = (56, 56);
1243+
E2Eq: array[1..2] of Double = (0.5, 0.5);
1244+
A2Neq: array[1..2] of Cardinal = (56, 36);
1245+
E2Neq: array[1..2] of Double = (0.60869565217391304347826086956522, 0.39130434782608695652173913043478);
1246+
A10: array[1..10] of Cardinal = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1247+
E10: array[1..10] of Double = (1/55, 2/55, 3/55, 4/55, 5/55, 6/55, 7/55, 8/55, 9/55, 10/55);
1248+
A9: array[1..9] of Cardinal = (0, 1, 2, 3, 0, 1, 2, 3, 0);
1249+
E9: array[1..9] of Double = (0.0, 1/12, 2/12, 3/12, 0.0, 1/12, 2/12, 3/12, 0.0);
1250+
A5: array[1..5] of Cardinal = (0, 0, 76595787, 0, 0);
1251+
E5: array[1..5] of Double = (0.0, 0.0, 1.0, 0.0, 0.0);
1252+
A6: array[1..6] of Cardinal = (High(Cardinal), 0, High(Cardinal), 0, High(Cardinal), High(Cardinal));
1253+
E6: array[1..6] of Double = (0.25, 0.0, 0.25, 0.0, 0.25, 0.25);
1254+
begin
1255+
CheckTrue(EqualArrays(E1, NormaliseByWeight(A1)), '#1');
1256+
CheckTrue(EqualArrays(E2Eq, NormaliseByWeight(A2Eq)), '#2Eq');
1257+
CheckTrue(EqualArrays(E2Neq, NormaliseByWeight(A2Neq)), '#2Neq');
1258+
CheckTrue(EqualArrays(E10, NormaliseByWeight(A10)), '#10');
1259+
CheckTrue(EqualArrays(E9, NormaliseByWeight(A9)), '#9');
1260+
CheckTrue(EqualArrays(E5, NormaliseByWeight(A5)), '#5');
1261+
CheckTrue(EqualArrays(E6, NormaliseByWeight(A6)), '#6');
1262+
CheckException(TestNormaliseByWeight_Cardinal_ExceptEmpty, EArgumentException, 'Empty array');
1263+
CheckException(TestNormaliseByWeight_Cardinal_ExceptZero, EArgumentException, 'Array sums to zero');
1264+
end;
1265+
1266+
procedure TestMathsCatSnippets.TestNormaliseByWeight_Cardinal_ExceptEmpty;
1267+
var
1268+
A: array of Cardinal;
1269+
begin
1270+
SetLength(A, 0);
1271+
NormaliseByWeight(A);
1272+
end;
1273+
1274+
procedure TestMathsCatSnippets.TestNormaliseByWeight_Cardinal_ExceptZero;
1275+
const
1276+
Zero = Cardinal(0);
1277+
begin
1278+
NormaliseByWeight([Zero, Zero]);
1279+
end;
1280+
1281+
procedure TestMathsCatSnippets.TestNormaliseByWeight_Double;
1282+
const
1283+
A1: array[1..1] of Double = (5.6);
1284+
E1: array[1..1] of Double = (1.0);
1285+
A2Eq: array[1..2] of Double = (56.42, 56.42);
1286+
E2Eq: array[1..2] of Double = (0.5, 0.5);
1287+
A2Neq: array[1..2] of Double = (56.42, 36.953);
1288+
E2Neq: array[1..2] of Double = (0.60424319664142739335782292525677, 0.39575680335857260664217707474323);
1289+
A10: array[1..10] of Double = (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0);
1290+
E10: array[1..10] of Double = (1/55, 2/55, 3/55, 4/55, 5/55, 6/55, 7/55, 8/55, 9/55, 10/55);
1291+
A9: array[1..9] of Double = (0.0, 1.1, 2.2, 3.3, 0.0, 1.4, 2.5, 3.6, 0.0);
1292+
E9: array[1..9] of Double = (
1293+
0.0, 0.07801418439716312056737588652482, 0.15602836879432624113475177304965,
1294+
0.23404255319148936170212765957447, 0.0, 0.09929078014184397163120567375887,
1295+
0.17730496453900709219858156028369, 0.25531914893617021276595744680851, 0.0
1296+
);
1297+
A5a: array[1..5] of Double = (0.0, 0.0, 7659574468.085176, 0.0, 0.0);
1298+
E5a: array[1..5] of Double = (0.0, 0.0, 1.0, 0.0, 0.0);
1299+
A5b: array[1..5] of Double = (0.0, 0.0, 7659574468.085176, 0.0, 7659574468.085176);
1300+
E5b: array[1..5] of Double = (0.0, 0.0, 0.5, 0.0, 0.5);
1301+
begin
1302+
CheckTrue(EqualArrays(E1, NormaliseByWeight(A1)), '#1');
1303+
CheckTrue(EqualArrays(E2Eq, NormaliseByWeight(A2Eq)), '#2Eq');
1304+
CheckTrue(EqualArrays(E2Neq, NormaliseByWeight(A2Neq)), '#2Neq');
1305+
CheckTrue(EqualArrays(E10, NormaliseByWeight(A10)), '#10');
1306+
CheckTrue(EqualArrays(E9, NormaliseByWeight(A9)), '#9');
1307+
CheckTrue(EqualArrays(E5a, NormaliseByWeight(A5a)), '#5a');
1308+
CheckTrue(EqualArrays(E5b, NormaliseByWeight(A5b)), '#5b');
1309+
CheckException(TestNormaliseByWeight_Double_ExceptEmpty, EArgumentException, 'Empty array');
1310+
CheckException(TestNormaliseByWeight_Double_ExceptNegative, EArgumentException, 'Negative values in array');
1311+
CheckException(TestNormaliseByWeight_Double_ExceptZero, EArgumentException, 'Array sums to zero');
1312+
end;
1313+
1314+
procedure TestMathsCatSnippets.TestNormaliseByWeight_Double_ExceptEmpty;
1315+
var
1316+
A: array of Double;
1317+
begin
1318+
SetLength(A, 0);
1319+
NormaliseByWeight(A);
1320+
end;
1321+
1322+
procedure TestMathsCatSnippets.TestNormaliseByWeight_Double_ExceptNegative;
1323+
begin
1324+
NormaliseByWeight([0.0, 4.2, -34.8736, 56.67]);
1325+
end;
1326+
1327+
procedure TestMathsCatSnippets.TestNormaliseByWeight_Double_ExceptZero;
1328+
begin
1329+
NormaliseByWeight([0.0, 0.0, 0.0]);
1330+
end;
1331+
11181332
procedure TestMathsCatSnippets.TestPowN;
11191333
begin
11201334
CheckEquals(0, PowN(0, 2), 'PowN(0,2)');
@@ -1193,6 +1407,143 @@ procedure TestMathsCatSnippets.TestPowNZZ;
11931407
CheckEquals(4294967296, PowNZZ(2, 32), 'PowNZZ(2, 32');
11941408
end;
11951409

1410+
procedure TestMathsCatSnippets.TestRangeOf_Double;
1411+
const
1412+
AA: array[1..5] of Double = (1.234, 4256.12345, 7000000000.0, PI, 0.000006758493);
1413+
EA: Double = 6999999999.999993241507;
1414+
AB: array[1..4] of Double = (0.00001, 0.00002, 0.00004, 0.00003);
1415+
EB: Double = 0.00003;
1416+
AC: array[1..4] of Double = (0.0, 0.0, 0.0, 0.0);
1417+
EC: Double = 0.0;
1418+
AD: array[1..4] of Double = (-42.0, 0.1, 36.8, 56.0);
1419+
ED: Double = 98.0;
1420+
AE: array[1..4] of Double = (-56.0, -60.6, -42.0, -56.0);
1421+
EE: Double = 18.6;
1422+
AF: array[1..1] of Double = (42.0);
1423+
EF: Double = 0.0;
1424+
begin
1425+
CheckTrue(SameValue(EA, RangeOf(AA)), 'A');
1426+
CheckTrue(SameValue(EB, RangeOf(AB)), 'B');
1427+
CheckTrue(SameValue(EC, RangeOf(AC)), 'C');
1428+
CheckTrue(SameValue(ED, RangeOf(AD)), 'D');
1429+
CheckTrue(SameValue(EE, RangeOf(AE)), 'E');
1430+
CheckTrue(SameValue(EF, RangeOf(AF)), 'F');
1431+
CheckException(TestRangeOf_Double_ExceptEmpty, EArgumentException, 'Empty array');
1432+
end;
1433+
1434+
procedure TestMathsCatSnippets.TestRangeOf_Double_ExceptEmpty;
1435+
var
1436+
A: array of Double;
1437+
begin
1438+
SetLength(A, 0);
1439+
RangeOf(A);
1440+
end;
1441+
1442+
procedure TestMathsCatSnippets.TestRangeOf_Integer;
1443+
const
1444+
AA: array[1..5] of Integer = (1, 4256, 7000000, 3, 1);
1445+
EA: Double = 6999999;
1446+
AB: array[1..4] of Integer = (0, 2, 4, 3);
1447+
EB: Integer = 4;
1448+
AC: array[1..4] of Integer = (0, 0, 0, 0);
1449+
EC: Integer = 0;
1450+
AD: array[1..4] of Integer = (-42, 1, 37, 56);
1451+
ED: Integer = 98;
1452+
AE: array[1..4] of Integer = (-56, -60, -42, -56);
1453+
EE: Integer = 18;
1454+
AF: array[1..1] of Integer = (42);
1455+
EF: Integer = 0;
1456+
begin
1457+
CheckEquals(EA, RangeOf(AA), 'A');
1458+
CheckEquals(EB, RangeOf(AB), 'B');
1459+
CheckEquals(EC, RangeOf(AC), 'C');
1460+
CheckEquals(ED, RangeOf(AD), 'D');
1461+
CheckEquals(EE, RangeOf(AE), 'E');
1462+
CheckEquals(EF, RangeOf(AF), 'F');
1463+
CheckException(TestRangeOf_Integer_ExceptEmpty, EArgumentException, 'Empty array');
1464+
end;
1465+
1466+
procedure TestMathsCatSnippets.TestRangeOf_Integer_ExceptEmpty;
1467+
var
1468+
A: array of Integer;
1469+
begin
1470+
SetLength(A, 0);
1471+
RangeOf(A);
1472+
end;
1473+
1474+
procedure TestMathsCatSnippets.TestRescaleRange_Double;
1475+
// Expected results marked with * were calculated using
1476+
// https://www.educba.com/normalization-formula/, with an accuracy to 2DP, so
1477+
// a fudge factor of 0.01 was used when comparing those results
1478+
const
1479+
Fudge2 = 0.01;
1480+
AA: array[1..5] of Double = (2.3, 5.4, 6.279, 1.4, 12.78);
1481+
EA{*}: array[1..5] of Double = (0.08, 0.35, 0.43, 0.00, 1.00);
1482+
AB: array[1..6] of Double = (-5.4, -2.3, -2.3, 0.0, 1.4, 3.7);
1483+
EB{*}: array[1..6] of Double = (0.00, 0.34, 0.34, 0.59, 0.75, 1.00);
1484+
AC1: array[1..2] of Double = (42.42, 56.56);
1485+
AC2: array[1..2] of Double = (-PI, +PI);
1486+
EC: array[1..2] of Double = (0.0, 1.0);
1487+
AD: array[1..4] of Double = (-2345.6, -1200.76, -999.99, -875.20);
1488+
ED{*}: array[1..4] of Double = (0.0, 0.78, 0.92, 1.0);
1489+
begin
1490+
CheckTrue(EqualArrays(EA, RescaleRange(AA), Fudge2), 'A');
1491+
CheckTrue(EqualArrays(EB, RescaleRange(AB), Fudge2), 'B');
1492+
CheckTrue(EqualArrays(EC, RescaleRange(AC1)), 'C1');
1493+
CheckTrue(EqualArrays(EC, RescaleRange(AC2)), 'C2');
1494+
CheckTrue(EqualArrays(ED, RescaleRange(AD), Fudge2), 'D');
1495+
CheckException(TestRescaleRange_Double_ExceptEmpty, EArgumentException, 'Empty');
1496+
CheckException(TestRescaleRange_Double_ExceptAllValuesSame, EArgumentException, 'All values same');
1497+
end;
1498+
1499+
procedure TestMathsCatSnippets.TestRescaleRange_Double_ExceptAllValuesSame;
1500+
begin
1501+
RescaleRange([1.345, 1.345, 1.345, 1.345]);
1502+
end;
1503+
1504+
procedure TestMathsCatSnippets.TestRescaleRange_Double_ExceptEmpty;
1505+
var
1506+
A: array of Double;
1507+
begin
1508+
SetLength(A, 0);
1509+
RescaleRange(A);
1510+
end;
1511+
1512+
procedure TestMathsCatSnippets.TestRescaleRange_Integer;
1513+
const
1514+
Fudge2 = 0.01;
1515+
AA: array[1..5] of Integer = (2, 5, 6, 1, 12);
1516+
EA{*}: array[1..5] of Double = (0.091, 0.364, 0.455, 0.00, 1.00);
1517+
AB: array[1..6] of Integer = (-5, -2, -2, 0, 1, 4);
1518+
EB{*}: array[1..6] of Double = (0.0, 0.3333, 0.3333, 0.555555, 0.666666, 1.00);
1519+
AC1: array[1..2] of Integer = (42, 56);
1520+
AC2: array[1..2] of Integer = (-8, +8);
1521+
EC: array[1..2] of Double = (0.0, 1.0);
1522+
AD: array[1..4] of Integer = (-2345, -1201, -1000, -875);
1523+
ED{*}: array[1..4] of Double = (0.0, 0.78, 0.91, 1.0);
1524+
begin
1525+
CheckTrue(EqualArrays(EA, RescaleRange(AA), Fudge2), 'A');
1526+
CheckTrue(EqualArrays(EB, RescaleRange(AB), Fudge2), 'B');
1527+
CheckTrue(EqualArrays(EC, RescaleRange(AC1)), 'C1');
1528+
CheckTrue(EqualArrays(EC, RescaleRange(AC2)), 'C2');
1529+
CheckTrue(EqualArrays(ED, RescaleRange(AD), Fudge2), 'D');
1530+
CheckException(TestRescaleRange_Integer_ExceptEmpty, EArgumentException, 'Empty');
1531+
CheckException(TestRescaleRange_Integer_ExceptAllValuesSame, EArgumentException, 'All values same');
1532+
end;
1533+
1534+
procedure TestMathsCatSnippets.TestRescaleRange_Integer_ExceptAllValuesSame;
1535+
begin
1536+
RescaleRange([3, 3, 3, 3]);
1537+
end;
1538+
1539+
procedure TestMathsCatSnippets.TestRescaleRange_Integer_ExceptEmpty;
1540+
var
1541+
A: array of Integer;
1542+
begin
1543+
SetLength(A, 0);
1544+
RescaleRange(A);
1545+
end;
1546+
11961547
procedure TestMathsCatSnippets.TestResizeRect_A;
11971548
var
11981549
R: Types.TRect;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /