Skip to content

Commit

Permalink
Add CustomStringConvertible conformances to Pair types (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbean authored Nov 17, 2018
1 parent 6624a39 commit 11e920b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/DataStructures/Pair/Cross.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ extension Cross: Comparable where T: Comparable, U: Comparable {

extension Cross: Equatable where T: Equatable, U: Equatable { }
extension Cross: Hashable where T: Hashable, U: Hashable { }

extension Cross: CustomStringConvertible {

// MARK: - CustomStringConvertible

/// Printable description of `Cross`.
public var description: String {
return "<\(a),\(b)>"
}
}
10 changes: 10 additions & 0 deletions Sources/DataStructures/Pair/OrderedPair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ public struct OrderedPair <T>: SwappablePair {

extension OrderedPair: Equatable where T: Equatable { }
extension OrderedPair: Hashable where T: Hashable { }

extension OrderedPair: CustomStringConvertible {

// MARK: - CustomStringConvertible

/// Printable description of `OrderedPair`.
public var description: String {
return "(\(a),\(b))"
}
}
10 changes: 10 additions & 0 deletions Sources/DataStructures/Pair/UnorderedPair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ extension UnorderedPair: Hashable where T: Hashable {
hasher.combine(a.hashValue ^ b.hashValue)
}
}

extension UnorderedPair: CustomStringConvertible {

// MARK: - CustomStringConvertible

/// Printable description of `UnorderedPair`.
public var description: String {
return "{\(a),\(b)}"
}
}
4 changes: 4 additions & 0 deletions Tests/DataStructuresTests/CrossTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ class CrossTests: XCTestCase {
}
XCTAssertEqual(start.map(function),expected)
}

func testDescription() {
XCTAssertEqual(Cross("a",0).description, "<a,0>")
}
}
4 changes: 4 additions & 0 deletions Tests/DataStructuresTests/PairTests/OrderedPairTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ class OrderedPairTests: XCTestCase {
let expected = OrderedPair(1,2)
XCTAssertEqual(start.map { $0.count }, expected)
}

func testDescription() {
XCTAssertEqual(OrderedPair("3","four").description, "(3,four)")
}
}
4 changes: 4 additions & 0 deletions Tests/DataStructuresTests/PairTests/UnorderedPairTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class UnorderedPairTests: XCTestCase {
let expected = UnorderedPair(1,2)
XCTAssertEqual(start.map { $0.count }, expected)
}

func testDescription() {
XCTAssertEqual(UnorderedPair("a","z").description, "{a,z}")
}
}

func randomString(maxLength: Int = 10) -> String {
Expand Down
3 changes: 3 additions & 0 deletions Tests/DataStructuresTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ extension CrossTests {
("testComparableFalseEqual", testComparableFalseEqual),
("testComparableLexicographic", testComparableLexicographic),
("testComparableLexicographicFalse", testComparableLexicographicFalse),
("testDescription", testDescription),
("testMap", testMap),
]
}
Expand Down Expand Up @@ -285,6 +286,7 @@ extension OrderedDictionaryTests {

extension OrderedPairTests {
static let __allTests = [
("testDescription", testDescription),
("testMap", testMap),
]
}
Expand Down Expand Up @@ -518,6 +520,7 @@ extension TreeTests {

extension UnorderedPairTests {
static let __allTests = [
("testDescription", testDescription),
("testEquatable", testEquatable),
("testHashValuesInt", testHashValuesInt),
("testHashValuesString", testHashValuesString),
Expand Down

0 comments on commit 11e920b

Please sign in to comment.