tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 1 | package org.onlab.graph; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import java.util.Set; |
| 6 | |
| 7 | import static org.junit.Assert.assertEquals; |
| 8 | |
| 9 | /** |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 10 | * Test of the BFS and similar path search algorithms. |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 11 | */ |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 12 | public class BreadthFirstSearchTest extends AbstractGraphPathSearchTest { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 13 | |
| 14 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 15 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
| 16 | return new BreadthFirstSearch<>(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 20 | public void defaultGraphTest() { |
| 21 | executeDefaultTest(7, 3, 8.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 25 | public void defaultHopCountWeight() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 26 | weight = null; |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 27 | executeDefaultTest(7, 3, 3.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 28 | } |
| 29 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 30 | // Executes the default test |
| 31 | protected void executeDefaultTest(int pathCount, int pathLength, double pathCost) { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 32 | g = new AdjacencyListsGraph<>(vertices(), edges()); |
| 33 | |
| 34 | GraphPathSearch<TestVertex, TestEdge> search = graphSearch(); |
| 35 | Set<Path<TestVertex, TestEdge>> paths = search.search(g, A, H, weight).paths(); |
| 36 | assertEquals("incorrect paths count", 1, paths.size()); |
| 37 | |
| 38 | Path p = paths.iterator().next(); |
| 39 | assertEquals("incorrect src", A, p.src()); |
| 40 | assertEquals("incorrect dst", H, p.dst()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 41 | assertEquals("incorrect path length", pathLength, p.edges().size()); |
| 42 | assertEquals("incorrect path cost", pathCost, p.cost(), 0.1); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 43 | |
| 44 | paths = search.search(g, A, null, weight).paths(); |
| 45 | printPaths(paths); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 46 | assertEquals("incorrect paths count", pathCount, paths.size()); |
| 47 | } |
| 48 | |
| 49 | // Executes the search and validates its results. |
| 50 | protected void executeSearch(GraphPathSearch<TestVertex, TestEdge> search, |
| 51 | Graph<TestVertex, TestEdge> graph, |
| 52 | TestVertex src, TestVertex dst, |
| 53 | EdgeWeight<TestVertex, TestEdge> weight, |
| 54 | int pathCount, double pathCost) { |
| 55 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
| 56 | search.search(graph, src, dst, weight); |
| 57 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 58 | printPaths(paths); |
| 59 | assertEquals("incorrect paths count", pathCount, paths.size()); |
| 60 | if (pathCount > 0) { |
| 61 | Path<TestVertex, TestEdge> path = paths.iterator().next(); |
| 62 | assertEquals("incorrect path cost", pathCost, path.cost(), 0.1); |
| 63 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } |