Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Csdsa.Algorithms.GraphTraversal;

using Xunit;

namespace Csdsa.Tests.Algorithms.GraphTraversals;

public sealed partial class GridDfsTests
{
[Fact]
public void FloodFill_FillsConnectedRegion()
{
int[,] grid = CreateFloodFillGrid();
Point start = new Point(0, 0);

GridDfs.FloodFill(grid, start, newColor: 9);

Assert.Equal(9, grid[0, 0]);
Assert.Equal(9, grid[0, 1]);
Assert.Equal(9, grid[1, 0]);

// Ensure other regions are unchanged.
Assert.Equal(0, grid[1, 1]);
Assert.Equal(0, grid[1, 2]);
Assert.Equal(1, grid[2, 2]);
}

[Fact]
public void FloodFill_DoesNothing_WhenColorIsSame()
{
int[,] grid = CreateFloodFillGrid();
Point start = new Point(0, 0);

GridDfs.FloodFill(grid, start, newColor: 1);

// Entire grid should be unchanged.
Assert.Equal(1, grid[0, 0]);
Assert.Equal(1, grid[0, 1]);
Assert.Equal(0, grid[0, 2]);
Assert.Equal(1, grid[1, 0]);
Assert.Equal(0, grid[1, 1]);
Assert.Equal(0, grid[1, 2]);
Assert.Equal(0, grid[2, 0]);
Assert.Equal(0, grid[2, 1]);
Assert.Equal(1, grid[2, 2]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Csdsa.Algorithms.GraphTraversal;

namespace Csdsa.Tests.Algorithms.GraphTraversals;

public sealed partial class GridDfsTests
{
private static int[,] CreateSimpleIslandGrid()
{
// 1 1 0 0
// 1 0 0 1
// 0 0 1 1
// 0 0 0 0
return new[,]
{
{ 1, 1, 0, 0 },
{ 1, 0, 0, 1 },
{ 0, 0, 1, 1 },
{ 0, 0, 0, 0 },
};
}

private static int[,] CreateFloodFillGrid()
{
// 1 1 0
// 1 0 0
// 0 0 1
return new[,]
{
{ 1, 1, 0 },
{ 1, 0, 0 },
{ 0, 0, 1 },
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Csdsa.Algorithms.GraphTraversal;

using Xunit;

namespace Csdsa.Tests.Algorithms.GraphTraversals;

public sealed partial class GridDfsTests
{
[Fact]
public void NumberOfIslandsDfs_CountsCorrectIslands()
{
int[,] grid = CreateSimpleIslandGrid();

int count = GridDfs.NumberOfIslandsDFS(grid);

Assert.Equal(2, count);
}

[Fact]
public void NumberOfIslandsDfs_ReturnsZero_WhenNoIslands()
{
int[,] grid =
{
{ 0, 0, 0 },
{ 0, 0, 0 },
};

int count = GridDfs.NumberOfIslandsDFS(grid);

Assert.Equal(0, count);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Csdsa.Algorithms.GraphTraversal;

namespace Csdsa.Tests.Algorithms.GraphTraversals;

public sealed partial class TreeDfsTests
{
// Builds a simple binary tree:
// 1
// / \
// 2 3
// / \
// 4 5
private static TreeNode CreateSampleTree()
{
return new TreeNode
{
Val = 1,
Left = new TreeNode
{
Val = 2,
Left = new TreeNode { Val = 4 },
Right = new TreeNode { Val = 5 },
},
Right = new TreeNode
{
Val = 3,
Left = new TreeNode { Val = 6 },
Right = new TreeNode { Val = 7 },
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;

using Csdsa.Algorithms.GraphTraversal;

using Xunit;

namespace Csdsa.Tests.Algorithms.GraphTraversals;

public sealed partial class TreeDfsTests
{
[Fact]
public void Postorder_ReturnsLeftRightRootOrder()
{
TreeNode root = CreateSampleTree();

IReadOnlyList<int> traversal = TreeDfs.Postorder(root);

// Expected postorder: 4,5,2,6,7,3,1
Assert.Equal(new[] { 4, 5, 2, 6, 7, 3, 1 }, traversal);
}

[Fact]
public void Postorder_OnNullRoot_ReturnsEmpty()
{
IReadOnlyList<int> traversal = TreeDfs.Postorder(root: null);

Assert.Empty(traversal);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;

using Csdsa.Algorithms.GraphTraversal;

using Xunit;

namespace Csdsa.Tests.Algorithms.GraphTraversals;

public sealed partial class TreeDfsTests
{
[Fact]
public void Preorder_ReturnsRootLeftRightOrder()
{
TreeNode root = CreateSampleTree();

IReadOnlyList<int> traversal = TreeDfs.Preorder(root);

// Expected preorder: 1,2,4,5,3,6,7
Assert.Equal(new[] { 1, 2, 4, 5, 3, 6, 7 }, traversal);
}

[Fact]
public void Preorder_OnNullRoot_ReturnsEmpty()
{
IReadOnlyList<int> traversal = TreeDfs.Preorder(root: null);

Assert.Empty(traversal);
}
}
25 changes: 0 additions & 25 deletions tests/Csdsa.Tests/DataStructures/Vectors/Vector.AsSSpan.Tests.cs

This file was deleted.

51 changes: 51 additions & 0 deletions tests/Csdsa.Tests/DataStructures/Vectors/Vector.AsSpan.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Csdsa.DataStructures.Vectors;
using Xunit;

namespace Csdsa.Tests.DataStructures.Vectors;

public class VectorAsSpanTests
{
private const double Tolerance = 1e-6;

[Fact]
public void AsSpan_ReturnsCorrectView()
{
double[] components = { 1, 2, 3 };
Vector<double> vector = new Vector<double>(components);

var span = vector.AsSpan();

Assert.Equal(components.Length, span.Length);

for (int i = 0; i < components.Length; i++)
{
Assert.Equal(components[i], span[i], Tolerance);
}
}

[Fact]
public void AsSpan_ContainsSameComponents()
{
Vector<int> vector = new Vector<int>(new[] { 1, 2, 3 });

Span<int> span = vector.AsSpan();

Assert.Equal(3, span.Length);
Assert.Equal(1, span[0]);
Assert.Equal(2, span[1]);
Assert.Equal(3, span[2]);
}

[Fact]
public void AsSpan_ReturnsIndependentCopy()
{
Vector<int> vector = new Vector<int>(new[] { 1, 2, 3 });

Span<int> span = vector.AsSpan();
span[0] = 99;

// Since AsSpan returns a span over a copy, original vector should be unchanged.
Span<int> span2 = vector.AsSpan();
Assert.Equal(1, span2[0]);
}
}