Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranpujolcamins committed Nov 12, 2018
1 parent c481e89 commit 4aede8c
Show file tree
Hide file tree
Showing 3 changed files with 72 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
64 changes: 64 additions & 0 deletions Tests/SwiftGraphTests/SwiftGraphSearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,38 @@ class SwiftGraphSearchTests: XCTestCase {
XCTAssertTrue(atLeastOnePathHasLenght2, "In a Complete Graph, the dfs must visit all nodes in the same path")
}

func testDFSVisitOrderWithCycle() {
let g = CompleteGraph.build(withVertices: ["A", "B", "C"])

let path = DFS(on: g).withVisitOrder({ $0.sorted(by: { $0.v < $1.v }) })
.from("A", to: "C")

XCTAssertEqual(path.count, 2, "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 +391,38 @@ class SwiftGraphSearchTests: XCTestCase {
let allPathsHavLenght1 = paths.allSatisfy { $0.count == 1 }
XCTAssertTrue(allPathsHavLenght1, "In a Triangle Graph, the bfs must visit all nodes directly.")
}

func testBFSVisitOrderWithCycle() {
let g = CompleteGraph.build(withVertices: ["A", "B", "C"])

let path = BFS(on: g).withVisitOrder({ $0.sorted(by: { $0.v < $1.v }) })
.from("A", to: "C")

XCTAssertEqual(path.count, 1, "In a Complete 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
6 changes: 6 additions & 0 deletions Tests/SwiftGraphTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ extension SwiftGraphSearchTests {
("testBFS4", testBFS4),
("testBFS5", testBFS5),
("testBFS6", testBFS6),
("testBfsGoalTestOnInitialVertex", testBfsGoalTestOnInitialVertex),
("testBfsInCompleteGraphWithGoalTestByIndex", testBfsInCompleteGraphWithGoalTestByIndex),
("testBFSNotFound", testBFSNotFound),
("testBFSVisitOrderWithCycle", testBFSVisitOrderWithCycle),
("testBFSWithCycle", testBFSWithCycle),
("testDFS1", testDFS1),
("testDFS2", testDFS2),
("testDFS3", testDFS3),
("testDFS4", testDFS4),
("testDFS5", testDFS5),
("testDFS6", testDFS6),
("testDfsGoalTestOnInitialVertex", testDfsGoalTestOnInitialVertex),
("testDfsInCompleteGraphWithGoalTestByIndex", testDfsInCompleteGraphWithGoalTestByIndex),
("testDFSNotFound", testDFSNotFound),
("testDFSVisitOrderWithCycle", testDFSVisitOrderWithCycle),
("testDFSWithCycle", testDFSWithCycle),
("testFindAll", testFindAll),
("testVisitBfs", testVisitBfs),
Expand Down

0 comments on commit 4aede8c

Please sign in to comment.