@@ -75,7 +75,18 @@ TestMathsCatSnippets = class(TTestCase)
75
75
procedure TestWeightedPowerMean_Double_ExceptNegativeWeight ;
76
76
procedure TestWeightedPowerMean_Double_ExceptZeroWeights ;
77
77
procedure TestWeightedPowerMean_Double_ExceptNegativeValues ;
78
+ procedure TestCountOccurrences_ExceptEmptyArray ;
79
+ procedure TestMode_ExceptEmptyArray ;
80
+ procedure TestMode_ExceptSingleElementArray ;
81
+ procedure TestModeAlt_ExceptEmptyArray ;
82
+ procedure TestModeAlt_ExceptSingleElementArray ;
83
+ procedure TestHasMode_ExceptEmptyArray ;
84
+ procedure TestHasMode_ExceptSingleElementArray ;
85
+ procedure TestModeCount_ExceptEmptyArray ;
86
+ procedure TestModeCount_ExceptSingleElementArray ;
78
87
function EqualArrays (const Left, Right: TBytes): Boolean; overload;
88
+ function EqualArrays (const Left, Right: array of Integer): Boolean;
89
+ overload;
79
90
function EqualArrays (const Left, Right: array of Double;
80
91
Fudge: Double = 0.0 ): Boolean; overload;
81
92
function ReverseArray (const A: TBytes): TBytes;
@@ -171,10 +182,18 @@ TestMathsCatSnippets = class(TTestCase)
171
182
procedure TestWeightedPowerMean_Double ; // required by Integer & Cardinal overloads
172
183
procedure TestWeightedPowerMean_Cardinal ;
173
184
procedure TestWeightedPowerMean_Integer ;
185
+ procedure TestCountOccurrences ; // required by Mode, ModeAlt, HasMode & ModeCount
186
+ procedure TestMode ;
187
+ procedure TestModeAlt ;
188
+ procedure TestHasMode ;
189
+ procedure TestModeCount ;
174
190
end ;
175
191
176
192
implementation
177
193
194
+ uses
195
+ Generics.Defaults, Generics.Collections;
196
+
178
197
const
179
198
First100Primes: array [1 ..100 ] of Int64 = (
180
199
2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 ,
@@ -306,6 +325,19 @@ function TestMathsCatSnippets.EqualArrays(const Left,
306
325
Exit(False);
307
326
end ;
308
327
328
+ function TestMathsCatSnippets.EqualArrays (const Left,
329
+ Right: array of Integer): Boolean;
330
+ var
331
+ Idx: Integer;
332
+ begin
333
+ Result := True;
334
+ if Length(Left) <> Length(Right) then
335
+ Exit(False);
336
+ for Idx := Low(Left) to High(Left) do
337
+ if Left[Idx] <> Right[Idx] then
338
+ Exit(False);
339
+ end ;
340
+
309
341
function TestMathsCatSnippets.ReverseArray (const A: TBytes): TBytes;
310
342
var
311
343
I: Integer;
@@ -470,6 +502,117 @@ procedure TestMathsCatSnippets.TestArraySum_UInt64;
470
502
CheckEquals(Expected, ArraySum(A));
471
503
end ;
472
504
505
+ procedure TestMathsCatSnippets.TestCountOccurrences ;
506
+ const
507
+ A: array [1 ..4 ] of Integer = (2 , 2 , 2 , 2 ); // [2->4]
508
+ B: array [1 ..9 ] of Integer = (-1 , 3 , 4 , -1 , 8 , 3 , -1 , 4 , 7 ); // [-1:3,3:2,4:2,7:1,8:1
509
+ C: array [1 ..10 ] of Integer = (2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 5 , 6 ); // [2:3,3:3,4:2,5:1,6:1
510
+ D: array [1 ..8 ] of Integer = (-42 , -1 , -1 , 0 , 56 , 0 , -42 , 56 ); // [-42:2,-1:2,0:2,56:2]
511
+ E: array [1 ..4 ] of Integer = (1 , 2 , 3 , 4 ); // [1:1,2:1,3:1,4:1]
512
+ F: array [1 ..10 ] of Integer = (42 , 42 , 42 , 42 , 56 , 56 , 56 , 56 , 56 , 56 ); // [42:4,56:6]
513
+ G: array [1 ..9 ] of Integer = (1 , 2 , 3 , 4 , 5 , 4 , 3 , 2 , 1 ); // [1:2,2:2,3:2,4:2,5:1]
514
+ H: array [1 ..8 ] of Integer = (1 , 2 , 4 , 4 , 4 , 4 , -2 , -3 ); // [-3:1,-2:1,1:1,2:1,4:4,]
515
+ I: array [1 ..5 ] of Integer = (21 , 22 , 23 , 24 , 25 ); // [21:1,22:1,23:1,24:1,25:1]
516
+
517
+ function EqualMaps (A, B: TArray<TPair<Integer,Cardinal>>): Boolean;
518
+ var
519
+ Idx: Integer;
520
+ begin
521
+ if Length(A) <> Length(B) then
522
+ Exit(False);
523
+ Result := True;
524
+ for Idx := 0 to Pred(Length(A)) do
525
+ if (A[Idx].Key <> B[Idx].Key) or (A[Idx].Value <> B[Idx].Value ) then
526
+ Exit(False);
527
+ end ;
528
+
529
+ var
530
+ EA, EB, EC, ED, EE, EF, EG, EH, EI: TArray<TPair<Integer,Cardinal>>;
531
+
532
+ begin
533
+ EA := TArray<TPair<Integer,Cardinal>>.Create(
534
+ TPair<Integer,Cardinal>.Create(2 , 4 )
535
+ );
536
+ CheckTrue(EqualMaps(EA, CountOccurrences(A)), ' A' );
537
+
538
+ EB := TArray<TPair<Integer,Cardinal>>.Create(
539
+ TPair<Integer,Cardinal>.Create(-1 , 3 ),
540
+ TPair<Integer,Cardinal>.Create(3 , 2 ),
541
+ TPair<Integer,Cardinal>.Create(4 , 2 ),
542
+ TPair<Integer,Cardinal>.Create(7 , 1 ),
543
+ TPair<Integer,Cardinal>.Create(8 , 1 )
544
+ );
545
+ CheckTrue(EqualMaps(EB, CountOccurrences(B)), ' B' );
546
+
547
+ EC := TArray<TPair<Integer,Cardinal>>.Create(
548
+ TPair<Integer,Cardinal>.Create(2 , 3 ),
549
+ TPair<Integer,Cardinal>.Create(3 , 3 ),
550
+ TPair<Integer,Cardinal>.Create(4 , 2 ),
551
+ TPair<Integer,Cardinal>.Create(5 , 1 ),
552
+ TPair<Integer,Cardinal>.Create(6 , 1 )
553
+ );
554
+ CheckTrue(EqualMaps(EC, CountOccurrences(C)), ' C' );
555
+
556
+ ED := TArray<TPair<Integer,Cardinal>>.Create(
557
+ TPair<Integer,Cardinal>.Create(-42 , 2 ),
558
+ TPair<Integer,Cardinal>.Create(-1 , 2 ),
559
+ TPair<Integer,Cardinal>.Create(0 , 2 ),
560
+ TPair<Integer,Cardinal>.Create(56 , 2 )
561
+ );
562
+ CheckTrue(EqualMaps(ED, CountOccurrences(D)), ' D' );
563
+
564
+ EE := TArray<TPair<Integer,Cardinal>>.Create(
565
+ TPair<Integer,Cardinal>.Create(1 , 1 ),
566
+ TPair<Integer,Cardinal>.Create(2 , 1 ),
567
+ TPair<Integer,Cardinal>.Create(3 , 1 ),
568
+ TPair<Integer,Cardinal>.Create(4 , 1 )
569
+ );
570
+ CheckTrue(EqualMaps(EE, CountOccurrences(E)), ' E' );
571
+
572
+ EF := TArray<TPair<Integer,Cardinal>>.Create(
573
+ TPair<Integer,Cardinal>.Create(42 , 4 ),
574
+ TPair<Integer,Cardinal>.Create(56 , 6 )
575
+ );
576
+ CheckTrue(EqualMaps(EF, CountOccurrences(F)), ' F' );
577
+
578
+ EG := TArray<TPair<Integer,Cardinal>>.Create(
579
+ TPair<Integer,Cardinal>.Create(1 , 2 ),
580
+ TPair<Integer,Cardinal>.Create(2 , 2 ),
581
+ TPair<Integer,Cardinal>.Create(3 , 2 ),
582
+ TPair<Integer,Cardinal>.Create(4 , 2 ),
583
+ TPair<Integer,Cardinal>.Create(5 , 1 )
584
+ );
585
+ CheckTrue(EqualMaps(EG, CountOccurrences(G)), ' G' );
586
+
587
+ EH := TArray<TPair<Integer,Cardinal>>.Create(
588
+ TPair<Integer,Cardinal>.Create(-3 , 1 ),
589
+ TPair<Integer,Cardinal>.Create(-2 , 1 ),
590
+ TPair<Integer,Cardinal>.Create(1 , 1 ),
591
+ TPair<Integer,Cardinal>.Create(2 , 1 ),
592
+ TPair<Integer,Cardinal>.Create(4 , 4 )
593
+ );
594
+ CheckTrue(EqualMaps(EH, CountOccurrences(H)), ' H' );
595
+
596
+ EI := TArray<TPair<Integer,Cardinal>>.Create(
597
+ TPair<Integer,Cardinal>.Create(21 , 1 ),
598
+ TPair<Integer,Cardinal>.Create(22 , 1 ),
599
+ TPair<Integer,Cardinal>.Create(23 , 1 ),
600
+ TPair<Integer,Cardinal>.Create(24 , 1 ),
601
+ TPair<Integer,Cardinal>.Create(25 , 1 )
602
+ );
603
+ CheckTrue(EqualMaps(EI, CountOccurrences(I)), ' I' );
604
+
605
+ CheckException(TestCountOccurrences_ExceptEmptyArray, EArgumentException, ' Empty array' );
606
+ end ;
607
+
608
+ procedure TestMathsCatSnippets.TestCountOccurrences_ExceptEmptyArray ;
609
+ var
610
+ A: array of Integer;
611
+ begin
612
+ SetLength(A, 0 );
613
+ CountOccurrences(A);
614
+ end ;
615
+
473
616
procedure TestMathsCatSnippets.TestDigitCount ;
474
617
begin
475
618
CheckEquals(1 , DigitCount(0 ), ' DigitCount(0)' );
@@ -853,6 +996,57 @@ procedure TestMathsCatSnippets.TestHarmonicMean_Integer;
853
996
// SumOfReciprocals
854
997
end ;
855
998
999
+ procedure TestMathsCatSnippets.TestHasMode ;
1000
+ const
1001
+ A: array [1 ..4 ] of Integer = (2 , 2 , 2 , 2 ); // mode = [2]
1002
+ B: array [1 ..9 ] of Integer = (-1 , 3 , 4 , -1 , 8 , 3 , -1 , 4 , 7 ); // mode = [-1]
1003
+ C: array [1 ..10 ] of Integer = (2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 5 , 6 ); // mode = [2,3]
1004
+ D: array [1 ..8 ] of Integer = (-42 , -1 , -1 , 0 , 56 , 0 , -42 , 56 ); // no mode
1005
+ E: array [1 ..4 ] of Integer = (1 , 2 , 3 , 4 ); // no mode
1006
+ F: array [1 ..2 ] of Integer = (42 , 56 ); // no mode
1007
+ G: array [1 ..10 ] of Integer = (42 , 42 , 42 , 42 , 56 , 56 , 56 , 56 , 56 , 56 ); // mode = [56]
1008
+ H: array [1 ..9 ] of Integer = (-1 , -999 , -888 , 4 , 67 , 10774 , -888 , 12 , 6 ); // mode = [-888]
1009
+ I: array [1 ..9 ] of Integer = (1 , 2 , 3 , 4 , 5 , 4 , 3 , 2 , 1 ); // mode = [1,2,3,4]
1010
+ J: array [1 ..8 ] of Integer = (1 , 2 , 4 , 4 , 4 , 4 , -2 , -3 ); // mode = [4]
1011
+ K: array [1 ..10 ] of Integer = (42 , 42 , 55 , 55 , 55 , 55 , 56 , 56 , 42 , 42 ); // mode = [42,55]
1012
+ L: array [1 ..11 ] of Integer = (21 , 21 , 22 , 22 , 23 , 23 , 24 , 24 , 25 , 25 , 26 ); // mode = [21,22,23,24,25]
1013
+ M: array [1 ..10 ] of Integer = (8 , 6 , 1 , 2 , 9 , 6 , 10 , 5 , 9 , 1 ); // mode = [1,6,9]
1014
+ N: array [1 ..36 ] of Integer = (1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ,
1015
+ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ); // no mode
1016
+ begin
1017
+ CheckTrue(HasMode(A), ' A' );
1018
+ CheckTrue(HasMode(B), ' B' );
1019
+ CheckTrue(HasMode(C), ' C' );
1020
+ CheckFalse(HasMode(D), ' D' );
1021
+ CheckFalse(HasMode(E), ' E' );
1022
+ CheckFalse(HasMode(F), ' F' );
1023
+ CheckTrue(HasMode(G), ' G' );
1024
+ CheckTrue(HasMode(H), ' H' );
1025
+ CheckTrue(HasMode(I), ' I' );
1026
+ CheckTrue(HasMode(J), ' J' );
1027
+ CheckTrue(HasMode(K), ' K' );
1028
+ CheckTrue(HasMode(L), ' L' );
1029
+ CheckTrue(HasMode(M), ' M' );
1030
+ CheckFalse(HasMode(N), ' N' );
1031
+ CheckException(TestHasMode_ExceptEmptyArray, EArgumentException, ' Empty array' );
1032
+ CheckException(TestHasMode_ExceptSingleElementArray, EArgumentException, ' Single element array' );
1033
+ end ;
1034
+
1035
+ procedure TestMathsCatSnippets.TestHasMode_ExceptEmptyArray ;
1036
+ var
1037
+ A: array of Integer;
1038
+ begin
1039
+ SetLength(A, 0 );
1040
+ HasMode(A);
1041
+ end ;
1042
+
1043
+ procedure TestMathsCatSnippets.TestHasMode_ExceptSingleElementArray ;
1044
+ const
1045
+ A: array [1 ..1 ] of Integer = (1 );
1046
+ begin
1047
+ HasMode(A);
1048
+ end ;
1049
+
856
1050
procedure TestMathsCatSnippets.TestIsNarcissistic ;
857
1051
const
858
1052
NarcNumsBase10: array [1 ..25 ] of Integer = (
@@ -1515,6 +1709,163 @@ procedure TestMathsCatSnippets.TestMinOfArray_Single;
1515
1709
Check(SameValue(N, MinOfArray(A)), ' Test 5' );
1516
1710
end ;
1517
1711
1712
+ procedure TestMathsCatSnippets.TestMode ;
1713
+ const
1714
+ A: array [1 ..4 ] of Integer = (2 , 2 , 2 , 2 ); // mode = [2]
1715
+ B: array [1 ..9 ] of Integer = (-1 , 3 , 4 , -1 , 8 , 3 , -1 , 4 , 7 ); // mode = [-1]
1716
+ C: array [1 ..10 ] of Integer = (2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 5 , 6 ); // mode = [2,3]
1717
+ D: array [1 ..8 ] of Integer = (-42 , -1 , -1 , 0 , 56 , 0 , -42 , 56 ); // no mode = [-42,-1,0,56]
1718
+ E: array [1 ..4 ] of Integer = (1 , 2 , 3 , 4 ); // no mode = [1,2,3,4]
1719
+ F: array [1 ..2 ] of Integer = (42 , 56 ); // no mode = [42,56]
1720
+ G: array [1 ..10 ] of Integer = (42 , 42 , 42 , 42 , 56 , 56 , 56 , 56 , 56 , 56 ); // mode = [56]
1721
+ H: array [1 ..9 ] of Integer = (-1 , -999 , -888 , 4 , 67 , 10774 , -888 , 12 , 6 ); // mode = [-888]
1722
+ I: array [1 ..9 ] of Integer = (1 , 2 , 3 , 4 , 5 , 4 , 3 , 2 , 1 ); // mode = [1,2,3,4]
1723
+ J: array [1 ..8 ] of Integer = (1 , 2 , 4 , 4 , 4 , 4 , -2 , -3 ); // mode = [4]
1724
+ K: array [1 ..10 ] of Integer = (42 , 42 , 55 , 55 , 55 , 55 , 56 , 56 , 42 , 42 ); // mode = [42,55]
1725
+ L: array [1 ..11 ] of Integer = (21 , 21 , 22 , 22 , 23 , 23 , 24 , 24 , 25 , 25 , 26 ); // mode = [21,22,23,24,25]
1726
+ M: array [1 ..10 ] of Integer = (8 , 6 , 1 , 2 , 9 , 6 , 10 , 5 , 9 , 1 ); // mode = [1,6,9]
1727
+ N: array [1 ..36 ] of Integer = (1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ,
1728
+ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ); // no mode = [1,2,3,4]
1729
+ begin
1730
+ // All expected results check using
1731
+ // https://www.calculatorsoup.com/calculators/statistics/mean-median-mode.php
1732
+ CheckTrue(EqualArrays([2 ], Mode(A)), ' A' );
1733
+ CheckTrue(EqualArrays([-1 ], Mode(B)), ' B' );
1734
+ CheckTrue(EqualArrays([2 , 3 ], Mode(C)), ' C' );
1735
+ CheckTrue(EqualArrays([-42 , -1 , 0 , 56 ], Mode(D)), ' D' );
1736
+ CheckTrue(EqualArrays([1 , 2 , 3 , 4 ], Mode(E)), ' E' );
1737
+ CheckTrue(EqualArrays([42 ,56 ], Mode(F)), ' F' );
1738
+ CheckTrue(EqualArrays([56 ], Mode(G)), ' G' );
1739
+ CheckTrue(EqualArrays([-888 ], Mode(H)), ' H' );
1740
+ CheckTrue(EqualArrays([1 , 2 , 3 , 4 ], Mode(I)), ' I' );
1741
+ CheckTrue(EqualArrays([4 ], Mode(J)), ' J' );
1742
+ CheckTrue(EqualArrays([42 , 55 ], Mode(K)), ' K' );
1743
+ CheckTrue(EqualArrays([21 , 22 , 23 , 24 , 25 ], Mode(L)), ' L' );
1744
+ CheckTrue(EqualArrays([1 , 6 , 9 ], Mode(M)), ' M' );
1745
+ CheckTrue(EqualArrays([1 , 2 , 3 , 4 ], Mode(N)), ' N' );
1746
+ CheckException(TestMode_ExceptEmptyArray, EArgumentException, ' Empty array' );
1747
+ CheckException(TestMode_ExceptSingleElementArray, EArgumentException, ' Single element array' );
1748
+ end ;
1749
+
1750
+ procedure TestMathsCatSnippets.TestModeAlt ;
1751
+ const
1752
+ A: array [1 ..4 ] of Integer = (2 , 2 , 2 , 2 ); // mode = [2]
1753
+ B: array [1 ..9 ] of Integer = (-1 , 3 , 4 , -1 , 8 , 3 , -1 , 4 , 7 ); // mode = [-1]
1754
+ C: array [1 ..10 ] of Integer = (2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 5 , 6 ); // mode = [2,3]
1755
+ D: array [1 ..8 ] of Integer = (-42 , -1 , -1 , 0 , 56 , 0 , -42 , 56 ); // no mode
1756
+ E: array [1 ..4 ] of Integer = (1 , 2 , 3 , 4 ); // no mode
1757
+ F: array [1 ..2 ] of Integer = (42 , 56 ); // no mode
1758
+ G: array [1 ..10 ] of Integer = (42 , 42 , 42 , 42 , 56 , 56 , 56 , 56 , 56 , 56 ); // mode = [56]
1759
+ H: array [1 ..9 ] of Integer = (-1 , -999 , -888 , 4 , 67 , 10774 , -888 , 12 , 6 ); // mode = [-888]
1760
+ I: array [1 ..9 ] of Integer = (1 , 2 , 3 , 4 , 5 , 4 , 3 , 2 , 1 ); // mode = [1,2,3,4]
1761
+ J: array [1 ..8 ] of Integer = (1 , 2 , 4 , 4 , 4 , 4 , -2 , -3 ); // mode = [4]
1762
+ K: array [1 ..10 ] of Integer = (42 , 42 , 55 , 55 , 55 , 55 , 56 , 56 , 42 , 42 ); // mode = [42,55]
1763
+ L: array [1 ..11 ] of Integer = (21 , 21 , 22 , 22 , 23 , 23 , 24 , 24 , 25 , 25 , 26 ); // mode = [21,22,23,24,25]
1764
+ M: array [1 ..10 ] of Integer = (8 , 6 , 1 , 2 , 9 , 6 , 10 , 5 , 9 , 1 ); // mode = [1,6,9]
1765
+ N: array [1 ..36 ] of Integer = (1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ,
1766
+ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ); // no mode
1767
+ begin
1768
+ // All expected results check using
1769
+ // https://www.calculatorsoup.com/calculators/statistics/mean-median-mode.php
1770
+ CheckTrue(EqualArrays([2 ], ModeAlt(A)), ' A' );
1771
+ CheckTrue(EqualArrays([-1 ], ModeAlt(B)), ' B' );
1772
+ CheckTrue(EqualArrays([2 , 3 ], ModeAlt(C)), ' C' );
1773
+ CheckTrue(EqualArrays([], ModeAlt(D)), ' D' );
1774
+ CheckTrue(EqualArrays([], ModeAlt(E)), ' E' );
1775
+ CheckTrue(EqualArrays([], ModeAlt(F)), ' F' );
1776
+ CheckTrue(EqualArrays([56 ], ModeAlt(G)), ' G' );
1777
+ CheckTrue(EqualArrays([-888 ], ModeAlt(H)), ' H' );
1778
+ CheckTrue(EqualArrays([1 , 2 , 3 , 4 ], ModeAlt(I)), ' I' );
1779
+ CheckTrue(EqualArrays([4 ], ModeAlt(J)), ' J' );
1780
+ CheckTrue(EqualArrays([42 , 55 ], ModeAlt(K)), ' K' );
1781
+ CheckTrue(EqualArrays([21 , 22 , 23 , 24 , 25 ], ModeAlt(L)), ' L' );
1782
+ CheckTrue(EqualArrays([1 , 6 , 9 ], ModeAlt(M)), ' M' );
1783
+ CheckTrue(EqualArrays([], ModeAlt(N)), ' N' );
1784
+ CheckException(TestModeAlt_ExceptEmptyArray, EArgumentException, ' Empty array' );
1785
+ CheckException(TestModeAlt_ExceptSingleElementArray, EArgumentException, ' Single element array' );
1786
+ end ;
1787
+
1788
+ procedure TestMathsCatSnippets.TestModeAlt_ExceptEmptyArray ;
1789
+ var
1790
+ A: array of Integer;
1791
+ begin
1792
+ SetLength(A, 0 );
1793
+ ModeAlt(A);
1794
+ end ;
1795
+
1796
+ procedure TestMathsCatSnippets.TestModeAlt_ExceptSingleElementArray ;
1797
+ const
1798
+ A: array [1 ..1 ] of Integer = (1 );
1799
+ begin
1800
+ ModeAlt(A);
1801
+ end ;
1802
+
1803
+ procedure TestMathsCatSnippets.TestModeCount ;
1804
+ const
1805
+ A: array [1 ..4 ] of Integer = (2 , 2 , 2 , 2 ); // ModeCount: 1
1806
+ B: array [1 ..9 ] of Integer = (-1 , 3 , 4 , -1 , 8 , 3 , -1 , 4 , 7 ); // ModeCount: 1
1807
+ C: array [1 ..10 ] of Integer = (2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 5 , 6 ); // ModeCount: 2
1808
+ D: array [1 ..8 ] of Integer = (-42 , -1 , -1 , 0 , 56 , 0 , -42 , 56 ); // ModeCount: 0
1809
+ E: array [1 ..4 ] of Integer = (1 , 2 , 3 , 4 ); // ModeCount: 0
1810
+ F: array [1 ..2 ] of Integer = (42 , 56 ); // ModeCount: 0
1811
+ G: array [1 ..10 ] of Integer = (42 , 42 , 42 , 42 , 56 , 56 , 56 , 56 , 56 , 56 ); // ModeCount: 1
1812
+ H: array [1 ..9 ] of Integer = (-1 , -999 , -888 , 4 , 67 , 10774 , -888 , 12 , 6 ); // ModeCount: 1
1813
+ I: array [1 ..9 ] of Integer = (1 , 2 , 3 , 4 , 5 , 4 , 3 , 2 , 1 ); // ModeCount: 4
1814
+ J: array [1 ..8 ] of Integer = (1 , 2 , 4 , 4 , 4 , 4 , -2 , -3 ); // ModeCount: 1
1815
+ K: array [1 ..10 ] of Integer = (42 , 42 , 55 , 55 , 55 , 55 , 56 , 56 , 42 , 42 ); // ModeCount: 2
1816
+ L: array [1 ..11 ] of Integer = (21 , 21 , 22 , 22 , 23 , 23 , 24 , 24 , 25 , 25 , 26 ); // ModeCount: 5
1817
+ M: array [1 ..10 ] of Integer = (8 , 6 , 1 , 2 , 9 , 6 , 10 , 5 , 9 , 1 ); // ModeCount: 3
1818
+ N: array [1 ..36 ] of Integer = (1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ,
1819
+ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 ); // ModeCount: 0
1820
+ begin
1821
+ CheckEquals(1 , ModeCount(A), ' A' );
1822
+ CheckEquals(1 , ModeCount(B), ' B' );
1823
+ CheckEquals(2 , ModeCount(C), ' C' );
1824
+ CheckEquals(0 , ModeCount(D), ' D' );
1825
+ CheckEquals(0 , ModeCount(E), ' E' );
1826
+ CheckEquals(0 , ModeCount(F), ' F' );
1827
+ CheckEquals(1 , ModeCount(G), ' G' );
1828
+ CheckEquals(1 , ModeCount(H), ' H' );
1829
+ CheckEquals(4 , ModeCount(I), ' I' );
1830
+ CheckEquals(1 , ModeCount(J), ' J' );
1831
+ CheckEquals(2 , ModeCount(K), ' K' );
1832
+ CheckEquals(5 , ModeCount(L), ' L' );
1833
+ CheckEquals(3 , ModeCount(M), ' M' );
1834
+ CheckEquals(0 , ModeCount(N), ' N' );
1835
+ CheckException(TestModeCount_ExceptEmptyArray, EArgumentException, ' Empty array' );
1836
+ CheckException(TestModeCount_ExceptSingleElementArray, EArgumentException, ' Single element array' );
1837
+ end ;
1838
+
1839
+ procedure TestMathsCatSnippets.TestModeCount_ExceptEmptyArray ;
1840
+ var
1841
+ A: array of Integer;
1842
+ begin
1843
+ SetLength(A, 0 );
1844
+ ModeCount(A);
1845
+ end ;
1846
+
1847
+ procedure TestMathsCatSnippets.TestModeCount_ExceptSingleElementArray ;
1848
+ const
1849
+ A: array [1 ..1 ] of Integer = (1 );
1850
+ begin
1851
+ ModeCount(A);
1852
+ end ;
1853
+
1854
+ procedure TestMathsCatSnippets.TestMode_ExceptEmptyArray ;
1855
+ var
1856
+ A: array of Integer;
1857
+ begin
1858
+ SetLength(A, 0 );
1859
+ Mode(A);
1860
+ end ;
1861
+
1862
+ procedure TestMathsCatSnippets.TestMode_ExceptSingleElementArray ;
1863
+ const
1864
+ A: array [1 ..1 ] of Integer = (1 );
1865
+ begin
1866
+ Mode(A);
1867
+ end ;
1868
+
1518
1869
procedure TestMathsCatSnippets.TestNormaliseByWeight_Cardinal ;
1519
1870
const
1520
1871
A1: array [1 ..1 ] of Cardinal = (6 );
0 commit comments