Skip to content

Commit c7b655b

Browse files
committed
Fix bug in SumOfLogs routines
Fixed error in exception raising code in all overloads of SumOfLogs that was causing access violations. Added tests for the expected exception to all SumOfLogs unit tests. Regenerated UMathsCatSnippets from CodeSnip to include the updated SumOfLogs overloaded routines. Fixes #46
1 parent 2e71488 commit c7b655b

File tree

9 files changed

+77
-14
lines changed

9 files changed

+77
-14
lines changed

Diff for: collection/643.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ begin
1212
for Elem in A do
1313
begin
1414
if Elem = 0 then
15-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
15+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
1616
Result := Result + System.Ln(Elem);
1717
end;
1818
end;

Diff for: collection/644.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ begin
1212
for Elem in A do
1313
begin
1414
if Elem <= 0 then
15-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
15+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
1616
Result := Result + System.Ln(Elem);
1717
end;
1818
end;

Diff for: collection/645.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ begin
1212
for Elem in A do
1313
begin
1414
if Elem <= 0 then
15-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
15+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
1616
Result := Result + System.Ln(Elem);
1717
end;
1818
end;

Diff for: collection/646.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ begin
1212
for Elem in A do
1313
begin
1414
if Elem = 0 then
15-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
15+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
1616
Result := Result + System.Ln(Elem);
1717
end;
1818
end;

Diff for: collection/647.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ begin
1212
for Elem in A do
1313
begin
1414
if Math.Sign(Elem) <> Math.PositiveValue then
15-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
15+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
1616
Result := Result + System.Ln(Elem);
1717
end;
1818
end;

Diff for: collection/648.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ begin
1212
for Elem in A do
1313
begin
1414
if Math.Sign(Elem) <> Math.PositiveValue then
15-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
15+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
1616
Result := Result + System.Ln(Elem);
1717
end;
1818
end;

Diff for: collection/649.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ begin
1212
for Elem in A do
1313
begin
1414
if Math.Sign(Elem) <> Math.PositiveValue then
15-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
15+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
1616
Result := Result + System.Ln(Elem);
1717
end;
1818
end;

Diff for: tests/Cat-Maths/TestUMathsCatSnippets.pas

+63
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ TestMathsCatSnippets = class(TTestCase)
1111
procedure StretchRect_A_Except1;
1212
procedure StretchRect_A_Except2;
1313
procedure StretchRect_B_Except;
14+
procedure TestSumOfLogs_Single_Except_NonPositive;
15+
procedure TestSumOfLogs_Double_Except_NonPositive;
16+
procedure TestSumOfLogs_Extended_Except_NonPositive;
17+
procedure TestSumOfLogs_Integer_Except_NonPositive;
18+
procedure TestSumOfLogs_Cardinal_Except_NonPositive;
19+
procedure TestSumOfLogs_Int64_Except_NonPositive;
20+
procedure TestSumOfLogs_UInt64_Except_NonPositive;
1421
procedure TestArithMean_Integer_Except;
1522
procedure TestArithMean_Cardinal_Except;
1623
procedure TestArithMean_Double_Except;
@@ -1698,6 +1705,14 @@ procedure TestMathsCatSnippets.TestSumOfLogs_Cardinal;
16981705
Res := SumOfLogs(PosCardinalArray);
16991706
BoolRes := SameValue(Expected, Res);
17001707
CheckTrue(BoolRes, 'Normal');
1708+
CheckException(TestSumOfLogs_Cardinal_Except_NonPositive, EArgumentOutOfRangeException, 'Non-positive value');
1709+
end;
1710+
1711+
procedure TestMathsCatSnippets.TestSumOfLogs_Cardinal_Except_NonPositive;
1712+
const
1713+
Bad: array [1..2] of Cardinal = (12, 0);
1714+
begin
1715+
SumOfLogs(Bad);
17011716
end;
17021717

17031718
procedure TestMathsCatSnippets.TestSumOfLogs_Double;
@@ -1709,6 +1724,14 @@ procedure TestMathsCatSnippets.TestSumOfLogs_Double;
17091724
Res := SumOfLogs(PosDoubleArray);
17101725
BoolRes := SameValue(Expected, Res);
17111726
CheckTrue(BoolRes, 'SumOfLogs_Double');
1727+
CheckException(TestSumOfLogs_Double_Except_NonPositive, EArgumentOutOfRangeException, 'Non-positive value');
1728+
end;
1729+
1730+
procedure TestMathsCatSnippets.TestSumOfLogs_Double_Except_NonPositive;
1731+
const
1732+
Bad: array [1..2] of Double = (122.0, -2.1);
1733+
begin
1734+
SumOfLogs(Bad);
17121735
end;
17131736

17141737
procedure TestMathsCatSnippets.TestSumOfLogs_Extended;
@@ -1720,6 +1743,14 @@ procedure TestMathsCatSnippets.TestSumOfLogs_Extended;
17201743
Res := SumOfLogs(PosExtendedArray);
17211744
BoolRes := SameValue(Expected, Res);
17221745
CheckTrue(BoolRes, 'SumOfLogs_Extended');
1746+
CheckException(TestSumOfLogs_Extended_Except_NonPositive, EArgumentOutOfRangeException, 'Non-positive value');
1747+
end;
1748+
1749+
procedure TestMathsCatSnippets.TestSumOfLogs_Extended_Except_NonPositive;
1750+
const
1751+
Bad: array [1..2] of Extended = (122.0, -2.1);
1752+
begin
1753+
SumOfLogs(Bad);
17231754
end;
17241755

17251756
procedure TestMathsCatSnippets.TestSumOfLogs_Int64;
@@ -1731,6 +1762,14 @@ procedure TestMathsCatSnippets.TestSumOfLogs_Int64;
17311762
Res := SumOfLogs(PosInt64Array);
17321763
BoolRes := SameValue(Expected, Res);
17331764
CheckTrue(BoolRes, 'SumOfLogs_Int64');
1765+
CheckException(TestSumOfLogs_Int64_Except_NonPositive, EArgumentOutOfRangeException, 'Non-positive value');
1766+
end;
1767+
1768+
procedure TestMathsCatSnippets.TestSumOfLogs_Int64_Except_NonPositive;
1769+
const
1770+
Bad: array [1..2] of Int64 = (12, -23);
1771+
begin
1772+
SumOfLogs(Bad);
17341773
end;
17351774

17361775
procedure TestMathsCatSnippets.TestSumOfLogs_Integer;
@@ -1742,6 +1781,14 @@ procedure TestMathsCatSnippets.TestSumOfLogs_Integer;
17421781
Res := SumOfLogs(PosIntegerArray);
17431782
BoolRes := SameValue(Expected, Res);
17441783
CheckTrue(BoolRes, 'SumOfLogs_Integer');
1784+
CheckException(TestSumOfLogs_Integer_Except_NonPositive, EArgumentOutOfRangeException, 'Non-positive value');
1785+
end;
1786+
1787+
procedure TestMathsCatSnippets.TestSumOfLogs_Integer_Except_NonPositive;
1788+
const
1789+
Bad: array [1..2] of Integer = (12, 0);
1790+
begin
1791+
SumOfLogs(Bad);
17451792
end;
17461793

17471794
procedure TestMathsCatSnippets.TestSumOfLogs_Single;
@@ -1753,6 +1800,14 @@ procedure TestMathsCatSnippets.TestSumOfLogs_Single;
17531800
Res := SumOfLogs(PosSingleArray);
17541801
BoolRes := SameValue(Expected, Res);
17551802
CheckTrue(BoolRes, 'SumOfLogs_Single');
1803+
CheckException(TestSumOfLogs_Single_Except_NonPositive, EArgumentOutOfRangeException, 'Non-positive value');
1804+
end;
1805+
1806+
procedure TestMathsCatSnippets.TestSumOfLogs_Single_Except_NonPositive;
1807+
const
1808+
Bad: array [1..2] of Single = (122.0, -2.1);
1809+
begin
1810+
SumOfLogs(Bad);
17561811
end;
17571812

17581813
procedure TestMathsCatSnippets.TestSumOfLogs_UInt64;
@@ -1764,6 +1819,14 @@ procedure TestMathsCatSnippets.TestSumOfLogs_UInt64;
17641819
Res := SumOfLogs(PosUInt64Array);
17651820
BoolRes := SameValue(Expected, Res);
17661821
CheckTrue(BoolRes, 'SumOfLogs_UInt64');
1822+
CheckException(TestSumOfLogs_UInt64_Except_NonPositive, EArgumentOutOfRangeException, 'Non-positive value');
1823+
end;
1824+
1825+
procedure TestMathsCatSnippets.TestSumOfLogs_UInt64_Except_NonPositive;
1826+
const
1827+
Bad: array [1..2] of UInt64 = (12, 0);
1828+
begin
1829+
SumOfLogs(Bad);
17671830
end;
17681831

17691832
procedure TestMathsCatSnippets.TestWeightedArithMean_Cardinal;

Diff for: tests/Cat-Maths/UMathsCatSnippets.pas

+7-7
Original file line numberDiff line numberDiff line change
@@ -2381,7 +2381,7 @@ function SumOfLogs(const A: array of Cardinal): Extended; overload;
23812381
for Elem in A do
23822382
begin
23832383
if Elem = 0 then
2384-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
2384+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
23852385
Result := Result + System.Ln(Elem);
23862386
end;
23872387
end;
@@ -2406,7 +2406,7 @@ function SumOfLogs(const A: array of Double): Double; overload;
24062406
for Elem in A do
24072407
begin
24082408
if Math.Sign(Elem) <> Math.PositiveValue then
2409-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
2409+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
24102410
Result := Result + System.Ln(Elem);
24112411
end;
24122412
end;
@@ -2431,7 +2431,7 @@ function SumOfLogs(const A: array of Extended): Extended; overload;
24312431
for Elem in A do
24322432
begin
24332433
if Math.Sign(Elem) <> Math.PositiveValue then
2434-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
2434+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
24352435
Result := Result + System.Ln(Elem);
24362436
end;
24372437
end;
@@ -2455,7 +2455,7 @@ function SumOfLogs(const A: array of Int64): Extended; overload;
24552455
for Elem in A do
24562456
begin
24572457
if Elem <= 0 then
2458-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
2458+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
24592459
Result := Result + System.Ln(Elem);
24602460
end;
24612461
end;
@@ -2479,7 +2479,7 @@ function SumOfLogs(const A: array of Integer): Extended; overload;
24792479
for Elem in A do
24802480
begin
24812481
if Elem <= 0 then
2482-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
2482+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
24832483
Result := Result + System.Ln(Elem);
24842484
end;
24852485
end;
@@ -2504,7 +2504,7 @@ function SumOfLogs(const A: array of Single): Single; overload;
25042504
for Elem in A do
25052505
begin
25062506
if Math.Sign(Elem) <> Math.PositiveValue then
2507-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
2507+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
25082508
Result := Result + System.Ln(Elem);
25092509
end;
25102510
end;
@@ -2528,7 +2528,7 @@ function SumOfLogs(const A: array of UInt64): Extended; overload;
25282528
for Elem in A do
25292529
begin
25302530
if Elem = 0 then
2531-
raise SysUtils.EArgumentOutOfRangeException(sNotPositive);
2531+
raise SysUtils.EArgumentOutOfRangeException.Create(sNotPositive);
25322532
Result := Result + System.Ln(Elem);
25332533
end;
25342534
end;

0 commit comments

Comments
 (0)