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
|
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
// UNSUPPORTED: OS=watchos
import Foundation
//===----------------------------------------------------------------------===//
// NSObject ==
//===----------------------------------------------------------------------===//
func printEquality<T : Equatable>(_ lhs: T, _ rhs: T, _ lhsName: String, _ rhsName: String) {
if lhs == lhs {
print("\(lhsName) == \(lhsName)")
}
if lhs != lhs {
print("\(lhsName) != \(lhsName)")
}
if lhs == rhs {
print("\(lhsName) == \(rhsName)")
}
if lhs != rhs {
print("\(lhsName) != \(rhsName)")
}
}
func printIdentity(_ lhs: AnyObject, _ rhs: AnyObject, _ lhsName: String, _ rhsName: String) {
if lhs === lhs {
print("\(lhsName) === \(lhsName)")
}
if lhs !== lhs {
print("\(lhsName) !== \(lhsName)")
}
if lhs === rhs {
print("\(lhsName) === \(rhsName)")
}
if lhs !== rhs {
print("\(lhsName) !== \(rhsName)")
}
}
print("NoisyEqual ==")
class NoisyEqual : NSObject {
override func isEqual(_ rhs: Any?) -> Bool {
print("wow much equal")
return super.isEqual(rhs)
}
}
let n1 = NoisyEqual.init()
let n2 = NoisyEqual.init()
printIdentity(n1, n2, "n1", "n2")
printEquality(n1, n2, "n1", "n2")
print("done NoisyEqual ==")
// CHECK: NoisyEqual ==
// CHECK-NEXT: n1 === n1
// CHECK-NEXT: n1 !== n2
// CHECK-NEXT: wow much equal
// CHECK-NEXT: n1 == n1
// CHECK-NEXT: wow much equal
// CHECK-NEXT: wow much equal
// CHECK-NEXT: wow much equal
// CHECK-NEXT: n1 != n2
// CHECK-NEXT: done NoisyEqual ==
print("NSObject ==")
let o1 = NSObject.init()
let o2 = NSObject.init()
printIdentity(o1, o2, "o1", "o2")
printEquality(o1, o2, "o1", "o2")
printIdentity(o1, 10 as NSNumber, "o1", "10")
printEquality(o1, 10 as NSNumber, "o1", "10")
printIdentity(10 as NSNumber, o1, "10", "o1")
printEquality(10 as NSNumber, o1, "10", "o1")
print("done NSObject ==")
// CHECK: NSObject ==
// CHECK-NEXT: o1 === o1
// CHECK-NEXT: o1 !== o2
// CHECK-NEXT: o1 == o1
// CHECK-NEXT: o1 != o2
// CHECK-NEXT: o1 === o1
// CHECK-NEXT: o1 !== 10
// CHECK-NEXT: o1 == o1
// CHECK-NEXT: o1 != 10
// CHECK-NEXT: 10 === 10
// CHECK-NEXT: 10 !== o1
// CHECK-NEXT: 10 == 10
// CHECK-NEXT: 10 != o1
// CHECK: done NSObject ==
print("NSMutableString ==")
let s1 = NSMutableString.init(string:"hazcam")
let s2 = NSMutableString.init(string:"hazcam")
printIdentity(s1, s2, "s1", "s2")
printEquality(s1, s2, "s1", "s2")
print("mutate")
s2.append("navcam")
printIdentity(s1, s2, "s1", "s2")
printEquality(s1, s2, "s1", "s2")
print("done NSMutableString ==")
// CHECK: NSMutableString ==
// CHECK-NEXT: s1 === s1
// CHECK-NEXT: s1 !== s2
// CHECK-NEXT: s1 == s1
// CHECK-NEXT: s1 == s2
// CHECK-NEXT: mutate
// CHECK-NEXT: s1 === s1
// CHECK-NEXT: s1 !== s2
// CHECK-NEXT: s1 == s1
// CHECK-NEXT: s1 != s2
// CHECK-NEXT: done NSMutableString ==
//===----------------------------------------------------------------------===//
// NSObject hashValue
//===----------------------------------------------------------------------===//
func printHashValue<T : Hashable>(_ x: T, _ name: String) {
print("\(name) hashes to \(x.hashValue)")
}
print("NSMutableString hashValue")
print("\(s1.hashValue)")
print("\(s1.hash)")
s1.append("pancam")
print("\(s1.hashValue)")
print("\(s1.hash)")
print("done NSMutableString hashValue")
// CHECK: NSMutableString hashValue
// CHECK-NEXT: [[H1:(-)?[0-9]+]]
// CHECK-NEXT: [[H1]]
// CHECK-NEXT: [[H2:(-)?[0-9]+]]
// CHECK-NEXT: [[H2]]
// CHECK-NEXT: done NSMutableString hashValue
class NoisyHash : NSObject {
override var hash : Int {
print("so hash")
return super.hash
}
}
print("NoisyHash hashValue")
let nh = NoisyHash.init()
printHashValue(nh, "nh")
print("done NoisyHash hashValue")
// CHECK: NoisyHash hashValue
// CHECK-NEXT: so hash
// CHECK-NEXT: nh hashes to {{(-)?[0-9]+}}
// CHECK: done NoisyHash hashValue
class ValueLike : NSObject {
var x: Int
init(int: Int) {
x = int
super.init()
}
override func isEqual(_ rhs: Any?) -> Bool {
if let rhs2 = rhs as? ValueLike {
return x == rhs2.x
}
return false
}
override var hash : Int {
return x
}
}
print("ValueLike hashValue")
let sh1 = ValueLike.init(int:10)
let sh2 = ValueLike.init(int:20)
let sh3 = ValueLike.init(int:10)
printIdentity(sh1, sh2, "sh1", "sh2")
printIdentity(sh1, sh3, "sh1", "sh3")
printIdentity(sh2, sh3, "sh2", "sh3")
printEquality(sh1, sh2, "sh1", "sh2")
printEquality(sh1, sh3, "sh1", "sh3")
printEquality(sh2, sh3, "sh2", "sh3")
printEquality(sh1.hashValue, sh2.hashValue, "sh1 hash", "sh2 hash")
printEquality(sh1.hashValue, sh3.hashValue, "sh1 hash", "sh3 hash")
printEquality(sh2.hashValue, sh3.hashValue, "sh2 hash", "sh3 hash")
var dict = Dictionary<ValueLike, Int>()
dict[sh1] = sh1.x
dict[sh2] = sh2.x
print("sh1 \(dict[sh1]!)")
print("sh2 \(dict[sh2]!)")
print("sh3 \(dict[sh3]!)")
print("done ValueLike hashValue")
// CHECK: ValueLike hashValue
// CHECK-NEXT: sh1 === sh1
// CHECK-NEXT: sh1 !== sh2
// CHECK-NEXT: sh1 === sh1
// CHECK-NEXT: sh1 !== sh3
// CHECK-NEXT: sh2 === sh2
// CHECK-NEXT: sh2 !== sh3
// CHECK-NEXT: sh1 == sh1
// CHECK-NEXT: sh1 != sh2
// CHECK-NEXT: sh1 == sh1
// CHECK-NEXT: sh1 == sh3
// CHECK-NEXT: sh2 == sh2
// CHECK-NEXT: sh2 != sh3
// CHECK-NEXT: sh1 hash == sh1 hash
// CHECK-NEXT: sh1 hash != sh2 hash
// CHECK-NEXT: sh1 hash == sh1 hash
// CHECK-NEXT: sh1 hash == sh3 hash
// CHECK-NEXT: sh2 hash == sh2 hash
// CHECK-NEXT: sh2 hash != sh3 hash
// CHECK-NEXT: sh1 10
// CHECK-NEXT: sh2 20
// CHECK-NEXT: sh3 10
// CHECK-NEXT: done ValueLike hashValue
// Native Swift objects should not have nontrivial structors from ObjC's point
// of view.
class NativeSwift {}
class GenericNativeSwift<T> {}
var native: AnyObject = NativeSwift()
if native.responds(to: ".cxx_construct") {
print("SwiftObject has nontrivial constructor")
} else {
print("no nontrivial constructor") // CHECK-NEXT: no nontrivial constructor
}
if native.responds(to: ".cxx_destruct") {
print("SwiftObject has nontrivial destructor")
} else {
print("no nontrivial destructor") // CHECK-NEXT: no nontrivial destructor
}
native = GenericNativeSwift<Int>()
if native.responds(to: ".cxx_construct") {
print("SwiftObject has nontrivial constructor")
} else {
print("no nontrivial constructor") // CHECK-NEXT: no nontrivial constructor
}
if native.responds(to: ".cxx_destruct") {
print("SwiftObject has nontrivial destructor")
} else {
print("no nontrivial destructor") // CHECK-NEXT: no nontrivial destructor
}
class D : NSObject {}
print(D.self) // CHECK-NEXT: D
print(_getSuperclass(D.self) == NSObject.self) // CHECK-NEXT: true
print(_getSuperclass(_getSuperclass(D.self)!) == nil) // CHECK-NEXT: true
class E : NSString {}
print( // CHECK-NEXT: true
_getSuperclass(E.self) == NSString.self)
print( // CHECK-NEXT: true
_getSuperclass(_getSuperclass(E.self)!) == NSObject.self)
print( // CHECK-NEXT: true
_getSuperclass(_getSuperclass(_getSuperclass(E.self)!)!) == nil)
print("NSObject's type id")
print(CFGetTypeID(NSObject()))
// CHECK: NSObject's type id
// CHECK-NEXT: 1
|