This commit is contained in:
Michael Schimmel
2025-08-28 13:32:54 +02:00
parent f4b5882080
commit 77d2cb3f92
6 changed files with 878 additions and 95 deletions
+32 -32
View File
@@ -87,10 +87,10 @@ const
procedure TTestDecimal.TestAdd(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
var
d1, d2, d3: TDecimal64;
d1, d2, d3: TDecimal;
begin
d1 := TDecimal64.Create(AValue1, TScale(AScale1));
d2 := TDecimal64.Create(AValue2, TScale(AScale2));
d1 := TDecimal.Create(AValue1, TScale(AScale1));
d2 := TDecimal.Create(AValue2, TScale(AScale2));
d3 := d1 + d2;
Assert.AreEqual(d3.GetValue, AResultValue, 'Value mismatch');
Assert.AreEqual(d3.GetScale, TScale(AResultScale), 'Scale mismatch');
@@ -98,10 +98,10 @@ end;
procedure TTestDecimal.TestSubtract(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
var
d1, d2, d3: TDecimal64;
d1, d2, d3: TDecimal;
begin
d1 := TDecimal64.Create(AValue1, TScale(AScale1));
d2 := TDecimal64.Create(AValue2, TScale(AScale2));
d1 := TDecimal.Create(AValue1, TScale(AScale1));
d2 := TDecimal.Create(AValue2, TScale(AScale2));
d3 := d1 - d2;
Assert.AreEqual(d3.GetValue, AResultValue, 'Value mismatch');
Assert.AreEqual(d3.GetScale, TScale(AResultScale), 'Scale mismatch');
@@ -109,10 +109,10 @@ end;
procedure TTestDecimal.TestMultiply(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
var
d1, d2, d3: TDecimal64;
d1, d2, d3: TDecimal;
begin
d1 := TDecimal64.Create(AValue1, TScale(AScale1));
d2 := TDecimal64.Create(AValue2, TScale(AScale2));
d1 := TDecimal.Create(AValue1, TScale(AScale1));
d2 := TDecimal.Create(AValue2, TScale(AScale2));
d3 := d1 * d2;
Assert.AreEqual(d3.GetValue, AResultValue, 'Value mismatch');
Assert.AreEqual(d3.GetScale, TScale(AResultScale), 'Scale mismatch');
@@ -120,10 +120,10 @@ end;
procedure TTestDecimal.TestDivide(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
var
d1, d2, d3: TDecimal64;
d1, d2, d3: TDecimal;
begin
d1 := TDecimal64.Create(AValue1, TScale(AScale1));
d2 := TDecimal64.Create(AValue2, TScale(AScale2));
d1 := TDecimal.Create(AValue1, TScale(AScale1));
d2 := TDecimal.Create(AValue2, TScale(AScale2));
d3 := d1 / d2;
Assert.AreEqual(d3.GetValue, AResultValue, 'Value mismatch');
Assert.AreEqual(d3.GetScale, TScale(AResultScale), 'Scale mismatch');
@@ -131,26 +131,26 @@ end;
procedure TTestDecimal.TestEqual(const AValue1, AScale1, AValue2, AScale2: Int64);
var
d1, d2: TDecimal64;
d1, d2: TDecimal;
begin
d1 := TDecimal64.Create(AValue1, TScale(AScale1));
d2 := TDecimal64.Create(AValue2, TScale(AScale2));
d1 := TDecimal.Create(AValue1, TScale(AScale1));
d2 := TDecimal.Create(AValue2, TScale(AScale2));
Assert.IsTrue(d1 = d2, 'Values should be equal');
end;
procedure TTestDecimal.TestNotEqual(const AValue1, AScale1, AValue2, AScale2: Int64);
var
d1, d2: TDecimal64;
d1, d2: TDecimal;
begin
d1 := TDecimal64.Create(AValue1, TScale(AScale1));
d2 := TDecimal64.Create(AValue2, TScale(AScale2));
d1 := TDecimal.Create(AValue1, TScale(AScale1));
d2 := TDecimal.Create(AValue2, TScale(AScale2));
Assert.IsFalse(d1 = d2, 'Values should not be equal');
end;
procedure TTestDecimal.TestImplicitConversion;
var
intValue: Int64;
decimalValue: TDecimal64;
decimalValue: TDecimal;
begin
intValue := 12345;
decimalValue := intValue;
@@ -160,48 +160,48 @@ end;
procedure TTestDecimal.TestExplicitConversionToInt64;
var
decimalValue: TDecimal64;
decimalValue: TDecimal;
intValue: Int64;
begin
decimalValue := TDecimal64.Create(98765, TScale(3));
decimalValue := TDecimal.Create(98765, TScale(3));
intValue := Int64(decimalValue);
Assert.AreEqual(intValue, Int64(98765), 'Explicit conversion to Int64 failed');
end;
procedure TTestDecimal.TestExplicitConversionToDouble;
var
decimalValue: TDecimal64;
decimalValue: TDecimal;
doubleValue: Double;
begin
decimalValue := TDecimal64.Create(12345, TScale(3));
decimalValue := TDecimal.Create(12345, TScale(3));
doubleValue := Double(decimalValue);
Assert.AreEqual(doubleValue, 12.345, 0.000001, 'Explicit conversion to Double failed');
end;
procedure TTestDecimal.TestMinMaxValues(AScaleValue: Integer);
var
minDec, maxDec: TDecimal64;
minDec, maxDec: TDecimal;
scale: TScale;
begin
scale := TScale(AScaleValue);
// Test MaxValue
maxDec := TDecimal64.MaxValue(scale);
maxDec := TDecimal.MaxValue(scale);
Assert.AreEqual(scale, maxDec.GetScale, 'MaxValue scale mismatch');
Assert.AreEqual(MAX_VALUE_61BIT, maxDec.GetValue, 'MaxValue value mismatch');
// Test MinValue
minDec := TDecimal64.MinValue(scale);
minDec := TDecimal.MinValue(scale);
Assert.AreEqual(scale, minDec.GetScale, 'MinValue scale mismatch');
Assert.AreEqual(MIN_VALUE_61BIT, minDec.GetValue, 'MinValue value mismatch');
end;
procedure TTestDecimal.TestRescaleConstructor(AValue, AScale, ANewScale, AExpectedValue: Int64);
var
d1, d2: TDecimal64;
d1, d2: TDecimal;
begin
d1 := TDecimal64.Create(AValue, TScale(AScale));
d2 := TDecimal64.Create(d1, TScale(ANewScale));
d1 := TDecimal.Create(AValue, TScale(AScale));
d2 := TDecimal.Create(d1, TScale(ANewScale));
Assert.AreEqual(TScale(ANewScale), d2.GetScale, 'New scale was not set correctly.');
Assert.AreEqual(AExpectedValue, d2.GetValue, 'Rescaled value is incorrect.');
@@ -209,13 +209,13 @@ end;
procedure TTestDecimal.TestRescaleOverflow;
var
d1: TDecimal64;
d1: TDecimal;
begin
// Create a value that is just over the overflow threshold for a multiplication by 10
d1 := TDecimal64.Create((MAX_VALUE_61BIT div 10) + 1, 0);
d1 := TDecimal.Create((MAX_VALUE_61BIT div 10) + 1, 0);
// Expect an EOverflow when scaling up from 0 to 1
Assert.WillRaise(procedure begin TDecimal64.Create(d1, 1); end, EOverflow, 'EOverflow was expected on rescale.');
Assert.WillRaise(procedure begin TDecimal.Create(d1, 1); end, EOverflow, 'EOverflow was expected on rescale.');
end;
initialization