Skip to content

Commit bfbd2ff

Browse files
committed
Merge pull request #2561 from rintaro/test-nondarwin-2
[test][stdlib] Enable several test cases on non-Darwin platforms - Part2
2 parents b9ed887 + 3c63c48 commit bfbd2ff

File tree

4 files changed

+235
-201
lines changed

4 files changed

+235
-201
lines changed

test/1_stdlib/DictionaryTraps.swift

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,11 @@
66
// RUN: %target-run %t/a.out_Debug
77
// RUN: %target-run %t/a.out_Release
88
// REQUIRES: executable_test
9-
//
10-
// FIXME: rdar://problem/19648117 Needs splitting objc parts out
11-
// XFAIL: linux
129

1310
import StdlibUnittest
14-
import Foundation
15-
16-
struct NotBridgedKeyTy : Equatable, Hashable {
17-
init(_ value: Int) {
18-
self.value = value
19-
}
20-
var hashValue: Int {
21-
return value
22-
}
23-
var value: Int
24-
}
25-
26-
func == (lhs: NotBridgedKeyTy, rhs: NotBridgedKeyTy) -> Bool {
27-
return lhs.value == rhs.value
28-
}
29-
30-
assert(!_isBridgedToObjectiveC(NotBridgedKeyTy.self))
31-
32-
struct NotBridgedValueTy {}
33-
34-
assert(!_isBridgedToObjectiveC(NotBridgedValueTy.self))
35-
36-
class BridgedVerbatimRefTy : Equatable, Hashable {
37-
init(_ value: Int) {
38-
self.value = value
39-
}
40-
var hashValue: Int {
41-
return value
42-
}
43-
var value: Int
44-
}
45-
46-
func == (lhs: BridgedVerbatimRefTy, rhs: BridgedVerbatimRefTy) -> Bool {
47-
return lhs.value == rhs.value
48-
}
49-
50-
assert(_isBridgedToObjectiveC(BridgedVerbatimRefTy.self))
51-
assert(_isBridgedVerbatimToObjectiveC(BridgedVerbatimRefTy.self))
5211

5312
var DictionaryTraps = TestSuite("DictionaryTraps")
5413

55-
DictionaryTraps.test("sanity") {
56-
// Sanity checks. This code should not trap.
57-
var d = Dictionary<BridgedVerbatimRefTy, BridgedVerbatimRefTy>()
58-
var nsd = d as NSDictionary
59-
}
60-
6114
DictionaryTraps.test("DuplicateKeys1")
6215
.skip(.custom(
6316
{ _isFastAssertConfiguration() },
@@ -136,118 +89,4 @@ DictionaryTraps.test("RemoveInvalidIndex4")
13689
d.remove(at: index)
13790
}
13891

139-
class TestObjCKeyTy : NSObject {
140-
init(_ value: Int) {
141-
self.value = value
142-
}
143-
144-
override func isEqual(_ object: AnyObject!) -> Bool {
145-
if let other = object {
146-
if let otherObjcKey = other as? TestObjCKeyTy {
147-
return self.value == otherObjcKey.value
148-
}
149-
}
150-
return false
151-
}
152-
153-
override var hash : Int {
154-
return value
155-
}
156-
157-
var value: Int
158-
}
159-
160-
struct TestBridgedKeyTy : Hashable, _ObjectiveCBridgeable {
161-
static func _isBridgedToObjectiveC() -> Bool {
162-
return true
163-
}
164-
165-
init(_ value: Int) { self.value = value }
166-
167-
var hashValue: Int { return value }
168-
169-
func _bridgeToObjectiveC() -> TestObjCKeyTy {
170-
return TestObjCKeyTy(value)
171-
}
172-
173-
static func _forceBridgeFromObjectiveC(
174-
_ x: TestObjCKeyTy,
175-
result: inout TestBridgedKeyTy?
176-
) {
177-
result = TestBridgedKeyTy(x.value)
178-
}
179-
180-
static func _conditionallyBridgeFromObjectiveC(
181-
_ x: TestObjCKeyTy,
182-
result: inout TestBridgedKeyTy?
183-
) -> Bool {
184-
result = TestBridgedKeyTy(x.value)
185-
return true
186-
}
187-
188-
static func _unconditionallyBridgeFromObjectiveC(_ source: TestObjCKeyTy?)
189-
-> TestBridgedKeyTy {
190-
var result: TestBridgedKeyTy? = nil
191-
_forceBridgeFromObjectiveC(source!, result: &result)
192-
return result!
193-
}
194-
195-
var value: Int
196-
}
197-
198-
func ==(x: TestBridgedKeyTy, y: TestBridgedKeyTy) -> Bool {
199-
return x.value == y.value
200-
}
201-
202-
DictionaryTraps.test("BridgedKeyIsNotNSCopyable1")
203-
.skip(.custom(
204-
{ _isFastAssertConfiguration() },
205-
reason: "this trap is not guaranteed to happen in -Ounchecked"))
206-
.crashOutputMatches("unrecognized selector sent to instance").code {
207-
// This Dictionary is bridged in O(1).
208-
var d = [ TestObjCKeyTy(10): NSObject() ]
209-
var nsd = d as NSDictionary
210-
expectCrashLater()
211-
nsd.mutableCopy()
212-
}
213-
214-
DictionaryTraps.test("BridgedKeyIsNotNSCopyable2")
215-
.skip(.custom(
216-
{ _isFastAssertConfiguration() },
217-
reason: "this trap is not guaranteed to happen in -Ounchecked"))
218-
.code {
219-
// This Dictionary is bridged in O(1).
220-
var d = [ TestObjCKeyTy(10): 10 ]
221-
var nsd = d as NSDictionary
222-
expectCrashLater()
223-
nsd.mutableCopy()
224-
}
225-
226-
DictionaryTraps.test("Downcast1") {
227-
let d: Dictionary<NSObject, NSObject> = [ TestObjCKeyTy(10): NSObject(),
228-
NSObject() : NSObject() ]
229-
let d2: Dictionary<TestObjCKeyTy, NSObject> = _dictionaryDownCast(d)
230-
expectCrashLater()
231-
let v1 = d2[TestObjCKeyTy(10)]
232-
let v2 = d2[TestObjCKeyTy(20)]
233-
234-
// This triggers failure.
235-
for (k, v) in d2 { }
236-
}
237-
238-
DictionaryTraps.test("Downcast2")
239-
.skip(.custom(
240-
{ _isFastAssertConfiguration() },
241-
reason: "this trap is not guaranteed to happen in -Ounchecked"))
242-
.code {
243-
let d: Dictionary<NSObject, NSObject> = [ TestObjCKeyTy(10): NSObject(),
244-
NSObject() : NSObject() ]
245-
246-
expectCrashLater()
247-
let d2: Dictionary<TestBridgedKeyTy, NSObject>
248-
= _dictionaryBridgeFromObjectiveC(d)
249-
let v1 = d2[TestBridgedKeyTy(10)]
250-
}
251-
25292
runAllTests()
253-
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: %target-build-swift %s -o %t/a.out_Debug
4+
// RUN: %target-build-swift %s -o %t/a.out_Release -O
5+
//
6+
// RUN: %target-run %t/a.out_Debug
7+
// RUN: %target-run %t/a.out_Release
8+
// REQUIRES: executable_test
9+
// REQUIRES: objc_interop
10+
11+
import StdlibUnittest
12+
import Foundation
13+
14+
var DictionaryTraps = TestSuite("DictionaryTraps")
15+
16+
struct NotBridgedKeyTy : Equatable, Hashable {
17+
init(_ value: Int) {
18+
self.value = value
19+
}
20+
var hashValue: Int {
21+
return value
22+
}
23+
var value: Int
24+
}
25+
26+
func == (lhs: NotBridgedKeyTy, rhs: NotBridgedKeyTy) -> Bool {
27+
return lhs.value == rhs.value
28+
}
29+
30+
assert(!_isBridgedToObjectiveC(NotBridgedKeyTy.self))
31+
32+
struct NotBridgedValueTy {}
33+
34+
assert(!_isBridgedToObjectiveC(NotBridgedValueTy.self))
35+
36+
class BridgedVerbatimRefTy : Equatable, Hashable {
37+
init(_ value: Int) {
38+
self.value = value
39+
}
40+
var hashValue: Int {
41+
return value
42+
}
43+
var value: Int
44+
}
45+
46+
func == (lhs: BridgedVerbatimRefTy, rhs: BridgedVerbatimRefTy) -> Bool {
47+
return lhs.value == rhs.value
48+
}
49+
50+
assert(_isBridgedToObjectiveC(BridgedVerbatimRefTy.self))
51+
assert(_isBridgedVerbatimToObjectiveC(BridgedVerbatimRefTy.self))
52+
53+
DictionaryTraps.test("sanity") {
54+
// Sanity checks. This code should not trap.
55+
var d = Dictionary<BridgedVerbatimRefTy, BridgedVerbatimRefTy>()
56+
var nsd = d as NSDictionary
57+
}
58+
59+
class TestObjCKeyTy : NSObject {
60+
init(_ value: Int) {
61+
self.value = value
62+
}
63+
64+
override func isEqual(_ object: AnyObject!) -> Bool {
65+
if let other = object {
66+
if let otherObjcKey = other as? TestObjCKeyTy {
67+
return self.value == otherObjcKey.value
68+
}
69+
}
70+
return false
71+
}
72+
73+
override var hash : Int {
74+
return value
75+
}
76+
77+
var value: Int
78+
}
79+
80+
struct TestBridgedKeyTy : Hashable, _ObjectiveCBridgeable {
81+
static func _isBridgedToObjectiveC() -> Bool {
82+
return true
83+
}
84+
85+
init(_ value: Int) { self.value = value }
86+
87+
var hashValue: Int { return value }
88+
89+
func _bridgeToObjectiveC() -> TestObjCKeyTy {
90+
return TestObjCKeyTy(value)
91+
}
92+
93+
static func _forceBridgeFromObjectiveC(
94+
_ x: TestObjCKeyTy,
95+
result: inout TestBridgedKeyTy?
96+
) {
97+
result = TestBridgedKeyTy(x.value)
98+
}
99+
100+
static func _conditionallyBridgeFromObjectiveC(
101+
_ x: TestObjCKeyTy,
102+
result: inout TestBridgedKeyTy?
103+
) -> Bool {
104+
result = TestBridgedKeyTy(x.value)
105+
return true
106+
}
107+
108+
static func _unconditionallyBridgeFromObjectiveC(_ source: TestObjCKeyTy?)
109+
-> TestBridgedKeyTy {
110+
var result: TestBridgedKeyTy? = nil
111+
_forceBridgeFromObjectiveC(source!, result: &result)
112+
return result!
113+
}
114+
115+
var value: Int
116+
}
117+
118+
func ==(x: TestBridgedKeyTy, y: TestBridgedKeyTy) -> Bool {
119+
return x.value == y.value
120+
}
121+
122+
DictionaryTraps.test("BridgedKeyIsNotNSCopyable1")
123+
.skip(.custom(
124+
{ _isFastAssertConfiguration() },
125+
reason: "this trap is not guaranteed to happen in -Ounchecked"))
126+
.crashOutputMatches("unrecognized selector sent to instance").code {
127+
// This Dictionary is bridged in O(1).
128+
var d = [ TestObjCKeyTy(10): NSObject() ]
129+
var nsd = d as NSDictionary
130+
expectCrashLater()
131+
nsd.mutableCopy()
132+
}
133+
134+
DictionaryTraps.test("BridgedKeyIsNotNSCopyable2")
135+
.skip(.custom(
136+
{ _isFastAssertConfiguration() },
137+
reason: "this trap is not guaranteed to happen in -Ounchecked"))
138+
.code {
139+
// This Dictionary is bridged in O(1).
140+
var d = [ TestObjCKeyTy(10): 10 ]
141+
var nsd = d as NSDictionary
142+
expectCrashLater()
143+
nsd.mutableCopy()
144+
}
145+
146+
DictionaryTraps.test("Downcast1") {
147+
let d: Dictionary<NSObject, NSObject> = [ TestObjCKeyTy(10): NSObject(),
148+
NSObject() : NSObject() ]
149+
let d2: Dictionary<TestObjCKeyTy, NSObject> = _dictionaryDownCast(d)
150+
expectCrashLater()
151+
let v1 = d2[TestObjCKeyTy(10)]
152+
let v2 = d2[TestObjCKeyTy(20)]
153+
154+
// This triggers failure.
155+
for (k, v) in d2 { }
156+
}
157+
158+
DictionaryTraps.test("Downcast2")
159+
.skip(.custom(
160+
{ _isFastAssertConfiguration() },
161+
reason: "this trap is not guaranteed to happen in -Ounchecked"))
162+
.code {
163+
let d: Dictionary<NSObject, NSObject> = [ TestObjCKeyTy(10): NSObject(),
164+
NSObject() : NSObject() ]
165+
166+
expectCrashLater()
167+
let d2: Dictionary<TestBridgedKeyTy, NSObject>
168+
= _dictionaryBridgeFromObjectiveC(d)
169+
let v1 = d2[TestBridgedKeyTy(10)]
170+
}
171+
172+
runAllTests()

0 commit comments

Comments
 (0)