Skip to content

Commit 8d3d2b3

Browse files
author
Kapil Borle
committed
Make digraph base class immutable
1 parent af33007 commit 8d3d2b3

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

Engine/Helper.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,6 +3566,23 @@ public Digraph()
35663566
graph = new List<List<int>> ();
35673567
}
35683568

3569+
public Digraph(int numVertices, List<Tuple<int, int>> edges) : this()
3570+
{
3571+
for (int v = 0; v < numVertices; v++)
3572+
{
3573+
AddVertex();
3574+
}
3575+
3576+
foreach (Tuple<int, int> tuple in edges)
3577+
{
3578+
var fromV = tuple.Item1;
3579+
var toV = tuple.Item2;
3580+
ValidateVertex(fromV);
3581+
ValidateVertex(toV);
3582+
AddEdge(fromV, toV);
3583+
}
3584+
}
3585+
35693586
public int GetNumVertices()
35703587
{
35713588
return graph.Count;
@@ -3587,12 +3604,12 @@ public bool ContainsVertex(int vertex)
35873604
return vertex >= 0 && vertex < graph.Count;
35883605
}
35893606

3590-
public void AddVertex()
3607+
protected void AddVertex()
35913608
{
35923609
graph.Add(new List<int>());
35933610
}
35943611

3595-
public void AddEdge(int fromVertex, int toVertex)
3612+
protected void AddEdge(int fromVertex, int toVertex)
35963613
{
35973614
ValidateVertex(fromVertex);
35983615
ValidateVertex(toVertex);

Tests/Engine/Helpers.tests.ps1

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
Import-Module PSScriptAnalyzer
22

3-
Describe "Test Directed Graph" {
4-
Context "When a graph is created" {
5-
$digraph = New-Object -TypeName 'Microsoft.Windows.PowerShell.ScriptAnalyzer.DiGraph'
6-
0..4 | ForEach-Object {$digraph.AddVertex()}
73

8-
$digraph.AddEdge(0, 1);
9-
$digraph.AddEdge(0, 4);
10-
$digraph.AddEdge(1, 3);
4+
Function ConvertType($x)
5+
{
6+
$z = [System.Collections.Generic.List[Tuple[int,int]]]::new()
7+
$x | ForEach-Object {$z.Add([System.Tuple[int,int]]::new($_[0], $_[1]))}
8+
return $z
9+
}
1110

11+
Describe "Test Directed Graph" {
12+
Context "When a graph is created" {
13+
$edges = ConvertType (0,1),(0,4),(1,3)
14+
$digraph = New-Object -TypeName 'Microsoft.Windows.PowerShell.ScriptAnalyzer.DiGraph' -ArgumentList 5,$edges
1215
It "correctly adds the vertices" {
1316
$digraph.GetNumVertices() | Should Be 5
1417
}

0 commit comments

Comments
 (0)