Skip to content

Commit

Permalink
Fix TPA.Rows/Columns going out of range + test
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Dec 31, 2024
1 parent 9939d4e commit df8ab8f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Examples/image_drawtext.simba
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ begin
myImage.DrawColor := Colors.RED;
myImage.DrawBox(myBox);
myImage.DrawColor := Colors.BLACK;
myImage.DrawText(Now.ToString('c'), myBox, [EDrawTextAlign.CENTER, EDrawTextAlign.VERTICAL_CENTER]);
myImage.DrawText(Now.ToString('c'), myBox, [EImageTextAlign.CENTER, EImageTextAlign.VERTICAL_CENTER]);

myImage.FontBold := True;
myImage.FontSize := 50;
Expand Down
12 changes: 6 additions & 6 deletions Examples/mouse_teleport_event.simba
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
var
TPA: TPointArray; // stores the teleport events so we can view at the end

procedure MouseTeleportEvent(var Sender: TTarget; P: TPoint);
procedure MouseTeleportEvent(var Target: TTarget; Data: TTargetEventData);
begin
WriteLn('Mouse teleported to: ', P);
TPA += P;
WriteLn('Mouse teleported to: ', Data.MouseTeleport);
TPA += [Data.MouseTeleport.X, Data.MouseTeleport.Y];
end;

var
Event: TMouseTeleportEvent;
Event: TTargetEvent;
begin
Event := Target.AddMouseEvent(@MouseTeleportEvent);
Event := Target.AddEvent(ETargetEventType.MOUSE_TELEPORT, @MouseTeleportEvent);
Target.MouseTeleport([200,200]);
Target.MouseMove([600,600]);
Target.RemoveMouseEvent(Event); // Remove the event
Target.RemoveEvent(ETargetEventType.MOUSE_TELEPORT, Event); // Remove the event

Target.MouseMove([200, 600]); // The event has been removed so this wont be "recorded" when we show the path

Expand Down
2 changes: 2 additions & 0 deletions Examples/randomleft.simba
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Random left returns numbers weighted to the left number

const
SampleCount = 1000000;
Range = 10;
Expand Down
4 changes: 2 additions & 2 deletions Source/simba.vartype_pointarray.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ function TPointArrayHelper.Rows: T2DPointArray;
begin
Start := I;
Current := TPA[I].Y;
while (TPA[I].Y = Current) do
while (I < Len) and (TPA[I].Y = Current) do
Inc(I);

Buffer.Add(Copy(TPA, Start, I-Start));
Expand All @@ -1824,7 +1824,7 @@ function TPointArrayHelper.Columns: T2DPointArray;
begin
Start := I;
Current := TPA[I].X;
while (TPA[I].X = Current) do
while (I < Len) and (TPA[I].X = Current) do
Inc(I);

Buffer.Add(Copy(TPA, Start, I-Start));
Expand Down
45 changes: 45 additions & 0 deletions Tests/tpa_rowcolumn.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{$assertions on}

var
TPA, TPASorted: TPointArray;
Rows, Columns: T2DPointArray;
I, J: Integer;
begin
TPA := TPointArray.CreateFromBox([0,0,99,99], True);
Rows := TPA.Rows;
Columns := TPA.Columns;

// row
TPASorted := TPA.SortByRow(True);
for I := 0 to High(TPASorted) do
Assert(TPASorted[I].Y = (I div 100));

TPASorted := TPA.SortByRow(False);
for I := 0 to High(TPASorted) do
Assert(TPASorted[I].Y = 99 - (I div 100));

Assert(Length(Rows) = 100);
for I := 0 to High(Rows) do
begin
Assert(Length(Rows[I]) = 100);
for j:=0 to High(Rows[I]) do
Assert(Rows[I,J] = [J,I]);
end;

// column
TPASorted := TPA.SortByColumn(True);
for I := 0 to High(TPASorted) do
Assert(TPASorted[I].X = (I div 100));

TPASorted := TPA.SortByColumn(False);
for I := 0 to High(TPASorted) do
Assert(TPASorted[I].X = 99 - (I div 100));

Assert(Length(Columns) = 100);
for I := 0 to High(Columns) do
begin
Assert(Length(Columns[I]) = 100);
for j:=0 to High(Columns[I]) do
Assert(Columns[I,J] = [I,J]);
end;
end;

0 comments on commit df8ab8f

Please sign in to comment.