B
BarbraFleg
Guest
I have a two points called stations where I need to count the distance between two different ones in the same city with specific set of rules in three functions GetShortestPath(IStation from, IStation to) and GetFastestPath(IStation from, IStation to) in the class City.
The rules for the functions of GetShortestPath & GetFastestPath:
- from point (0,0)
- none of the stations (point A from & point B to) cannot be null
- the path has to be at one line
- no path between stations without a line
** based on following unit test
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);
}
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>();
}
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();
}
public void T4_can_go_to_station_on_another_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 du nord", 20, 20);
ILine l1 = c.AddLine("RER A");
ILine l2 = c.AddLine("RER B");
l1.AddBefore(s1);
l1.AddBefore(s2);
l2.AddBefore(s2);
l2.AddBefore(s3);
var path = c.GetShortestPath(s1, s2);
(path.Count == 2).Should().BeTrue();
(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();
}
public void T5_no_path_between_stations_wih_no_line()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s = c.AddStation("Opera", 0, 0);
IStation s1 = c.AddStation("Chatelet", 10, 10);
var path = c.GetShortestPath(s, s1);
(path.Count == 0).Should().BeTrue();
ILine l = c.AddLine("RER A");
l.AddBefore(s);
path = c.GetShortestPath(s, s1);
(path.Count == 0).Should().BeTrue();
ILine l1 = c.AddLine("RER b");
l1.AddBefore(s1);
path = c.GetShortestPath(s, s1);
(path.Count == 0).Should().BeTrue();
}
public void T6_shortest_path_complexe_map()
{
ICity city = CityFactory.CreateCity("Paris");
IStation a = city.AddStation("A", 0, 0);
IStation b = city.AddStation("B", 0, 10);
ILine l1 = city.AddLine("ligne 1");
l1.AddBefore(a);
l1.AddBefore(b);
var path = city.GetShortestPath(a, b);
(path.Count == 2).Should().BeTrue();
(path[0] == a && path[1] == b && path[2] == c && path[3] == d).Should().BeTrue();
IStation e = city.AddStation("E", 5, 5);
ILine l2 = city.AddLine("ligne 2");
l2.AddBefore(a);
l2.AddBefore(e);
l2.AddBefore(d);
path = city.GetShortestPath(a, b);
(path.Count == 3).Should().BeTrue();
(path[0] == a && path[1] == e && path[2] == d).Should().BeTrue();
ILine l3 = city.AddLine("ligne 3");
l3.AddBefore(a);
l3.AddBefore(b);
path = city.GetShortestPath(a, d);
(path.Count == 2).Should().BeTrue();
(path[0] == a && path[1] == b).Should().BeTrue();
}
My place to apply all those rules is in the class City in three functions:
public IReadOnlyList<IStation> GetFastestPath(IStation from, IStation to)
{
throw new NotImplementedException();
}
public IReadOnlyList<IStation> GetShortestPath(IStation from, IStation to)
{
throw new NotImplementedException();
}
Continue reading...
The rules for the functions of GetShortestPath & GetFastestPath:
- from point (0,0)
- none of the stations (point A from & point B to) cannot be null
- the path has to be at one line
- no path between stations without a line
** based on following unit test
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);
}
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>();
}
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();
}
public void T4_can_go_to_station_on_another_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 du nord", 20, 20);
ILine l1 = c.AddLine("RER A");
ILine l2 = c.AddLine("RER B");
l1.AddBefore(s1);
l1.AddBefore(s2);
l2.AddBefore(s2);
l2.AddBefore(s3);
var path = c.GetShortestPath(s1, s2);
(path.Count == 2).Should().BeTrue();
(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();
}
public void T5_no_path_between_stations_wih_no_line()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s = c.AddStation("Opera", 0, 0);
IStation s1 = c.AddStation("Chatelet", 10, 10);
var path = c.GetShortestPath(s, s1);
(path.Count == 0).Should().BeTrue();
ILine l = c.AddLine("RER A");
l.AddBefore(s);
path = c.GetShortestPath(s, s1);
(path.Count == 0).Should().BeTrue();
ILine l1 = c.AddLine("RER b");
l1.AddBefore(s1);
path = c.GetShortestPath(s, s1);
(path.Count == 0).Should().BeTrue();
}
public void T6_shortest_path_complexe_map()
{
ICity city = CityFactory.CreateCity("Paris");
IStation a = city.AddStation("A", 0, 0);
IStation b = city.AddStation("B", 0, 10);
ILine l1 = city.AddLine("ligne 1");
l1.AddBefore(a);
l1.AddBefore(b);
var path = city.GetShortestPath(a, b);
(path.Count == 2).Should().BeTrue();
(path[0] == a && path[1] == b && path[2] == c && path[3] == d).Should().BeTrue();
IStation e = city.AddStation("E", 5, 5);
ILine l2 = city.AddLine("ligne 2");
l2.AddBefore(a);
l2.AddBefore(e);
l2.AddBefore(d);
path = city.GetShortestPath(a, b);
(path.Count == 3).Should().BeTrue();
(path[0] == a && path[1] == e && path[2] == d).Should().BeTrue();
ILine l3 = city.AddLine("ligne 3");
l3.AddBefore(a);
l3.AddBefore(b);
path = city.GetShortestPath(a, d);
(path.Count == 2).Should().BeTrue();
(path[0] == a && path[1] == b).Should().BeTrue();
}
My place to apply all those rules is in the class City in three functions:
public IReadOnlyList<IStation> GetFastestPath(IStation from, IStation to)
{
throw new NotImplementedException();
}
public IReadOnlyList<IStation> GetShortestPath(IStation from, IStation to)
{
throw new NotImplementedException();
}
Continue reading...