-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathGraphsUndirectedSparseGraphTest.cs
92 lines (74 loc) · 4.14 KB
/
GraphsUndirectedSparseGraphTest.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
using System.Linq;
using DataStructures.Graphs;
using Xunit;
namespace UnitTest.DataStructuresTests
{
public static class GraphsUndirectedSparseGraphTest
{
[Fact]
public static void DoTest()
{
var graph = new UndirectedSparseGraph<string>();
var verticesSet1 = new[] { "a", "z", "s", "x", "d", "c", "f", "v" };
graph.AddVertices(verticesSet1);
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'.");
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");
Assert.True(graph.BreadthFirstWalk("s").SequenceEqual(new[] { "s", "a", "x", "z", "d", "c", "f", "v" }));
graph.AddVertices(new[] { "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 == 10, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 17, "Wrong edges count.");
Assert.True(graph.DepthFirstWalk().SequenceEqual(new[] { "a", "d", "e", "c", "v", "f", "x", "s", "b", "z" }));
}
}
}