forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionaryKeyValueTypes.swift
398 lines (333 loc) · 9.01 KB
/
DictionaryKeyValueTypes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import Swift
import SwiftPrivate
import StdlibUnittest
func acceptsAnySet<T : Hashable>(_ s: Set<T>) {}
func acceptsAnyDictionary<KeyTy : Hashable, ValueTy>(
_ d: Dictionary<KeyTy, ValueTy>) {
}
// Compare two arrays as sets.
func equalsUnordered<T : Comparable>(
_ lhs: Array<(T, T)>, _ rhs: Array<(T, T)>
) -> Bool {
func areInAscendingOrder(_ lhs: (T, T), _ rhs: (T, T)) -> Bool {
return [ lhs.0, lhs.1 ].lexicographicallyPrecedes([ rhs.0, rhs.1 ])
}
return lhs.sorted(by: areInAscendingOrder)
.elementsEqual(rhs.sorted(by: areInAscendingOrder)) {
(lhs: (T, T), rhs: (T, T)) -> Bool in
lhs.0 == rhs.0 && lhs.1 == rhs.1
}
}
func equalsUnordered<T : Comparable>(_ lhs: [T], _ rhs: [T]) -> Bool {
return lhs.sorted().elementsEqual(rhs.sorted())
}
// A COW wrapper type that holds an Int.
struct TestValueCOWTy {
class Base {
var value: Int
init(_ value: Int) { self.value = value }
}
private var base: Base
init(_ value: Int = 0) { self.base = Base(value) }
var value: Int {
get { return base.value }
set {
if !isKnownUniquelyReferenced(&base) {
base = Base(newValue)
} else {
base.value = newValue
}
}
}
var baseAddress: Int { return unsafeBitCast(base, to: Int.self) }
}
var _keyCount = _stdlib_AtomicInt(0)
var _keySerial = _stdlib_AtomicInt(0)
class _BaseKeyTy: CustomStringConvertible {
final var value: Int
final var serial: Int
class var objectCount: Int {
get {
return _keyCount.load()
}
set {
_keyCount.store(newValue)
}
}
init(_ value: Int) {
_keyCount.fetchAndAdd(1)
self.serial = _keySerial.addAndFetch(1)
self.value = value
}
deinit {
assert(serial > 0, "double destruction")
_keyCount.fetchAndAdd(-1)
serial = -serial
}
var description: String {
assert(serial > 0, "dead TestKeyTy")
return value.description
}
}
// A wrapper class that can help us track allocations and find issues with
// object lifetime.
final class TestKeyTy
: _BaseKeyTy, Equatable, Hashable, ExpressibleByIntegerLiteral
{
override init(_ value: Int) {
super.init(value)
}
convenience init(integerLiteral value: Int) {
self.init(value)
}
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
var hashValue: Int {
return value
}
static func ==(lhs: TestKeyTy, rhs: TestKeyTy) -> Bool {
return lhs.value == rhs.value
}
}
// A variant of TestKeyTy with precise control over hashing.
// This is useful for bucket-level tests.
final class RawTestKeyTy: _BaseKeyTy, Equatable, Hashable
{
var _hash: Int
init(value: Int, hashValue: Int) {
self._hash = hashValue
super.init(value)
}
var hashValue: Int {
return _hash
}
func hash(into hasher: inout Hasher) {
hasher.combine(_hash)
}
func _rawHashValue(seed: Int) -> Int {
return _hash
}
static func ==(lhs: RawTestKeyTy, rhs: RawTestKeyTy) -> Bool {
return lhs.value == rhs.value
}
}
var _valueCount = _stdlib_AtomicInt(0)
var _valueSerial = _stdlib_AtomicInt(0)
class TestValueTy : CustomStringConvertible {
class var objectCount: Int {
get {
return _valueCount.load()
}
set {
_valueCount.store(newValue)
}
}
init(_ value: Int) {
_valueCount.fetchAndAdd(1)
serial = _valueSerial.addAndFetch(1)
self.value = value
}
deinit {
assert(serial > 0, "double destruction")
_valueCount.fetchAndAdd(-1)
serial = -serial
}
var description: String {
assert(serial > 0, "dead TestValueTy")
return value.description
}
var value: Int
var serial: Int
}
var _equatableValueCount = _stdlib_AtomicInt(0)
var _equatableValueSerial = _stdlib_AtomicInt(0)
class TestEquatableValueTy : Equatable, CustomStringConvertible {
class var objectCount: Int {
get {
return _equatableValueCount.load()
}
set {
_equatableValueCount.store(newValue)
}
}
init(_ value: Int) {
_equatableValueCount.fetchAndAdd(1)
serial = _equatableValueSerial.addAndFetch(1)
self.value = value
}
deinit {
assert(serial > 0, "double destruction")
_equatableValueCount.fetchAndAdd(-1)
serial = -serial
}
var description: String {
assert(serial > 0, "dead TestEquatableValueTy")
return value.description
}
var value: Int
var serial: Int
}
func == (lhs: TestEquatableValueTy, rhs: TestEquatableValueTy) -> Bool {
return lhs.value == rhs.value
}
func resetLeaksOfDictionaryKeysValues() {
TestKeyTy.objectCount = 0
TestValueTy.objectCount = 0
TestEquatableValueTy.objectCount = 0
}
func expectNoLeaksOfDictionaryKeysValues() {
expectEqual(0, TestKeyTy.objectCount, "TestKeyTy leak")
expectEqual(0, TestValueTy.objectCount, "TestValueTy leak")
expectEqual(0, TestEquatableValueTy.objectCount, "TestEquatableValueTy leak")
}
struct ExpectedArrayElement : Comparable, CustomStringConvertible {
var value: Int
var valueIdentity: UInt
init(value: Int, valueIdentity: UInt = 0) {
self.value = value
self.valueIdentity = valueIdentity
}
var description: String {
return "(\(value), \(valueIdentity))"
}
}
func == (
lhs: ExpectedArrayElement,
rhs: ExpectedArrayElement
) -> Bool {
return
lhs.value == rhs.value &&
lhs.valueIdentity == rhs.valueIdentity
}
func < (
lhs: ExpectedArrayElement,
rhs: ExpectedArrayElement
) -> Bool {
let lhsElements = [ lhs.value, Int(bitPattern: lhs.valueIdentity) ]
let rhsElements = [ rhs.value, Int(bitPattern: rhs.valueIdentity) ]
return lhsElements.lexicographicallyPrecedes(rhsElements)
}
func _equalsWithoutElementIdentity(
_ lhs: [ExpectedArrayElement], _ rhs: [ExpectedArrayElement]
) -> Bool {
func stripIdentity(
_ list: [ExpectedArrayElement]
) -> [ExpectedArrayElement] {
return list.map { ExpectedArrayElement(value: $0.value) }
}
return stripIdentity(lhs).elementsEqual(stripIdentity(rhs))
}
func _makeExpectedArrayContents(
_ expected: [Int]
) -> [ExpectedArrayElement] {
var result = [ExpectedArrayElement]()
for value in expected {
result.append(ExpectedArrayElement(value: value))
}
return result
}
struct ExpectedSetElement : Comparable, CustomStringConvertible {
var value: Int
var valueIdentity: UInt
init(value: Int, valueIdentity: UInt = 0) {
self.value = value
self.valueIdentity = valueIdentity
}
var description: String {
return "(\(value), \(valueIdentity))"
}
}
func == (
lhs: ExpectedSetElement,
rhs: ExpectedSetElement
) -> Bool {
return
lhs.value == rhs.value &&
lhs.valueIdentity == rhs.valueIdentity
}
func < (
lhs: ExpectedSetElement,
rhs: ExpectedSetElement
) -> Bool {
let lhsElements = [ lhs.value, Int(bitPattern: lhs.valueIdentity) ]
let rhsElements = [ rhs.value, Int(bitPattern: rhs.valueIdentity) ]
return lhsElements.lexicographicallyPrecedes(rhsElements)
}
func _makeExpectedSetContents(
_ expected: [Int]
) -> [ExpectedSetElement] {
var result = [ExpectedSetElement]()
for value in expected {
result.append(ExpectedSetElement(value: value))
}
return result
}
func _equalsUnorderedWithoutElementIdentity(
_ lhs: [ExpectedSetElement], _ rhs: [ExpectedSetElement]
) -> Bool {
func stripIdentity(
_ list: [ExpectedSetElement]
) -> [ExpectedSetElement] {
return list.map { ExpectedSetElement(value: $0.value) }
}
return equalsUnordered(stripIdentity(lhs), stripIdentity(rhs))
}
struct ExpectedDictionaryElement : Comparable, CustomStringConvertible {
var key: Int
var value: Int
var keyIdentity: UInt
var valueIdentity: UInt
init(key: Int, value: Int, keyIdentity: UInt = 0, valueIdentity: UInt = 0) {
self.key = key
self.value = value
self.keyIdentity = keyIdentity
self.valueIdentity = valueIdentity
}
var description: String {
return "(\(key), \(value), \(keyIdentity), \(valueIdentity))"
}
}
func == (
lhs: ExpectedDictionaryElement,
rhs: ExpectedDictionaryElement
) -> Bool {
return
lhs.key == rhs.key &&
lhs.value == rhs.value &&
lhs.keyIdentity == rhs.keyIdentity &&
lhs.valueIdentity == rhs.valueIdentity
}
func < (
lhs: ExpectedDictionaryElement,
rhs: ExpectedDictionaryElement
) -> Bool {
let lhsElements = [
lhs.key, lhs.value, Int(bitPattern: lhs.keyIdentity),
Int(bitPattern: lhs.valueIdentity)
]
let rhsElements = [
rhs.key, rhs.value, Int(bitPattern: rhs.keyIdentity),
Int(bitPattern: rhs.valueIdentity)
]
return lhsElements.lexicographicallyPrecedes(rhsElements)
}
func _equalsUnorderedWithoutElementIdentity(
_ lhs: [ExpectedDictionaryElement], _ rhs: [ExpectedDictionaryElement]
) -> Bool {
func stripIdentity(
_ list: [ExpectedDictionaryElement]
) -> [ExpectedDictionaryElement] {
return list.map { ExpectedDictionaryElement(key: $0.key, value: $0.value) }
}
return equalsUnordered(stripIdentity(lhs), stripIdentity(rhs))
}
func _makeExpectedDictionaryContents(
_ expected: [(Int, Int)]
) -> [ExpectedDictionaryElement] {
var result = [ExpectedDictionaryElement]()
for (key, value) in expected {
result.append(ExpectedDictionaryElement(key: key, value: value))
}
return result
}