Skip to content

Commit 3c9657a

Browse files
committed
UnitConverter Equality fixed
1 parent 99689f9 commit 3c9657a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Foundation/Unit.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ open class UnitConverterLinear : UnitConverter, NSSecureCoding {
7979
}
8080

8181
public static var supportsSecureCoding: Bool { return true }
82+
83+
open override func isEqual(_ object: Any?) -> Bool {
84+
guard let other = object as? UnitConverterLinear else {
85+
return false
86+
}
87+
88+
if self === other {
89+
return true
90+
}
91+
92+
return self.coefficient == other.coefficient
93+
&& self.constant == other.constant
94+
}
8295
}
8396

8497
private class UnitConverterReciprocal : UnitConverter, NSSecureCoding {
@@ -115,6 +128,18 @@ private class UnitConverterReciprocal : UnitConverter, NSSecureCoding {
115128
}
116129

117130
fileprivate static var supportsSecureCoding: Bool { return true }
131+
132+
open override func isEqual(_ object: Any?) -> Bool {
133+
guard let other = object as? UnitConverterReciprocal else {
134+
return false
135+
}
136+
137+
if self === other {
138+
return true
139+
}
140+
141+
return self.reciprocal == other.reciprocal
142+
}
118143
}
119144

120145
/*

TestFoundation/TestUnitConverter.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class TestUnitConverter: XCTestCase {
2424
("test_baseUnit", test_linearity),
2525
("test_linearity", test_linearity),
2626
("test_bijectivity", test_bijectivity),
27+
("test_equality", test_equality),
2728
]
2829
}
2930

@@ -267,5 +268,22 @@ class TestUnitConverter: XCTestCase {
267268
XCTAssertEqual(testIdentity(UnitVolume.imperialGallons), 1, accuracy: delta)
268269
XCTAssertEqual(testIdentity(UnitVolume.metricCups), 1, accuracy: delta)
269270
}
271+
272+
func test_equality() {
273+
let u1 = UnitConverterLinear(coefficient: 1, constant: 2)
274+
let u2 = UnitConverterLinear(coefficient: 1, constant: 2)
275+
XCTAssertEqual(u1, u2)
276+
XCTAssertEqual(u2, u1)
277+
278+
let u3 = UnitConverterLinear(coefficient: 1, constant: 3)
279+
XCTAssertNotEqual(u1, u3)
280+
XCTAssertNotEqual(u3, u1)
281+
282+
let u4 = UnitConverterLinear(coefficient: 2, constant: 2)
283+
XCTAssertNotEqual(u1, u4)
284+
XCTAssertNotEqual(u4, u1)
285+
286+
// Cannot test UnitConverterReciprocal due to no support for @testable import.
287+
}
270288

271289
}

0 commit comments

Comments
 (0)