Skip to content

Commit 11e920b

Browse files
authored
Add CustomStringConvertible conformances to Pair types (#204)
1 parent 6624a39 commit 11e920b

File tree

7 files changed

+45
-0
lines changed

7 files changed

+45
-0
lines changed

Sources/DataStructures/Pair/Cross.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ extension Cross: Comparable where T: Comparable, U: Comparable {
4141

4242
extension Cross: Equatable where T: Equatable, U: Equatable { }
4343
extension Cross: Hashable where T: Hashable, U: Hashable { }
44+
45+
extension Cross: CustomStringConvertible {
46+
47+
// MARK: - CustomStringConvertible
48+
49+
/// Printable description of `Cross`.
50+
public var description: String {
51+
return "<\(a),\(b)>"
52+
}
53+
}

Sources/DataStructures/Pair/OrderedPair.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ public struct OrderedPair <T>: SwappablePair {
2828

2929
extension OrderedPair: Equatable where T: Equatable { }
3030
extension OrderedPair: Hashable where T: Hashable { }
31+
32+
extension OrderedPair: CustomStringConvertible {
33+
34+
// MARK: - CustomStringConvertible
35+
36+
/// Printable description of `OrderedPair`.
37+
public var description: String {
38+
return "(\(a),\(b))"
39+
}
40+
}

Sources/DataStructures/Pair/UnorderedPair.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,13 @@ extension UnorderedPair: Hashable where T: Hashable {
5858
hasher.combine(a.hashValue ^ b.hashValue)
5959
}
6060
}
61+
62+
extension UnorderedPair: CustomStringConvertible {
63+
64+
// MARK: - CustomStringConvertible
65+
66+
/// Printable description of `UnorderedPair`.
67+
public var description: String {
68+
return "{\(a),\(b)}"
69+
}
70+
}

Tests/DataStructuresTests/CrossTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ class CrossTests: XCTestCase {
3939
}
4040
XCTAssertEqual(start.map(function),expected)
4141
}
42+
43+
func testDescription() {
44+
XCTAssertEqual(Cross("a",0).description, "<a,0>")
45+
}
4246
}

Tests/DataStructuresTests/PairTests/OrderedPairTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ class OrderedPairTests: XCTestCase {
1515
let expected = OrderedPair(1,2)
1616
XCTAssertEqual(start.map { $0.count }, expected)
1717
}
18+
19+
func testDescription() {
20+
XCTAssertEqual(OrderedPair("3","four").description, "(3,four)")
21+
}
1822
}

Tests/DataStructuresTests/PairTests/UnorderedPairTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class UnorderedPairTests: XCTestCase {
6464
let expected = UnorderedPair(1,2)
6565
XCTAssertEqual(start.map { $0.count }, expected)
6666
}
67+
68+
func testDescription() {
69+
XCTAssertEqual(UnorderedPair("a","z").description, "{a,z}")
70+
}
6771
}
6872

6973
func randomString(maxLength: Int = 10) -> String {

Tests/DataStructuresTests/XCTestManifests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ extension CrossTests {
110110
("testComparableFalseEqual", testComparableFalseEqual),
111111
("testComparableLexicographic", testComparableLexicographic),
112112
("testComparableLexicographicFalse", testComparableLexicographicFalse),
113+
("testDescription", testDescription),
113114
("testMap", testMap),
114115
]
115116
}
@@ -285,6 +286,7 @@ extension OrderedDictionaryTests {
285286

286287
extension OrderedPairTests {
287288
static let __allTests = [
289+
("testDescription", testDescription),
288290
("testMap", testMap),
289291
]
290292
}
@@ -518,6 +520,7 @@ extension TreeTests {
518520

519521
extension UnorderedPairTests {
520522
static let __allTests = [
523+
("testDescription", testDescription),
521524
("testEquatable", testEquatable),
522525
("testHashValuesInt", testHashValuesInt),
523526
("testHashValuesString", testHashValuesString),

0 commit comments

Comments
 (0)