@@ -11,6 +11,14 @@ TestMathsCatSnippets = class(TTestCase)
11
11
procedure StretchRect_A_Except1 ;
12
12
procedure StretchRect_A_Except2 ;
13
13
procedure StretchRect_B_Except ;
14
+ procedure TestArithMean_Integer_Except ;
15
+ procedure TestArithMean_Cardinal_Except ;
16
+ procedure TestArithMean_Double_Except ;
17
+ procedure TestWeightedArithMean_Double_Except1 ;
18
+ procedure TestWeightedArithMean_Double_Except2 ;
19
+ procedure TestWeightedArithMean_Double_Except3 ;
20
+ procedure TestWeightedArithMean_Double_Except4 ;
21
+
14
22
published
15
23
procedure TestDigitCount ;
16
24
procedure TestDigitCount2 ;
@@ -57,6 +65,13 @@ TestMathsCatSnippets = class(TTestCase)
57
65
procedure TestSumOfLogs_Cardinal ;
58
66
procedure TestSumOfLogs_Int64 ;
59
67
procedure TestSumOfLogs_UInt64 ;
68
+ procedure TestArithMean_Integer ;
69
+ procedure TestArithMean_Cardinal ;
70
+ procedure TestArithMean_Double ;
71
+
72
+ procedure TestWeightedArithMean_Integer ;
73
+ procedure TestWeightedArithMean_Cardinal ;
74
+ procedure TestWeightedArithMean_Double ;
60
75
end ;
61
76
62
77
implementation
@@ -194,6 +209,81 @@ procedure TestMathsCatSnippets.StretchRect_B_Except;
194
209
R1 := StretchRect(R0, 1234567890.0 );
195
210
end ;
196
211
212
+ procedure TestMathsCatSnippets.TestArithMean_Cardinal ;
213
+ const
214
+ A0: array [0 ..1 ] of Cardinal = (0 , 0 );
215
+ A1: array [0 ..0 ] of Cardinal = (56 );
216
+ A4: array [0 ..3 ] of Cardinal = (12 , 78 , 0 , 3 );
217
+ E0 = 0.0 ;
218
+ E1 = 56.0 ;
219
+ E4 = (12 .+78 .+0.0 +3.0 )/4.0 ;
220
+ begin
221
+ CheckTrue(Math.SameValue(E0, ArithMean(A0)), ' A0' );
222
+ CheckTrue(Math.SameValue(E1, ArithMean(A1)), ' A1' );
223
+ CheckTrue(Math.SameValue(E4, ArithMean(A4)), ' A4' );
224
+ CheckException(
225
+ TestArithMean_Cardinal_Except, EArgumentException, ' Exception'
226
+ );
227
+ end ;
228
+
229
+ procedure TestMathsCatSnippets.TestArithMean_Cardinal_Except ;
230
+ var
231
+ A: array of Cardinal;
232
+ begin
233
+ SetLength(A, 0 );
234
+ ArithMean(A);
235
+ end ;
236
+
237
+ procedure TestMathsCatSnippets.TestArithMean_Double ;
238
+ const
239
+ A0: array [0 ..1 ] of Double = (0.0 , 0.0 );
240
+ A1: array [0 ..0 ] of Double = (-PI);
241
+ A4: array [0 ..3 ] of Double = (12.42 , -56.47 , 0.0 , 3.0 );
242
+ E0 = 0.0 ;
243
+ E1 = -PI;
244
+ E4 = (12.42 -56.47 +3.0 )/4.0 ;
245
+ begin
246
+ CheckTrue(Math.SameValue(E0, ArithMean(A0)), ' A0' );
247
+ CheckTrue(Math.SameValue(E1, ArithMean(A1)), ' A1' );
248
+ CheckTrue(Math.SameValue(E4, ArithMean(A4)), ' A4' );
249
+ CheckException(
250
+ TestArithMean_Double_Except, EArgumentException, ' Exception'
251
+ );
252
+ end ;
253
+
254
+ procedure TestMathsCatSnippets.TestArithMean_Double_Except ;
255
+ var
256
+ A: array of Double;
257
+ begin
258
+ SetLength(A, 0 );
259
+ ArithMean(A);
260
+ end ;
261
+
262
+ procedure TestMathsCatSnippets.TestArithMean_Integer ;
263
+ const
264
+ A0: array [0 ..1 ] of Integer = (0 , 0 );
265
+ A1: array [0 ..0 ] of Integer = (-56 );
266
+ A4: array [0 ..3 ] of Integer = (12 , -42 , 0 , 3 );
267
+ E0 = 0.0 ;
268
+ E1 = -56.0 ;
269
+ E4 = (12.0 -42.0 +3.0 )/4.0 ;
270
+ begin
271
+ CheckTrue(Math.SameValue(E0, ArithMean(A0)), ' A0' );
272
+ CheckTrue(Math.SameValue(E1, ArithMean(A1)), ' A1' );
273
+ CheckTrue(Math.SameValue(E4, ArithMean(A4)), ' A4' );
274
+ CheckException(
275
+ TestArithMean_Integer_Except, EArgumentException, ' Exception'
276
+ );
277
+ end ;
278
+
279
+ procedure TestMathsCatSnippets.TestArithMean_Integer_Except ;
280
+ var
281
+ A: array of Integer;
282
+ begin
283
+ SetLength(A, 0 );
284
+ ArithMean(A);
285
+ end ;
286
+
197
287
procedure TestMathsCatSnippets.TestArraySum_Cardinal ;
198
288
const
199
289
A: array [0 ..3 ] of Cardinal = (12 , 78 , 0 , 3 );
@@ -944,6 +1034,84 @@ procedure TestMathsCatSnippets.TestSumOfLogs_UInt64;
944
1034
CheckTrue(BoolRes, ' SumOfLogs_UInt64' );
945
1035
end ;
946
1036
1037
+ procedure TestMathsCatSnippets.TestWeightedArithMean_Cardinal ;
1038
+ const
1039
+ A: array [1 ..3 ] of Cardinal = (12 , 6 , 3 );
1040
+ W: array [0 ..2 ] of Double = (0.5 , 0.25 , 0.75 );
1041
+ E = 6.5 ;
1042
+ begin
1043
+ CheckTrue(Math.SameValue(E, WeightedArithMean(A, W)));
1044
+ end ;
1045
+
1046
+ procedure TestMathsCatSnippets.TestWeightedArithMean_Double ;
1047
+ const
1048
+ A1: array [1 ..1 ] of Double = (42.456 );
1049
+ W1: array [3 ..3 ] of Double = (1.7 );
1050
+ E1 = 42.456 ;
1051
+ A2: array [1 ..5 ] of Double = (23.5 , -3.9987 , 66.0 , 0.0 , 47.6864 );
1052
+ W2: array [1 ..5 ] of Double = (7.6 , 0.0 , 12.7 , 4.5 , 3.2 );
1053
+ E2 = 41.7642 ; // from online calculator: accurate to 4 decimal places
1054
+ FudgeFactor2 = 10000 ;
1055
+ A3: array [1 ..3 ] of Double = (0.0 , 0.0 , 0.0 );
1056
+ W3: array [1 ..3 ] of Double = (23.5 , 99.7 , 27.898 );
1057
+ begin
1058
+ CheckTrue(Math.SameValue(E1, WeightedArithMean(A1, W1)), ' A1' );
1059
+ // E2 is accurate to 4DP - so round of both values multiplied by 10,000
1060
+ CheckEquals(Round(E2 * FudgeFactor2), Round(WeightedArithMean(A2, W2) * FudgeFactor2), ' A2' );
1061
+ CheckTrue(Math.IsZero(WeightedArithMean(A3, W3)), ' A3' );
1062
+
1063
+ CheckException(TestWeightedArithMean_Double_Except1, EArgumentException, ' Except 1' );
1064
+ CheckException(TestWeightedArithMean_Double_Except2, EArgumentException, ' Except 2' );
1065
+ CheckException(TestWeightedArithMean_Double_Except3, EArgumentException, ' Except 3' );
1066
+ CheckException(TestWeightedArithMean_Double_Except4, EArgumentException, ' Except 4' );
1067
+ end ;
1068
+
1069
+ procedure TestMathsCatSnippets.TestWeightedArithMean_Double_Except1 ;
1070
+ var
1071
+ A, W: array of Double;
1072
+ begin
1073
+ // Empty array error
1074
+ SetLength(A, 0 );
1075
+ SetLength(W, 0 );
1076
+ WeightedArithMean(A, W);
1077
+ end ;
1078
+
1079
+ procedure TestMathsCatSnippets.TestWeightedArithMean_Double_Except2 ;
1080
+ const
1081
+ A: array [1 ..3 ] of Double = (1.0 , 2.0 , PI);
1082
+ W: array [1 ..2 ] of Double = (0.5 , 0.7 );
1083
+ begin
1084
+ // Different size A & W arrays
1085
+ WeightedArithMean(A, W);
1086
+ end ;
1087
+
1088
+ procedure TestMathsCatSnippets.TestWeightedArithMean_Double_Except3 ;
1089
+ const
1090
+ A: array [1 ..4 ] of Double = (1.0 , 2.0 , PI, -67.948 );
1091
+ W: array [1 ..4 ] of Double = (0.5 , 0 , -42.0 , 0.7 );
1092
+ begin
1093
+ // W array contains -ve value
1094
+ WeightedArithMean(A, W);
1095
+ end ;
1096
+
1097
+ procedure TestMathsCatSnippets.TestWeightedArithMean_Double_Except4 ;
1098
+ const
1099
+ A: array [1 ..3 ] of Double = (1.0 , 2.0 , PI);
1100
+ W: array [1 ..3 ] of Double = (0.0 , 0.0 , 0.0 );
1101
+ begin
1102
+ // W array sums to 0
1103
+ WeightedArithMean(A, W);
1104
+ end ;
1105
+
1106
+ procedure TestMathsCatSnippets.TestWeightedArithMean_Integer ;
1107
+ const
1108
+ A: array [1 ..3 ] of Integer = (12 , -6 , 3 );
1109
+ W: array [0 ..2 ] of Double = (0.5 , 0.25 , 0.75 );
1110
+ E = 4.5 ;
1111
+ begin
1112
+ CheckTrue(Math.SameValue(E, WeightedArithMean(A, W)));
1113
+ end ;
1114
+
947
1115
initialization
948
1116
// Register any test cases with the test runner
949
1117
RegisterTest(TestMathsCatSnippets.Suite);
0 commit comments