forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectedGraphTest.cs
192 lines (143 loc) · 5.05 KB
/
DirectedGraphTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using NFluent;
using ReClassNET.Util;
using Xunit;
namespace ReClass.NET_Tests.Util
{
public class DirectedGraphTest
{
[Fact]
public void TestAddContainsVertex()
{
const int Vertex = 0;
var sut = new DirectedGraph<int>();
Check.That(sut.AddVertex(Vertex)).IsTrue();
Check.That(sut.ContainsVertex(Vertex)).IsTrue();
}
[Fact]
public void TestAddExistingVertex()
{
const int Vertex = 0;
var sut = new DirectedGraph<int>();
sut.AddVertex(Vertex);
Check.That(sut.AddVertex(Vertex)).IsFalse();
}
[Fact]
public void TestContainsWithEmptyGraph()
{
const int Vertex = 0;
var sut = new DirectedGraph<int>();
Check.That(sut.ContainsVertex(Vertex)).IsFalse();
}
[Fact]
public void TestAddContainsMultipleVertices()
{
var vertices = new[] { 0, 1, 2, 3 };
var sut = new DirectedGraph<int>();
sut.AddVertices(vertices);
foreach (var vertex in vertices)
{
Check.That(sut.ContainsVertex(vertex)).IsTrue();
}
Check.That(sut.Vertices).IsEquivalentTo(vertices);
}
[Fact]
public void TestAddEdgeToNonExistingVertex()
{
const int Vertex1 = 0;
const int Vertex2 = 1;
var sut = new DirectedGraph<int>();
Check.ThatCode(() => sut.AddEdge(Vertex1, Vertex2)).Throws<ArgumentException>();
sut.AddVertex(Vertex1);
Check.ThatCode(() => sut.AddEdge(Vertex1, Vertex2)).Throws<ArgumentException>();
Check.ThatCode(() => sut.AddEdge(Vertex2, Vertex1)).Throws<ArgumentException>();
}
[Fact]
public void TestContainsEdgeToNonExistingVertex()
{
const int Vertex1 = 0;
const int Vertex2 = 1;
var sut = new DirectedGraph<int>();
Check.ThatCode(() => sut.ContainsEdge(Vertex1, Vertex2)).Throws<ArgumentException>();
sut.AddVertex(Vertex1);
Check.ThatCode(() => sut.ContainsEdge(Vertex1, Vertex2)).Throws<ArgumentException>();
Check.ThatCode(() => sut.ContainsEdge(Vertex2, Vertex1)).Throws<ArgumentException>();
}
[Fact]
public void TestAddContainsEdge()
{
const int Vertex1 = 0;
const int Vertex2 = 1;
var sut = new DirectedGraph<int>();
sut.AddVertex(Vertex1);
sut.AddVertex(Vertex2);
Check.That(sut.ContainsEdge(Vertex1, Vertex2)).IsFalse();
Check.That(sut.AddEdge(Vertex1, Vertex2)).IsTrue();
Check.That(sut.ContainsEdge(Vertex1, Vertex2)).IsTrue();
}
[Fact]
public void TestAddExistingEdge()
{
const int Vertex1 = 0;
const int Vertex2 = 1;
var sut = new DirectedGraph<int>();
sut.AddVertex(Vertex1);
sut.AddVertex(Vertex2);
sut.AddEdge(Vertex1, Vertex2);
Check.That(sut.AddEdge(Vertex1, Vertex2)).IsFalse();
}
[Fact]
public void TestGetNeighboursOfNonExistingVertex()
{
const int Vertex = 0;
var sut = new DirectedGraph<int>();
Check.ThatCode(() => sut.GetNeighbours(Vertex)).Throws<ArgumentException>();
}
public static IEnumerable<object[]> GetTestGetNeighboursData() => new List<object[]>
{
new object[] { new[] { 1 }, new[] { new[] { 1, 1 } }, new[] { 1 } },
new object[] { new[] { 1, 2 }, new[] { new[] { 2, 1 } }, new int[0] },
new object[] { new[] { 1, 2 }, new[] { new[] { 1, 2 } }, new[] { 2 } },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 2, 1 }, new[] { 2, 3 } }, new int[0] },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 1, 2 }, new[] { 2, 3 } }, new[] { 2 } },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 1, 2 }, new[] { 1, 3 } }, new[] { 2, 3 } },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 1, 1 }, new[] { 1, 2 }, new[] { 1, 3 } }, new[] { 1, 2, 3 } }
};
[Theory]
[MemberData(nameof(GetTestGetNeighboursData))]
public void TestGetNeighbours(int[] vertices, int[][] edges, int[] neighbours)
{
var sut = new DirectedGraph<int>();
sut.AddVertices(vertices);
foreach (var edge in edges)
{
sut.AddEdge(edge[0], edge[1]);
}
Check.That(sut.GetNeighbours(vertices[0])).IsEquivalentTo(neighbours);
}
public static IEnumerable<object[]> GetTestContainsCycleData() => new List<object[]>
{
new object[] { new[] { 1 }, new[] { new[] { 1, 1 } }, true },
new object[] { new[] { 1, 2 }, new[] { new[] { 1, 2 } }, false },
new object[] { new[] { 1, 2 }, new[] { new[] { 1, 2 }, new[] { 2, 1 } }, true },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 1, 2 }, new[] { 2, 3 } }, false },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 1, 2 }, new[] { 1, 3 }, new[] { 2, 3 } }, false },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 1, 2 }, new[] { 2, 3 }, new[] { 3, 1 } }, true },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 1, 2 }, new[] { 2, 3 }, new[] { 3, 2 } }, true },
new object[] { new[] { 1, 2, 3 }, new[] { new[] { 1, 2 }, new[] { 2, 1 }, new[] { 2, 3 }, new[] { 3, 2 } }, true }
};
[Theory]
[MemberData(nameof(GetTestContainsCycleData))]
public void TestContainsCycle(int[] vertices, int[][] edges, bool containsCycle)
{
var sut = new DirectedGraph<int>();
sut.AddVertices(vertices);
foreach (var edge in edges)
{
sut.AddEdge(edge[0], edge[1]);
}
Check.That(sut.ContainsCycle()).IsEqualTo(containsCycle);
}
}
}