DataSeries.Copy

This commit is contained in:
Michael Schimmel
2025-06-24 12:42:28 +02:00
parent 6077d094f7
commit 3048c28fe3
4 changed files with 211 additions and 26 deletions
+83 -2
View File
@@ -95,6 +95,14 @@ type
procedure TestIndexOfSingleItemSeries;
[Test]
procedure TestIndexOfRespectsMaxLookback;
// Tests for Copy
[Test]
procedure TestCopyHasSameContent;
[Test]
procedure TestCopyIsImmutableAfterOriginalChanges;
[Test]
procedure TestCopyRespectsMaxLookback;
end;
implementation
@@ -403,7 +411,7 @@ end;
procedure TTestDataSeries.Setup;
begin
FArray := TDataSeries<TAskBidItem>.CreateArray(0);
FSeries.Create(FArray);
FSeries := FArray;
end;
procedure TTestDataSeries.Teardown;
@@ -419,7 +427,7 @@ var
baseTime: TDateTime;
begin
FArray := TDataSeries<TAskBidItem>.CreateArray(MaxLookBack);
FSeries.Create(FArray);
FSeries := FArray;
baseTime := EncodeDate(2020, 7, 7);
// Add data points with increasing timestamps.
@@ -430,6 +438,79 @@ begin
end;
end;
procedure TTestDataSeries.TestCopyHasSameContent;
var
copy: TDataSeries<TAskBidItem>;
i: Integer;
begin
SetupSeriesWithData(15);
copy := FSeries.Copy;
Assert.AreEqual(FSeries.Count, copy.Count, 'Copy must have the same count as the original');
Assert.AreEqual(FSeries.TotalCount, copy.TotalCount, 'Copy must have the same total count as the original');
for i := 0 to FSeries.Count - 1 do
begin
Assert.AreEqual(FSeries[i].Time, copy[i].Time, 'Copied item timestamp must match original');
Assert.AreEqual(FSeries[i].Data.Ask, copy[i].Data.Ask, 'Copied item data must match original');
end;
end;
procedure TTestDataSeries.TestCopyIsImmutableAfterOriginalChanges;
var
copy: TDataSeries<TAskBidItem>;
originalCount, originalTotalCount: Int64;
newPoint: TDataPoint<TAskBidItem>;
lastTime: TDateTime;
begin
SetupSeriesWithData(10);
// Create the copy
copy := FSeries.Copy;
originalCount := copy.Count;
originalTotalCount := copy.TotalCount;
// Modify the original series
lastTime := FArray[0].Time;
newPoint := TDataPoint<TAskBidItem>.Create(lastTime + 1, TAskBidItem.Create(99, 99.1));
FArray.Add(newPoint);
Assert.AreEqual(Int64(11), FArray.Count, 'Original array count should have increased');
// Verify the copy remains unchanged
Assert.AreEqual(originalCount, copy.Count, 'Copy count must not change after original is modified');
Assert.AreEqual(originalTotalCount, copy.TotalCount, 'Copy total count must not change after original is modified');
Assert.AreNotEqual(FArray[0].Time, copy[0].Time, 'Newest item in copy should not be the new item from original');
end;
procedure TTestDataSeries.TestCopyRespectsMaxLookback;
var
copy: TDataSeries<TAskBidItem>;
newPoint: TDataPoint<TAskBidItem>;
lastTime: TDateTime;
begin
SetupSeriesWithData(20, 15); // 20 items total, but series count is capped at 15
Assert.AreEqual(Int64(15), FSeries.Count, 'Pre-condition: Series count should be capped by MaxLookback');
Assert.AreEqual(Int64(20), FSeries.TotalCount, 'Pre-condition: Series total count should be 20');
copy := FSeries.Copy;
// Verify the copy reflects the MaxLookback state
Assert.AreEqual(Int64(15), copy.Count, 'Copy count should be the MaxLookback value of the original');
Assert.AreEqual(Int64(20), copy.TotalCount, 'Copy total count should be the total items added to the original');
// Modify original
lastTime := FArray[0].Time;
newPoint := TDataPoint<TAskBidItem>.Create(lastTime + 1, TAskBidItem.Create(99, 99.1));
FArray.Add(newPoint);
// Verify the copy is still unchanged
Assert.AreEqual(Int64(15), copy.Count, 'Copy count must remain unchanged after original is modified');
Assert.AreEqual(Int64(20), copy.TotalCount, 'Copy total count must remain unchanged after original is modified');
end;
procedure TTestDataSeries.TestIndexOfExistingTimeStamp;
var
ATimeStamp: TDateTime;