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;
    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.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.
