|
| 1 | +package algorithm |
| 2 | + |
| 3 | +import spock.lang.Specification |
| 4 | +import spock.lang.Subject |
| 5 | +import spock.lang.Unroll |
| 6 | + |
| 7 | +class SearchAlgorithmSpec extends Specification { |
| 8 | + @Subject |
| 9 | + def shortest = new BreadthFirstSearch<String>() |
| 10 | + |
| 11 | + @Subject |
| 12 | + def fastest = new DijkstrasAlgorithm<String>() |
| 13 | + |
| 14 | + @Unroll("from #source to #target the time is #time and the fastest path is #fastestPath") |
| 15 | + def 'should find a route for a complex graph'() { |
| 16 | + given: |
| 17 | + def graph = new Graph<String>([ |
| 18 | + A: [B: 5, H: 2], |
| 19 | + B: [A: 5, C: 7], |
| 20 | + C: [B: 7, D: 3, G: 4], |
| 21 | + D: [C: 20, E: 4], |
| 22 | + E: [F: 5], |
| 23 | + F: [G: 6], |
| 24 | + G: [C: 4], |
| 25 | + H: [G: 3] |
| 26 | + ]) |
| 27 | + |
| 28 | + when: |
| 29 | + def routeOne = shortest.findPath(graph, source, target) |
| 30 | + |
| 31 | + and: |
| 32 | + def routeTwo = fastest.findPath(graph, source, target) |
| 33 | + |
| 34 | + then: |
| 35 | + routeOne == shortestPath |
| 36 | + |
| 37 | + and: |
| 38 | + routeTwo == fastestPath |
| 39 | + |
| 40 | + where: |
| 41 | + source | target || time | shortestPath | fastestPath |
| 42 | + 'A' | 'A' || 0 | ['A'] | ['A'] |
| 43 | + 'B' | 'B' || 0 | ['B'] | ['B'] |
| 44 | + 'A' | 'B' || 5 | ['A', 'B'] | ['A', 'B'] |
| 45 | + 'B' | 'A' || 5 | ['B', 'A'] | ['B', 'A'] |
| 46 | + 'A' | 'C' || 9 | ['A', 'B', 'C'] | ['A', 'H', 'G', 'C'] |
| 47 | + 'C' | 'A' || 12 | ['C', 'B', 'A'] | ['C', 'B', 'A'] |
| 48 | + 'A' | 'G' || 5 | ['A', 'H', 'G'] | ['A', 'H', 'G'] |
| 49 | + 'C' | 'D' || 3 | ['C', 'D'] | ['C', 'D'] |
| 50 | + 'D' | 'C' || 19 | ['D', 'C'] | ['D', 'E', 'F', 'G', 'C'] |
| 51 | + 'B' | 'D' || 10 | ['B', 'C', 'D'] | ['B', 'C', 'D'] |
| 52 | + 'D' | 'B' || 26 | ['D', 'C', 'B'] | ['D', 'E', 'F', 'G', 'C', 'B'] |
| 53 | + 'D' | 'H' || 33 | ['D', 'C', 'B', 'A', 'H'] | ['D', 'E', 'F', 'G', 'C', 'B', 'A', 'H'] |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments