140 lines
5.2 KiB
ObjectPascal
140 lines
5.2 KiB
ObjectPascal
unit TestDataPOD;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
Myc.Data.Scalar,
|
|
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('Ordinal_Zero', '0,Ordinal')]
|
|
[TestCase('Ordinal_Positive', '9876543210,Ordinal')]
|
|
[TestCase('Ordinal_Negative', '-1234567890,Ordinal')]
|
|
[TestCase('Ordinal_Max', '9223372036854775807,Ordinal')]
|
|
[TestCase('Float_Zero', '0.0,Float')]
|
|
[TestCase('Float_Positive', '1.23,Float')]
|
|
[TestCase('Float_Negative', '-3.1415926535,Float')]
|
|
procedure TestScalarCreation(const AValueStr: string; AExpectedKind: TScalar.TKind);
|
|
|
|
// More detailed tests for complex POD types.
|
|
[Test]
|
|
procedure TestScalarArray;
|
|
[Test]
|
|
procedure TestScalarTuple;
|
|
[Test]
|
|
procedure TestScalarRecordAndDefinition;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.Variants;
|
|
|
|
{ TTestPOD }
|
|
|
|
procedure TTestPOD.TestScalarCreation(const AValueStr: string; AExpectedKind: TScalar.TKind);
|
|
var
|
|
scalar: TScalar;
|
|
begin
|
|
case AExpectedKind of
|
|
TScalar.TKind.Ordinal:
|
|
begin
|
|
scalar := TScalar.FromInt64(StrToInt64(AValueStr));
|
|
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be Ordinal');
|
|
Assert.AreEqual(StrToInt64(AValueStr), scalar.Value.AsInt64, 'Value mismatch for AsInt64');
|
|
end;
|
|
TScalar.TKind.Float:
|
|
begin
|
|
scalar := TScalar.FromDouble(StrToFloat(AValueStr));
|
|
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be Float');
|
|
Assert.AreEqual(StrToFloat(AValueStr), scalar.Value.AsDouble, 1E-12, 'Value mismatch for AsDouble');
|
|
end;
|
|
else
|
|
Assert.Fail('Unhandled TScalar.TKind in test');
|
|
end;
|
|
end;
|
|
|
|
procedure TTestPOD.TestScalarArray;
|
|
var
|
|
arr: TScalarArray;
|
|
values: TArray<TScalar.TValue>;
|
|
begin
|
|
// Test with values
|
|
values := [TScalar.FromInt64(10).Value, TScalar.FromInt64(20).Value, TScalar.FromInt64(30).Value];
|
|
arr := TScalarArray.Create(TScalar.TKind.Ordinal, values);
|
|
|
|
Assert.AreEqual(TScalar.TKind.Ordinal, arr.Kind, 'Array kind mismatch');
|
|
Assert.AreEqual(Int64(3), Length(arr.Items), 'Array length mismatch');
|
|
Assert.AreEqual(Int64(20), arr.Items[1].AsInt64, 'Array item content mismatch');
|
|
|
|
// Test empty
|
|
arr := TScalarArray.Create(TScalar.TKind.Float, []);
|
|
Assert.AreEqual(TScalar.TKind.Float, arr.Kind, 'Empty array kind mismatch');
|
|
Assert.AreEqual(Int64(0), Length(arr.Items), 'Empty array length should be zero');
|
|
end;
|
|
|
|
procedure TTestPOD.TestScalarRecordAndDefinition;
|
|
var
|
|
recDef: TScalarRecordDefinition;
|
|
values: TArray<TScalar.TValue>;
|
|
rec: TScalarRecord;
|
|
mismatchedValues: TArray<TScalar.TValue>;
|
|
begin
|
|
// 1. Create a definition
|
|
recDef :=
|
|
TScalarRecordDefinition
|
|
.Create([TScalarRecordField.Create('ID', TScalar.TKind.Ordinal), TScalarRecordField.Create('Price', TScalar.TKind.Float)]);
|
|
Assert.AreEqual(Int64(2), Length(recDef.Fields), 'Record definition field count mismatch');
|
|
Assert.AreEqual('Price', recDef.Fields[1].Name, 'Record definition field name mismatch');
|
|
|
|
// 2. Create a matching set of values
|
|
values := [TScalar.FromInt64(99).Value, TScalar.FromDouble(19.95).Value];
|
|
|
|
// 3. Create the record
|
|
rec := TScalarRecord.Create(recDef, values);
|
|
Assert.AreEqual(Int64(2), Length(rec.Fields), 'Record field count mismatch');
|
|
Assert.AreEqual(19.95, rec.Fields[1].AsDouble, 1E-12, 'Record field value mismatch');
|
|
Assert.AreEqual(Int64(99), rec.Items['ID'].Value.AsInt64, 'Record item by name mismatch');
|
|
Assert.AreEqual(TScalar.TKind.Float, rec.Items['Price'].Kind, 'Record item kind by name 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.TestScalarTuple;
|
|
var
|
|
tuple: TScalarTuple;
|
|
begin
|
|
// A tuple is a heterogeneous array of TScalar
|
|
SetLength(tuple, 3);
|
|
tuple[0] := TScalar.FromInt64(123);
|
|
tuple[1] := TScalar.FromDouble(3.14);
|
|
tuple[2] := TScalar.FromInt64(-456);
|
|
|
|
Assert.AreEqual(Int64(3), Length(tuple), 'Tuple length mismatch');
|
|
Assert.AreEqual(TScalar.TKind.Ordinal, tuple[0].Kind, 'Tuple item 0 kind mismatch');
|
|
Assert.AreEqual(Int64(123), tuple[0].Value.AsInt64, 'Tuple item 0 value mismatch');
|
|
Assert.AreEqual(TScalar.TKind.Float, tuple[1].Kind, 'Tuple item 1 kind mismatch');
|
|
Assert.AreEqual(3.14, tuple[1].Value.AsDouble, 1E-12, 'Tuple item 1 value mismatch');
|
|
Assert.AreEqual(TScalar.TKind.Ordinal, tuple[2].Kind, 'Tuple item 2 kind mismatch');
|
|
Assert.AreEqual(Int64(-456), tuple[2].Value.AsInt64, 'Tuple item 2 value mismatch');
|
|
end;
|
|
|
|
initialization
|
|
FormatSettings.DecimalSeparator := '.';
|
|
TDUnitX.RegisterTestFixture(TTestPOD);
|
|
|
|
end.
|