Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranpujolcamins committed Nov 11, 2018
1 parent c481e89 commit cab1921
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/SwiftGraph/GraphTraverser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ protocol GraphTraverser {
/// - initalVertexIndex: The index of the initial vertex
/// - goalTest: Returns true if a given vertex index is a goal. When this closure returns true
/// The algorithm stops traversing the graph when this closure returns true.
/// The initial vertex is checked against the closure too.
/// - reducer: A reducer that is fed with each visited vertex. The input parameter
/// is the edge from the previous vertex to the visited vertex.
/// If the return value is false, the neighbours of the input vertex will not be visited.
/// The initialVertex is not fed to the reducer.
/// - Returns: The index of the first vertex found to satisfy goalTest or nil if no vertex is found.
func from(_ initalVertexIndex: Int, goalTest: (Int) -> Bool, reducer: G.Reducer) -> Int?
}
Expand Down
46 changes: 46 additions & 0 deletions Tests/SwiftGraphTests/SwiftGraphSearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,29 @@ class SwiftGraphSearchTests: XCTestCase {
XCTAssertTrue(atLeastOnePathHasLenght2, "In a Complete Graph, the dfs must visit all nodes in the same path")
}

func testDfsInCompleteGraphWithGoalTestByIndex() {
var visited = Array<Bool>.init(repeating: false, count: 200)
let graph = CompleteGraph.build(withVertices: Array(0...199))
_ = graph.dfs(fromIndex: 0, goalTest: { i in
visited[i] = true
return false
})
let numVisitedVertices = visited.filter({ $0 }).count
XCTAssertEqual(numVisitedVertices, 200, "The DFS must visit all the vertices")
}

func testDfsGoalTestOnInitialVertex() {
var visited = false
let graph = CompleteGraph.build(withVertices: Array(0...3))
_ = graph.dfs(fromIndex: 0, goalTest: { i in
if (i == 0) {
visited = true
}
return true
})
XCTAssertTrue(visited, "The DFS must check if the initialVertex is already a goal.")
}

func testBFS1() {
// Seattle -> Miami
let result = cityGraph.bfs(from: "Seattle", to: "Miami")
Expand Down Expand Up @@ -359,6 +382,29 @@ class SwiftGraphSearchTests: XCTestCase {
let allPathsHavLenght1 = paths.allSatisfy { $0.count == 1 }
XCTAssertTrue(allPathsHavLenght1, "In a Triangle Graph, the bfs must visit all nodes directly.")
}

func testBfsInCompleteGraphWithGoalTestByIndex() {
var visited = Array<Bool>.init(repeating: false, count: 200)
let graph = CompleteGraph.build(withVertices: Array(0...199))
_ = graph.bfs(fromIndex: 0, goalTest: { i in
visited[i] = true
return false
})
let numVisitedVertices = visited.filter({ $0 }).count
XCTAssertEqual(numVisitedVertices, 200, "The BFS must visit all the vertices")
}

func testBfsGoalTestOnInitialVertex() {
var visited = false
let graph = CompleteGraph.build(withVertices: Array(0...3))
_ = graph.bfs(fromIndex: 0, goalTest: { i in
if (i == 0) {
visited = true
}
return true
})
XCTAssertTrue(visited, "The BFS must check if the initialVertex is already a goal.")
}

func testFindAll() {
// New York -> all cities starting with "S"
Expand Down

0 comments on commit cab1921

Please sign in to comment.