-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathBreadthFirstSearchTests.cs
131 lines (83 loc) · 3.27 KB
/
BreadthFirstSearchTests.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
using Algorithms.Graph;
using DataStructures.Graph;
using NUnit.Framework;
using System.Collections.Generic;
namespace Algorithms.Tests.Graph;
public class BreadthFirstSearchTests
{
[Test]
public void VisitAll_ShouldCountNumberOfVisitedVertix_ResultShouldBeTheSameAsNumberOfVerticesInGraph()
{
//Arrange
var graph = new DirectedWeightedGraph<int>(10);
var vertex1 = graph.AddVertex(1);
var vertex2 = graph.AddVertex(20);
var vertex3 = graph.AddVertex(40);
var vertex4 = graph.AddVertex(40);
graph.AddEdge(vertex1, vertex2, 1);
graph.AddEdge(vertex2, vertex3, 1);
graph.AddEdge(vertex2, vertex4, 1);
graph.AddEdge(vertex4, vertex1, 1);
var dfsSearcher = new BreadthFirstSearch<int>();
long countOfVisitedVertices = 0;
//Act
dfsSearcher.VisitAll(graph, vertex1, _ => countOfVisitedVertices++);
//Assert
Assert.That(graph.Count, Is.EqualTo(countOfVisitedVertices));
}
[Test]
public void VisitAll_ShouldCountNumberOfVisitedVerices_TwoSeparatedGraphInOne()
{
//Arrange
var graph = new DirectedWeightedGraph<int>(10);
var vertex1 = graph.AddVertex(1);
var vertex2 = graph.AddVertex(20);
var vertex3 = graph.AddVertex(40);
var vertex4 = graph.AddVertex(40);
var vertex5 = graph.AddVertex(40);
var vertex6 = graph.AddVertex(40);
graph.AddEdge(vertex1, vertex2, 1);
graph.AddEdge(vertex2, vertex3, 1);
graph.AddEdge(vertex4, vertex5, 1);
graph.AddEdge(vertex5, vertex6, 1);
var dfsSearcher = new BreadthFirstSearch<int>();
long countOfVisitedVerticesPerFirstGraph = 0;
long countOfVisitedVerticesPerSecondGraph = 0;
//Act
dfsSearcher.VisitAll(graph, vertex1, _ => countOfVisitedVerticesPerFirstGraph++);
dfsSearcher.VisitAll(graph, vertex4, _ => countOfVisitedVerticesPerSecondGraph++);
//Assert
Assert.That(countOfVisitedVerticesPerFirstGraph, Is.EqualTo(3));
Assert.That(countOfVisitedVerticesPerSecondGraph, Is.EqualTo(3));
}
[Test]
public void VisitAll_ReturnTheSuqenceOfVertices_ShouldBeTheSameAsExpected()
{
//Arrange
var graph = new DirectedWeightedGraph<int>(10);
var vertex1 = graph.AddVertex(1);
var vertex2 = graph.AddVertex(20);
var vertex3 = graph.AddVertex(40);
var vertex4 = graph.AddVertex(40);
var vertex5 = graph.AddVertex(40);
graph.AddEdge(vertex1, vertex2, 1);
graph.AddEdge(vertex1, vertex5, 1);
graph.AddEdge(vertex2, vertex3, 1);
graph.AddEdge(vertex2, vertex5, 1);
graph.AddEdge(vertex2, vertex4, 1);
var dfsSearcher = new BreadthFirstSearch<int>();
var expectedSequenceOfVisitedVertices = new List<Vertex<int>>
{
vertex1,
vertex2,
vertex5,
vertex3,
vertex4,
};
var sequenceOfVisitedVertices = new List<Vertex<int>>();
//Act
dfsSearcher.VisitAll(graph, vertex1, vertex => sequenceOfVisitedVertices.Add(vertex));
//Assert
Assert.That(sequenceOfVisitedVertices, Is.EqualTo(expectedSequenceOfVisitedVertices));
}
}