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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
|
#if _runtime(_ObjC)
import Swift
import SwiftPrivate
import Darwin
import StdlibUnittest
import Foundation
func convertDictionaryToNSDictionary<Key, Value>(
_ d: [Key : Value]
) -> NSDictionary {
return d._bridgeToObjectiveC()
}
public func convertNSDictionaryToDictionary<
Key : Hashable, Value
>(_ d: NSDictionary?) -> [Key : Value] {
if _slowPath(d == nil) { return [:] }
var result: [Key : Value]?
Dictionary._forceBridgeFromObjectiveC(d!, result: &result)
return result!
}
func isNativeDictionary<KeyTy : Hashable, ValueTy>(
_ d: Dictionary<KeyTy, ValueTy>) -> Bool {
return d._variant.isNative
}
func isCocoaDictionary<KeyTy : Hashable, ValueTy>(
_ d: Dictionary<KeyTy, ValueTy>) -> Bool {
return !isNativeDictionary(d)
}
func isNativeNSDictionary(_ d: NSDictionary) -> Bool {
let className: NSString = NSStringFromClass(type(of: d)) as NSString
return [
"_SwiftDeferredNSDictionary",
"__EmptyDictionarySingleton",
"_DictionaryStorage"].contains {
className.range(of: $0).length > 0
}
}
func isCocoaNSDictionary(_ d: NSDictionary) -> Bool {
let className: NSString = NSStringFromClass(type(of: d)) as NSString
return className.range(of: "NSDictionary").length > 0 ||
className.range(of: "NSCFDictionary").length > 0
}
func isNativeNSArray(_ d: NSArray) -> Bool {
let className: NSString = NSStringFromClass(type(of: d)) as NSString
return ["__SwiftDeferredNSArray", "_ContiguousArray", "_EmptyArray"].contains {
className.range(of: $0).length > 0
}
}
var _objcKeyCount = _stdlib_AtomicInt(0)
var _objcKeySerial = _stdlib_AtomicInt(0)
class TestObjCKeyTy : NSObject, NSCopying {
class var objectCount: Int {
get {
return _objcKeyCount.load()
}
set {
_objcKeyCount.store(newValue)
}
}
init(_ value: Int) {
_objcKeyCount.fetchAndAdd(1)
serial = _objcKeySerial.addAndFetch(1)
self.value = value
self._hashValue = value
super.init()
}
convenience init(value: Int, hashValue: Int) {
self.init(value)
self._hashValue = hashValue
}
deinit {
assert(serial > 0, "double destruction")
_objcKeyCount.fetchAndAdd(-1)
serial = -serial
}
@objc(copyWithZone:)
func copy(with zone: NSZone?) -> Any {
return TestObjCKeyTy(value)
}
override var description: String {
assert(serial > 0, "dead TestObjCKeyTy")
return value.description
}
override func isEqual(_ object: Any!) -> Bool {
if let other = object {
if let otherObjcKey = other as? TestObjCKeyTy {
return self.value == otherObjcKey.value
}
}
return false
}
override var hash : Int {
return _hashValue
}
func _bridgeToObjectiveC() -> TestObjCKeyTy {
return self
}
var value: Int
var _hashValue: Int
var serial: Int
}
// A type that satisfies the requirements of an NSDictionary key (or an NSSet
// member), but traps when any of its methods are called.
class TestObjCInvalidKeyTy {
init() {
_objcKeyCount.fetchAndAdd(1)
serial = _objcKeySerial.addAndFetch(1)
}
deinit {
assert(serial > 0, "double destruction")
_objcKeyCount.fetchAndAdd(-1)
serial = -serial
}
@objc
var description: String {
assert(serial > 0, "dead TestObjCInvalidKeyTy")
fatalError()
}
@objc
func isEqual(_ object: Any!) -> Bool {
fatalError()
}
@objc
var hash : Int {
fatalError()
}
var serial: Int
}
var _objcValueCount = _stdlib_AtomicInt(0)
var _objcValueSerial = _stdlib_AtomicInt(0)
class TestObjCValueTy : NSObject {
class var objectCount: Int {
get {
return _objcValueCount.load()
}
set {
_objcValueCount.store(newValue)
}
}
init(_ value: Int) {
_objcValueCount.fetchAndAdd(1)
serial = _objcValueSerial.addAndFetch(1)
self.value = value
}
deinit {
assert(serial > 0, "double destruction")
_objcValueCount.fetchAndAdd(-1)
serial = -serial
}
override var description: String {
assert(serial > 0, "dead TestObjCValueTy")
return value.description
}
var value: Int
var serial: Int
}
var _objcEquatableValueCount = _stdlib_AtomicInt(0)
var _objcEquatableValueSerial = _stdlib_AtomicInt(0)
class TestObjCEquatableValueTy : NSObject {
class var objectCount: Int {
get {
return _objcEquatableValueCount.load()
}
set {
_objcEquatableValueCount.store(newValue)
}
}
init(_ value: Int) {
_objcEquatableValueCount.fetchAndAdd(1)
serial = _objcEquatableValueSerial.addAndFetch(1)
self.value = value
}
deinit {
assert(serial > 0, "double destruction")
_objcEquatableValueCount.fetchAndAdd(-1)
serial = -serial
}
override func isEqual(_ object: Any!) -> Bool {
if let other = object {
if let otherObjcKey = other as? TestObjCEquatableValueTy {
return self.value == otherObjcKey.value
}
}
return false
}
override var description: String {
assert(serial > 0, "dead TestObjCValueTy")
return value.description
}
var value: Int
var serial: Int
}
func == (lhs: TestObjCEquatableValueTy, rhs: TestObjCEquatableValueTy) -> Bool {
return lhs.value == rhs.value
}
var _bridgedKeySerial = _stdlib_AtomicInt(0)
var _bridgedKeyBridgeOperations = _stdlib_AtomicInt(0)
struct TestBridgedKeyTy
: Equatable, Hashable, CustomStringConvertible, _ObjectiveCBridgeable {
var value: Int
var _hashValue: Int
var serial: Int
static var bridgeOperations: Int {
get {
return _bridgedKeyBridgeOperations.load()
}
set {
_bridgedKeyBridgeOperations.store(newValue)
}
}
init(_ value: Int) {
serial = _bridgedKeySerial.addAndFetch(1)
self.value = value
self._hashValue = value
}
var description: String {
assert(serial > 0, "dead TestBridgedKeyTy")
return value.description
}
var hashValue: Int {
return _hashValue
}
func hash(into hasher: inout Hasher) {
hasher.combine(_hashValue)
}
func _bridgeToObjectiveC() -> TestObjCKeyTy {
_bridgedKeyBridgeOperations.fetchAndAdd(1)
return TestObjCKeyTy(value)
}
static func _forceBridgeFromObjectiveC(
_ x: TestObjCKeyTy,
result: inout TestBridgedKeyTy?
) {
_bridgedKeyBridgeOperations.fetchAndAdd(1)
result = TestBridgedKeyTy(x.value)
}
static func _conditionallyBridgeFromObjectiveC(
_ x: TestObjCKeyTy,
result: inout TestBridgedKeyTy?
) -> Bool {
self._forceBridgeFromObjectiveC(x, result: &result)
return true
}
static func _unconditionallyBridgeFromObjectiveC(_ source: TestObjCKeyTy?)
-> TestBridgedKeyTy {
var result: TestBridgedKeyTy? = nil
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
}
func == (lhs: TestBridgedKeyTy, rhs: TestBridgedKeyTy) -> Bool {
return lhs.value == rhs.value
}
func == (lhs: TestBridgedKeyTy, rhs: TestKeyTy) -> Bool {
return lhs.value == rhs.value
}
var _bridgedValueSerial = _stdlib_AtomicInt(0)
var _bridgedValueBridgeOperations = _stdlib_AtomicInt(0)
struct TestBridgedValueTy : CustomStringConvertible, _ObjectiveCBridgeable {
static var bridgeOperations: Int {
get {
return _bridgedValueBridgeOperations.load()
}
set {
_bridgedValueBridgeOperations.store(newValue)
}
}
init(_ value: Int) {
serial = _bridgedValueSerial.fetchAndAdd(1)
self.value = value
}
var description: String {
assert(serial > 0, "dead TestBridgedValueTy")
return value.description
}
func _bridgeToObjectiveC() -> TestObjCValueTy {
TestBridgedValueTy.bridgeOperations += 1
return TestObjCValueTy(value)
}
static func _forceBridgeFromObjectiveC(
_ x: TestObjCValueTy,
result: inout TestBridgedValueTy?
) {
TestBridgedValueTy.bridgeOperations += 1
result = TestBridgedValueTy(x.value)
}
static func _conditionallyBridgeFromObjectiveC(
_ x: TestObjCValueTy,
result: inout TestBridgedValueTy?
) -> Bool {
self._forceBridgeFromObjectiveC(x, result: &result)
return true
}
static func _unconditionallyBridgeFromObjectiveC(_ source: TestObjCValueTy?)
-> TestBridgedValueTy {
var result: TestBridgedValueTy? = nil
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
var value: Int
var serial: Int
}
var _bridgedEquatableValueSerial = _stdlib_AtomicInt(0)
var _bridgedEquatableValueBridgeOperations = _stdlib_AtomicInt(0)
struct TestBridgedEquatableValueTy
: Equatable, CustomStringConvertible, _ObjectiveCBridgeable {
static var bridgeOperations: Int {
get {
return _bridgedEquatableValueBridgeOperations.load()
}
set {
_bridgedEquatableValueBridgeOperations.store(newValue)
}
}
init(_ value: Int) {
serial = _bridgedEquatableValueSerial.addAndFetch(1)
self.value = value
}
var description: String {
assert(serial > 0, "dead TestBridgedValueTy")
return value.description
}
func _bridgeToObjectiveC() -> TestObjCEquatableValueTy {
_bridgedEquatableValueBridgeOperations.fetchAndAdd(1)
return TestObjCEquatableValueTy(value)
}
static func _forceBridgeFromObjectiveC(
_ x: TestObjCEquatableValueTy,
result: inout TestBridgedEquatableValueTy?
) {
_bridgedEquatableValueBridgeOperations.fetchAndAdd(1)
result = TestBridgedEquatableValueTy(x.value)
}
static func _conditionallyBridgeFromObjectiveC(
_ x: TestObjCEquatableValueTy,
result: inout TestBridgedEquatableValueTy?
) -> Bool {
self._forceBridgeFromObjectiveC(x, result: &result)
return true
}
static func _unconditionallyBridgeFromObjectiveC(
_ source: TestObjCEquatableValueTy?
) -> TestBridgedEquatableValueTy {
var result: TestBridgedEquatableValueTy? = nil
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
var value: Int
var serial: Int
}
func == (lhs: TestBridgedEquatableValueTy, rhs: TestBridgedEquatableValueTy) -> Bool {
return lhs.value == rhs.value
}
/// Expect some number of autoreleased key and value objects.
///
/// - parameter opt: applies to platforms that have the return-autoreleased
/// optimization.
///
/// - parameter unopt: applies to platforms that don't.
///
/// FIXME: Some non-zero `opt` might be cases of missed return-autorelease.
func expectAutoreleasedKeysAndValues(
opt: (Int, Int) = (0, 0), unopt: (Int, Int) = (0, 0)) {
var expectedKeys = 0
var expectedValues = 0
#if arch(i386)
(expectedKeys, expectedValues) = unopt
#else
(expectedKeys, expectedValues) = opt
#endif
TestObjCKeyTy.objectCount -= expectedKeys
TestObjCValueTy.objectCount -= expectedValues
}
/// Expect some number of autoreleased value objects.
///
/// - parameter opt: applies to platforms that have the return-autoreleased
/// optimization.
///
/// - parameter unopt: applies to platforms that don't.
///
/// FIXME: Some non-zero `opt` might be cases of missed return-autorelease.
func expectAutoreleasedValues(
opt: Int = 0, unopt: Int = 0) {
expectAutoreleasedKeysAndValues(opt: (0, opt), unopt: (0, unopt))
}
func resetLeaksOfObjCDictionaryKeysValues() {
TestObjCKeyTy.objectCount = 0
TestObjCValueTy.objectCount = 0
TestObjCEquatableValueTy.objectCount = 0
}
func expectNoLeaksOfObjCDictionaryKeysValues() {
expectEqual(0, TestObjCKeyTy.objectCount, "TestObjCKeyTy leak")
expectEqual(0, TestObjCValueTy.objectCount, "TestObjCValueTy leak")
expectEqual(
0, TestObjCEquatableValueTy.objectCount, "TestObjCEquatableValueTy leak")
}
func getBridgedNSDictionaryOfRefTypesBridgedVerbatim() -> NSDictionary {
assert(_isBridgedVerbatimToObjectiveC(TestObjCKeyTy.self))
assert(_isBridgedVerbatimToObjectiveC(TestObjCValueTy.self))
var d = Dictionary<TestObjCKeyTy, TestObjCValueTy>(minimumCapacity: 32)
d[TestObjCKeyTy(10)] = TestObjCValueTy(1010)
d[TestObjCKeyTy(20)] = TestObjCValueTy(1020)
d[TestObjCKeyTy(30)] = TestObjCValueTy(1030)
let bridged = convertDictionaryToNSDictionary(d)
assert(isNativeNSDictionary(bridged))
return bridged
}
func getBridgedEmptyNSDictionary() -> NSDictionary {
let d = Dictionary<TestObjCKeyTy, TestObjCValueTy>()
let bridged = convertDictionaryToNSDictionary(d)
assert(isNativeNSDictionary(bridged))
return bridged
}
func getBridgedNSDictionaryOfKeyValue_ValueTypesCustomBridged(
numElements: Int = 3
) -> NSDictionary {
assert(!_isBridgedVerbatimToObjectiveC(TestBridgedKeyTy.self))
assert(!_isBridgedVerbatimToObjectiveC(TestBridgedValueTy.self))
var d = Dictionary<TestBridgedKeyTy, TestBridgedValueTy>()
for i in 1..<(numElements + 1) {
d[TestBridgedKeyTy(i * 10)] = TestBridgedValueTy(i * 10 + 1000)
}
let bridged = convertDictionaryToNSDictionary(d)
assert(isNativeNSDictionary(bridged))
return bridged
}
import SlurpFastEnumeration
func slurpFastEnumerationFromSwift(
_ a: NSArray, _ fe: NSFastEnumeration, _ sink: (AnyObject) -> Void,
maxItems: Int? = nil
) {
var state = NSFastEnumerationState()
let bufferSize = 3
let buffer =
UnsafeMutableBufferPointer<AnyObject?>.allocate(capacity: bufferSize)
defer { buffer.deallocate() }
var itemsReturned = 0
while true {
let returnedCount = fe.countByEnumerating(
with: &state,
objects: AutoreleasingUnsafeMutablePointer(buffer.baseAddress!),
count: buffer.count)
expectNotEqual(0, state.state)
expectNotNil(state.mutationsPtr)
if returnedCount == 0 {
break
}
for i in 0..<returnedCount {
let value: AnyObject = state.itemsPtr![i]!
sink(value)
itemsReturned += 1
}
if maxItems != nil && itemsReturned >= maxItems! {
return
}
}
for _ in 0..<3 {
let returnedCount = fe.countByEnumerating(
with: &state,
objects: AutoreleasingUnsafeMutablePointer(buffer.baseAddress!),
count: buffer.count)
expectNotEqual(0, state.state)
expectNotNil(state.mutationsPtr)
expectEqual(0, returnedCount)
}
}
typealias AnyObjectTuple2 = (AnyObject, AnyObject)
func slurpFastEnumerationFromSwift(
_ d: NSDictionary, _ fe: NSFastEnumeration, _ sink: (AnyObjectTuple2) -> Void,
maxItems: Int? = nil
) {
var state = NSFastEnumerationState()
let bufferSize = 3
let buffer =
UnsafeMutableBufferPointer<AnyObject?>.allocate(capacity: bufferSize)
defer { buffer.deallocate() }
var itemsReturned = 0
while true {
let returnedCount = fe.countByEnumerating(
with: &state,
objects: AutoreleasingUnsafeMutablePointer(buffer.baseAddress!),
count: buffer.count)
expectNotEqual(0, state.state)
expectNotNil(state.mutationsPtr)
if returnedCount == 0 {
break
}
for i in 0..<returnedCount {
let key: AnyObject = state.itemsPtr![i]!
let value: AnyObject = d.object(forKey: key)! as AnyObject
let kv = (key, value)
sink(kv)
itemsReturned += 1
}
if maxItems != nil && itemsReturned >= maxItems! {
return
}
}
for _ in 0..<3 {
let returnedCount = fe.countByEnumerating(
with: &state,
objects: AutoreleasingUnsafeMutablePointer(buffer.baseAddress!),
count: buffer.count)
expectEqual(0, returnedCount)
}
}
func slurpFastEnumerationOfNSEnumeratorFromSwift(
_ a: NSArray, _ enumerator: NSEnumerator, _ sink: (AnyObject) -> Void,
maxFastEnumerationItems: Int
) {
slurpFastEnumerationFromSwift(
a, enumerator, sink, maxItems: maxFastEnumerationItems)
while let value = enumerator.nextObject() {
sink(value as AnyObject)
}
}
func slurpFastEnumerationOfNSEnumeratorFromSwift(
_ d: NSDictionary, _ enumerator: NSEnumerator,
_ sink: (AnyObjectTuple2) -> Void,
maxFastEnumerationItems: Int
) {
slurpFastEnumerationFromSwift(
d, enumerator, sink, maxItems: maxFastEnumerationItems)
while let key = enumerator.nextObject() {
let value: AnyObject = d.object(forKey: key)! as AnyObject
let kv = (key as AnyObject, value)
sink(kv)
}
}
func slurpFastEnumerationFromObjC(
_ a: NSArray, _ fe: NSFastEnumeration, _ sink: (AnyObject) -> Void
) {
let objcValues = NSMutableArray()
slurpFastEnumerationOfArrayFromObjCImpl(a, fe, objcValues)
for value in objcValues {
sink(value as AnyObject)
}
}
func _checkArrayFastEnumerationImpl(
_ expected: [Int],
_ a: NSArray,
_ makeEnumerator: () -> NSFastEnumeration,
_ useEnumerator: (NSArray, NSFastEnumeration, (AnyObject) -> ()) -> Void,
_ convertValue: @escaping (AnyObject) -> Int
) {
let expectedContentsWithoutIdentity =
_makeExpectedArrayContents(expected)
var expectedContents = [ExpectedArrayElement]()
for i in 0..<3 {
var actualContents = [ExpectedArrayElement]()
let sink = {
(value: AnyObject) in
actualContents.append(ExpectedArrayElement(
value: convertValue(value),
valueIdentity: unsafeBitCast(value, to: UInt.self)))
}
useEnumerator(a, makeEnumerator(), sink)
expectTrue(
_equalsWithoutElementIdentity(
expectedContentsWithoutIdentity, actualContents),
"expected: \(expectedContentsWithoutIdentity)\n" +
"actual: \(actualContents)\n")
if i == 0 {
expectedContents = actualContents
}
expectEqualSequence(expectedContents, actualContents)
}
}
func checkArrayFastEnumerationFromSwift(
_ expected: [Int],
_ a: NSArray, _ makeEnumerator: () -> NSFastEnumeration,
_ convertValue: @escaping (AnyObject) -> Int
) {
_checkArrayFastEnumerationImpl(
expected, a, makeEnumerator,
{ (a, fe, sink) in
slurpFastEnumerationFromSwift(a, fe, sink)
},
convertValue)
}
func checkArrayFastEnumerationFromObjC(
_ expected: [Int],
_ a: NSArray, _ makeEnumerator: () -> NSFastEnumeration,
_ convertValue: @escaping (AnyObject) -> Int
) {
_checkArrayFastEnumerationImpl(
expected, a, makeEnumerator,
{ (a, fe, sink) in
slurpFastEnumerationFromObjC(a, fe, sink)
},
convertValue)
}
func checkArrayEnumeratorPartialFastEnumerationFromSwift(
_ expected: [Int],
_ a: NSArray,
maxFastEnumerationItems: Int,
_ convertValue: @escaping (AnyObject) -> Int
) {
_checkArrayFastEnumerationImpl(
expected, a, { a.objectEnumerator() },
{ (a, fe, sink) in
slurpFastEnumerationOfNSEnumeratorFromSwift(
a, fe as! NSEnumerator, sink,
maxFastEnumerationItems: maxFastEnumerationItems)
},
convertValue)
}
func _checkSetFastEnumerationImpl(
_ expected: [Int],
_ s: NSSet,
_ makeEnumerator: () -> NSFastEnumeration,
_ useEnumerator: (NSSet, NSFastEnumeration, (AnyObject) -> ()) -> Void,
_ convertMember: @escaping (AnyObject) -> Int
) {
let expectedContentsWithoutIdentity =
_makeExpectedSetContents(expected)
var expectedContents = [ExpectedSetElement]()
for i in 0..<3 {
var actualContents = [ExpectedSetElement]()
let sink = {
(value: AnyObject) in
actualContents.append(ExpectedSetElement(
value: convertMember(value),
valueIdentity: unsafeBitCast(value, to: UInt.self)))
}
useEnumerator(s, makeEnumerator(), sink)
expectTrue(
_equalsUnorderedWithoutElementIdentity(
expectedContentsWithoutIdentity, actualContents),
"expected: \(expectedContentsWithoutIdentity)\n" +
"actual: \(actualContents)\n")
if i == 0 {
expectedContents = actualContents
}
expectTrue(equalsUnordered(expectedContents, actualContents))
}
}
func slurpFastEnumerationFromObjC(
_ s: NSSet, _ fe: NSFastEnumeration, _ sink: (AnyObject) -> Void
) {
let objcValues = NSMutableArray()
slurpFastEnumerationOfArrayFromObjCImpl(s, fe, objcValues)
for value in objcValues {
sink(value as AnyObject)
}
}
func slurpFastEnumerationOfNSEnumeratorFromSwift(
_ s: NSSet, _ enumerator: NSEnumerator, _ sink: (AnyObject) -> Void,
maxFastEnumerationItems: Int
) {
slurpFastEnumerationFromSwift(
s, enumerator, sink, maxItems: maxFastEnumerationItems)
while let value = enumerator.nextObject() {
sink(value as AnyObject)
}
}
func slurpFastEnumerationFromSwift(
_ s: NSSet, _ fe: NSFastEnumeration, _ sink: (AnyObject) -> Void,
maxItems: Int? = nil
) {
var state = NSFastEnumerationState()
let bufferSize = 3
let buffer =
UnsafeMutableBufferPointer<AnyObject?>.allocate(capacity: bufferSize)
defer { buffer.deallocate() }
var itemsReturned = 0
while true {
let returnedCount = fe.countByEnumerating(
with: &state,
objects: AutoreleasingUnsafeMutablePointer(buffer.baseAddress!),
count: buffer.count)
expectNotEqual(0, state.state)
expectNotNil(state.mutationsPtr)
if returnedCount == 0 {
break
}
for i in 0..<returnedCount {
let value: AnyObject = state.itemsPtr![i]!
sink(value)
itemsReturned += 1
}
if maxItems != nil && itemsReturned >= maxItems! {
return
}
}
for _ in 0..<3 {
let returnedCount = fe.countByEnumerating(
with: &state,
objects: AutoreleasingUnsafeMutablePointer(buffer.baseAddress!),
count: buffer.count)
expectNotEqual(0, state.state)
expectNotNil(state.mutationsPtr)
expectEqual(0, returnedCount)
}
}
func checkSetFastEnumerationFromSwift(
_ expected: [Int],
_ s: NSSet, _ makeEnumerator: () -> NSFastEnumeration,
_ convertMember: @escaping (AnyObject) -> Int
) {
_checkSetFastEnumerationImpl(
expected, s, makeEnumerator,
{ (s, fe, sink) in
slurpFastEnumerationFromSwift(s, fe, sink)
},
convertMember)
}
func checkSetFastEnumerationFromObjC(
_ expected: [Int],
_ s: NSSet, _ makeEnumerator: () -> NSFastEnumeration,
_ convertMember: @escaping (AnyObject) -> Int
) {
_checkSetFastEnumerationImpl(
expected, s, makeEnumerator,
{ (s, fe, sink) in
slurpFastEnumerationFromObjC(s, fe, sink)
},
convertMember)
}
func checkSetEnumeratorPartialFastEnumerationFromSwift(
_ expected: [Int],
_ s: NSSet,
maxFastEnumerationItems: Int,
_ convertMember: @escaping (AnyObject) -> Int
) {
_checkSetFastEnumerationImpl(
expected, s, { s.objectEnumerator() },
{ (s, fe, sink) in
slurpFastEnumerationOfNSEnumeratorFromSwift(
s, fe as! NSEnumerator, sink,
maxFastEnumerationItems: maxFastEnumerationItems)
},
convertMember)
}
func slurpFastEnumerationFromObjC(
_ d: NSDictionary, _ fe: NSFastEnumeration, _ sink: (AnyObjectTuple2) -> Void
) {
let objcPairs = NSMutableArray()
slurpFastEnumerationOfDictionaryFromObjCImpl(d, fe, objcPairs)
for i in 0..<objcPairs.count/2 {
let key = objcPairs[i * 2] as AnyObject
let value = objcPairs[i * 2 + 1] as AnyObject
let kv = (key, value)
sink(kv)
}
}
func _checkDictionaryFastEnumerationImpl(
_ expected: [(Int, Int)],
_ d: NSDictionary,
_ makeEnumerator: () -> NSFastEnumeration,
_ useEnumerator: (NSDictionary, NSFastEnumeration, (AnyObjectTuple2) -> ()) -> Void,
_ convertKey: @escaping (AnyObject) -> Int,
_ convertValue: @escaping (AnyObject) -> Int
) {
let expectedContentsWithoutIdentity =
_makeExpectedDictionaryContents(expected)
var expectedContents = [ExpectedDictionaryElement]()
for i in 0..<3 {
var actualContents = [ExpectedDictionaryElement]()
let sink: (AnyObjectTuple2) -> Void = {
let (key, value) = $0
actualContents.append(ExpectedDictionaryElement(
key: convertKey(key),
value: convertValue(value),
keyIdentity: unsafeBitCast(key, to: UInt.self),
valueIdentity: unsafeBitCast(value, to: UInt.self)))
}
useEnumerator(d, makeEnumerator(), sink)
expectTrue(
_equalsUnorderedWithoutElementIdentity(
expectedContentsWithoutIdentity, actualContents),
"expected: \(expectedContentsWithoutIdentity)\n" +
"actual: \(actualContents)\n")
if i == 0 {
expectedContents = actualContents
}
expectTrue(equalsUnordered(expectedContents, actualContents))
}
}
func checkDictionaryFastEnumerationFromSwift(
_ expected: [(Int, Int)],
_ d: NSDictionary, _ makeEnumerator: () -> NSFastEnumeration,
_ convertKey: @escaping (AnyObject) -> Int,
_ convertValue: @escaping (AnyObject) -> Int
) {
_checkDictionaryFastEnumerationImpl(
expected, d, makeEnumerator,
{ (d, fe, sink) in
slurpFastEnumerationFromSwift(d, fe, sink)
},
convertKey, convertValue)
}
func checkDictionaryFastEnumerationFromObjC(
_ expected: [(Int, Int)],
_ d: NSDictionary, _ makeEnumerator: () -> NSFastEnumeration,
_ convertKey: @escaping (AnyObject) -> Int,
_ convertValue: @escaping (AnyObject) -> Int
) {
_checkDictionaryFastEnumerationImpl(
expected, d, makeEnumerator,
{ (d, fe, sink) in
slurpFastEnumerationFromObjC(d, fe, sink)
},
convertKey, convertValue)
}
func checkDictionaryEnumeratorPartialFastEnumerationFromSwift(
_ expected: [(Int, Int)],
_ d: NSDictionary,
maxFastEnumerationItems: Int,
_ convertKey: @escaping (AnyObject) -> Int,
_ convertValue: @escaping (AnyObject) -> Int
) {
_checkDictionaryFastEnumerationImpl(
expected, d, { d.keyEnumerator() },
{ (d, fe, sink) in
slurpFastEnumerationOfNSEnumeratorFromSwift(
d, fe as! NSEnumerator, sink,
maxFastEnumerationItems: maxFastEnumerationItems)
},
convertKey, convertValue)
}
func getBridgedNSArrayOfRefTypeVerbatimBridged(
numElements: Int = 3,
capacity: Int? = nil
) -> NSArray {
assert(_isBridgedVerbatimToObjectiveC(TestObjCValueTy.self))
var a = [TestObjCValueTy]()
if let requestedCapacity = capacity {
a.reserveCapacity(requestedCapacity)
}
for i in 1..<(numElements + 1) {
a.append(TestObjCValueTy(i * 10))
}
let bridged = convertArrayToNSArray(a)
assert(isNativeNSArray(bridged))
return bridged
}
func convertNSArrayToArray<T>(_ source: NSArray?) -> [T] {
if _slowPath(source == nil) { return [] }
var result: [T]?
Array._forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
func convertArrayToNSArray<T>(_ array: [T]) -> NSArray {
return array._bridgeToObjectiveC()
}
func getBridgedNSArrayOfValueTypeCustomBridged(
numElements: Int = 3,
capacity: Int? = nil
) -> NSArray {
assert(!_isBridgedVerbatimToObjectiveC(TestBridgedValueTy.self))
var a = [TestBridgedValueTy]()
if let requestedCapacity = capacity {
a.reserveCapacity(requestedCapacity)
}
for i in 1..<(numElements + 1) {
a.append(TestBridgedValueTy(i * 10))
}
let bridged = convertArrayToNSArray(a)
assert(isNativeNSArray(bridged))
return bridged
}
#endif
|