-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathGraphsUndirectedDenseGraphTest.cs
116 lines (94 loc) · 5.24 KB
/
GraphsUndirectedDenseGraphTest.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
using DataStructures.Graphs;
using System.Linq;
using Xunit;
namespace UnitTest.DataStructuresTests
{
public static class GraphsUndirectedDenseGraphTests
{
[Fact]
public static void DoTest()
{
var graph = new UndirectedDenseGraph<string>();
graph.AddVertices(new string[] { "a", "z", "s", "x", "d", "c", "f", "v" });
graph.AddEdge("a", "s");
graph.AddEdge("a", "z");
graph.AddEdge("s", "x");
graph.AddEdge("x", "d");
graph.AddEdge("x", "c");
graph.AddEdge("d", "f");
graph.AddEdge("d", "c");
graph.AddEdge("c", "f");
graph.AddEdge("c", "v");
graph.AddEdge("v", "f");
var allEdges = graph.Edges.ToList();
Assert.True(graph.VerticesCount == 8, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 10, "Wrong edges count.");
Assert.True(graph.EdgesCount == allEdges.Count, "Wrong edges count.");
Assert.True(graph.OutgoingEdges("a").ToList().Count == 2, "Wrong outgoing edges from 'a'.");
Assert.True(graph.OutgoingEdges("s").ToList().Count == 2, "Wrong outgoing edges from 's'.");
Assert.True(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'.");
Assert.True(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'.");
Assert.True(graph.OutgoingEdges("c").ToList().Count == 4, "Wrong outgoing edges from 'c'.");
Assert.True(graph.OutgoingEdges("v").ToList().Count == 2, "Wrong outgoing edges from 'v'.");
Assert.True(graph.OutgoingEdges("f").ToList().Count == 3, "Wrong outgoing edges from 'f'.");
Assert.True(graph.OutgoingEdges("z").ToList().Count == 1, "Wrong outgoing edges from 'z'.");
Assert.True(graph.IncomingEdges("a").ToList().Count == 2, "Wrong incoming edges from 'a'.");
Assert.True(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'.");
Assert.True(graph.IncomingEdges("x").ToList().Count == 3, "Wrong incoming edges from 'x'.");
Assert.True(graph.IncomingEdges("d").ToList().Count == 3, "Wrong incoming edges from 'd'.");
Assert.True(graph.IncomingEdges("c").ToList().Count == 4, "Wrong incoming edges from 'c'.");
Assert.True(graph.IncomingEdges("v").ToList().Count == 2, "Wrong incoming edges from 'v'.");
Assert.True(graph.IncomingEdges("f").ToList().Count == 3, "Wrong incoming edges from 'f'.");
Assert.True(graph.IncomingEdges("z").ToList().Count == 1, "Wrong incoming edges from 'z'.");
// TEST REMOVE nodes v and f
graph.RemoveVertex("v");
graph.RemoveVertex("f");
Assert.True(graph.VerticesCount == 6, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 6, "Wrong edges count.");
Assert.True(graph.HasVertex("v") == false, "Vertex v must have been deleted.");
Assert.True(graph.HasVertex("f") == false, "Vertex f must have been deleted.");
// TEST RE-ADD REMOVED NODES AND EDGES
graph.AddVertex("v");
graph.AddVertex("f");
graph.AddEdge("d", "f");
graph.AddEdge("c", "f");
graph.AddEdge("c", "v");
graph.AddEdge("v", "f");
Assert.True(graph.VerticesCount == 8, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 10, "Wrong edges count.");
Assert.True(graph.HasVertex("v") == true, "Vertex v does not exist.");
Assert.True(graph.HasVertex("f") == true, "Vertex f does not exist.");
// RE-TEST REMOVE AND ADD NODES AND EDGES
graph.RemoveEdge("d", "c");
graph.RemoveEdge("c", "v");
graph.RemoveEdge("a", "z");
Assert.True(graph.VerticesCount == 8, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 7, "Wrong edges count.");
graph.RemoveVertex("x");
Assert.True(graph.VerticesCount == 7, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 4, "Wrong edges count.");
graph.AddVertex("x");
graph.AddEdge("s", "x");
graph.AddEdge("x", "d");
graph.AddEdge("x", "c");
graph.AddEdge("d", "c");
graph.AddEdge("c", "v");
graph.AddEdge("a", "z");
// output: (s) (a) (x) (z) (d) (c) (f) (v)
Assert.True(graph.BreadthFirstWalk("s").SequenceEqual(new string[] { "s", "a", "x", "z", "d", "c", "f", "v" }));
graph.Clear();
graph.AddVertices(new string[] { "a", "b", "c", "d", "e", "f" });
graph.AddEdge("a", "b");
graph.AddEdge("a", "d");
graph.AddEdge("b", "e");
graph.AddEdge("d", "b");
graph.AddEdge("d", "e");
graph.AddEdge("e", "c");
graph.AddEdge("c", "f");
graph.AddEdge("f", "f");
Assert.True(graph.VerticesCount == 6, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 8, "Wrong edges count.");
Assert.True(graph.DepthFirstWalk().SequenceEqual(new string[] { "a", "d", "e", "c", "f", "b" }));
}
}
}