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
+2 -1
View File
@@ -39,7 +39,8 @@ uses
TestDataTypes.JSON in '..\Src\Data\TestDataTypes.JSON.pas',
Myc.Data.POD in '..\Src\Data\Myc.Data.POD.pas',
Myc.Data.Decimal in '..\Src\Data\Myc.Data.Decimal.pas',
TestDataDecimal in 'TestDataDecimal.pas';
TestDataDecimal in 'TestDataDecimal.pas',
TestDataPOD in 'TestDataPOD.pas';
{ keep comment here to protect the following conditional from being removed by the IDE when adding a unit }
{$IFNDEF TESTINSIGHT}
+1
View File
@@ -141,6 +141,7 @@ $(PreBuildEvent)]]></PreBuildEvent>
<DCCReference Include="..\Src\Data\Myc.Data.POD.pas"/>
<DCCReference Include="..\Src\Data\Myc.Data.Decimal.pas"/>
<DCCReference Include="TestDataDecimal.pas"/>
<DCCReference Include="TestDataPOD.pas"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
+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
+284
View File
@@ -0,0 +1,284 @@
unit TestDataPOD;
interface
uses
System.SysUtils,
Myc.Data.POD,
Myc.Data.Decimal,
Myc.Data.Series,
DUnitX.TestFramework;
type
[TestFixture]
TTestPOD = class
public
// Tests the TScalar factory methods using a wide range of values.
[TestCase('IntegerZero', '0,skInteger')]
[TestCase('IntegerPositive', '12345,skInteger')]
[TestCase('IntegerNegative', '-54321,skInteger')]
[TestCase('Int64Zero', '0,skInt64')]
[TestCase('Int64Positive', '9876543210,skInt64')]
[TestCase('Int64Negative', '-1234567890,skInt64')]
[TestCase('Int64Max', '9223372036854775807,skInt64')]
[TestCase('UInt64Zero', '0,skUInt64')]
[TestCase('UInt64Positive', '12345678901234567890,skUInt64')]
[TestCase('UInt64Max', '18446744073709551615,skUInt64')]
[TestCase('Single', '1.23,skSingle')]
[TestCase('Double', '-3.1415926535,skDouble')]
[TestCase('DateTime', '45896.75,skDateTime')] // 28.08.2025 18:00
[TestCase('BooleanTrue', 'True,skBoolean')]
[TestCase('BooleanFalse', 'False,skBoolean')]
[TestCase('Char', 'Z,skChar')]
[TestCase('String', 'Hello World,skString')]
[TestCase('StringTruncated', 'This string is definitely longer than 15 chars,skString')]
procedure TestScalarCreation(const AValueStr: string; AExpectedKind: TScalarKind);
[Test]
procedure TestFromBytes;
[Test]
procedure TestFromDecimal;
[Test]
procedure TestFromTimestamp;
// More detailed tests for complex POD types.
[Test]
procedure TestScalarArray;
[Test]
procedure TestScalarTuple;
[Test]
procedure TestScalarRecordAndDefinition;
[Test]
procedure TestScalarSeries;
end;
implementation
uses
System.DateUtils,
System.Variants;
{ TTestPOD }
procedure TTestPOD.TestScalarCreation(const AValueStr: string; AExpectedKind: TScalarKind);
var
scalar: TScalar;
s: string;
begin
case AExpectedKind of
skInteger:
begin
scalar := TScalar.FromInteger(StrToInt(AValueStr));
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skInteger');
Assert.AreEqual(StrToInt(AValueStr), scalar.Value.AsInteger, 'Value mismatch for AsInteger');
end;
skInt64:
begin
scalar := TScalar.FromInt64(StrToInt64(AValueStr));
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skInt64');
Assert.AreEqual(StrToInt64(AValueStr), scalar.Value.AsInt64, 'Value mismatch for AsInt64');
end;
skUInt64:
begin
scalar := TScalar.FromUInt64(StrToUInt64(AValueStr));
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skUInt64');
Assert.AreEqual(StrToUInt64(AValueStr), scalar.Value.AsUInt64, 'Value mismatch for AsUInt64');
end;
skSingle:
begin
scalar := TScalar.FromSingle(StrToFloat(AValueStr));
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skSingle');
Assert.AreEqual(StrToFloat(AValueStr), scalar.Value.AsSingle, 1E-6, 'Value mismatch for AsSingle');
end;
skDouble:
begin
scalar := TScalar.FromDouble(StrToFloat(AValueStr));
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skDouble');
Assert.AreEqual(StrToFloat(AValueStr), scalar.Value.AsDouble, 1E-12, 'Value mismatch for AsDouble');
end;
skDateTime:
begin
scalar := TScalar.FromDateTime(StrToFloat(AValueStr));
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skDateTime');
Assert.AreEqual(StrToFloat(AValueStr), scalar.Value.AsDateTime, 'Value mismatch for AsDateTime');
end;
skBoolean:
begin
scalar := TScalar.FromBoolean(AValueStr = 'True');
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skBoolean');
Assert.AreEqual(AValueStr = 'True', scalar.Value.AsBoolean, 'Value mismatch for AsBoolean');
end;
skChar:
begin
scalar := TScalar.FromChar(AValueStr[1]);
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skChar');
Assert.AreEqual(AValueStr[1], scalar.Value.AsChar, 'Value mismatch for AsChar');
end;
skString:
begin
scalar := TScalar.FromString(AValueStr);
s := Copy(AValueStr, 1, 15); // ShortString truncates to 15 chars
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skString');
Assert.AreEqual(s, string(scalar.Value.AsString), 'Value mismatch for AsString');
end;
else
Assert.Fail('Unhandled TScalarKind in test');
end;
end;
procedure TTestPOD.TestFromBytes;
var
bytes, val: TScalarBytes;
scalar: TScalar;
begin
bytes[0] := $DE;
bytes[1] := $AD;
bytes[2] := $BE;
bytes[3] := $EF;
bytes[4] := $FE;
bytes[5] := $ED;
bytes[6] := $FA;
bytes[7] := $CE;
scalar := TScalar.FromBytes(bytes);
Assert.AreEqual(skBytes, scalar.Kind, 'Kind should be skBytes');
val := scalar.Value.AsBytes;
Assert.IsTrue(CompareMem(@bytes, @val, Length(bytes)), 'Byte array content mismatch');
end;
procedure TTestPOD.TestFromDecimal;
var
dec: TDecimal;
scalar: TScalar;
begin
dec := TDecimal.Create(12345, 2); // 123.45
scalar := TScalar.FromDecimal(dec);
Assert.AreEqual(skDecimal, scalar.Kind, 'Kind should be skDecimal');
Assert.AreEqual(dec, scalar.Value.AsDecimal, 'Decimal value mismatch');
end;
procedure TTestPOD.TestFromTimestamp;
var
dt: TDateTime;
ts: TTimestamp;
scalar: TScalar;
begin
dt := EncodeDateTime(2025, 8, 28, 12, 53, 2, 500);
ts := DateTimeToTimeStamp(dt);
scalar := TScalar.FromTimestamp(ts);
Assert.AreEqual(skTimestamp, scalar.Kind, 'Kind should be skTimestamp');
Assert.AreEqual(ts.Date, scalar.Value.AsTimestamp.Date, 'Timestamp.Date mismatch');
Assert.AreEqual(ts.Time, scalar.Value.AsTimestamp.Time, 'Timestamp.Time mismatch');
end;
procedure TTestPOD.TestScalarArray;
var
arr: TScalarArray;
values: TArray<TScalarValue>;
begin
// Test with values
values := [TScalar.FromInteger(10).Value, TScalar.FromInteger(20).Value, TScalar.FromInteger(30).Value];
arr := TScalarArray.Create(skInteger, values);
Assert.AreEqual(skInteger, arr.Kind, 'Array kind mismatch');
Assert.AreEqual(Int64(3), Length(arr.Items), 'Array length mismatch');
Assert.AreEqual(20, arr.Items[1].AsInteger, 'Array item content mismatch');
// Test empty
arr := TScalarArray.Create(skDouble, []);
Assert.AreEqual(skDouble, arr.Kind, 'Empty array kind mismatch');
Assert.AreEqual(Int64(0), Length(arr.Items), 'Empty array length should be zero');
end;
procedure TTestPOD.TestScalarTuple;
var
tuple: TScalarTuple;
begin
// A tuple is a heterogeneous array of TScalar
SetLength(tuple, 3);
tuple[0] := TScalar.FromString('Test');
tuple[1] := TScalar.FromInteger(123);
tuple[2] := TScalar.FromBoolean(True);
Assert.AreEqual(Int64(3), Length(tuple), 'Tuple length mismatch');
Assert.AreEqual(skString, tuple[0].Kind, 'Tuple item 0 kind mismatch');
Assert.AreEqual('Test', string(tuple[0].Value.AsString), 'Tuple item 0 value mismatch');
Assert.AreEqual(skInteger, tuple[1].Kind, 'Tuple item 1 kind mismatch');
Assert.AreEqual(123, tuple[1].Value.AsInteger, 'Tuple item 1 value mismatch');
Assert.AreEqual(skBoolean, tuple[2].Kind, 'Tuple item 2 kind mismatch');
Assert.AreEqual(True, tuple[2].Value.AsBoolean, 'Tuple item 2 value mismatch');
end;
procedure TTestPOD.TestScalarRecordAndDefinition;
var
recDef: TScalarRecordDefinition;
values: TArray<TScalarValue>;
rec: TScalarRecord;
mismatchedValues: TArray<TScalarValue>;
begin
// 1. Create a definition
recDef :=
TScalarRecordDefinition.Create(
[
TScalarRecordField.Create('ID', skInt64),
TScalarRecordField.Create('Name', skString),
TScalarRecordField.Create('Price', skDecimal)
]
);
Assert.AreEqual(Int64(3), Length(recDef.Fields), 'Record definition field count mismatch');
Assert.AreEqual('Name', recDef.Fields[1].Name, 'Record definition field name mismatch');
// 2. Create a matching set of values
values := [TScalar.FromInt64(99).Value, TScalar.FromString('ProductX').Value, TScalar.FromDecimal(TDecimal.Create(1995, 2)).Value];
// 3. Create the record
rec := TScalarRecord.Create(recDef, values);
Assert.AreEqual(Int64(3), Length(rec.Fields), 'Record field count mismatch');
Assert.AreEqual('ProductX', string(rec.Fields[1].AsString), 'Record field value mismatch');
Assert.AreEqual(TDecimal.Create(1995, 2), rec.Fields[2].AsDecimal, 'Record decimal field value mismatch');
// 4. Test assertion for mismatched field count
mismatchedValues := [TScalar.FromInt64(1).Value];
Assert.WillRaise(
procedure begin rec := TScalarRecord.Create(recDef, mismatchedValues); end,
EAssertionFailed,
'Mismatched field/value count should raise an assertion'
);
end;
procedure TTestPOD.TestScalarSeries;
var
seriesData: TArray<TScalarValue>;
series: TSeries<TScalarValue>;
scalarSeries: TScalarSeries;
begin
seriesData :=
[
TScalar.FromDouble(101.5).Value,
TScalar.FromDouble(102.0).Value,
TScalar.FromDouble(101.8).Value,
TScalar.FromDouble(103.2).Value
];
series := TSeries<TScalarValue>.CreateFromArray(seriesData, -1);
scalarSeries := TScalarSeries.Create(skDouble, series);
Assert.AreEqual(skDouble, scalarSeries.Kind, 'Series kind mismatch');
Assert.AreEqual(4, scalarSeries.Items.Count, 'Series count mismatch');
Assert.AreEqual(Int64(4), scalarSeries.Items.TotalCount, 'Series total count mismatch');
// TSeries index is reversed: 0 is the last item
Assert.AreEqual(103.2, scalarSeries.Items[0].AsDouble, 1E-12, 'Series item [0] mismatch');
Assert.AreEqual(101.5, scalarSeries.Items[3].AsDouble, 1E-12, 'Series item [3] mismatch');
end;
initialization
FormatSettings.DecimalSeparator := '.';
TDUnitX.RegisterTestFixture(TTestPOD);
end.