diff --git a/Src/Data/Myc.Data.Decimal.pas b/Src/Data/Myc.Data.Decimal.pas index 77d3163..bc11b9c 100644 --- a/Src/Data/Myc.Data.Decimal.pas +++ b/Src/Data/Myc.Data.Decimal.pas @@ -7,27 +7,27 @@ interface type TScale = 0..7; - TDecimal64 = record + TDecimal = record strict private FValue: Int64; public constructor Create(AValue: Int64; AScale: TScale); overload; - constructor Create(const AValue: TDecimal64; ANewScale: TScale); overload; + constructor Create(const AValue: TDecimal; ANewScale: TScale); overload; - class operator Add(const A, B: TDecimal64): TDecimal64; - class operator Subtract(const A, B: TDecimal64): TDecimal64; - class operator Multiply(const A, B: TDecimal64): TDecimal64; - class operator Divide(const A, B: TDecimal64): TDecimal64; + class operator Add(const A, B: TDecimal): TDecimal; + class operator Subtract(const A, B: TDecimal): TDecimal; + class operator Multiply(const A, B: TDecimal): TDecimal; + class operator Divide(const A, B: TDecimal): TDecimal; - class operator Equal(const A, B: TDecimal64): Boolean; - class operator NotEqual(const A, B: TDecimal64): Boolean; + class operator Equal(const A, B: TDecimal): Boolean; + class operator NotEqual(const A, B: TDecimal): Boolean; - class operator Implicit(const A: Int64): TDecimal64; - class operator Explicit(const A: TDecimal64): Int64; - class operator Explicit(const A: TDecimal64): Double; + class operator Implicit(const A: Int64): TDecimal; + class operator Explicit(const A: TDecimal): Int64; + class operator Explicit(const A: TDecimal): Double; - class function MinValue(AScale: TScale): TDecimal64; static; - class function MaxValue(AScale: TScale): TDecimal64; static; + class function MinValue(AScale: TScale): TDecimal; static; + class function MaxValue(AScale: TScale): TDecimal; static; function GetValue: Int64; function GetScale: TScale; @@ -52,14 +52,14 @@ const // Use a lookup table for powers of 10 for performance PowersOf10: array[TScale] of Int64 = (1, 10, 100, 1000, 10000, 100000, 1000000, 10000000); -{ TDecimal64 } +{ TDecimal } -constructor TDecimal64.Create(AValue: Int64; AScale: TScale); +constructor TDecimal.Create(AValue: Int64; AScale: TScale); begin FValue := (Int64(AScale) shl VALUE_BITS) or (AValue and ((1 shl VALUE_BITS) - 1)); end; -constructor TDecimal64.Create(const AValue: TDecimal64; ANewScale: TScale); +constructor TDecimal.Create(const AValue: TDecimal; ANewScale: TScale); var oldScale: TScale; newValue: Int64; @@ -99,7 +99,7 @@ begin FValue := (Int64(ANewScale) shl VALUE_BITS) or (newValue and ((1 shl VALUE_BITS) - 1)); end; -class operator TDecimal64.Add(const A, B: TDecimal64): TDecimal64; +class operator TDecimal.Add(const A, B: TDecimal): TDecimal; var scale: TScale; resultValue: Int64; @@ -109,10 +109,10 @@ begin raise EArgumentException.Create('Operands must have the same scale.'); resultValue := A.GetValue + B.GetValue; - Result := TDecimal64.Create(resultValue, A.GetScale); + Result := TDecimal.Create(resultValue, A.GetScale); end; -class operator TDecimal64.Subtract(const A, B: TDecimal64): TDecimal64; +class operator TDecimal.Subtract(const A, B: TDecimal): TDecimal; var scale: TScale; resultValue: Int64; @@ -122,10 +122,10 @@ begin raise EArgumentException.Create('Operands must have the same scale.'); resultValue := A.GetValue - B.GetValue; - Result := TDecimal64.Create(resultValue, scale); + Result := TDecimal.Create(resultValue, scale); end; -class operator TDecimal64.Multiply(const A, B: TDecimal64): TDecimal64; +class operator TDecimal.Multiply(const A, B: TDecimal): TDecimal; var scale: TScale; resultValue: Int64; @@ -135,10 +135,10 @@ begin raise EArgumentException.Create('Operands must have the same scale.'); resultValue := Trunc(A.GetValue * B.GetValue / PowersOf10[scale]); - Result := TDecimal64.Create(resultValue, scale); + Result := TDecimal.Create(resultValue, scale); end; -class operator TDecimal64.Divide(const A, B: TDecimal64): TDecimal64; +class operator TDecimal.Divide(const A, B: TDecimal): TDecimal; var scale: TScale; resultValue: Int64; @@ -152,10 +152,10 @@ begin // Correctly scale the dividend to preserve precision resultValue := Trunc(A.GetValue * PowersOf10[scale] / B.GetValue); - Result := TDecimal64.Create(resultValue, scale); + Result := TDecimal.Create(resultValue, scale); end; -class operator TDecimal64.Equal(const A, B: TDecimal64): Boolean; +class operator TDecimal.Equal(const A, B: TDecimal): Boolean; begin if A.GetScale <> B.GetScale then Result := False @@ -163,38 +163,38 @@ begin Result := (A.GetValue = B.GetValue); end; -class operator TDecimal64.NotEqual(const A, B: TDecimal64): Boolean; +class operator TDecimal.NotEqual(const A, B: TDecimal): Boolean; begin Result := not (A = B); end; -class operator TDecimal64.Implicit(const A: Int64): TDecimal64; +class operator TDecimal.Implicit(const A: Int64): TDecimal; begin - // Correctly create a TDecimal64 with scale 0 - Result := TDecimal64.Create(A, 0); + // Correctly create a TDecimal with scale 0 + Result := TDecimal.Create(A, 0); end; -class operator TDecimal64.Explicit(const A: TDecimal64): Int64; +class operator TDecimal.Explicit(const A: TDecimal): Int64; begin Result := A.GetValue; end; -class operator TDecimal64.Explicit(const A: TDecimal64): Double; +class operator TDecimal.Explicit(const A: TDecimal): Double; begin Result := A.GetValue / PowersOf10[A.GetScale]; end; -class function TDecimal64.MaxValue(AScale: TScale): TDecimal64; +class function TDecimal.MaxValue(AScale: TScale): TDecimal; begin - Result := TDecimal64.Create(MAX_VALUE_61BIT, AScale); + Result := TDecimal.Create(MAX_VALUE_61BIT, AScale); end; -class function TDecimal64.MinValue(AScale: TScale): TDecimal64; +class function TDecimal.MinValue(AScale: TScale): TDecimal; begin - Result := TDecimal64.Create(MIN_VALUE_61BIT, AScale); + Result := TDecimal.Create(MIN_VALUE_61BIT, AScale); end; -function TDecimal64.GetValue: Int64; +function TDecimal.GetValue: Int64; var value: Int64; begin @@ -206,12 +206,12 @@ begin Result := value; end; -function TDecimal64.GetScale: TScale; +function TDecimal.GetScale: TScale; begin Result := TScale((FValue shr VALUE_BITS) and SCALE_MASK); end; -function TDecimal64.GetRawValue: Int64; +function TDecimal.GetRawValue: Int64; begin Result := FValue; end; diff --git a/Src/Data/Myc.Data.POD.pas b/Src/Data/Myc.Data.POD.pas index df7bcdc..078d9ae 100644 --- a/Src/Data/Myc.Data.POD.pas +++ b/Src/Data/Myc.Data.POD.pas @@ -3,62 +3,559 @@ unit Myc.Data.POD; interface uses + System.SysUtils, + Myc.Data.Decimal, Myc.Data.Series; type - // POD: Plain old data (that fits into a 64 bit register) + // POD: Plain old data (...that fits into 128 bits) - TScalarKind = (skInteger, skInt64, skUInt64, skSingle, skDouble, skTimestamp, skBoolean, skChar, skString, skBytes); + TScalarKind = ( + skInteger, + skInt64, + skUInt64, + skSingle, + skDouble, + skDateTime, + skTimestamp, + skBoolean, + skChar, + skPChar, + skString, + skBytes, + skDecimal + ); + + TScalarBytes = array[0..15] of Byte; + + TScalarPChar = array[0..7] of Char; + TScalarString = String[15]; TScalarValue = record + public + // Factory methods for creating a TScalarValue without a kind. + class function FromInteger(AValue: Integer): TScalarValue; static; inline; + class function FromInt64(AValue: Int64): TScalarValue; static; inline; + class function FromUInt64(AValue: UInt64): TScalarValue; static; inline; + class function FromSingle(AValue: Single): TScalarValue; static; inline; + class function FromDouble(AValue: Double): TScalarValue; static; inline; + class function FromDateTime(AValue: TDateTime): TScalarValue; static; inline; + class function FromTimestamp(AValue: TTimestamp): TScalarValue; static; inline; + class function FromBoolean(AValue: Boolean): TScalarValue; static; inline; + class function FromChar(AValue: Char): TScalarValue; static; inline; + class function FromPChar(const AValue: String): TScalarValue; overload; static; inline; + class function FromPChar(const AValue: TScalarPChar): TScalarValue; overload; static; inline; + class function FromString(const AValue: String): TScalarValue; overload; static; inline; + class function FromString(const AValue: TScalarString): TScalarValue; overload; static; inline; + class function FromBytes(const AValue: TScalarBytes): TScalarValue; static; inline; + class function FromDecimal(AValue: TDecimal): TScalarValue; static; inline; + + function GetAsInteger: Integer; inline; + function GetAsInt64: Int64; inline; + function GetAsUInt64: UInt64; inline; + function GetAsSingle: Single; inline; + function GetAsDouble: Double; inline; + function GetAsDateTime: TDateTime; inline; + function GetAsTimestamp: TTimestamp; inline; + function GetAsBoolean: Boolean; inline; + function GetAsChar: Char; inline; + function GetAsPChar: PChar; inline; + function GetAsString: ShortString; inline; + function GetAsBytes: TScalarBytes; inline; + function GetAsDecimal: TDecimal; inline; + + property AsInteger: Integer read GetAsInteger; + property AsInt64: Int64 read GetAsInt64; + property AsUInt64: UInt64 read GetAsUInt64; + property AsSingle: Single read GetAsSingle; + property AsDouble: Double read GetAsDouble; + property AsDateTime: TDateTime read GetAsDateTime; + property AsTimestamp: TTimestamp read GetAsTimestamp; + property AsBoolean: Boolean read GetAsBoolean; + property AsChar: Char read GetAsChar; + property AsPChar: PChar read GetAsPChar; + property AsString: ShortString read GetAsString; + property AsBytes: TScalarBytes read GetAsBytes; + property AsDecimal: TDecimal read GetAsDecimal; + + strict private case TScalarKind of - skInteger: (AsInteger: Integer); - skInt64: (AsInt64: Int64); - skUInt64: (AsUInt64: UInt64); - skSingle: (AsSingle: Single); - skDouble: (AsDouble: Double); - skTimestamp: (AsTimeStamp: TDateTime); - skBoolean: (AsBoolean: Boolean); - skChar: (AsChar: Char); - skString: (AsString: String[15]); - skBytes: (AsBytes: array[0..15] of Byte); + skInteger: (FInteger: Integer); + skInt64: (FInt64: Int64); + skUInt64: (FUInt64: UInt64); + skSingle: (FSingle: Single); + skDouble: (FDouble: Double); + skDateTime: (FDateTime: TDateTime); + skTimestamp: (FTimestamp: TTimestamp); + skBoolean: (FBoolean: Boolean); + skChar: (FChar: Char); + skPChar: (FPChar: TScalarPChar); + skString: (FString: String[15]); + skBytes: (FBytes: TScalarBytes); + skDecimal: (FDecimal: TDecimal); end; + // A scalar value with a type identifier. TScalar = record - Kind: TScalarKind; - Value: TScalarValue; + public + // Creates a new scalar from a kind and a value. + constructor Create(AKind: TScalarKind; const AValue: TScalarValue); + + // Factory methods for specific scalar types. + class function FromInteger(AValue: Integer): TScalar; static; inline; + class function FromInt64(AValue: Int64): TScalar; static; inline; + class function FromUInt64(AValue: UInt64): TScalar; static; inline; + class function FromSingle(AValue: Single): TScalar; static; inline; + class function FromDouble(AValue: Double): TScalar; static; inline; + class function FromDateTime(AValue: TDateTime): TScalar; static; inline; + class function FromTimestamp(AValue: TTimestamp): TScalar; static; inline; + class function FromBoolean(AValue: Boolean): TScalar; static; inline; + class function FromChar(AValue: Char): TScalar; static; inline; + class function FromPChar(const AValue: TScalarPChar): TScalar; static; inline; + class function FromString(const AValue: String): TScalar; static; inline; + class function FromBytes(const AValue: TScalarBytes): TScalar; static; inline; + class function FromDecimal(AValue: TDecimal): TScalar; static; inline; + + function GetKind: TScalarKind; inline; + function GetValue: TScalarValue; inline; + + property Kind: TScalarKind read GetKind; + property Value: TScalarValue read GetValue; + + strict private + FKind: TScalarKind; + FValue: TScalarValue; end; - // Basic data structures using the scalar type (these ar not POD of course) + // Basic data structures using the scalar type (these are not POD of course) + // An array of scalar values of the same kind. TScalarArray = record - Kind: TScalarKind; - Items: TArray; + public + // Creates a new scalar array. + constructor Create(AKind: TScalarKind; const AItems: TArray); + + function GetKind: TScalarKind; inline; + function GetItems: TArray; inline; + + property Kind: TScalarKind read GetKind; + property Items: TArray read GetItems; + + strict private + FKind: TScalarKind; + FItems: TArray; end; TScalarTuple = TArray; + // A field definition for a scalar record. TScalarRecordField = record - Name: String; - Kind: TScalarKind; + public + // Creates a new record field definition. + constructor Create(const AName: String; AKind: TScalarKind); + + function GetName: String; inline; + function GetKind: TScalarKind; inline; + + property Name: String read GetName; + property Kind: TScalarKind read GetKind; + + strict private + FName: String; + FKind: TScalarKind; end; + // A collection of field definitions that describe a scalar record. TScalarRecordDefinition = record - Fields: TArray; + public + // Creates a new record definition. + constructor Create(const AFields: TArray); + + function GetFields: TArray; inline; + property Fields: TArray read GetFields; + + strict private + FFields: TArray; end; + // A record of scalar values, based on a definition. TScalarRecord = record - Def: TScalarRecordDefinition; - Fields: TArray; + public + // Creates a new scalar record. + constructor Create(const ADef: TScalarRecordDefinition; const AFields: TArray); + + function GetDef: TScalarRecordDefinition; inline; + function GetFields: TArray; inline; + + property Def: TScalarRecordDefinition read GetDef; + property Fields: TArray read GetFields; + + strict private + FDef: TScalarRecordDefinition; + FFields: TArray; end; + // A time series of scalar values of the same kind. TScalarSeries = record - Kind: TScalarKind; - Items: TSeries; + public + // Creates a new scalar series. + constructor Create(AKind: TScalarKind; const AItems: TSeries); + + function GetKind: TScalarKind; inline; + function GetItems: TSeries; inline; + + property Kind: TScalarKind read GetKind; + property Items: TSeries read GetItems; + + strict private + FKind: TScalarKind; + FItems: TSeries; end; implementation +{ TScalarValue } + +class function TScalarValue.FromBoolean(AValue: Boolean): TScalarValue; +begin + Result.FBoolean := AValue; +end; + +class function TScalarValue.FromBytes(const AValue: TScalarBytes): TScalarValue; +var + len: Integer; +begin + FillChar(Result.FBytes, SizeOf(Result.FBytes), 0); + len := Length(AValue); + if (len > 0) then + begin + if (len > SizeOf(Result.FBytes)) then + len := SizeOf(Result.FBytes); + Move(AValue[0], Result.FBytes[0], len); + end; +end; + +class function TScalarValue.FromChar(AValue: Char): TScalarValue; +begin + Result.FChar := AValue; +end; + +class function TScalarValue.FromDateTime(AValue: TDateTime): TScalarValue; +begin + Result.FDateTime := AValue; +end; + +class function TScalarValue.FromDecimal(AValue: TDecimal): TScalarValue; +begin + Result.FDecimal := AValue; +end; + +class function TScalarValue.FromDouble(AValue: Double): TScalarValue; +begin + Result.FDouble := AValue; +end; + +class function TScalarValue.FromInt64(AValue: Int64): TScalarValue; +begin + Result.FInt64 := AValue; +end; + +class function TScalarValue.FromInteger(AValue: Integer): TScalarValue; +begin + Result.FInteger := AValue; +end; + +class function TScalarValue.FromPChar(const AValue: String): TScalarValue; +var + len, i: Integer; +begin + FillChar(Result.FPChar, SizeOf(Result.FPChar), #0); + len := Length(AValue); + if (len > 0) then + begin + if (len > Length(Result.FPChar)) then + len := Length(Result.FPChar); + for i := 0 to len - 1 do + Result.FPChar[i] := AValue[i + 1]; + end; +end; + +class function TScalarValue.FromPChar(const AValue: TScalarPChar): TScalarValue; +begin + Result.FPChar := AValue; +end; + +class function TScalarValue.FromSingle(AValue: Single): TScalarValue; +begin + Result.FSingle := AValue; +end; + +class function TScalarValue.FromString(const AValue: String): TScalarValue; +begin + Result.FString := ShortString(AValue); +end; + +class function TScalarValue.FromString(const AValue: TScalarString): TScalarValue; +begin + Result.FString := AValue; +end; + +class function TScalarValue.FromTimestamp(AValue: TTimestamp): TScalarValue; +begin + Result.FTimestamp := AValue; +end; + +class function TScalarValue.FromUInt64(AValue: UInt64): TScalarValue; +begin + Result.FUInt64 := AValue; +end; + +function TScalarValue.GetAsBoolean: Boolean; +begin + Result := FBoolean; +end; + +function TScalarValue.GetAsBytes: TScalarBytes; +begin + Result := FBytes; +end; + +function TScalarValue.GetAsChar: Char; +begin + Result := FChar; +end; + +function TScalarValue.GetAsDateTime: TDateTime; +begin + Result := FDateTime; +end; + +function TScalarValue.GetAsDecimal: TDecimal; +begin + Result := FDecimal; +end; + +function TScalarValue.GetAsDouble: Double; +begin + Result := FDouble; +end; + +function TScalarValue.GetAsInt64: Int64; +begin + Result := FInt64; +end; + +function TScalarValue.GetAsInteger: Integer; +begin + Result := FInteger; +end; + +function TScalarValue.GetAsPChar: PChar; +begin + Result := @FPChar; +end; + +function TScalarValue.GetAsSingle: Single; +begin + Result := FSingle; +end; + +function TScalarValue.GetAsString: ShortString; +begin + Result := FString; +end; + +function TScalarValue.GetAsTimestamp: TTimestamp; +begin + Result := FTimestamp; +end; + +function TScalarValue.GetAsUInt64: UInt64; +begin + Result := FUInt64; +end; + +{ TScalar } + +constructor TScalar.Create(AKind: TScalarKind; const AValue: TScalarValue); +begin + FKind := AKind; + FValue := AValue; +end; + +class function TScalar.FromBoolean(AValue: Boolean): TScalar; +begin + Result.FKind := skBoolean; + Result.FValue := TScalarValue.FromBoolean(AValue); +end; + +class function TScalar.FromBytes(const AValue: TScalarBytes): TScalar; +begin + Result.FKind := skBytes; + Result.FValue := TScalarValue.FromBytes(AValue); +end; + +class function TScalar.FromChar(AValue: Char): TScalar; +begin + Result.FKind := skChar; + Result.FValue := TScalarValue.FromChar(AValue); +end; + +class function TScalar.FromDateTime(AValue: TDateTime): TScalar; +begin + Result.FKind := skDateTime; + Result.FValue := TScalarValue.FromDateTime(AValue); +end; + +class function TScalar.FromDecimal(AValue: TDecimal): TScalar; +begin + Result.FKind := skDecimal; + Result.FValue := TScalarValue.FromDecimal(AValue); +end; + +class function TScalar.FromDouble(AValue: Double): TScalar; +begin + Result.FKind := skDouble; + Result.FValue := TScalarValue.FromDouble(AValue); +end; + +class function TScalar.FromInt64(AValue: Int64): TScalar; +begin + Result.FKind := skInt64; + Result.FValue := TScalarValue.FromInt64(AValue); +end; + +class function TScalar.FromInteger(AValue: Integer): TScalar; +begin + Result.FKind := skInteger; + Result.FValue := TScalarValue.FromInteger(AValue); +end; + +class function TScalar.FromPChar(const AValue: TScalarPChar): TScalar; +begin + Result.FKind := skPChar; + Result.FValue := TScalarValue.FromPChar(AValue); +end; + +class function TScalar.FromSingle(AValue: Single): TScalar; +begin + Result.FKind := skSingle; + Result.FValue := TScalarValue.FromSingle(AValue); +end; + +class function TScalar.FromString(const AValue: String): TScalar; +begin + Result.FKind := skString; + Result.FValue := TScalarValue.FromString(AValue); +end; + +class function TScalar.FromTimestamp(AValue: TTimestamp): TScalar; +begin + Result.FKind := skTimestamp; + Result.FValue := TScalarValue.FromTimestamp(AValue); +end; + +class function TScalar.FromUInt64(AValue: UInt64): TScalar; +begin + Result.FKind := skUInt64; + Result.FValue := TScalarValue.FromUInt64(AValue); +end; + +function TScalar.GetKind: TScalarKind; +begin + Result := FKind; +end; + +function TScalar.GetValue: TScalarValue; +begin + Result := FValue; +end; + +{ TScalarArray } + +constructor TScalarArray.Create(AKind: TScalarKind; const AItems: TArray); +begin + FKind := AKind; + FItems := AItems; +end; + +function TScalarArray.GetKind: TScalarKind; +begin + Result := FKind; +end; + +function TScalarArray.GetItems: TArray; +begin + Result := FItems; +end; + +{ TScalarRecordField } + +constructor TScalarRecordField.Create(const AName: String; AKind: TScalarKind); +begin + FName := AName; + FKind := AKind; +end; + +function TScalarRecordField.GetName: String; +begin + Result := FName; +end; + +function TScalarRecordField.GetKind: TScalarKind; +begin + Result := FKind; +end; + +{ TScalarRecordDefinition } + +constructor TScalarRecordDefinition.Create(const AFields: TArray); +begin + FFields := AFields; +end; + +function TScalarRecordDefinition.GetFields: TArray; +begin + Result := FFields; +end; + +{ TScalarRecord } + +constructor TScalarRecord.Create(const ADef: TScalarRecordDefinition; const AFields: TArray); +begin + Assert(Length(ADef.Fields) = Length(AFields), 'Field definition and value count must match.'); + FDef := ADef; + FFields := AFields; +end; + +function TScalarRecord.GetDef: TScalarRecordDefinition; +begin + Result := FDef; +end; + +function TScalarRecord.GetFields: TArray; +begin + Result := FFields; +end; + +{ TScalarSeries } + +constructor TScalarSeries.Create(AKind: TScalarKind; const AItems: TSeries); +begin + FKind := AKind; + FItems := AItems; +end; + +function TScalarSeries.GetKind: TScalarKind; +begin + Result := FKind; +end; + +function TScalarSeries.GetItems: TSeries; +begin + Result := FItems; +end; + initialization Assert(sizeof(TScalarValue) = 16); diff --git a/Test/MycTests.dpr b/Test/MycTests.dpr index 68a1531..71aeb8c 100644 --- a/Test/MycTests.dpr +++ b/Test/MycTests.dpr @@ -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} diff --git a/Test/MycTests.dproj b/Test/MycTests.dproj index 1b372ce..6955d15 100644 --- a/Test/MycTests.dproj +++ b/Test/MycTests.dproj @@ -141,6 +141,7 @@ $(PreBuildEvent)]]> + Base diff --git a/Test/TestDataDecimal.pas b/Test/TestDataDecimal.pas index e861c2b..44a1e86 100644 --- a/Test/TestDataDecimal.pas +++ b/Test/TestDataDecimal.pas @@ -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 diff --git a/Test/TestDataPOD.pas b/Test/TestDataPOD.pas new file mode 100644 index 0000000..3d14f4d --- /dev/null +++ b/Test/TestDataPOD.pas @@ -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; +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; + rec: TScalarRecord; + mismatchedValues: TArray; +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; + series: TSeries; + 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.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.