Skip to content

Commit 0f939bb

Browse files
committed
rewrite tests
1 parent 548267b commit 0f939bb

File tree

5 files changed

+164
-84
lines changed

5 files changed

+164
-84
lines changed
Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,58 @@
11
namespace Dijkstra.NET.Tests
22
{
3+
using System;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
45
using Model;
56
using ShortestPath;
67

78
[TestClass]
89
public class BfsParallelTest
910
{
11+
private TheShortestPathFixture _fixture;
12+
private readonly Func<Graph<int, string>, Dijkstra<int, string>> _dijkstraFactory = g => new BfsParallel<int, string>(g);
13+
14+
[TestInitialize]
15+
public void Initialise()
16+
{
17+
_fixture = new TheShortestPathFixture();
18+
}
19+
1020

1121
[TestMethod]
1222
public void BfsParallel_Should_Find_Path_In_Multi_Paths_Graph()
1323
{
14-
var fixture = new TheShortestPathFixture();
15-
fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(g => new BfsParallel<int, string>(g));
24+
_fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(_dijkstraFactory);
1625
}
1726

1827
[TestMethod]
1928
public void Guard_In_BfsParallel_Should_Always_Works()
2029
{
21-
var fixture = new TheShortestPathFixture();
22-
2330
for (int i = 0; i < 500; i++)
24-
fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(g => new BfsParallel<int, string>(g));
31+
_fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(g => new BfsParallel<int, string>(g));
32+
}
33+
34+
[TestMethod]
35+
public void BfsParallel_Should_Find_Path_With_One_Vertex_In_Graph()
36+
{
37+
_fixture.Algorithm_Should_Find_Path_With_One_Vertex_In_Graph(_dijkstraFactory);
38+
}
39+
40+
[TestMethod]
41+
public void BfsParallel_Should_Find_Path_With_One_Vertex_And_One_Edge_In_Graph()
42+
{
43+
_fixture.Algorithm_Should_Find_Path_With_One_Vertex_And_One_Edge_In_Graph(_dijkstraFactory);
44+
}
45+
46+
[TestMethod]
47+
public void BfsParallel_Should_Find_Path_In_Multi_Edges_Graph()
48+
{
49+
_fixture.Algorithm_Should_Find_Path_In_Multi_Edges_Graph(_dijkstraFactory);
50+
}
51+
52+
[TestMethod]
53+
public void BfsParallel_Not_Should_Find_Path_In_Graph()
54+
{
55+
_fixture.Algorithm_Not_Should_Find_Path_In_Graph(_dijkstraFactory);
2556
}
2657
}
2758
}
Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,51 @@
11
namespace Dijkstra.NET.Tests
22
{
3-
using System.Linq;
3+
using System;
44
using Microsoft.VisualStudio.TestTools.UnitTesting;
55
using Model;
66
using ShortestPath;
77

88
[TestClass]
99
public class DijkstraTest
1010
{
11+
private TheShortestPathFixture _fixture;
12+
13+
private readonly Func<Graph<int, string>, Dijkstra<int, string>> _dijkstraFactory = g => new Dijkstra<int, string>(g);
14+
15+
[TestInitialize]
16+
public void Initialise()
17+
{
18+
_fixture = new TheShortestPathFixture();
19+
}
20+
1121
[TestMethod]
1222
public void Dijkstra_Should_Find_Path_In_Multi_Paths_Graph()
1323
{
14-
var fixture = new TheShortestPathFixture();
15-
fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(g => new Dijkstra<int, string>(g));
24+
_fixture.Algorithm_Should_Find_Path_In_Multi_Paths_Graph(_dijkstraFactory);
1625
}
1726

1827
[TestMethod]
1928
public void Dijkstra_Should_Find_Path_With_One_Vertex_In_Graph()
2029
{
21-
var graph = new Graph<int, string>();
22-
graph.AddNode(0);
23-
24-
var dijkstra = new Dijkstra<int, string>(graph);
25-
var result = dijkstra.Process(0, 0);
26-
uint[] path = result.GetPath().ToArray();
27-
28-
Assert.AreEqual<uint>(0, path[0]);
29-
Assert.AreEqual(0, result.Distance);
30-
Assert.IsTrue(result.IsFounded);
30+
_fixture.Algorithm_Should_Find_Path_With_One_Vertex_In_Graph(_dijkstraFactory);
3131
}
3232

3333
[TestMethod]
3434
public void Dijkstra_Should_Find_Path_With_One_Vertex_And_One_Edge_In_Graph()
3535
{
36-
var graph = new Graph<int, string>();
37-
graph.AddNode(0);
38-
39-
graph.Connect(0, 0, 5, null);
40-
41-
var dijkstra = new Dijkstra<int, string>(graph);
42-
var result = dijkstra.Process(0, 0);
43-
uint[] path = result.GetPath().ToArray();
44-
45-
Assert.AreEqual<uint>(0, path[0]);
46-
Assert.AreEqual(0, result.Distance);
47-
Assert.IsTrue(result.IsFounded);
36+
_fixture.Algorithm_Should_Find_Path_With_One_Vertex_And_One_Edge_In_Graph(_dijkstraFactory);
4837
}
4938

5039
[TestMethod]
5140
public void Dijkstra_Should_Find_Path_In_Multi_Edges_Graph()
5241
{
53-
var graph = new Graph<int, string>();
54-
55-
graph.AddNode(0);
56-
graph.AddNode(0);
57-
graph.AddNode(0);
58-
graph.AddNode(0);
59-
graph.AddNode(0);
60-
graph.AddNode(0);
61-
62-
graph.Connect(0, 1, 2, null);
63-
graph.Connect(0, 2, 3, null);
64-
graph.Connect(1, 3, 4, null);
65-
graph.Connect(2, 3, 3, null);
66-
graph.Connect(2, 3, 2, null);
67-
graph.Connect(2, 3, 4, null);
68-
graph.Connect(2, 4, 1, null);
69-
graph.Connect(3, 5, 6, null);
70-
71-
var dijkstra = new Dijkstra<int, string>(graph);
72-
var result = dijkstra.Process(0, 5);
73-
uint[] path = result.GetPath().ToArray();
74-
75-
Assert.AreEqual<uint>(0, path[0]);
76-
Assert.AreEqual<uint>(2, path[1]);
77-
Assert.AreEqual<uint>(3, path[2]);
78-
Assert.AreEqual<uint>(5, path[3]);
79-
80-
Assert.AreEqual(11, result.Distance);
81-
Assert.IsTrue(result.IsFounded);
42+
_fixture.Algorithm_Should_Find_Path_In_Multi_Edges_Graph(_dijkstraFactory);
8243
}
8344

8445
[TestMethod]
8546
public void Dijkstra_Not_Should_Find_Path_In_Graph()
8647
{
87-
var graph = new Graph<int, string>();
88-
89-
graph.AddNode(0);
90-
graph.AddNode(0);
91-
graph.AddNode(0);
92-
graph.AddNode(0);
93-
graph.AddNode(0);
94-
graph.AddNode(0);
95-
96-
graph.Connect(0, 1, 2, null);
97-
graph.Connect(0, 2, 3, null);
98-
graph.Connect(1, 3, 4, null);
99-
graph.Connect(2, 3, 2, null);
100-
graph.Connect(2, 4, 1, null);
101-
102-
var dijkstra = new Dijkstra<int, string>(graph);
103-
var result = dijkstra.Process(0, 5);
104-
Assert.IsFalse(result.IsFounded);
48+
_fixture.Algorithm_Not_Should_Find_Path_In_Graph(_dijkstraFactory);
10549
}
10650
}
10751
}

src/Dijkstra.NET/Dijkstra.NET.Tests/TheShortestPathFixture.cs

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,76 @@ public void Algorithm_Should_Find_Path_In_Multi_Paths_Graph(Func<Graph<int, stri
3030
var result = dijkstra.Process(0, 5);
3131
uint[] path = result.GetPath().ToArray();
3232

33-
IDisposable disposable = dijkstra as IDisposable;
34-
disposable?.Dispose();
33+
Dispose(dijkstra);
34+
35+
Assert.AreEqual<uint>(0, path[0]);
36+
Assert.AreEqual<uint>(2, path[1]);
37+
Assert.AreEqual<uint>(3, path[2]);
38+
Assert.AreEqual<uint>(5, path[3]);
39+
40+
Assert.AreEqual(11, result.Distance);
41+
Assert.IsTrue(result.IsFounded);
42+
}
43+
44+
public void Algorithm_Should_Find_Path_With_One_Vertex_In_Graph(Func<Graph<int, string>, Dijkstra<int, string>> algorithm)
45+
{
46+
var graph = new Graph<int, string>();
47+
graph.AddNode(0);
48+
49+
var dijkstra = algorithm(graph);
50+
var result = dijkstra.Process(0, 0);
51+
uint[] path = result.GetPath().ToArray();
52+
53+
Dispose(dijkstra);
54+
55+
Assert.AreEqual<uint>(0, path[0]);
56+
Assert.AreEqual(0, result.Distance);
57+
Assert.IsTrue(result.IsFounded);
58+
}
59+
60+
public void Algorithm_Should_Find_Path_With_One_Vertex_And_One_Edge_In_Graph(Func<Graph<int, string>, Dijkstra<int, string>> algorithm)
61+
{
62+
var graph = new Graph<int, string>();
63+
graph.AddNode(0);
64+
65+
graph.Connect(0, 0, 5, null);
66+
67+
var dijkstra = algorithm(graph);
68+
var result = dijkstra.Process(0, 0);
69+
uint[] path = result.GetPath().ToArray();
70+
71+
Dispose(dijkstra);
72+
73+
Assert.AreEqual<uint>(0, path[0]);
74+
Assert.AreEqual(0, result.Distance);
75+
Assert.IsTrue(result.IsFounded);
76+
}
77+
78+
public void Algorithm_Should_Find_Path_In_Multi_Edges_Graph(Func<Graph<int, string>, Dijkstra<int, string>> algorithm)
79+
{
80+
var graph = new Graph<int, string>();
81+
82+
graph.AddNode(0);
83+
graph.AddNode(0);
84+
graph.AddNode(0);
85+
graph.AddNode(0);
86+
graph.AddNode(0);
87+
graph.AddNode(0);
88+
89+
graph.Connect(0, 1, 2, null);
90+
graph.Connect(0, 2, 3, null);
91+
graph.Connect(1, 3, 4, null);
92+
graph.Connect(2, 3, 3, null);
93+
graph.Connect(2, 3, 2, null);
94+
graph.Connect(2, 3, 4, null);
95+
graph.Connect(2, 4, 1, null);
96+
graph.Connect(3, 5, 6, null);
97+
98+
var dijkstra = algorithm(graph);
99+
var result = dijkstra.Process(0, 5);
100+
uint[] path = result.GetPath().ToArray();
101+
102+
Dispose(dijkstra);
35103

36104
Assert.AreEqual<uint>(0, path[0]);
37105
Assert.AreEqual<uint>(2, path[1]);
@@ -41,5 +109,38 @@ public void Algorithm_Should_Find_Path_In_Multi_Paths_Graph(Func<Graph<int, stri
41109
Assert.AreEqual(11, result.Distance);
42110
Assert.IsTrue(result.IsFounded);
43111
}
112+
113+
public void Algorithm_Not_Should_Find_Path_In_Graph(Func<Graph<int, string>, Dijkstra<int, string>> algorithm)
114+
{
115+
var graph = new Graph<int, string>();
116+
117+
graph.AddNode(0);
118+
graph.AddNode(0);
119+
graph.AddNode(0);
120+
graph.AddNode(0);
121+
graph.AddNode(0);
122+
graph.AddNode(0);
123+
124+
graph.Connect(0, 1, 2, null);
125+
graph.Connect(0, 2, 3, null);
126+
graph.Connect(1, 3, 4, null);
127+
graph.Connect(2, 3, 2, null);
128+
graph.Connect(2, 4, 1, null);
129+
130+
var dijkstra = algorithm(graph);
131+
132+
133+
var result = dijkstra.Process(0, 5);
134+
135+
Dispose(dijkstra);
136+
137+
Assert.IsFalse(result.IsFounded);
138+
}
139+
140+
private void Dispose(Dijkstra<int, string> algorithm)
141+
{
142+
IDisposable disposable = algorithm as IDisposable;
143+
disposable?.Dispose();
144+
}
44145
}
45146
}

src/Dijkstra.NET/Dijkstra.NET/ShortestPath/BfsParallel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public BfsParallel(IConcurrentGraph<T, TEdgeCustom> graph) : base(graph)
2222
_table = new ProducerConsumer<T, TEdgeCustom>();
2323
}
2424

25+
public BfsParallel(IConcurrentGraph<T, TEdgeCustom> graph, double guardInterval): base(graph)
26+
{
27+
_graph = graph;
28+
_table = new ProducerConsumer<T, TEdgeCustom>(guardInterval);
29+
}
30+
2531
public override IShortestPathResult Process(uint @from, uint to)
2632
{
2733
_result = new DijkstraConcurrentResult(from, to);

src/Dijkstra.NET/Dijkstra.NET/ShortestPath/Utility/ProducerConsumer.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
internal sealed class ProducerConsumer<T, TEdgeCustom> : IDisposable where TEdgeCustom : IEquatable<TEdgeCustom>
1212
{
1313
private readonly Collection<Emitter> _table = new Collection<Emitter>();
14-
15-
private readonly Timer _guardTimer = new Timer(10);
14+
private readonly Timer _guardTimer;
1615

1716
private bool _isDisposed;
1817
private int _guardIsWorking;
@@ -21,13 +20,12 @@ internal sealed class ProducerConsumer<T, TEdgeCustom> : IDisposable where TEdge
2120

2221
private int _counter;
2322

24-
public ProducerConsumer()
23+
public ProducerConsumer(double guardInterval = 20)
2524
{
26-
_currentJobs = 0;
25+
_guardTimer = new Timer(guardInterval);
2726

2827
_guardTimer.Elapsed += (sender, args) =>
2928
{
30-
Debug.WriteLine($"Guard thread id: {Thread.CurrentThread.ManagedThreadId}");
3129
if (Interlocked.CompareExchange(ref _counter, Insurance, Insurance) == Insurance)
3230
{
3331
_guardTimer.Stop();

0 commit comments

Comments
 (0)