Skip to content

Instantly share code, notes, and snippets.

@fbiville
Created August 11, 2013 10:41
Show Gist options
  • Save fbiville/6204354 to your computer and use it in GitHub Desktop.
Save fbiville/6204354 to your computer and use it in GitHub Desktop.
Week 5 - simple test cases
package com.github.fbiville.coursera.algorithmics_stanford.week5;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import org.testng.annotations.Test;
import com.google.common.collect.Lists;
public class DijkstraTest {
private Graph graph;
@Test
public void should_compute_shortest_path_for_single_vertex() {
given_a_single_vertex_graph();
ShortestPaths paths = new Dijkstra(graph).computeShortestPaths(vertex("1"));
assertThat(paths.contains(vertex("1"))).isTrue();
assertThat(paths.cost(vertex("1"))).isZero();
}
@Test
public void should_compute_shortest_path_for_single_edge() {
given_a_single_edge_graph("1", "2", 42);
ShortestPaths paths = new Dijkstra(graph).computeShortestPaths(vertex("1"));
assertThat(paths.contains(vertex("1"), vertex("2"))).isTrue();
assertThat(paths.cost(vertex("2"))).isEqualTo(42);
}
@Test
public void should_compute_shortest_path_for_two_different_edges() {
given_a_graph(edge("1", "2", 23), edge("1", "3", 24));
ShortestPaths paths = new Dijkstra(graph).computeShortestPaths(vertex("1"));
assertThat(paths.contains(vertex("1"), vertex("2"), vertex("3"))).isTrue();
assertThat(paths.cost(vertex("2"))).isEqualTo(23);
assertThat(paths.cost(vertex("3"))).isEqualTo(24);
}
@Test
public void should_compute_shortest_path_for_two_connected_edges() {
given_a_graph(edge("1", "2", 23), edge("2", "3", 24));
ShortestPaths paths = new Dijkstra(graph).computeShortestPaths(vertex("1"));
assertThat(paths.contains(vertex("1"), vertex("2"), vertex("3"))).isTrue();
assertThat(paths.cost(vertex("2"))).isEqualTo(23);
assertThat(paths.cost(vertex("3"))).isEqualTo(47);
}
@Test
public void should_compute_shortest_path_for_redundant_edges() {
given_a_graph(edge("1", "2", 23), edge("1", "3", 24), edge("2", "3", 2));
ShortestPaths paths = new Dijkstra(graph).computeShortestPaths(vertex("1"));
assertThat(paths.contains(vertex("1"), vertex("2"), vertex("3"))).isTrue();
assertThat(paths.cost(vertex("2"))).isEqualTo(23);
assertThat(paths.cost(vertex("3"))).isEqualTo(24);
}
private void given_a_single_vertex_graph() {
graph = new Graph(Lists.<WeightedEdge>newArrayList());
}
private void given_a_single_edge_graph(String label, String otherLabel, long weight) {
graph = new Graph(newArrayList(new WeightedEdge(vertex(label), vertex(otherLabel), weight)));
}
private void given_a_graph(WeightedEdge... edges) {
graph = new Graph(newArrayList(edges));
}
private WeightedEdge edge(String label1, String label2, int weight) {
return new WeightedEdge(vertex(label1), vertex(label2), weight);
}
private Vertex vertex(String label) {
return new Vertex(label);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment