-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBfs_Should.cs
188 lines (157 loc) · 5.62 KB
/
Bfs_Should.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using NUnit.Framework;
namespace Dungeon
{
[TestFixture]
public class Bfs_Should
{
[Test, Order(0)]
public void ReturnNoPaths_WhenNoPathsToChests()
{
var textMap = new[]
{
"P ",
"##",
"C "
};
var map = Map.FromLines(textMap);
var paths = GetPaths(map);
Assert.IsEmpty(paths);
}
[Test, Order(1)]
public void ReturnCorrectPaths_OnEmptyDungeon()
{
var textMap = new[]
{
"P ",
"C "
};
var map = Map.FromLines(textMap);
var expectedLengths = new[] { 2 };
var paths = GetPaths(map);
AssertPaths(paths, map, expectedLengths);
}
[Test, Order(2)]
public void ReturnCorrectPaths_OnSimpleDungeon()
{
var textMap = new[]
{
"P #",
"# #",
"C "
};
var map = Map.FromLines(textMap);
var expectedLengths = new[] { 5 };
var paths = GetPaths(map);
AssertPaths(paths, map, expectedLengths);
}
[Test, Order(3)]
public void Return_ShortestPaths1()
{
var textMap = new[]
{
" ",
" P ",
" C "
};
var map = Map.FromLines(textMap);
var expectedLengths = new[] { 2 };
var paths = GetPaths(map);
AssertPaths(paths, map, expectedLengths);
}
[Test, Order(4)]
public void Return_ShortestPaths2()
{
var textMap = new[]
{
" C ",
"CPC",
" C "
};
var map = Map.FromLines(textMap);
var expectedLengths = new[] { 2, 2, 2, 2 };
var paths = GetPaths(map);
AssertPaths(paths, map, expectedLengths);
}
[Test, Order(5)]
public void Return_ShortestPaths3()
{
var textMap = new[]
{
"CC",
"CP",
};
var map = Map.FromLines(textMap);
var expectedLengths = new[] { 3, 2, 2 };
var paths = GetPaths(map)
.OrderByDescending(x => x.Count)
.ToArray();
AssertPaths(paths, map, expectedLengths);
}
[Test, Order(6)]
public void Return_ShortestPaths_OnBigTestDangeon()
{
var map = Map.FromText(Properties.Resources.BigTestDungeon);
var expectedLengths = new[] { 170, 156, 144, 137, 84 };
var paths = GetPaths(map)
.OrderByDescending(x => x.Count)
.ToArray();
AssertPaths(paths, map, expectedLengths);
}
[Test, Order(7)]
public void WorksCorrect_ShortestPaths_OnManyCalls()
{
var miniMap = Map.FromLines(new[]
{
" C ",
"CPC",
" C "
});
var miniPaths = GetPaths(miniMap);
AssertPaths(miniPaths, miniMap, new[] { 2, 2, 2, 2 });
var map = Map.FromText(Properties.Resources.BigTestDungeon);
var paths = GetPaths(map)
.OrderByDescending(x => x.Count)
.ToArray();
AssertPaths(paths, map, new[] { 170, 156, 144, 137, 84 });
}
private static List<Point>[] GetPaths(Map map)
{
var paths = BfsTask.FindPaths(map, map.InitialPosition, map.Chests)
.Select(x => x.ToList())
.ToArray();
return paths;
}
private void AssertPaths(List<Point>[] paths, Map map, int[] expectedLengths)
{
var uniqueEndpoints = paths
.Select(x => x[0])
.Distinct()
.ToArray();
var expectedPathCount = expectedLengths.Length;
Assert.AreEqual(expectedPathCount, paths.Length, $"The number of returned paths should be {expectedPathCount}");
for (var i = 0; i < paths.Length; i++)
{
AssertPath(map, paths[i], expectedLengths[i]);
}
Assert.AreEqual(expectedPathCount, uniqueEndpoints.Length, "Each path must lead to unique chest");
}
private void AssertPath(Map map, List<Point> path, int expectedLength)
{
var directions = Walker.PossibleDirections.Select(s => new Point(s)).ToList();
Assert.IsNotEmpty(path, "path should not be empty");
Assert.Contains(path[0], map.Chests, $"The first point in the path should be one of the chest, but was {path[0]}");
for (var i = 0; i < path.Count - 1; i++)
{
Point offset = path[i + 1] - new Size(path[i]);
Assert.IsTrue(directions.Contains(offset), $"Incorrect step #{i} in your path: from {path[i]} to {path[i + 1]}");
Assert.AreNotEqual(MapCell.Wall, map.Dungeon[path[i + 1].X, path[i + 1].Y], $"Collided with wall at {i}th path point: {path[i + 1]}");
}
Assert.AreEqual(map.InitialPosition, path.Last(), "The last point in path must be 'start'");
Assert.GreaterOrEqual(path.Count, expectedLength, "Checker bug?! Leave a comment above this slide, to notify task authors, please");
Assert.AreEqual(expectedLength, path.Count, "Your path is not the shortest one");
}
}
}