Skip to content

Commit f3eb766

Browse files
authored
Merge pull request #80 from mattpaletta/master
Reversed Graph and Topological Sort Tests
2 parents 1e1f999 + e85f88f commit f3eb766

File tree

5 files changed

+122
-1
lines changed

5 files changed

+122
-1
lines changed

Sources/SwiftGraph/Graph.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ extension Graph {
244244
public func edgeExists(_ edge: E) -> Bool {
245245
return edges[edge.u].contains(edge)
246246
}
247-
248247

249248
// MARK: Implement Printable protocol
250249
public var description: String {

Sources/SwiftGraph/Reversed.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Reversed.swift
3+
// SwiftGraph
4+
//
5+
// Created by Matthew Paletta on 2021-02-18.
6+
// Copyright © 2021 Oak Snow Consulting. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension Graph {
12+
/// Returns a graph of the same type with all edges reversed.
13+
///
14+
/// - returns: Graph of the same type with all edges reversed.
15+
public func reversed() -> Self {
16+
let g = Self(vertices: self.vertices)
17+
for e in self.edgeList() {
18+
g.addEdge(e.reversed(), directed: e.directed)
19+
}
20+
return g
21+
}
22+
}

SwiftGraph.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
343882F925DF04E7007D2401 /* Reversed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 343882F825DF04E7007D2401 /* Reversed.swift */; };
11+
3438830525DF056C007D2401 /* SwiftGraphReversedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 343882FD25DF0528007D2401 /* SwiftGraphReversedTests.swift */; };
1012
55DCCBF61F8ADA12001913F7 /* Cycle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55DCCBF51F8ADA12001913F7 /* Cycle.swift */; };
1113
55DCCBF81F8AE2F1001913F7 /* CycleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55DCCBF71F8AE2F1001913F7 /* CycleTests.swift */; };
1214
55E620081A194C80000A5F7B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E620071A194C80000A5F7B /* AppDelegate.swift */; };
@@ -93,6 +95,8 @@
9395
/* End PBXCopyFilesBuildPhase section */
9496

9597
/* Begin PBXFileReference section */
98+
343882F825DF04E7007D2401 /* Reversed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Reversed.swift; path = Sources/SwiftGraph/Reversed.swift; sourceTree = SOURCE_ROOT; };
99+
343882FD25DF0528007D2401 /* SwiftGraphReversedTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftGraphReversedTests.swift; sourceTree = "<group>"; };
96100
553746441DA56CC500C0E0F6 /* Sort.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Sort.swift; path = Sources/SwiftGraph/Sort.swift; sourceTree = SOURCE_ROOT; };
97101
557F55E91F8AB247002AF0BF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = ../Info.plist; sourceTree = "<group>"; };
98102
5589B4621C0E72CC00D6664E /* Edge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Edge.swift; path = Sources/SwiftGraph/Edge.swift; sourceTree = SOURCE_ROOT; };
@@ -284,6 +288,7 @@
284288
7985B91A1E5A4FCB00C100E7 /* SwiftGraphSearchTests.swift */,
285289
7985B91B1E5A4FCB00C100E7 /* SwiftGraphSortTests.swift */,
286290
7985B91C1E5A4FCB00C100E7 /* SwiftGraphTests.swift */,
291+
343882FD25DF0528007D2401 /* SwiftGraphReversedTests.swift */,
287292
B5100A4B208B97A800C7A73A /* UnweightedGraphTests.swift */,
288293
B52ABD37208955B500FBF10C /* UnionTests.swift */,
289294
55F5EA5A2151E33100DFC301 /* SwiftGraphCodableTests.swift */,
@@ -318,6 +323,7 @@
318323
isa = PBXGroup;
319324
children = (
320325
B51B460A2083E14200CD0463 /* Union.swift */,
326+
343882F825DF04E7007D2401 /* Reversed.swift */,
321327
);
322328
name = Operations;
323329
sourceTree = "<group>";
@@ -538,6 +544,7 @@
538544
7985B9211E5A503200C100E7 /* Edge.swift in Sources */,
539545
7985B9271E5A503200C100E7 /* SwiftPriorityQueue.swift in Sources */,
540546
55DCCBF61F8ADA12001913F7 /* Cycle.swift in Sources */,
547+
343882F925DF04E7007D2401 /* Reversed.swift in Sources */,
541548
);
542549
runOnlyForDeploymentPostprocessing = 0;
543550
};
@@ -560,6 +567,7 @@
560567
B5EF143321791348008FCC5C /* UniqueElementsGraphHashableTests.swift in Sources */,
561568
55F5EA5B2151E33100DFC301 /* SwiftGraphCodableTests.swift in Sources */,
562569
B523F2EA2094F0E2006587ED /* UniqueElementsGraphHashableInitTests.swift in Sources */,
570+
3438830525DF056C007D2401 /* SwiftGraphReversedTests.swift in Sources */,
563571
B5EF143121791009008FCC5C /* UniqueElementsGraphInitTests.swift in Sources */,
564572
B54FDA712172A34D00057C51 /* ConstructorsTests.swift in Sources */,
565573
B5D229BF207BF3A800151820 /* UniqueElementsGraphTests.swift in Sources */,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// SwiftGraphReversedTests.swift
3+
// SwiftGraph
4+
//
5+
// Created by Matthew Paletta on 2021-02-18.
6+
// Copyright © 2021 Oak Snow Consulting. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import SwiftGraph
11+
12+
class SwiftGraphReversedTests: XCTestCase {
13+
14+
override func setUp() {
15+
super.setUp()
16+
// Put setup code here. This method is called before the invocation of each test method in the class.
17+
}
18+
19+
override func tearDown() {
20+
// Put teardown code here. This method is called after the invocation of each test method in the class.
21+
super.tearDown()
22+
}
23+
24+
func testCitesReversedDirected() {
25+
let g = UnweightedGraph<String>(vertices: ["Atlanta", "New York", "Miami"])
26+
g.addEdge(from: "Atlanta", to: "New York", directed: true)
27+
g.addEdge(from: "Miami", to: "Atlanta", directed: true)
28+
g.addEdge(from: "New York", to: "Miami", directed: true)
29+
30+
XCTAssertTrue(g.edgeExists(from: "Atlanta", to: "New York"))
31+
XCTAssertTrue(g.edgeExists(from: "Miami", to: "Atlanta"))
32+
XCTAssertTrue(g.edgeExists(from: "New York", to: "Miami"))
33+
34+
let r: UnweightedGraph<String> = g.reversed()
35+
XCTAssertFalse(r.edgeExists(from: "Atlanta", to: "New York"))
36+
XCTAssertFalse(r.edgeExists(from: "Miami", to: "Atlanta"))
37+
XCTAssertFalse(r.edgeExists(from: "New York", to: "Miami"))
38+
39+
XCTAssertTrue(r.edgeExists(from: "New York", to: "Atlanta"))
40+
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "Miami"))
41+
XCTAssertTrue(r.edgeExists(from: "Miami", to: "New York"))
42+
}
43+
44+
func testCitesReversedUndirected() {
45+
let g = UnweightedGraph<String>(vertices: ["Atlanta", "New York", "Miami"])
46+
g.addEdge(from: "Atlanta", to: "New York", directed: false)
47+
g.addEdge(from: "Miami", to: "Atlanta", directed: false)
48+
g.addEdge(from: "New York", to: "Miami", directed: false)
49+
50+
XCTAssertTrue(g.edgeExists(from: "Atlanta", to: "New York"))
51+
XCTAssertTrue(g.edgeExists(from: "Miami", to: "Atlanta"))
52+
XCTAssertTrue(g.edgeExists(from: "New York", to: "Miami"))
53+
54+
let r: UnweightedGraph<String> = g.reversed()
55+
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "New York"))
56+
XCTAssertTrue(r.edgeExists(from: "Miami", to: "Atlanta"))
57+
XCTAssertTrue(r.edgeExists(from: "New York", to: "Miami"))
58+
59+
XCTAssertTrue(r.edgeExists(from: "New York", to: "Atlanta"))
60+
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "Miami"))
61+
XCTAssertTrue(r.edgeExists(from: "Miami", to: "New York"))
62+
}
63+
}

Tests/SwiftGraphTests/SwiftGraphSortTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ class SwiftGraphSortTests: XCTestCase {
2323
// pg 1016 Liang
2424
let dressDAG: UnweightedGraph<String> = UnweightedGraph<String>(vertices: ["undershorts", "socks", "pants", "shoes", "watch", "belt", "shirt", "tie", "jacket"])
2525

26+
private func comesBefore<T: Equatable>(order: [T], a: T, b: T) -> Bool {
27+
let aPos = order.firstIndex { (i) -> Bool in
28+
return i == a
29+
}
30+
let bPos = order.firstIndex { (i) -> Bool in
31+
return i == b
32+
}
33+
34+
guard let A = aPos, let B = bPos else { return false }
35+
return A < B
36+
}
37+
38+
func testComesBefore() {
39+
XCTAssertTrue(comesBefore(order: [5, 4, 3, 2, 1], a: 3, b: 1))
40+
XCTAssertTrue(comesBefore(order: [5, 4, 3, 2, 1], a: 5, b: 2))
41+
XCTAssertTrue(comesBefore(order: ["A", "B", "C"], a: "A", b: "C"))
42+
43+
}
44+
2645
override func setUp() {
2746
super.setUp()
2847
// Put setup code here. This method is called before the invocation of each test method in the class.
@@ -56,5 +75,15 @@ class SwiftGraphSortTests: XCTestCase {
5675
}
5776
print(result)
5877
XCTAssertEqual(result.count, 9, "All items in sort.")
78+
XCTAssertTrue(comesBefore(order: result, a: "undershorts", b: "shoes"))
79+
XCTAssertTrue(comesBefore(order: result, a: "pants", b: "shoes"))
80+
XCTAssertTrue(comesBefore(order: result, a: "socks", b: "shoes"))
81+
XCTAssertTrue(comesBefore(order: result, a: "undershorts", b: "pants"))
82+
XCTAssertTrue(comesBefore(order: result, a: "pants", b: "belt"))
83+
XCTAssertTrue(comesBefore(order: result, a: "belt", b: "jacket"))
84+
XCTAssertTrue(comesBefore(order: result, a: "shirt", b: "tie"))
85+
XCTAssertTrue(comesBefore(order: result, a: "shirt", b: "belt"))
86+
XCTAssertTrue(comesBefore(order: result, a: "tie", b: "jacket"))
87+
5988
}
6089
}

0 commit comments

Comments
 (0)