@@ -41,17 +41,17 @@ class MyDimensionalUnit : Dimension {
41
41
}
42
42
43
43
@available ( OSX 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
44
- class BugUnit : Unit {
44
+ class CustomUnit : Unit {
45
45
override init ( symbol: String ) {
46
- precondition ( symbol == " bug " )
47
46
super. init ( symbol: symbol)
48
47
}
49
48
50
49
required init ? ( coder aDecoder: NSCoder ) {
51
50
super. init ( coder: aDecoder)
52
51
}
53
52
54
- public static let bugs = BugUnit ( symbol: " bug " )
53
+ public static let bugs = CustomUnit ( symbol: " bug " )
54
+ public static let features = CustomUnit ( symbol: " feature " )
55
55
}
56
56
57
57
@available ( OSX 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
@@ -66,9 +66,9 @@ class TestMeasurement : TestMeasurementSuper {
66
66
expectEqual ( 6 , m3. value)
67
67
expectEqual ( m1, m2)
68
68
69
- let m10 = Measurement ( value: 2 , unit: BugUnit . bugs)
70
- let m11 = Measurement ( value: 2 , unit: BugUnit . bugs)
71
- let m12 = Measurement ( value: 3 , unit: BugUnit . bugs)
69
+ let m10 = Measurement ( value: 2 , unit: CustomUnit . bugs)
70
+ let m11 = Measurement ( value: 2 , unit: CustomUnit . bugs)
71
+ let m12 = Measurement ( value: 3 , unit: CustomUnit . bugs)
72
72
73
73
expectEqual ( m10, m11)
74
74
expectNotEqual ( m10, m12)
@@ -89,7 +89,7 @@ class TestMeasurement : TestMeasurementSuper {
89
89
90
90
// This correctly fails to build
91
91
92
- // let m2 = Measurement(value: 1, unit: BugUnit .bugs)
92
+ // let m2 = Measurement(value: 1, unit: CustomUnit .bugs)
93
93
// m2.converted(to: MyDimensionalUnit.unitKiloA)
94
94
}
95
95
@@ -113,9 +113,9 @@ class TestMeasurement : TestMeasurementSuper {
113
113
// Dynamically different dimensions
114
114
expectEqual ( Measurement ( value: 1_001_000 , unit: MyDimensionalUnit . unitA) , oneMegaA + oneKiloA)
115
115
116
- var bugCount = Measurement ( value: 1 , unit: BugUnit . bugs)
116
+ var bugCount = Measurement ( value: 1 , unit: CustomUnit . bugs)
117
117
expectEqual ( bugCount. value, 1 )
118
- bugCount = bugCount + Measurement( value: 4 , unit: BugUnit . bugs)
118
+ bugCount = bugCount + Measurement( value: 4 , unit: CustomUnit . bugs)
119
119
expectEqual ( bugCount. value, 5 )
120
120
}
121
121
@@ -154,6 +154,51 @@ class TestMeasurement : TestMeasurementSuper {
154
154
expectTrue ( fiveKM <= fiveThousandM)
155
155
}
156
156
157
+ func testHashing( ) {
158
+ let lengths : [ [ Measurement < UnitLength > ] ] = [
159
+ [
160
+ Measurement ( value: 5 , unit: UnitLength . kilometers) ,
161
+ Measurement ( value: 5000 , unit: UnitLength . meters) ,
162
+ Measurement ( value: 5000 , unit: UnitLength . meters) ,
163
+ ] ,
164
+ [
165
+ Measurement ( value: 1 , unit: UnitLength . kilometers) ,
166
+ Measurement ( value: 1000 , unit: UnitLength . meters) ,
167
+ ] ,
168
+ [
169
+ Measurement ( value: 1 , unit: UnitLength . meters) ,
170
+ Measurement ( value: 1000 , unit: UnitLength . millimeters) ,
171
+ ] ,
172
+ ]
173
+ checkHashableGroups ( lengths)
174
+
175
+ let durations : [ [ Measurement < UnitDuration > ] ] = [
176
+ [
177
+ Measurement ( value: 3600 , unit: UnitDuration . seconds) ,
178
+ Measurement ( value: 60 , unit: UnitDuration . minutes) ,
179
+ Measurement ( value: 1 , unit: UnitDuration . hours) ,
180
+ ] ,
181
+ [
182
+ Measurement ( value: 1800 , unit: UnitDuration . seconds) ,
183
+ Measurement ( value: 30 , unit: UnitDuration . minutes) ,
184
+ Measurement ( value: 0.5 , unit: UnitDuration . hours) ,
185
+ ]
186
+ ]
187
+ checkHashableGroups ( durations)
188
+
189
+ let custom : [ Measurement < CustomUnit > ] = [
190
+ Measurement ( value: 1 , unit: CustomUnit . bugs) ,
191
+ Measurement ( value: 2 , unit: CustomUnit . bugs) ,
192
+ Measurement ( value: 3 , unit: CustomUnit . bugs) ,
193
+ Measurement ( value: 4 , unit: CustomUnit . bugs) ,
194
+ Measurement ( value: 1 , unit: CustomUnit . features) ,
195
+ Measurement ( value: 2 , unit: CustomUnit . features) ,
196
+ Measurement ( value: 3 , unit: CustomUnit . features) ,
197
+ Measurement ( value: 4 , unit: CustomUnit . features) ,
198
+ ]
199
+ checkHashable ( custom, equalityOracle: { $0 == $1 } )
200
+ }
201
+
157
202
func test_AnyHashableContainingMeasurement( ) {
158
203
let values : [ Measurement < UnitLength > ] = [
159
204
Measurement ( value: 100 , unit: UnitLength . meters) ,
@@ -193,6 +238,7 @@ if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
193
238
MeasurementTests . test ( " testMeasurementFormatter " ) { TestMeasurement ( ) . testMeasurementFormatter ( ) }
194
239
MeasurementTests . test ( " testEquality " ) { TestMeasurement ( ) . testEquality ( ) }
195
240
MeasurementTests . test ( " testComparison " ) { TestMeasurement ( ) . testComparison ( ) }
241
+ MeasurementTests . test ( " testHashing " ) { TestMeasurement ( ) . testHashing ( ) }
196
242
MeasurementTests . test ( " test_AnyHashableContainingMeasurement " ) { TestMeasurement ( ) . test_AnyHashableContainingMeasurement ( ) }
197
243
MeasurementTests . test ( " test_AnyHashableCreatedFromNSMeasurement " ) { TestMeasurement ( ) . test_AnyHashableCreatedFromNSMeasurement ( ) }
198
244
runAllTests ( )
0 commit comments