Skip to content

Commit

Permalink
Add tests for grid
Browse files Browse the repository at this point in the history
  • Loading branch information
roy-t committed Apr 16, 2024
1 parent 7d78427 commit b617f6d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
44 changes: 44 additions & 0 deletions LibGame.Tests/Collections/GridTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using LibGame.Collections;
using LibGame.Mathematics;

namespace LibGame.Tests.Collections;
public static class GridTests
{
private readonly record struct CR(int Column, int Row);

[Fact(DisplayName = "Smoke test for `Grid<T>`")]
public static void SmokeTest()
{
var values = new CR[]
{
new CR(0, 0), new CR(1, 0), new CR(2, 0),
new CR(0, 1), new CR(1, 1), new CR(2, 1),
new CR(0, 2), new CR(1, 2), new CR(2, 2),
new CR(0, 3), new CR(1, 3), new CR(2, 3),
};

var grid = new Grid<CR>(values, 3, 4);

Equal(3, grid.Columns);
Equal(4, grid.Rows);
Equal(12, grid.Count);

for (var i = 0; i < 12; i++)
{
var (c, r) = Indexes.ToTwoDimensional(i, 3);
var value = new CR(c, r);
Equal(grid[i], value);
Equal(grid[c, r], value);
}

var slice = grid.Slice(2, 1, 2, 1);
Equal(1, slice.Columns);
Equal(1, slice.Rows);

Equal(new CR(2, 2), slice[0]);

var safeSlice = grid.SliceAtMost(2, 10, 2, 10);
Equal(1, safeSlice.Columns);
Equal(2, safeSlice.Rows);
}
}
12 changes: 12 additions & 0 deletions LibGame/Collections/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ internal ReadOnlyGridSlice(T[] tiles, int stride, int columnOffset, int columnSp
Debug.Assert(rowOffset >= 0);
Debug.Assert(rowSpan > 0);



this.tiles = tiles;
this.stride = stride;

Expand Down Expand Up @@ -172,6 +174,16 @@ public IReadOnlyGrid<T> AsReadOnly()

public IReadOnlyGrid<T> Slice(int columnOffset, int columnSpan, int rowOffset, int rowSpan)
{
if (columnOffset + columnSpan > this.Columns)
{
throw new ArgumentOutOfRangeException(nameof(columnSpan));
}

if (rowOffset + rowSpan > this.Rows)
{
throw new ArgumentOutOfRangeException(nameof(rowSpan));
}

return new ReadOnlyGridSlice<T>(this.Tiles, this.Columns, columnOffset, columnSpan, rowOffset, rowSpan);
}
}
34 changes: 0 additions & 34 deletions LibGame/Tiles/TileUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Numerics;
using LibGame.Mathematics;

namespace LibGame.Tiles;

Expand Down Expand Up @@ -40,39 +39,6 @@ public static TileSide GetOppositeSide(TileSide side)
};
}

public static Neighbours<T> GetNeighboursFromGrid<T>(T[] grid, int columns, int rows, int index, T fallback)
where T : struct
{
var nw = GetFromGrid(grid, columns, rows, index, -1, -1, fallback);
var n = GetFromGrid(grid, columns, rows, index, 0, -1, fallback);
var ne = GetFromGrid(grid, columns, rows, index, 1, -1, fallback);
var w = GetFromGrid(grid, columns, rows, index, -1, 0, fallback);
var e = GetFromGrid(grid, columns, rows, index, 1, 0, fallback);
var sw = GetFromGrid(grid, columns, rows, index, -1, 1, fallback);
var s = GetFromGrid(grid, columns, rows, index, 0, 1, fallback);
var se = GetFromGrid(grid, columns, rows, index, 1, 1, fallback);

return new Neighbours<T>(nw, n, ne, w, e, sw, s, se);
}


public static T GetFromGrid<T>(T[] grid, int columns, int rows, int index, int offsetColumn, int offsetRow, T fallBack)
{
var (c, r) = Indexes.ToTwoDimensional(index, columns);
c += offsetColumn;
r += offsetRow;

if (c >= 0 && c < columns && r >= 0 && r < rows)
{
var i = Indexes.ToOneDimensional(c, r, columns);
return grid[i];
}

return fallBack;
}



public static Vector3 GetCornerPosition(int column, int row, Tile tile, TileCorner c)
{
var offset = tile.GetHeight(c);
Expand Down

0 comments on commit b617f6d

Please sign in to comment.