-
Notifications
You must be signed in to change notification settings - Fork 1
/
TopologicalSortTests.cs
117 lines (89 loc) · 3.62 KB
/
TopologicalSortTests.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
using AlgorithmsAndDataStructures.Algorithms.Graph.Misc;
using AlgorithmsAndDataStructures.DataStructures.Graph;
using System;
using Xunit;
namespace AlgorithmsAndDataStructures.Tests.Algorithm.Graph
{
public class TopologicalSortTests
{
[Fact]
public void TopologicalSortForOneVertexGraph()
{
var sut = new TopologicalSort();
var node1 = new GraphVertex<int>();
var topologicalOrder = sut.GetTopologicalOrder(new[] { node1 });
Assert.Single(topologicalOrder);
Assert.Collection<int>(topologicalOrder, arg => Assert.Equal(0, arg));
}
[Fact]
public void TopologicalSortForTwoVertexGraph()
{
var sut = new TopologicalSort();
var node1 = new GraphVertex<int>
{
AdjacentVertices = new System.Collections.Generic.List<int> { 1 }
};
var node2 = new GraphVertex<int>();
var topologicalOrder = sut.GetTopologicalOrder(new[] { node1, node2 });
Assert.Equal(2,topologicalOrder.Count);
Assert.Collection<int>(topologicalOrder, arg => Assert.Equal(0, arg), arg => Assert.Equal(1, arg));
}
[Fact]
public void TopologicalSortForGraphWithCycle()
{
var sut = new TopologicalSort();
var node1 = new GraphVertex<int>
{
AdjacentVertices = new System.Collections.Generic.List<int> { 1 }
};
var node2 = new GraphVertex<int>
{
AdjacentVertices = new System.Collections.Generic.List<int> { 0 }
};
Assert.Throws<ArgumentException>(() => sut.GetTopologicalOrder(new[] { node1, node2 }));
}
[Fact]
public void TopologicalSortForDisconectedGraph()
{
var sut = new TopologicalSort();
var node1 = new GraphVertex<int>
{
AdjacentVertices = new System.Collections.Generic.List<int> { 1 }
};
var node2 = new GraphVertex<int>();
var node3 = new GraphVertex<int>();
var topologicalOrder = sut.GetTopologicalOrder(new[] { node1, node2, node3 });
Assert.Equal(3, topologicalOrder.Count);
Assert.Collection<int>(topologicalOrder, arg => Assert.Equal(2, arg), arg => Assert.Equal(0, arg), arg => Assert.Equal(1, arg));
}
[Fact]
public void Baseline()
{
var sut = new TopologicalSort();
var node1 = new GraphVertex<int>
{
AdjacentVertices = new System.Collections.Generic.List<int> { 1, 2 }
};
var node2 = new GraphVertex<int>();
var node3 = new GraphVertex<int>
{
AdjacentVertices = new System.Collections.Generic.List<int> { 3, 5 }
};
var node4 = new GraphVertex<int>
{
AdjacentVertices = new System.Collections.Generic.List<int> { 4 }
};
var node5 = new GraphVertex<int>();
var node6 = new GraphVertex<int>();
var topologicalOrder = sut.GetTopologicalOrder(new[] { node1, node2, node3, node4, node5, node6 });
Assert.Equal(6, topologicalOrder.Count);
Assert.Collection<int>(topologicalOrder,
arg => Assert.Equal(0, arg),
arg => Assert.Equal(2, arg),
arg => Assert.Equal(5, arg),
arg => Assert.Equal(3, arg),
arg => Assert.Equal(4, arg),
arg => Assert.Equal(1, arg));
}
}
}