Skip to content

Commit eeaf30f

Browse files
committed
Add conditional Equatable (for Swift 4.1+) and Hashable (4.2+) conformance
Closes #25. I initially thought we could also conform to Codable but that seems impossible because there's no way to serialize the comparator function.
1 parent feaf704 commit eeaf30f

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

Sources/SortedArray.swift

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,27 @@ extension SortedArray {
416416
}
417417
}
418418

419-
public func ==<Element: Equatable> (lhs: SortedArray<Element>, rhs: SortedArray<Element>) -> Bool {
420-
return lhs._elements == rhs._elements
421-
}
419+
#if swift(>=4.1)
420+
extension SortedArray: Equatable where Element: Equatable {
421+
public static func == (lhs: SortedArray<Element>, rhs: SortedArray<Element>) -> Bool {
422+
// Ignore the comparator function for Equatable
423+
return lhs._elements == rhs._elements
424+
}
425+
}
426+
#else
427+
public func ==<Element: Equatable> (lhs: SortedArray<Element>, rhs: SortedArray<Element>) -> Bool {
428+
return lhs._elements == rhs._elements
429+
}
422430

423-
public func !=<Element: Equatable> (lhs: SortedArray<Element>, rhs: SortedArray<Element>) -> Bool {
424-
return lhs._elements != rhs._elements
425-
}
431+
public func !=<Element: Equatable> (lhs: SortedArray<Element>, rhs: SortedArray<Element>) -> Bool {
432+
return lhs._elements != rhs._elements
433+
}
434+
#endif
435+
436+
#if swift(>=4.1.50)
437+
extension SortedArray: Hashable where Element: Hashable {
438+
public func hash(into hasher: inout Hasher) {
439+
hasher.combine(_elements)
440+
}
441+
}
442+
#endif

Tests/UnitTests/SortedArrayTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,24 @@ class SortedArrayTests: XCTestCase {
332332
assertElementsEqual(sut, [1,2])
333333
}
334334

335+
func testIsEquatableInSwift4_1AndHigher() {
336+
#if swift(>=4.1)
337+
let array1 = SortedArray(unsorted: [3,2,1])
338+
let array2 = SortedArray(unsorted: 1...3)
339+
XCTAssertEqual(array1, array2)
340+
#endif
341+
}
342+
343+
func testComparatorFunctionIsNotRelevantForEquatable() {
344+
#if swift(>=4.1)
345+
let array1 = SortedArray(unsorted: [1,1,1], areInIncreasingOrder: <)
346+
let array2 = SortedArray(unsorted: [1,1,1], areInIncreasingOrder: >)
347+
let array3 = SortedArray(unsorted: [3,2,1,4])
348+
XCTAssertEqual(array1, array2)
349+
XCTAssertNotEqual(array1, array3)
350+
#endif
351+
}
352+
335353
func testImplementsEqual() {
336354
let sut = SortedArray(unsorted: [3,2,1])
337355
XCTAssertTrue(sut == SortedArray(unsorted: 1...3))
@@ -341,6 +359,16 @@ class SortedArrayTests: XCTestCase {
341359
let sut = SortedArray(unsorted: 1...3)
342360
XCTAssertTrue(sut != SortedArray(unsorted: 1...4))
343361
}
362+
363+
func testIsHashableInSwift4_2AndHigher() {
364+
#if swift(>=4.1.50)
365+
let array1 = SortedArray(unsorted: [3,2,1])
366+
let array2 = SortedArray(unsorted: 1...3)
367+
let array3 = SortedArray(unsorted: [3,2,1,4])
368+
XCTAssertEqual(array1.hashValue, array2.hashValue)
369+
XCTAssertNotEqual(array1.hashValue, array3.hashValue)
370+
#endif
371+
}
344372
}
345373

346374
extension SortedArrayTests {
@@ -398,8 +426,11 @@ extension SortedArrayTests {
398426
("testRemoveElementAtBeginningPreservesSortOrder", testRemoveElementAtBeginningPreservesSortOrder),
399427
("testRemoveElementInMiddlePreservesSortOrder", testRemoveElementInMiddlePreservesSortOrder),
400428
("testRemoveElementAtEndPreservesSortOrder", testRemoveElementAtEndPreservesSortOrder),
429+
("testIsEquatableInSwift4_1AndHigher", testIsEquatableInSwift4_1AndHigher),
430+
("testComparatorFunctionIsNotRelevantForEquatable", testComparatorFunctionIsNotRelevantForEquatable),
401431
("testImplementsEqual", testImplementsEqual),
402432
("testImplementsNotEqual", testImplementsNotEqual),
433+
("testIsHashableInSwift4_2AndHigher", testIsHashableInSwift4_2AndHigher),
403434
]
404435
}
405436
}

0 commit comments

Comments
 (0)