File: SwiftNativeNSBase.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (98 lines) | stat: -rw-r--r-- 4,369 bytes parent folder | download
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
//===--- SwiftNativeNSBase.swift - Test __SwiftNativeNS*Base classes -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// RUN: %empty-directory(%t)
// 
// RUN: %target-clang %S/Inputs/SwiftNativeNSBase/SwiftNativeNSBase.m -c -o %t/SwiftNativeNSBase.o -g
// RUN: %target-clang -fobjc-arc %S/Inputs/SlurpFastEnumeration/SlurpFastEnumeration.m -c -o %t/SlurpFastEnumeration.o
// RUN: echo '#sourceLocation(file: "%s", line: 1)' > "%t/main.swift" && cat "%s" >> "%t/main.swift" && chmod -w "%t/main.swift"
// RUN: %target-build-swift -Xfrontend -disable-access-control  %t/main.swift %S/Inputs/DictionaryKeyValueTypes.swift %S/Inputs/DictionaryKeyValueTypesObjC.swift -I %S/Inputs/SwiftNativeNSBase/ -I %S/Inputs/SlurpFastEnumeration/ -Xlinker %t/SlurpFastEnumeration.o -Xlinker %t/SwiftNativeNSBase.o -o %t/SwiftNativeNSBase -swift-version 4.2
// RUN: %target-codesign %t/SwiftNativeNSBase
// RUN: %target-run %t/SwiftNativeNSBase
// REQUIRES: executable_test

// REQUIRES: objc_interop

// The oldest ABI-stable stdlibs don't have a __SwiftNativeNSMutableArrayBase
// class, so they can't run the UnwantedCdtors test.
// FIXME: This should be based on a runtime library version check.
// UNSUPPORTED: use_os_stdlib

import Foundation
import StdlibUnittest

@_silgen_name("TestSwiftNativeNSBase_UnwantedCdtors") 
func TestSwiftNativeNSBase_UnwantedCdtors() -> Bool
@_silgen_name("TestSwiftNativeNSBase_RetainCount") 
func TestSwiftNativeNSBase_RetainCount(_: UnsafeMutableRawPointer) -> Bool

func classChain(of cls: AnyClass) -> [String] {
  var chain: [String] = []
  var cls: AnyClass? = cls
  while cls != nil {
    chain.append(NSStringFromClass(cls!))
    cls = class_getSuperclass(cls)
  }
  return chain
}

var SwiftNativeNSBaseTestSuite = TestSuite("SwiftNativeNSBase")

SwiftNativeNSBaseTestSuite.test("UnwantedCdtors")
  .skip(.osxMinorRange(10, 0...15, reason: "lazy objc naming is not available on these OSes"))
  .skip(.iOSMajorRange(0...13, reason: "lazy objc naming is not available on these OSes"))
  .skip(.tvOSMajorRange(0...13, reason: "lazy objc naming is not available on these OSes"))
  .skip(.watchOSMajorRange(0...6, reason: "lazy objc naming is not available on these OSes"))
  .skip(.iOSSimulatorAny(/*Range(0...13), reason: */"lazy objc naming is not available on these OSes"))
  .skip(.tvOSSimulatorAny(/*TODO: 0...13, reason: */"lazy objc naming is not available on these OSes"))
  .skip(.watchOSSimulatorAny(/*TODO: 0...6, reason: */"lazy objc naming is not available on these OSes"))
  .skip(.always("rdar://78931257 This test crashes on macOS 12"))
  .code {
  expectTrue(TestSwiftNativeNSBase_UnwantedCdtors())
}

SwiftNativeNSBaseTestSuite.test("__SwiftNativeNSArrayBase.retainCount") {
  let bridged = getBridgedNSArrayOfRefTypeVerbatimBridged()
  assert(classChain(of: type(of: bridged)).contains("__SwiftNativeNSArrayBase"))
  expectTrue(TestSwiftNativeNSBase_RetainCount(
      Unmanaged.passUnretained(bridged).toOpaque()))
  _fixLifetime(bridged)
}

SwiftNativeNSBaseTestSuite.test("__SwiftNativeNSDictionaryBase.retainCount") {
  let bridged = getBridgedNSDictionaryOfRefTypesBridgedVerbatim()
  assert(classChain(of: type(of: bridged))
    .contains("__SwiftNativeNSDictionaryBase"))
  expectTrue(TestSwiftNativeNSBase_RetainCount(
      Unmanaged.passUnretained(bridged).toOpaque()))
  _fixLifetime(bridged)
}

SwiftNativeNSBaseTestSuite.test("__SwiftNativeNSSetBase.retainCount") {
  let bridged = Set([10, 20, 30].map{ TestObjCKeyTy($0) })._bridgeToObjectiveC()
  assert(classChain(of: type(of: bridged)).contains("__SwiftNativeNSSetBase"))
  expectTrue(TestSwiftNativeNSBase_RetainCount(
      Unmanaged.passUnretained(bridged).toOpaque()))
  _fixLifetime(bridged)
}

SwiftNativeNSBaseTestSuite.setUp {
  resetLeaksOfDictionaryKeysValues()
  resetLeaksOfObjCDictionaryKeysValues()
}

SwiftNativeNSBaseTestSuite.tearDown {
  expectNoLeaksOfDictionaryKeysValues()
  expectNoLeaksOfObjCDictionaryKeysValues()
}

runAllTests()