Skip to content

Commit 9d7df73

Browse files
committed
Fixed test for Linux hopefully; Eliminated a couple if statements
1 parent c254522 commit 9d7df73

4 files changed

Lines changed: 22 additions & 29 deletions

File tree

Sources/SwiftGraph/UniqueElementsGraph.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,14 @@ open class UniqueElementsGraph<V: Equatable>: UnweightedGraph<V> {
7373
/// - parameter to: The ending vertex.
7474
/// - parameter directed: Is the edge directed? (default `false`)
7575
public override func addEdge(from: V, to: V, directed: Bool = false) {
76-
if let u = indexOfVertex(from) {
77-
if let v = indexOfVertex(to) {
78-
if !edgeExists(from: u, to: v) {
79-
addEdge(UnweightedEdge(u: u, v: v))
80-
if !directed && !edgeExists(from: v, to: u) {
81-
addEdge(UnweightedEdge(u: v, v: u))
82-
}
76+
if let u = indexOfVertex(from), let v = indexOfVertex(to) {
77+
if !edgeExists(from: u, to: v) {
78+
addEdge(UnweightedEdge(u: u, v: v))
79+
if !directed && !edgeExists(from: v, to: u) {
80+
addEdge(UnweightedEdge(u: v, v: u))
8381
}
84-
8582
}
83+
8684
}
8785
}
8886
}

Sources/SwiftGraph/UnweightedGraph.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ open class UnweightedGraph<V: Equatable>: Graph {
9898
/// - parameter to: The ending vertex.
9999
/// - parameter directed: Is the edge directed? (default `false`)
100100
public func addEdge(from: V, to: V, directed: Bool = false) {
101-
if let u = indexOfVertex(from) {
102-
if let v = indexOfVertex(to) {
103-
addEdge(UnweightedEdge(u: u, v: v))
104-
if !directed {
105-
addEdge(UnweightedEdge(u: v, v: u))
106-
}
101+
if let u = indexOfVertex(from), let v = indexOfVertex(to) {
102+
addEdge(UnweightedEdge(u: u, v: v))
103+
if !directed {
104+
addEdge(UnweightedEdge(u: v, v: u))
107105
}
108106
}
109107
}

Sources/SwiftGraph/WeightedGraph.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ open class WeightedGraph<V: Equatable, W: Comparable & Numeric & Codable>: Graph
6262
/// - parameter directed: Is the edge directed? (default false)
6363
/// - parameter weight: the Weight of the edge to add.
6464
public func addEdge(from: V, to: V, weight: W, directed: Bool = false) {
65-
if let u = indexOfVertex(from) {
66-
if let v = indexOfVertex(to) {
67-
addEdge(from: u, to: v, weight: weight, directed: directed)
68-
}
65+
if let u = indexOfVertex(from), let v = indexOfVertex(to) {
66+
addEdge(from: u, to: v, weight: weight, directed: directed)
6967
}
7068
}
7169

Tests/SwiftGraphTests/SwiftGraphCodableTests.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SwiftGraphCodableTests: XCTestCase {
3636
{"edges":[[{"u":0,"v":1}],[{"u":1,"v":0}]],"vertices":["New York","Miami"]}
3737
"""
3838

39-
func testEncodable() {
39+
func testEncodableDecodable() {
4040
let g = expectedUnweightedGraph
4141

4242
let jsonData: Data
@@ -47,24 +47,24 @@ class SwiftGraphCodableTests: XCTestCase {
4747
return
4848
}
4949
let jsonString = String(data: jsonData, encoding: .utf8)
50-
XCTAssertEqual(jsonString, expectedString)
51-
}
52-
53-
func testDecodable() {
54-
guard let jsonData = expectedString.data(using: .utf8) else {
50+
51+
guard let jsonData2 = expectedString.data(using: .utf8) else {
5552
XCTFail("Unable to serialize expected JSON string into Data")
5653
return
5754
}
5855

59-
let g: UnweightedGraph<String>
56+
let g2: CodableUnweightedGraph<String>
6057
do {
61-
g = try JSONDecoder().decode(CodableUnweightedGraph<String>.self, from: jsonData)
58+
g2 = try JSONDecoder().decode(CodableUnweightedGraph<String>.self, from: jsonData2)
6259
} catch {
6360
XCTFail("JSONDecoder().decode(CodableUnweightedGraph<String>.self, from: jsonData) threw: \(error)")
6461
return
6562
}
66-
XCTAssertEqual(g.neighborsForVertex("Miami")!, g.neighborsForVertex(g.neighborsForVertex("New York")![0])!, "Miami and New York Connected bi-directionally")
63+
XCTAssertEqual(g2.neighborsForVertex("Miami")!, g2.neighborsForVertex(g.neighborsForVertex("New York")![0])!, "Miami and New York Connected bi-directionally")
6764
// XCTAssertEqual(g, expectedUnweightedGraph)
65+
let jsonData3 = try! JSONEncoder().encode(g2)
66+
let jsonString2: String = String(data: jsonData3, encoding: .utf8)!
67+
XCTAssertEqual(jsonString, jsonString2)
6868
}
6969
}
7070

@@ -236,8 +236,7 @@ extension SwiftGraphCodableTests {
236236
}
237237

238238
static var allTests = [
239-
("testEncodable", testEncodable),
240-
("testDecodable", testDecodable),
239+
("testEncodableDecodable", testEncodableDecodable),
241240
("testComplexWeightedEncodableDecodable", testComplexWeightedEncodableDecodable)
242241
]
243242
}

0 commit comments

Comments
 (0)