B
BarbraFleg
Guest
I have a point from and to on the same train/metro line and need to find the shortest path between those points. Each point has values (x,y) and I have a unit test that defines the rules for finding the path. I have three tests from which only T2(test two) is verified by my function but Test 1(T1 path_to_where_i_am)and Test 3 (T3 get_path_to_station_on_same_line) do not pass as I have problem to identify the steps and then translate them to the code.
Function GetShortestPath
public IReadOnlyList<IStation> GetShortestPath(IStation from, IStation to)
{
//T2 _cant_get_path_from_and_or_to_null()
if (from == null || to == null || from == to)
throw new ArgumentException();
//T1 _path_to_where_i_am()
//s is added and then only one should be same as s
//T3 get_path_to_station_on_same_line()
// from to have to be on same line
throw new NotImplementedException();
//return pathBetweenFromTo;
}
Unit Test
[Test1]
public void T1_path_to_where_i_am()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s = c.AddStation("Opera", 0, 0);
c.GetShortestPath(s, s).Single().Should().BeSameAs(s);
}
[Test2]
public void T2_cant_get_path_from_and_or_to_null()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s = c.AddStation("Opera", 0, 0);
Action action = (() => c.GetShortestPath(s, null));
action.ShouldThrow<ArgumentException>();
action = (() => c.GetShortestPath(null, s));
action.ShouldThrow<ArgumentException>();
action = (() => c.GetShortestPath(null, null));
action.ShouldThrow<ArgumentException>();
}
[Test3]
public void T3_get_path_to_station_on_same_line()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s1 = c.AddStation("Opera", 0, 0);
IStation s2 = c.AddStation("Chatelet", 10, 10);
IStation s3 = c.AddStation("Gare de Lyon", 20, 20);
ILine l = c.AddLine("RER A");
l.AddBefore(s1);
l.AddBefore(s2);
l.AddBefore(s3);
var path = c.GetShortestPath(s1, s2);
path.Count.Should().Be(2);
(path[0] == s1 && path[1] == s2).Should().BeTrue();
path = c.GetShortestPath(s2, s1);
(path.Count == 2).Should().BeTrue();
(path[0] == s2 && path[1] == s1).Should().BeTrue();
path = c.GetShortestPath(s1, s3);
(path.Count == 3).Should().BeTrue();
(path[0] == s1 && path[1] == s2 && path[2] == s3).Should().BeTrue();
path = c.GetShortestPath(s3, s1);
(path.Count == 3).Should().BeTrue();
(path[0] == s3 && path[1] == s2 && path[2] == s1).Should().BeTrue();
path = c.GetShortestPath(s2, s3);
(path.Count == 2).Should().BeTrue();
(path[0] == s2 && path[1] == s3).Should().BeTrue();
}
Continue reading...
Function GetShortestPath
public IReadOnlyList<IStation> GetShortestPath(IStation from, IStation to)
{
//T2 _cant_get_path_from_and_or_to_null()
if (from == null || to == null || from == to)
throw new ArgumentException();
//T1 _path_to_where_i_am()
//s is added and then only one should be same as s
//T3 get_path_to_station_on_same_line()
// from to have to be on same line
throw new NotImplementedException();
//return pathBetweenFromTo;
}
Unit Test
[Test1]
public void T1_path_to_where_i_am()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s = c.AddStation("Opera", 0, 0);
c.GetShortestPath(s, s).Single().Should().BeSameAs(s);
}
[Test2]
public void T2_cant_get_path_from_and_or_to_null()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s = c.AddStation("Opera", 0, 0);
Action action = (() => c.GetShortestPath(s, null));
action.ShouldThrow<ArgumentException>();
action = (() => c.GetShortestPath(null, s));
action.ShouldThrow<ArgumentException>();
action = (() => c.GetShortestPath(null, null));
action.ShouldThrow<ArgumentException>();
}
[Test3]
public void T3_get_path_to_station_on_same_line()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s1 = c.AddStation("Opera", 0, 0);
IStation s2 = c.AddStation("Chatelet", 10, 10);
IStation s3 = c.AddStation("Gare de Lyon", 20, 20);
ILine l = c.AddLine("RER A");
l.AddBefore(s1);
l.AddBefore(s2);
l.AddBefore(s3);
var path = c.GetShortestPath(s1, s2);
path.Count.Should().Be(2);
(path[0] == s1 && path[1] == s2).Should().BeTrue();
path = c.GetShortestPath(s2, s1);
(path.Count == 2).Should().BeTrue();
(path[0] == s2 && path[1] == s1).Should().BeTrue();
path = c.GetShortestPath(s1, s3);
(path.Count == 3).Should().BeTrue();
(path[0] == s1 && path[1] == s2 && path[2] == s3).Should().BeTrue();
path = c.GetShortestPath(s3, s1);
(path.Count == 3).Should().BeTrue();
(path[0] == s3 && path[1] == s2 && path[2] == s1).Should().BeTrue();
path = c.GetShortestPath(s2, s3);
(path.Count == 2).Should().BeTrue();
(path[0] == s2 && path[1] == s3).Should().BeTrue();
}
Continue reading...