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 6372ee4

Browse files
Add tests for Median function overloads
Added unit tests to TestUMathsCatSnippets for Integer & Double array overloads of Median function. Regenerated UMathsCatSnippets unit using CodeSnip to include the Median functions.
1 parent 34297bd commit 6372ee4

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

‎tests/Cat-Maths/TestUMathsCatSnippets.pas

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ TestMathsCatSnippets = class(TTestCase)
8686
procedure TestIsNarcissistic;
8787
procedure TestLSE; // required by SoftMax
8888
procedure TestSoftMax;
89+
procedure TestMedian_Integer;
90+
procedure TestMedian_Double;
8991
end;
9092

9193
implementation
@@ -881,6 +883,38 @@ procedure TestMathsCatSnippets.TestMaxOfArray_Single;
881883
Check(SameValue(N, MaxOfArray(A)), 'Test 5');
882884
end;
883885

886+
procedure TestMathsCatSnippets.TestMedian_Double;
887+
const
888+
Fudge = 0.000001;
889+
A1: array[1..1] of Double = (436.57);
890+
A2: array[1..2] of Double = (-123.45, 170.05);
891+
A5: array[1..5] of Double = (1.234, 4256.12345, 7000000000.0, PI, 0.000006758493);
892+
A6: array[1..6] of Double = (4883.937382, 37473.0, 235.00001, -99.9282, 42.32654, 56.986382);
893+
A7: array[1..7] of Double = (938298.0837, 729837.3627, 80001.34, 79876.46372, 67012.1234, 38983.12, 3500.93937);
894+
begin
895+
CheckTrue(SameValue(436.57, Median(A1), Fudge), '#1');
896+
CheckTrue(SameValue(23.3, Median(A2), Fudge), '#2');
897+
CheckTrue(SameValue(PI, Median(A5), Fudge), '#5');
898+
CheckTrue(SameValue(145.993196, Median(A6), Fudge), '#6');
899+
CheckTrue(SameValue(79876.46372, Median(A7), Fudge), '#7');
900+
end;
901+
902+
procedure TestMathsCatSnippets.TestMedian_Integer;
903+
const
904+
A1: array[1..1] of Integer = (4);
905+
A2: array[1..2] of Integer = (-6, 1);
906+
A5: array[1..5] of Integer = (1, 3, 5, 7, 9);
907+
A6: array[1..6] of Integer = (4883, 37473, 235, -99, 42, 56);
908+
A7: array[1..7] of Integer = (77, 66, 55, 44, 33, 22, 11);
909+
begin
910+
CheckTrue(SameValue(4.0, Median(A1)), '#1');
911+
CheckTrue(SameValue(-2.5, Median(A2)), '#2');
912+
CheckTrue(SameValue(5.0, Median(A5)), '#5');
913+
CheckTrue(SameValue(145.5, Median(A6)), '#6');
914+
CheckTrue(SameValue(44.0, Median(A7)), '#7');
915+
916+
end;
917+
884918
procedure TestMathsCatSnippets.TestMid_Double;
885919
var
886920
A, B, C, D, E: Double;

‎tests/Cat-Maths/UMathsCatSnippets.pas

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* The unit is copyright © 2005-2024 by Peter Johnson & Contributors and is
77
* licensed under the MIT License (https://opensource.org/licenses/MIT).
88
*
9-
* Generated on : 2025年1月10日 09:52:09 GMT.
9+
* Generated on : 2025年1月10日 19:22:03 GMT.
1010
* Generated by : DelphiDabbler CodeSnip Release 4.24.0.
1111
*
1212
* The latest version of CodeSnip is available from the CodeSnip GitHub project
@@ -18,7 +18,7 @@
1818
interface
1919

2020
uses
21-
SysUtils, Math, Types, Windows;
21+
SysUtils, Math, Types, Windows, Generics.Collections;
2222

2323
{
2424
Encapsulates a point with double precision floating point coordinates.
@@ -268,6 +268,18 @@ function MaxOfArray(const A: array of Integer): Integer; overload;
268268
}
269269
function MaxOfArray(const A: array of Single): Single; overload;
270270

271+
{
272+
Returns the median of an array of floating point values.
273+
Raises an EArgumentException exception if the array is empty.
274+
}
275+
function Median(A: array of Double): Double; overload;
276+
277+
{
278+
Returns the median of an array of integer values.
279+
Raises an EArgumentException exception if the array is empty.
280+
}
281+
function Median(A: array of Integer): Double; overload;
282+
271283
{
272284
Returns the middle of three double precision floating point values.
273285
}
@@ -1314,6 +1326,52 @@ function MaxOfArray(const A: array of Single): Single; overload;
13141326
Result := A[Idx];
13151327
end;
13161328

1329+
{
1330+
Returns the median of an array of floating point values.
1331+
Raises an EArgumentException exception if the array is empty.
1332+
}
1333+
function Median(A: array of Double): Double; overload;
1334+
var
1335+
MiddleLo: Integer;
1336+
begin
1337+
if System.Length(A) = 0 then
1338+
raise SysUtils.EArgumentException.Create('Array is empty');
1339+
// optimisations for array lengths 1 & 2 to avoid sorting
1340+
if System.Length(A) = 1 then
1341+
Exit(A[0]);
1342+
if System.Length(A) = 2 then
1343+
Exit((A[0] + A[1]) / 2.0);
1344+
Generics.Collections.TArray.Sort<Double>(A); // using standard comparer
1345+
MiddleLo := System.Length(A) div 2 - 1;
1346+
if System.Odd(System.Length(A)) then
1347+
Result := A[MiddleLo + 1]
1348+
else
1349+
Result := (A[MiddleLo] + A[MiddleLo + 1]) / 2.0;
1350+
end;
1351+
1352+
{
1353+
Returns the median of an array of integer values.
1354+
Raises an EArgumentException exception if the array is empty.
1355+
}
1356+
function Median(A: array of Integer): Double; overload;
1357+
var
1358+
MiddleLo: Integer;
1359+
begin
1360+
if System.Length(A) = 0 then
1361+
raise SysUtils.EArgumentException.Create('Array is empty');
1362+
// optimisations for array lengths 1 & 2 to avoid sorting
1363+
if System.Length(A) = 1 then
1364+
Exit(A[0]);
1365+
if System.Length(A) = 2 then
1366+
Exit((A[0] + A[1]) / 2);
1367+
Generics.Collections.TArray.Sort<Integer>(A); // using standard comparer
1368+
MiddleLo := System.Length(A) div 2 - 1;
1369+
if System.Odd(Length(A)) then
1370+
Result := A[MiddleLo + 1]
1371+
else
1372+
Result := (A[MiddleLo] + A[MiddleLo + 1]) / 2;
1373+
end;
1374+
13171375
{
13181376
Returns the middle of three double precision floating point values.
13191377
}

0 commit comments

Comments
(0)

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