-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathDictionaryTraps.swift
218 lines (177 loc) · 5.78 KB
/
DictionaryTraps.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
// These tests should crash.
// RUN: mkdir -p %t
// RUN: xcrun -sdk %target-sdk-name clang++ -arch %target-cpu %S/Inputs/CatchCrashes.cpp -c -o %t/CatchCrashes.o
// RUN: %target-build-swift %s -Xlinker %t/CatchCrashes.o -o %t/a.out
//
// RUN: %target-run %t/a.out KeyTypeNotBridged 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out ValueTypeNotBridged 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out DuplicateKeys1 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out DuplicateKeys2 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out DuplicateKeys3 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out RemoveInvalidIndex1 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out RemoveInvalidIndex2 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out RemoveInvalidIndex3 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out RemoveInvalidIndex4 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out BridgedKeyIsNotNSCopyable1 2>&1 | FileCheck %s -check-prefix=CHECK-UNRECOGNIZED-SELECTOR
// RUN: %target-run %t/a.out BridgedKeyIsNotNSCopyable2 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out Downcast1 2>&1 | FileCheck %s -check-prefix=CHECK
// RUN: %target-run %t/a.out Downcast2 2>&1 | FileCheck %s -check-prefix=CHECK
// CHECK: OK
// CHECK: CRASHED: SIG{{ILL|TRAP|ABRT}}
// CHECK-UNRECOGNIZED-SELECTOR: OK
// CHECK-UNRECOGNIZED-SELECTOR: unrecognized selector sent to instance
// CHECK-UNRECOGNIZED-SELECTOR: CRASHED: SIGABRT
import Foundation
// Interpret the command line arguments.
var arg = Process.arguments[1]
struct NotBridgedKeyTy : Equatable, Hashable {
init(_ value: Int) {
self.value = value
}
var hashValue: Int {
return value
}
var value: Int
}
func == (lhs: NotBridgedKeyTy, rhs: NotBridgedKeyTy) -> Bool {
return lhs.value == rhs.value
}
assert(!_isBridgedToObjectiveC(NotBridgedKeyTy.self))
struct NotBridgedValueTy {}
assert(!_isBridgedToObjectiveC(NotBridgedValueTy.self))
class BridgedVerbatimRefTy : Equatable, Hashable {
init(_ value: Int) {
self.value = value
}
var hashValue: Int {
return value
}
var value: Int
}
func == (lhs: BridgedVerbatimRefTy, rhs: BridgedVerbatimRefTy) -> Bool {
return lhs.value == rhs.value
}
assert(_isBridgedToObjectiveC(BridgedVerbatimRefTy.self))
assert(_isBridgedVerbatimToObjectiveC(BridgedVerbatimRefTy.self))
if true {
// Sanity checks. This code should not trap.
var d = Dictionary<BridgedVerbatimRefTy, BridgedVerbatimRefTy>()
var nsd: NSDictionary = d
}
if arg == "KeyTypeNotBridged" {
var d = Dictionary<NotBridgedKeyTy, BridgedVerbatimRefTy>()
println("OK")
var nsd: NSDictionary = d
}
if arg == "ValueTypeNotBridged" {
var d = Dictionary<BridgedVerbatimRefTy, NotBridgedValueTy>()
println("OK")
var nsd: NSDictionary = d
}
if arg == "DuplicateKeys1" {
println("OK")
Dictionary.convertFromDictionaryLiteral(
(10, 1010), (20, 1020), (30, 1030), (10, 0))
}
if arg == "DuplicateKeys2" {
println("OK")
Dictionary.convertFromDictionaryLiteral(
(10, 1010), (20, 1020), (30, 1030), (10, 1010))
}
if arg == "DuplicateKeys3" {
println("OK")
var d = [ 10: 1010, 10: 0 ]
}
if arg == "RemoveInvalidIndex1" {
var d = Dictionary<Int, Int>()
let index = d.startIndex
println("OK")
d.removeAtIndex(index)
}
if arg == "RemoveInvalidIndex2" {
var d = Dictionary<Int, Int>()
let index = d.endIndex
println("OK")
d.removeAtIndex(index)
}
if arg == "RemoveInvalidIndex3" {
var d = [ 10: 1010, 20: 1020, 30: 1030 ]
let index = d.endIndex
println("OK")
d.removeAtIndex(index)
}
if arg == "RemoveInvalidIndex4" {
var d = [ 10: 1010 ]
let index = d.indexForKey(10)!
d.removeAtIndex(index)
assert(!d[10])
println("OK")
d.removeAtIndex(index)
}
class TestObjCKeyTy : NSObject {
init(_ value: Int) {
self.value = value
}
override func isEqual(object: AnyObject!) -> Bool {
if let other: AnyObject = object {
if let otherObjcKey = other as? TestObjCKeyTy {
return self.value == otherObjcKey.value
}
}
return false
}
override var hash : Int {
return value
}
var value: Int
}
struct TestBridgedKeyTy : Hashable, _BridgedToObjectiveC {
init(_ value: Int) { self.value = value }
var hashValue: Int { return value }
static func getObjectiveCType() -> Any.Type {
return TestObjCKeyTy.self
}
func bridgeToObjectiveC() -> TestObjCKeyTy {
return TestObjCKeyTy(value)
}
static func bridgeFromObjectiveC(x: TestObjCKeyTy) -> TestBridgedKeyTy {
return TestBridgedKeyTy(x.value)
}
var value: Int
}
func ==(x: TestBridgedKeyTy, y: TestBridgedKeyTy) -> Bool {
return x.value == y.value
}
if arg == "BridgedKeyIsNotNSCopyable1" {
// This Dictionary is bridged in O(1).
var d = [ TestObjCKeyTy(10): NSObject() ]
var nsd: NSDictionary = d
println("OK")
nsd.mutableCopy()
}
if arg == "BridgedKeyIsNotNSCopyable2" {
// This Dictionary is bridged in O(N).
var d = [ TestObjCKeyTy(10): 10 ]
println("OK")
var nsd: NSDictionary = d
}
if arg == "Downcast1" {
let d: Dictionary<NSObject, NSObject> = [ TestObjCKeyTy(10): NSObject(),
NSObject() : NSObject() ]
let d2: Dictionary<TestObjCKeyTy, NSObject> = _dictionaryDownCast(d)
println("OK")
let v1 = d2[TestObjCKeyTy(10)]
let v2 = d2[TestObjCKeyTy(20)]
// This triggers failure.
for (k, v) in d2 { }
}
if arg == "Downcast2" {
let d: Dictionary<NSObject, NSObject> = [ TestObjCKeyTy(10): NSObject(),
NSObject() : NSObject() ]
println("OK")
let d2: Dictionary<TestBridgedKeyTy, NSObject>
= _dictionaryBridgeFromObjectiveC(d)
let v1 = d2[TestBridgedKeyTy(10)]
}
println("BUSTED: should have crashed already")
exit(1)