Refactoring DataFlow, 1st Generic Data records working

This commit is contained in:
Michael Schimmel
2025-07-22 01:13:01 +02:00
parent dd50049b06
commit bf4ef71cba
11 changed files with 1074 additions and 907 deletions
+78 -42
View File
@@ -3,6 +3,7 @@ unit TestDataRecord.RawAccess;
interface
uses
System.SysuTils,
DUnitX.TestFramework,
Myc.DataRecord;
@@ -26,6 +27,7 @@ uses
type
// A simple interface for testing managed type behavior.
IMyTestInterface = interface
['{1D23E456-AB78-4CD9-89EF-0123456789AB}'] // GUID for good practice, not strictly needed here
function GetValue: integer;
end;
@@ -38,6 +40,8 @@ type
function GetValue: integer;
end;
{ TMyTestInterfaceImpl }
constructor TMyTestInterfaceImpl.Create(const AValue: Integer);
begin
inherited Create;
@@ -52,86 +56,118 @@ end;
procedure TTestDataRecordRawAccess.TestRawAccess_Integer;
var
rec: TDataRecord;
buffer: TBytes;
field: TDataRecord.IField;
readValue, newValue: Integer;
initialValue, readValue, newValue: Integer;
begin
// Arrange: Create a record with an integer field.
// Arrange: Create a record layout and its buffer.
var builder := TDataRecord.CreateBuilder;
builder.AddField<Integer>('IntField', 123);
builder.AddField<Integer>('IntField');
rec := builder.CreateRec;
field := rec.GetField('IntField');
Assert.IsNotNull(field, 'Field should exist');
// Act & Assert (GetRaw): Read the initial value via raw pointer.
readValue := 0;
field.GetRaw(@readValue);
Assert.AreEqual(123, readValue, 'GetRaw should retrieve the correct integer value');
SetLength(buffer, rec.GetBufferSize);
rec.Initialize(buffer);
try
// Set initial value using raw access
initialValue := 123;
field.SetRaw(buffer, @initialValue);
// Act (SetRaw): Set a new value via raw pointer.
newValue := 456;
field.SetRaw(@newValue);
// Act & Assert (GetRaw): Read the initial value via raw pointer.
readValue := 0;
field.GetRaw(buffer, @readValue);
Assert.AreEqual(123, readValue, 'GetRaw should retrieve the correct integer value');
// Assert: Verify the new value using the typed accessor.
var typedField := rec.GetField<Integer>('IntField');
Assert.AreEqual(456, typedField.Value, 'SetRaw should update the integer value correctly');
// Act (SetRaw): Set a new value via raw pointer.
newValue := 456;
field.SetRaw(buffer, @newValue);
// Assert: Verify the new value using the typed accessor.
var typedField := rec.GetField<Integer>('IntField');
Assert.AreEqual(456, typedField.Value[buffer], 'SetRaw should update the integer value correctly');
finally
rec.Finalize(buffer);
end;
end;
procedure TTestDataRecordRawAccess.TestRawAccess_Interface;
var
rec: TDataRecord;
buffer: TBytes;
field: TDataRecord.IField;
initialObj, readObj, newObj: IMyTestInterface;
begin
// Arrange: Create a record with an interface field.
initialObj := TMyTestInterfaceImpl.Create(10);
// Arrange: Create a record layout and its buffer.
var builder := TDataRecord.CreateBuilder;
builder.AddField<IMyTestInterface>('IField', initialObj);
builder.AddField<IMyTestInterface>('IField');
rec := builder.CreateRec;
field := rec.GetField('IField');
Assert.IsNotNull(field, 'Field should exist');
// Act & Assert (GetRaw): Read the initial interface via raw pointer.
readObj := nil;
field.GetRaw(@readObj);
Assert.IsNotNull(readObj, 'GetRaw should retrieve a non-nil interface');
Assert.AreSame(initialObj, readObj, 'GetRaw should retrieve the same interface instance');
Assert.AreEqual(10, readObj.GetValue, 'Value of retrieved interface should be correct');
SetLength(buffer, rec.GetBufferSize);
rec.Initialize(buffer);
try
// Set initial value
initialObj := TMyTestInterfaceImpl.Create(10);
field.SetRaw(buffer, @initialObj);
// Act (SetRaw): Set a new interface via raw pointer.
newObj := TMyTestInterfaceImpl.Create(20);
field.SetRaw(@newObj);
// Act & Assert (GetRaw): Read the initial interface via raw pointer.
readObj := nil;
field.GetRaw(buffer, @readObj);
Assert.IsNotNull(readObj, 'GetRaw should retrieve a non-nil interface');
Assert.AreSame(initialObj, readObj, 'GetRaw should retrieve the same interface instance');
Assert.AreEqual(10, readObj.GetValue, 'Value of retrieved interface should be correct');
// Assert: Verify the new interface is set correctly.
var typedField := rec.GetField<IMyTestInterface>('IField');
Assert.AreSame(newObj, typedField.Value, 'SetRaw should update to the new interface instance');
Assert.AreEqual(20, typedField.Value.GetValue, 'Value of new interface should be correct');
// Act (SetRaw): Set a new interface via raw pointer.
newObj := TMyTestInterfaceImpl.Create(20);
field.SetRaw(buffer, @newObj);
// Assert: Verify the new interface is set correctly.
var typedField := rec.GetField<IMyTestInterface>('IField');
Assert.AreSame(newObj, typedField.Value[buffer], 'SetRaw should update to the new interface instance');
Assert.AreEqual(20, typedField.Value[buffer].GetValue, 'Value of new interface should be correct');
finally
rec.Finalize(buffer);
end;
end;
procedure TTestDataRecordRawAccess.TestRawAccess_String;
var
rec: TDataRecord;
buffer: TBytes;
field: TDataRecord.IField;
readString, newString: string;
initialString, readString, newString: string;
begin
// Arrange: Create a record with a string field.
// Arrange: Create a record layout and its buffer.
var builder := TDataRecord.CreateBuilder;
builder.AddField<string>('StrField', 'Hello');
builder.AddField<string>('StrField');
rec := builder.CreateRec;
field := rec.GetField('StrField');
Assert.IsNotNull(field, 'Field should exist');
// Act & Assert (GetRaw): Read the initial string via raw pointer.
readString := '';
field.GetRaw(@readString);
Assert.AreEqual('Hello', readString, 'GetRaw should retrieve the correct string value');
SetLength(buffer, rec.GetBufferSize);
rec.Initialize(buffer);
try
// Set initial value
initialString := 'Hello';
field.SetRaw(buffer, @initialString);
// Act (SetRaw): Set a new string via raw pointer.
newString := 'World';
field.SetRaw(@newString);
// Act & Assert (GetRaw): Read the initial string via raw pointer.
readString := '';
field.GetRaw(buffer, @readString);
Assert.AreEqual('Hello', readString, 'GetRaw should retrieve the correct string value');
// Assert: Verify the new string using the typed accessor.
var typedField := rec.GetField<string>('StrField');
Assert.AreEqual('World', typedField.Value, 'SetRaw should update the string value correctly');
// Act (SetRaw): Set a new string via raw pointer.
newString := 'World';
field.SetRaw(buffer, @newString);
// Assert: Verify the new string using the typed accessor.
var typedField := rec.GetField<string>('StrField');
Assert.AreEqual('World', typedField.Value[buffer], 'SetRaw should update the string value correctly');
finally
rec.Finalize(buffer);
end;
end;
initialization