-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
200 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using PSGraph.Model; | ||
|
||
namespace PSGraph.Common.Model; | ||
|
||
public class PSDistanceVectorRecord | ||
{ | ||
public required PSVertex Vertex; | ||
public double Level; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Xunit; | ||
using System; | ||
using System.Management.Automation; | ||
using PSGraph.Model; | ||
using FluentAssertions; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using PSGraph.Common.Model; | ||
|
||
namespace PSGraph.Tests | ||
{ | ||
public class GetGraphDistanceVectorCmdletTests : IDisposable | ||
{ | ||
private PowerShell _powershell; | ||
|
||
public GetGraphDistanceVectorCmdletTests() | ||
{ | ||
_powershell = PowerShell.Create(); | ||
_powershell.AddCommand("Import-Module") | ||
.AddParameter("Assembly", typeof(PSGraph.Cmdlets.GetGraphDistanceVector).Assembly); | ||
_powershell.Invoke(); | ||
_powershell.Commands.Clear(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_powershell.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void GetGraphDistanceVector_ValidAcyclicGraph_ReturnsDistances() | ||
{ | ||
// Arrange | ||
_powershell.AddCommand("New-Graph"); | ||
var graphResults = _powershell.Invoke(); | ||
var graph = graphResults[0].BaseObject as PsBidirectionalGraph; | ||
|
||
_powershell.Commands.Clear(); | ||
|
||
var vertexA = new PSVertex("A"); | ||
var vertexB = new PSVertex("B"); | ||
var vertexC = new PSVertex("C"); | ||
|
||
graph.AddVertexRange(new[] { vertexA, vertexB, vertexC }); | ||
graph.AddEdge(new PSEdge(vertexA, vertexB, new PSEdgeTag())); | ||
graph.AddEdge(new PSEdge(vertexB, vertexC, new PSEdgeTag())); | ||
|
||
_powershell.AddCommand("Get-GraphDistanceVector") | ||
.AddParameter("Graph", graph); | ||
|
||
// Act | ||
var results = _powershell.Invoke(); | ||
|
||
// Assert | ||
results.Should().NotBeNullOrEmpty(); | ||
|
||
var distances = results.Select(r => r.BaseObject as PSDistanceVectorRecord).ToList(); | ||
distances.Should().NotBeNull(); | ||
distances.Should().Contain(d => d.Vertex == vertexA && d.Level == 0); | ||
distances.Should().Contain(d => d.Vertex == vertexB && d.Level == 1); | ||
distances.Should().Contain(d => d.Vertex == vertexC && d.Level == 2); | ||
} | ||
|
||
[Fact] | ||
public void GetGraphDistanceVector_EmptyGraph_ReturnsEmptyDistances() | ||
{ | ||
// Arrange | ||
_powershell.AddCommand("New-Graph"); | ||
var graphResults = _powershell.Invoke(); | ||
var graph = graphResults[0].BaseObject as PsBidirectionalGraph; | ||
|
||
_powershell.Commands.Clear(); | ||
|
||
_powershell.AddCommand("Get-GraphDistanceVector") | ||
.AddParameter("Graph", graph); | ||
|
||
// Act | ||
var results = _powershell.Invoke(); | ||
|
||
// Assert | ||
results.Should().BeEmpty("because the graph is empty and has no vertices to calculate distances"); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Management.Automation; | ||
using QuikGraph.Algorithms; | ||
using PSGraph.Model; | ||
using System.Collections.Generic; | ||
using QuikGraph.Algorithms.TopologicalSort; | ||
using QuikGraph.Algorithms.Search; | ||
using QuikGraph.Algorithms.Observers; | ||
using System.Diagnostics.CodeAnalysis; | ||
using PSGraph.Common.Model; | ||
namespace PSGraph.Cmdlets | ||
{ | ||
[Cmdlet(VerbsCommon.Get, "GraphDistanceVector")] | ||
public class GetGraphDistanceVector : PSCmdlet | ||
{ | ||
[Parameter(Mandatory = true)] | ||
[ValidateNotNullOrEmpty] | ||
public PsBidirectionalGraph Graph; | ||
|
||
protected override void EndProcessing() | ||
{ | ||
|
||
var dfs = new DepthFirstSearchAlgorithm<PSVertex, PSEdge>(Graph); | ||
var distanceRecorder = new VertexDistanceRecorderObserver<PSVertex, PSEdge>(v => 1); | ||
|
||
var rootVertices = Graph.Vertices.Where(v => Graph.InDegree(v) == 0); | ||
|
||
using (distanceRecorder.Attach(dfs)) | ||
{ | ||
foreach (var vertex in rootVertices) | ||
{ | ||
dfs.SetRootVertex(vertex); | ||
dfs.Compute(); | ||
} | ||
|
||
} | ||
|
||
var res = new List<PSDistanceVectorRecord>(); | ||
foreach (var record in distanceRecorder.Distances) | ||
{ | ||
var rr = new PSDistanceVectorRecord() { Vertex = record.Key, Level = record.Value }; | ||
res.Add(rr); | ||
} | ||
|
||
WriteObject(res, enumerateCollection: true); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
PsGraph.Pester.Tests/PSGraph.GraphDistanceVector.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
BeforeAll { | ||
Import-Module "/workspaces/PSGraph/PSGraph.Tests/bin/Debug/net8.0/PSQuickGraph.psd1" | ||
} | ||
|
||
Describe 'Get-GraphDistanceVector' { | ||
BeforeEach { | ||
# Initialize a new graph before each test | ||
$graph = New-Graph | ||
|
||
# Add vertices | ||
Add-Vertex -Graph $graph -Vertex 'A' | ||
Add-Vertex -Graph $graph -Vertex 'B' | ||
Add-Vertex -Graph $graph -Vertex 'C' | ||
Add-Vertex -Graph $graph -Vertex 'D' | ||
|
||
# Add edges | ||
Add-Edge -From 'A' -To 'B' -Graph $graph | Out-Null | ||
Add-Edge -From 'A' -To 'C' -Graph $graph | Out-Null | ||
Add-Edge -From 'B' -To 'D' -Graph $graph | Out-Null | ||
Add-Edge -From 'C' -To 'D' -Graph $graph | Out-Null | ||
} | ||
|
||
It 'Should return the correct distance vector for the graph' { | ||
# Run the Get-GraphDistanceVector cmdlet | ||
$distanceVector = Get-GraphDistanceVector -Graph $graph | ||
|
||
# Check the results are not empty | ||
$distanceVector | Should -Not -BeNullOrEmpty | ||
|
||
# Expected results for distance from root vertices | ||
$expectedResults = @( | ||
@{ Vertex = 'A'; Level = 0 }, | ||
@{ Vertex = 'B'; Level = 1 }, | ||
@{ Vertex = 'C'; Level = 1 }, | ||
@{ Vertex = 'D'; Level = 2 } | ||
) | ||
|
||
# Validate each record in the distance vector | ||
foreach ($record in $distanceVector) { | ||
$vertex = $record.Vertex.Name | ||
$level = $record.Level | ||
|
||
$expected = $expectedResults | Where-Object { $_.Vertex -eq $vertex } | ||
|
||
$expected | Should -Not -BeNullOrEmpty | ||
$level | Should -BeExactly $expected.Level | ||
} | ||
} | ||
|
||
It 'Should return an empty result if the graph has no vertices' { | ||
$emptyGraph = New-Graph | ||
$result = Get-GraphDistanceVector -Graph $emptyGraph | ||
|
||
# The result should be empty | ||
$result | Should -BeNullOrEmpty | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.