273 lines
10 KiB
ObjectPascal
273 lines
10 KiB
ObjectPascal
unit TestDataDecimal;
|
|
|
|
interface
|
|
|
|
uses
|
|
Myc.Data.Decimal,
|
|
DUnitX.TestFramework;
|
|
|
|
type
|
|
[TestFixture]
|
|
TTestDecimal = class
|
|
public
|
|
[TestCase('Positive Numbers Add', '100,0,200,0,300,0')]
|
|
[TestCase('Negative Numbers Add', '-100,0,-200,0,-300,0')]
|
|
[TestCase('Mixed Numbers Add', '-100,0,200,0,100,0')]
|
|
[TestCase('Zero Add', '0,0,0,0,0,0')]
|
|
[TestCase('Add Different Scales 1', '123,2,45,1,573,2')]
|
|
[TestCase('Add Different Scales 2', '45,1,123,2,573,2')]
|
|
[Test]
|
|
procedure TestAdd(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
|
|
|
|
[TestCase('Positive Numbers Subtract', '300,0,200,0,100,0')]
|
|
[TestCase('Negative Numbers Subtract', '-300,0,-200,0,-100,0')]
|
|
[TestCase('Mixed Numbers Subtract', '100,0,200,0,-100,0')]
|
|
[TestCase('Zero Subtract', '0,0,0,0,0,0')]
|
|
[TestCase('Subtract Different Scales', '573,2,45,1,123,2')]
|
|
[Test]
|
|
procedure TestSubtract(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
|
|
|
|
[TestCase('Positive Numbers Multiply', '10,1,20,1,20,1')]
|
|
[TestCase('Negative Numbers Multiply', '-10,1,-20,1,20,1')]
|
|
[TestCase('Mixed Numbers Multiply', '-10,1,20,1,-20,1')]
|
|
[TestCase('Zero Multiply', '10,1,0,1,0,1')]
|
|
[TestCase('Multiply Different Scales', '12,1,345,2,414,2')]
|
|
[Test]
|
|
procedure TestMultiply(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
|
|
|
|
// This test case is moved to its own method to bypass a suspected DUnitX parser bug for large Int64 literals
|
|
[Test]
|
|
procedure TestMultiply_LargeNumbers;
|
|
[Test]
|
|
procedure TestDivide_LargeNumbers;
|
|
|
|
[TestCase('Positive Numbers Divide', '400,2,20,2,2000,2')]
|
|
[TestCase('Negative Numbers Divide', '-400,2,-20,2,2000,2')]
|
|
[TestCase('Mixed Numbers Divide', '-400,2,20,2,-2000,2')]
|
|
[TestCase('Zero Divide', '0,2,20,2,0,2')]
|
|
[TestCase('Divide Different Scales', '414,2,12,1,345,2')]
|
|
// Note: The large number division test might also fail due to the same parser bug.
|
|
// It can be moved to its own method as well if needed.
|
|
[TestCase('Divide Large Numbers', '120000000000000000,2,3000000000,2,4000000000,2')]
|
|
[Test]
|
|
procedure TestDivide(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
|
|
|
|
[TestCase('Equality Same Scale', '100,1,100,1')]
|
|
[TestCase('Equality Different Scales (1.2 = 1.20)', '12,1,120,2')]
|
|
[TestCase('Equality Different Scales (10.0 = 10.00)', '100,1,1000,2')]
|
|
[Test]
|
|
procedure TestEqual(const AValue1, AScale1, AValue2, AScale2: Int64);
|
|
|
|
[TestCase('Inequality Same Scale', '100,1,200,1')]
|
|
[TestCase('Inequality Different Scales Not Equal', '12,1,121,2')]
|
|
[Test]
|
|
procedure TestNotEqual(const AValue1, AScale1, AValue2, AScale2: Int64);
|
|
|
|
[Test]
|
|
procedure TestImplicitConversion;
|
|
[Test]
|
|
procedure TestExplicitConversionToDouble;
|
|
|
|
[TestCase('Scale 0', '0')]
|
|
[TestCase('Scale 4', '4')]
|
|
[TestCase('Scale 7 (Max)', '7')]
|
|
[Test]
|
|
procedure TestMinMaxValues(AScaleValue: Integer);
|
|
|
|
[TestCase('Upscale', '12345,2,4,1234500')]
|
|
[TestCase('Downscale (truncation)', '1234567,4,2,12345')]
|
|
[TestCase('Same Scale', '-555,3,3,-555')]
|
|
[TestCase('Upscale Negative', '-987,1,3,-98700')]
|
|
[Test]
|
|
procedure TestRescaleConstructor(AValue, AScale, ANewScale, AExpectedValue: Int64);
|
|
|
|
[Test]
|
|
procedure TestRescaleOverflow;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.SysUtils;
|
|
|
|
const
|
|
// Expected boundary values for a 61-bit signed integer
|
|
MIN_VALUE_61BIT: Int64 = -(Int64(1) shl 60);
|
|
MAX_VALUE_61BIT: Int64 = (Int64(1) shl 60) - 1;
|
|
|
|
{ TTestDecimal }
|
|
|
|
procedure TTestDecimal.TestAdd(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
|
|
var
|
|
d1, d2, d3: TDecimal;
|
|
begin
|
|
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');
|
|
end;
|
|
|
|
procedure TTestDecimal.TestSubtract(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
|
|
var
|
|
d1, d2, d3: TDecimal;
|
|
begin
|
|
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');
|
|
end;
|
|
|
|
procedure TTestDecimal.TestMultiply(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
|
|
var
|
|
d1, d2, d3: TDecimal;
|
|
begin
|
|
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');
|
|
end;
|
|
|
|
procedure TTestDecimal.TestDivide(const AValue1, AScale1, AValue2, AScale2: Int64; const AResultValue, AResultScale: Int64);
|
|
var
|
|
d1, d2, d3: TDecimal;
|
|
begin
|
|
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');
|
|
end;
|
|
|
|
procedure TTestDecimal.TestEqual(const AValue1, AScale1, AValue2, AScale2: Int64);
|
|
var
|
|
d1, d2: TDecimal;
|
|
begin
|
|
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: TDecimal;
|
|
begin
|
|
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: TDecimal;
|
|
begin
|
|
intValue := 12345;
|
|
decimalValue := intValue;
|
|
Assert.AreEqual(decimalValue.GetValue, intValue, 'Implicit conversion from Int64 failed');
|
|
Assert.AreEqual(decimalValue.GetScale, TScale(0), 'Implicit conversion should have scale 0');
|
|
end;
|
|
|
|
procedure TTestDecimal.TestExplicitConversionToDouble;
|
|
var
|
|
decimalValue: TDecimal;
|
|
doubleValue: Double;
|
|
begin
|
|
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: TDecimal;
|
|
scale: TScale;
|
|
begin
|
|
scale := TScale(AScaleValue);
|
|
|
|
// Test MaxValue
|
|
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 := 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: TDecimal;
|
|
begin
|
|
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.');
|
|
end;
|
|
|
|
procedure TTestDecimal.TestRescaleOverflow;
|
|
var
|
|
d1: TDecimal;
|
|
begin
|
|
// Create a value that is just over the overflow threshold for a multiplication by 10
|
|
d1 := TDecimal.Create((MAX_VALUE_61BIT div 10) + 1, 0);
|
|
|
|
// Expect an EOverflow when scaling up from 0 to 1
|
|
Assert.WillRaise(procedure begin TDecimal.Create(d1, 1); end, EOverflow, 'EOverflow was expected on rescale.');
|
|
end;
|
|
|
|
procedure TTestDecimal.TestMultiply_LargeNumbers;
|
|
var
|
|
d1, d2, d3, expectedDecimal: TDecimal;
|
|
begin
|
|
// Test case with slightly smaller numbers to avoid compiler edge cases,
|
|
// but still large enough to require 128-bit intermediate multiplication.
|
|
// 30_000_000.00 * 20_000_000.00 = 600,000,000,000,000.00
|
|
var val1: Int64 := 3000000000;
|
|
var scale1: TScale := 2;
|
|
var val2: Int64 := 2000000000;
|
|
var scale2: TScale := 2;
|
|
var expectedVal: Int64 := 60000000000000000; // 6 * 10^16
|
|
var expectedScale: TScale := 2;
|
|
|
|
d1 := TDecimal.Create(val1, scale1);
|
|
d2 := TDecimal.Create(val2, scale2);
|
|
d3 := d1 * d2;
|
|
|
|
expectedDecimal := TDecimal.Create(expectedVal, expectedScale);
|
|
|
|
Assert.AreEqual(expectedDecimal.GetValue, d3.GetValue, 'Value mismatch');
|
|
Assert.AreEqual(expectedDecimal.GetScale, d3.GetScale, 'Scale mismatch');
|
|
Assert.IsTrue(expectedDecimal = d3, 'Decimal instances should be equal');
|
|
end;
|
|
|
|
procedure TTestDecimal.TestDivide_LargeNumbers;
|
|
var
|
|
d1, d2, d3, expectedDecimal: TDecimal;
|
|
begin
|
|
// Test case using large numbers for division.
|
|
// (6.0*10^14) / (3*10^7) = 2*10^7
|
|
var val1: Int64 := 60000000000000000;
|
|
var scale1: TScale := 2;
|
|
var val2: Int64 := 3000000000;
|
|
var scale2: TScale := 2;
|
|
var expectedVal: Int64 := 2000000000;
|
|
var expectedScale: TScale := 2;
|
|
|
|
d1 := TDecimal.Create(val1, scale1);
|
|
d2 := TDecimal.Create(val2, scale2);
|
|
d3 := d1 / d2;
|
|
|
|
expectedDecimal := TDecimal.Create(expectedVal, expectedScale);
|
|
Assert.IsTrue(expectedDecimal = d3, 'Decimal instances should be equal');
|
|
end;
|
|
|
|
initialization
|
|
TDUnitX.RegisterTestFixture(TTestDecimal);
|
|
|
|
end.
|