Refactoring
This commit is contained in:
@@ -4,7 +4,7 @@ interface
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.Generics.Collections, // E2003: Added for TList<T>
|
||||
System.Generics.Collections,
|
||||
DUnitX.TestFramework,
|
||||
Myc.Signals,
|
||||
Myc.Lazy,
|
||||
@@ -14,6 +14,8 @@ uses
|
||||
Myc.Trade.DataServer;
|
||||
|
||||
type
|
||||
// Test fixture for TDataServer and TAuraTABFileServer, focusing on data equivalence
|
||||
// with TDataSeries.LoadDataSeries.
|
||||
[TestFixture]
|
||||
[IgnoreMemoryLeaks(true)]
|
||||
TTest_TABFileServer_Equivalence = class(TObject)
|
||||
@@ -48,65 +50,68 @@ const
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.SetupFixture;
|
||||
begin
|
||||
// IsMemoryLow := function: Boolean begin exit(false); end;
|
||||
// Load the ground truth data using the original LoadDataSeries function.
|
||||
// SetupTaskManagerMock;
|
||||
// TAskBid.ClearCache ensures a clean state for data series loading.
|
||||
TAskBid.ClearCache;
|
||||
end;
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.TearDownFixture;
|
||||
begin
|
||||
// Resets the global IsMemoryLow function pointer, if it was set for testing.
|
||||
IsMemoryLow := nil;
|
||||
end;
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.Setup;
|
||||
begin
|
||||
FServer := TTABFileServer.Create(C_TEST_FILENAME);
|
||||
// Initializes the data server with a specific test file.
|
||||
FServer := TAuraTABFileServer.Create(C_TEST_FILENAME);
|
||||
end;
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.TearDown;
|
||||
begin
|
||||
// Releases the data server instance.
|
||||
FServer := nil;
|
||||
// Clears any cached data to prevent interference with subsequent tests.
|
||||
TAskBid.ClearCache;
|
||||
end;
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.Test_ServerReturnsSameDataAs_LoadDataSeries;
|
||||
var
|
||||
chunk: array[0..C_MAX_FETCH-1] of TDataPoint<TAskBidItem>;
|
||||
chunk: array[0..C_MAX_FETCH - 1] of TDataPoint<TAskBidItem>;
|
||||
Dst: TArray<TDataPoint<TAskBidItem>>;
|
||||
i: Int64;
|
||||
begin
|
||||
// Loads the expected data directly using TAskBid.LoadDataSeries.WaitFor.
|
||||
var ExpectedData := TAskBid.LoadDataSeries(C_TEST_FILENAME).WaitFor;
|
||||
SetLength( Dst, Length( ExpectedData ) );
|
||||
SetLength(Dst, Length(ExpectedData));
|
||||
|
||||
var cnt: Int64 := 0;
|
||||
var n: Integer;
|
||||
// Fetches data chunks from the server until all data is retrieved or server indicates live data.
|
||||
while not FServer.IsLiveData.Value and (cnt < Length(Dst)) do
|
||||
begin
|
||||
n := FServer.GetChunk(chunk);
|
||||
if n > 0 then
|
||||
Move( chunk[0], dst[cnt], n * sizeof( TDataPoint<TAskBidItem> ) );
|
||||
inc( cnt, n );
|
||||
Move(chunk[0], dst[cnt], n * sizeof(TDataPoint<TAskBidItem>));
|
||||
inc(cnt, n);
|
||||
end;
|
||||
|
||||
// --- Assertions ---
|
||||
|
||||
// 1. Verify that the total number of records matches.
|
||||
// 1. Verify that the total number of records fetched from the server matches the expected count.
|
||||
Assert.AreEqual(Length(ExpectedData), cnt, 'Data record count mismatch');
|
||||
|
||||
// 2. Verify that the content of each record is identical.
|
||||
// The loop checks every 1000th element for efficiency.
|
||||
for n := 0 to High(ExpectedData) div 1000 do
|
||||
begin
|
||||
i := n * 1000;
|
||||
// E2003: Removed assertion for 'Index', as it's not part of the IDataPoint interface.
|
||||
// The index is an implementation detail of the faulty ConvertTicks and cannot be tested here.
|
||||
Assert.AreEqual(Dst[i].Idx, i, 'Index mismatch ');
|
||||
Assert.AreEqual(ExpectedData[i].TimeStamp, Dst[i].Time, 'Timestamp mismatch ');
|
||||
Assert.AreEqual(ExpectedData[i].TimeStamp, Dst[i].Time, 'Timestamp mismatch');
|
||||
Assert.AreEqual(ExpectedData[i].Data.Ask, Dst[i].Data.Ask, 'Ask price mismatch');
|
||||
Assert.AreEqual(ExpectedData[i].Data.Bid, Dst[i].Data.Bid, 'Bid price mismatch');
|
||||
end;
|
||||
end;
|
||||
|
||||
initialization
|
||||
// Registers the test fixture with DUnitX.
|
||||
TDUnitX.RegisterTestFixture(TTest_TABFileServer_Equivalence);
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user