Merge pull request #1440 from compnerd/closure
closure: make it build on Windows x86_64
diff --git a/CoreFoundation/PlugIn.subproj/CFBundle_Executable.c b/CoreFoundation/PlugIn.subproj/CFBundle_Executable.c
index 64d0f05..568a6e4 100644
--- a/CoreFoundation/PlugIn.subproj/CFBundle_Executable.c
+++ b/CoreFoundation/PlugIn.subproj/CFBundle_Executable.c
@@ -207,13 +207,15 @@
if (lookupMainExe && bundle && bundle->_isFHSInstalledBundle) {
// For a FHS installed bundle, the URL points to share/Bundle.resources, and the binary is in:
- CFURLRef prefix = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorSystemDefault, url);
-
+ CFURLRef sharePath = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorSystemDefault, url);
+ CFURLRef prefixPath = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorSystemDefault, sharePath);
+ CFRelease(sharePath);
+
CFStringRef directories[] = { _CFBundleFHSDirectoriesInExecutableSearchOrder };
size_t directoriesCount = sizeof(directories) / sizeof(directories[0]);
for (size_t i = 0; i < directoriesCount; i++) {
- CFURLRef where = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, prefix, directories[i], true);
+ CFURLRef where = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, prefixPath, directories[i], true);
executableURL = _CFBundleCopyExecutableURLRaw(where, executableName);
CFRelease(where);
@@ -223,7 +225,7 @@
}
}
- CFRelease(prefix);
+ CFRelease(prefixPath);
}
#endif // !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
@@ -290,11 +292,7 @@
if (lookupMainExe && !ignoreCache && bundle && executableURL) {
// We found it. Cache the path.
CFURLRef absURL = CFURLCopyAbsoluteURL(executableURL);
-#if DEPLOYMENT_TARGET_WINDOWS
- executablePath = CFURLCopyFileSystemPath(absURL, kCFURLWindowsPathStyle);
-#else
- executablePath = CFURLCopyFileSystemPath(absURL, kCFURLPOSIXPathStyle);
-#endif
+ executablePath = CFURLCopyFileSystemPath(absURL, PLATFORM_PATH_STYLE);
CFRelease(absURL);
__CFLock(&bundle->_lock);
bundle->_executablePath = (CFStringRef)CFRetain(executablePath);
diff --git a/Foundation/Bundle.swift b/Foundation/Bundle.swift
index 5c66d37..921126a 100644
--- a/Foundation/Bundle.swift
+++ b/Foundation/Bundle.swift
@@ -115,8 +115,8 @@
return CFBundleCopyExecutableURL(_bundle)?._swiftObject
}
- open func url(forAuxiliaryExecutable executableName: String) -> NSURL? {
- return CFBundleCopyAuxiliaryExecutableURL(_bundle, executableName._cfObject)?._nsObject
+ open func url(forAuxiliaryExecutable executableName: String) -> URL? {
+ return CFBundleCopyAuxiliaryExecutableURL(_bundle, executableName._cfObject)?._swiftObject
}
open var privateFrameworksURL: URL? {
diff --git a/Foundation/NSDictionary.swift b/Foundation/NSDictionary.swift
index 4dd44e6..ad5675a 100644
--- a/Foundation/NSDictionary.swift
+++ b/Foundation/NSDictionary.swift
@@ -586,7 +586,7 @@
guard type(of: self) === NSDictionary.self || type(of: self) === NSMutableDictionary.self else {
NSRequiresConcreteImplementation()
}
- _storage[(aKey as! NSObject)] = _SwiftValue.store(anObject)
+ _storage[_SwiftValue.store(aKey)] = _SwiftValue.store(anObject)
}
public convenience required init() {
diff --git a/Foundation/NSIndexSet.swift b/Foundation/NSIndexSet.swift
index 3b03f3b..5214318 100644
--- a/Foundation/NSIndexSet.swift
+++ b/Foundation/NSIndexSet.swift
@@ -589,7 +589,7 @@
let nextRange = _ranges[rangeIndex + 1]
let currentEnd = currentRange.location + currentRange.length
let nextEnd = nextRange.location + nextRange.length
- if nextEnd >= nextRange.location {
+ if currentEnd >= nextRange.location {
// overlaps
if currentEnd < nextEnd {
// next range extends beyond current range
diff --git a/Foundation/NSPathUtilities.swift b/Foundation/NSPathUtilities.swift
index 92808ad..d4d617f 100755
--- a/Foundation/NSPathUtilities.swift
+++ b/Foundation/NSPathUtilities.swift
@@ -103,25 +103,23 @@
internal func _stringByFixingSlashes(compress : Bool = true, stripTrailing: Bool = true) -> String {
var result = self
if compress {
- result.withMutableCharacters { characterView in
- let startPos = characterView.startIndex
- var endPos = characterView.endIndex
- var curPos = startPos
-
- while curPos < endPos {
- if characterView[curPos] == "/" {
- var afterLastSlashPos = curPos
- while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
- afterLastSlashPos = characterView.index(after: afterLastSlashPos)
- }
- if afterLastSlashPos != characterView.index(after: curPos) {
- characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
- endPos = characterView.endIndex
- }
- curPos = afterLastSlashPos
- } else {
- curPos = characterView.index(after: curPos)
+ let startPos = result.startIndex
+ var endPos = result.endIndex
+ var curPos = startPos
+
+ while curPos < endPos {
+ if result[curPos] == "/" {
+ var afterLastSlashPos = curPos
+ while afterLastSlashPos < endPos && result[afterLastSlashPos] == "/" {
+ afterLastSlashPos = result.index(after: afterLastSlashPos)
}
+ if afterLastSlashPos != result.index(after: curPos) {
+ result.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
+ endPos = result.endIndex
+ }
+ curPos = afterLastSlashPos
+ } else {
+ curPos = result.index(after: curPos)
}
}
}
@@ -214,25 +212,23 @@
var result = _swiftObject
if compress {
- result.withMutableCharacters { characterView in
- let startPos = characterView.startIndex
- var endPos = characterView.endIndex
- var curPos = startPos
-
- while curPos < endPos {
- if characterView[curPos] == "/" {
- var afterLastSlashPos = curPos
- while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
- afterLastSlashPos = characterView.index(after: afterLastSlashPos)
- }
- if afterLastSlashPos != characterView.index(after: curPos) {
- characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
- endPos = characterView.endIndex
- }
- curPos = afterLastSlashPos
- } else {
- curPos = characterView.index(after: curPos)
+ let startPos = result.startIndex
+ var endPos = result.endIndex
+ var curPos = startPos
+
+ while curPos < endPos {
+ if result[curPos] == "/" {
+ var afterLastSlashPos = curPos
+ while afterLastSlashPos < endPos && result[afterLastSlashPos] == "/" {
+ afterLastSlashPos = result.index(after: afterLastSlashPos)
}
+ if afterLastSlashPos != result.index(after: curPos) {
+ result.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
+ endPos = result.endIndex
+ }
+ curPos = afterLastSlashPos
+ } else {
+ curPos = result.index(after: curPos)
}
}
}
diff --git a/Foundation/NSURL.swift b/Foundation/NSURL.swift
index 62f803e..fa094f2 100644
--- a/Foundation/NSURL.swift
+++ b/Foundation/NSURL.swift
@@ -719,25 +719,23 @@
var result = p
if compress {
- result.withMutableCharacters { characterView in
- let startPos = characterView.startIndex
- var endPos = characterView.endIndex
- var curPos = startPos
+ let startPos = result.startIndex
+ var endPos = result.endIndex
+ var curPos = startPos
- while curPos < endPos {
- if characterView[curPos] == "/" {
- var afterLastSlashPos = curPos
- while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
- afterLastSlashPos = characterView.index(after: afterLastSlashPos)
- }
- if afterLastSlashPos != characterView.index(after: curPos) {
- characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
- endPos = characterView.endIndex
- }
- curPos = afterLastSlashPos
- } else {
- curPos = characterView.index(after: curPos)
+ while curPos < endPos {
+ if result[curPos] == "/" {
+ var afterLastSlashPos = curPos
+ while afterLastSlashPos < endPos && result[afterLastSlashPos] == "/" {
+ afterLastSlashPos = result.index(after: afterLastSlashPos)
}
+ if afterLastSlashPos != result.index(after: curPos) {
+ result.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
+ endPos = result.endIndex
+ }
+ curPos = afterLastSlashPos
+ } else {
+ curPos = result.index(after: curPos)
}
}
}
diff --git a/Foundation/ProcessInfo.swift b/Foundation/ProcessInfo.swift
index af3f9aa..e2e8625 100644
--- a/Foundation/ProcessInfo.swift
+++ b/Foundation/ProcessInfo.swift
@@ -41,13 +41,22 @@
}
-
- internal static var _environment: [String : String] = {
- return Dictionary<String, String>._unconditionallyBridgeFromObjectiveC(__CFGetEnvironment()._nsObject)
- }()
-
open var environment: [String : String] {
- return ProcessInfo._environment
+ let equalSign = Character("=")
+ let strEncoding = String.defaultCStringEncoding
+ let envp = _CFEnviron()
+ var env: [String : String] = [:]
+ var idx = 0
+
+ while let entry = envp.advanced(by: idx).pointee {
+ if let entry = String(cString: entry, encoding: strEncoding), let i = entry.index(of: equalSign) {
+ let key = String(entry.prefix(upTo: i))
+ let value = String(entry.suffix(from: i).dropFirst())
+ env[key] = value
+ }
+ idx += 1
+ }
+ return env
}
open var arguments: [String] {
diff --git a/Foundation/RunLoop.swift b/Foundation/RunLoop.swift
index ce4cf81..522664f 100644
--- a/Foundation/RunLoop.swift
+++ b/Foundation/RunLoop.swift
@@ -43,6 +43,17 @@
extension RunLoopMode {
public static let defaultRunLoopMode = RunLoopMode("kCFRunLoopDefaultMode")
public static let commonModes = RunLoopMode("kCFRunLoopCommonModes")
+
+ // Use this instead of .rawValue._cfObject; this will allow CFRunLoop to use pointer equality internally.
+ fileprivate var _cfStringUniquingKnown: CFString {
+ if self == .defaultRunLoopMode {
+ return kCFRunLoopDefaultMode
+ } else if self == .commonModes {
+ return kCFRunLoopCommonModes
+ } else {
+ return rawValue._cfObject
+ }
+ }
}
internal func _NSRunLoopNew(_ cf: CFRunLoop) -> Unmanaged<AnyObject> {
@@ -81,7 +92,7 @@
}
open func add(_ timer: Timer, forMode mode: RunLoopMode) {
- CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer._cfObject, mode.rawValue._cfObject)
+ CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer._cfObject, mode._cfStringUniquingKnown)
}
open func add(_ aPort: Port, forMode mode: RunLoopMode) {
@@ -135,7 +146,7 @@
if _cfRunLoop !== CFRunLoopGetCurrent() {
return false
}
- let modeArg = mode.rawValue._cfObject
+ let modeArg = mode._cfStringUniquingKnown
if _CFRunLoopFinished(_cfRunLoop, modeArg) {
return false
}
@@ -147,7 +158,7 @@
}
public func perform(inModes modes: [RunLoopMode], block: @escaping () -> Void) {
- CFRunLoopPerformBlock(getCFRunLoop(), (modes.map { $0.rawValue._nsObject })._cfObject, block)
+ CFRunLoopPerformBlock(getCFRunLoop(), (modes.map { $0._cfStringUniquingKnown })._cfObject, block)
}
public func perform(_ block: @escaping () -> Void) {
diff --git a/TestFoundation/TestBundle.swift b/TestFoundation/TestBundle.swift
index 3db11bf..1b8cb7d 100644
--- a/TestFoundation/TestBundle.swift
+++ b/TestFoundation/TestBundle.swift
@@ -9,26 +9,26 @@
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
- import Foundation
- import XCTest
+import Foundation
+import XCTest
#else
- import SwiftFoundation
- import SwiftXCTest
+import SwiftFoundation
+import SwiftXCTest
#endif
import CoreFoundation
internal func testBundle() -> Bundle {
-#if DARWIN_COMPATIBILITY_TESTS
+ #if DARWIN_COMPATIBILITY_TESTS
for bundle in Bundle.allBundles {
if let bundleId = bundle.bundleIdentifier, bundleId == "org.swift.DarwinCompatibilityTests", bundle.resourcePath != nil {
return bundle
}
}
fatalError("Cant find test bundle")
-#else
+ #else
return Bundle.main
-#endif
+ #endif
}
class BundlePlayground {
@@ -58,6 +58,7 @@
}
let bundleName: String
+ let bundleExtension: String
let resourceFilenames: [String]
let resourceSubdirectory: String
let subdirectoryResourcesFilenames: [String]
@@ -68,12 +69,14 @@
private var playgroundPath: String?
init?(bundleName: String,
- resourceFilenames: [String],
- resourceSubdirectory: String,
- subdirectoryResourcesFilenames: [String],
- auxiliaryExecutableName: String,
- layout: Layout) {
+ bundleExtension: String,
+ resourceFilenames: [String],
+ resourceSubdirectory: String,
+ subdirectoryResourcesFilenames: [String],
+ auxiliaryExecutableName: String,
+ layout: Layout) {
self.bundleName = bundleName
+ self.bundleExtension = bundleExtension
self.resourceFilenames = resourceFilenames
self.resourceSubdirectory = resourceSubdirectory
self.subdirectoryResourcesFilenames = subdirectoryResourcesFilenames
@@ -88,53 +91,55 @@
private func _create() -> Bool {
// Make sure the directory is uniquely named
- let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
+
+ let temporaryDirectory = FileManager.default.temporaryDirectory.appendingPathComponent("TestFoundation_Playground_" + UUID().uuidString)
switch (layout) {
case .flat:
- do {
- try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
-
- // Make a flat bundle in the playground
- let bundlePath = tempDir + bundleName
- try FileManager.default.createDirectory(atPath: bundlePath, withIntermediateDirectories: false, attributes: nil)
-
- // Make a main and an auxiliary executable:
- guard FileManager.default.createFile(atPath: bundlePath + "/" + bundleName, contents: nil) else {
- return false
- }
- guard FileManager.default.createFile(atPath: bundlePath + "/" + auxiliaryExecutableName, contents: nil) else {
+ do {
+ try FileManager.default.createDirectory(atPath: temporaryDirectory.path, withIntermediateDirectories: false, attributes: nil)
+
+ // Make a flat bundle in the playground
+ let bundleURL = temporaryDirectory.appendingPathComponent(bundleName).appendingPathExtension(self.bundleExtension)
+ try FileManager.default.createDirectory(atPath: bundleURL.path, withIntermediateDirectories: false, attributes: nil)
+
+ // Make a main and an auxiliary executable:
+ guard FileManager.default.createFile(atPath: bundleURL.appendingPathComponent(bundleName).path, contents: nil) else {
+ return false
+ }
+ guard FileManager.default.createFile(atPath: bundleURL.appendingPathComponent(auxiliaryExecutableName).path, contents: nil) else {
+ return false
+ }
+
+ // Put some resources in the bundle
+ for resourceName in resourceFilenames {
+ guard FileManager.default.createFile(atPath: bundleURL.appendingPathComponent(resourceName).path, contents: nil, attributes: nil) else {
+ return false
+ }
+ }
+
+ // Add a resource into a subdirectory
+ let subdirectoryURL = bundleURL.appendingPathComponent(resourceSubdirectory)
+ try FileManager.default.createDirectory(atPath: subdirectoryURL.path, withIntermediateDirectories: false, attributes: nil)
+
+ for resourceName in subdirectoryResourcesFilenames {
+ guard FileManager.default.createFile(atPath: subdirectoryURL.appendingPathComponent(resourceName).path, contents: nil, attributes: nil) else {
+ return false
+ }
+ }
+
+ self.bundlePath = bundleURL.path
+ } catch _ {
return false
}
- // Put some resources in the bundle
- for n in resourceFilenames {
- guard FileManager.default.createFile(atPath: bundlePath + "/" + n, contents: nil, attributes: nil) else {
- return false
- }
- }
- // Add a resource into a subdirectory
- let subDirPath = bundlePath + "/" + resourceSubdirectory
- try FileManager.default.createDirectory(atPath: subDirPath, withIntermediateDirectories: false, attributes: nil)
- for n in subdirectoryResourcesFilenames {
- guard FileManager.default.createFile(atPath: subDirPath + "/" + n, contents: nil, attributes: nil) else {
- return false
- }
- }
-
- self.bundlePath = bundlePath
- } catch _ {
- return false
- }
-
case .fhsInstalled:
do {
- let bundleName = URL(string:self.bundleName)!.deletingPathExtension().path
// Create a FHS /usr/local-style hierarchy:
- try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
- try FileManager.default.createDirectory(atPath: tempDir + "/share", withIntermediateDirectories: false, attributes: nil)
- try FileManager.default.createDirectory(atPath: tempDir + "/lib", withIntermediateDirectories: false, attributes: nil)
+ try FileManager.default.createDirectory(atPath: temporaryDirectory.path, withIntermediateDirectories: false, attributes: nil)
+ try FileManager.default.createDirectory(atPath: temporaryDirectory.appendingPathComponent("share").path, withIntermediateDirectories: false, attributes: nil)
+ try FileManager.default.createDirectory(atPath: temporaryDirectory.appendingPathComponent("lib").path, withIntermediateDirectories: false, attributes: nil)
// Make a main and an auxiliary executable:
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
@@ -143,28 +148,30 @@
let pathExtension = "so"
#endif
- guard FileManager.default.createFile(atPath: tempDir + "/lib/lib" + bundleName + ".\(pathExtension)", contents: nil) else { return false }
+ guard FileManager.default.createFile(atPath: temporaryDirectory.appendingPathComponent("lib").appendingPathComponent("lib\(bundleName).\(pathExtension)").path, contents: nil) else { return false }
- let executables = tempDir + "/libexec/" + bundleName + ".executables"
- try FileManager.default.createDirectory(atPath: executables, withIntermediateDirectories: true, attributes: nil)
- guard FileManager.default.createFile(atPath: executables + "/" + auxiliaryExecutableName, contents: nil) else { return false }
+ let executablesDirectory = temporaryDirectory.appendingPathComponent("libexec").appendingPathComponent("\(bundleName).executables")
+ try FileManager.default.createDirectory(atPath: executablesDirectory.path, withIntermediateDirectories: true, attributes: nil)
+ guard FileManager.default.createFile(atPath: executablesDirectory.appendingPathComponent(auxiliaryExecutableName).path, contents: nil) else { return false }
// Make a .resources directory in …/share:
- let resourcesPath = tempDir + "/share/" + bundleName + ".resources"
- try FileManager.default.createDirectory(atPath: resourcesPath, withIntermediateDirectories: false, attributes: nil)
+ let resourcesDirectory = temporaryDirectory.appendingPathComponent("share").appendingPathComponent("\(bundleName).resources")
+ try FileManager.default.createDirectory(atPath: resourcesDirectory.path, withIntermediateDirectories: false, attributes: nil)
// Put some resources in the bundle
- for n in resourceFilenames {
- guard FileManager.default.createFile(atPath: resourcesPath + "/" + n, contents: nil, attributes: nil) else { return false }
- }
- // Add a resource into a subdirectory
- let subDirPath = resourcesPath + "/" + resourceSubdirectory
- try FileManager.default.createDirectory(atPath: subDirPath, withIntermediateDirectories: false, attributes: nil)
- for n in subdirectoryResourcesFilenames {
- guard FileManager.default.createFile(atPath: subDirPath + "/" + n, contents: nil, attributes: nil) else { return false }
+ for resourceName in resourceFilenames {
+ guard FileManager.default.createFile(atPath: resourcesDirectory.appendingPathComponent(resourceName).path, contents: nil, attributes: nil) else { return false }
}
- self.bundlePath = resourcesPath
+ // Add a resource into a subdirectory
+ let subdirectoryURL = resourcesDirectory.appendingPathComponent(resourceSubdirectory)
+ try FileManager.default.createDirectory(atPath: subdirectoryURL.path, withIntermediateDirectories: false, attributes: nil)
+
+ for resourceName in subdirectoryResourcesFilenames {
+ guard FileManager.default.createFile(atPath: subdirectoryURL.appendingPathComponent(resourceName).path, contents: nil, attributes: nil) else { return false }
+ }
+
+ self.bundlePath = resourcesDirectory.path
} catch _ {
return false
}
@@ -173,36 +180,38 @@
do {
let bundleName = URL(string:self.bundleName)!.deletingPathExtension().path
- try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
+ try FileManager.default.createDirectory(atPath: temporaryDirectory.path, withIntermediateDirectories: false, attributes: nil)
// Make a main executable:
- guard FileManager.default.createFile(atPath: tempDir + "/" + bundleName, contents: nil) else { return false }
+ guard FileManager.default.createFile(atPath: temporaryDirectory.appendingPathComponent(bundleName).path, contents: nil) else { return false }
// Make a .resources directory:
- let resourcesPath = tempDir + "/" + bundleName + ".resources"
- try FileManager.default.createDirectory(atPath: resourcesPath, withIntermediateDirectories: false, attributes: nil)
+ let resourcesDirectory = temporaryDirectory.appendingPathComponent("\(bundleName).resources")
+ try FileManager.default.createDirectory(atPath: resourcesDirectory.path, withIntermediateDirectories: false, attributes: nil)
// Make an auxiliary executable:
- guard FileManager.default.createFile(atPath: resourcesPath + "/" + auxiliaryExecutableName, contents: nil) else { return false }
+ guard FileManager.default.createFile(atPath: resourcesDirectory.appendingPathComponent(auxiliaryExecutableName).path, contents: nil) else { return false }
// Put some resources in the bundle
- for n in resourceFilenames {
- guard FileManager.default.createFile(atPath: resourcesPath + "/" + n, contents: nil, attributes: nil) else { return false }
- }
- // Add a resource into a subdirectory
- let subDirPath = resourcesPath + "/" + resourceSubdirectory
- try FileManager.default.createDirectory(atPath: subDirPath, withIntermediateDirectories: false, attributes: nil)
- for n in subdirectoryResourcesFilenames {
- guard FileManager.default.createFile(atPath: subDirPath + "/" + n, contents: nil, attributes: nil) else { return false }
+ for resourceName in resourceFilenames {
+ guard FileManager.default.createFile(atPath: resourcesDirectory.appendingPathComponent(resourceName).path, contents: nil, attributes: nil) else { return false }
}
- self.bundlePath = resourcesPath
+ // Add a resource into a subdirectory
+ let subdirectoryURL = resourcesDirectory.appendingPathComponent(resourceSubdirectory)
+ try FileManager.default.createDirectory(atPath: subdirectoryURL.path, withIntermediateDirectories: false, attributes: nil)
+
+ for resourceName in subdirectoryResourcesFilenames {
+ guard FileManager.default.createFile(atPath: subdirectoryURL.appendingPathComponent(resourceName).path, contents: nil, attributes: nil) else { return false }
+ }
+
+ self.bundlePath = resourcesDirectory.path
} catch _ {
return false
}
}
- self.playgroundPath = tempDir
+ self.playgroundPath = temporaryDirectory.path
return true
}
@@ -235,40 +244,41 @@
("test_bundleLoadWithError", test_bundleLoadWithError),
("test_bundleWithInvalidPath", test_bundleWithInvalidPath),
("test_bundlePreflight", test_bundlePreflight),
+ ("test_bundleFindExecutable", test_bundleFindExecutable),
("test_bundleFindAuxiliaryExecutables", test_bundleFindAuxiliaryExecutables),
]
}
func test_paths() {
let bundle = testBundle()
-
+
// bundlePath
XCTAssert(!bundle.bundlePath.isEmpty)
XCTAssertEqual(bundle.bundleURL.path, bundle.bundlePath)
let path = bundle.bundlePath
-
+
// etc
#if os(OSX)
XCTAssertEqual("\(path)/Contents/Resources", bundle.resourcePath)
-#if DARWIN_COMPATIBILITY_TESTS
+ #if DARWIN_COMPATIBILITY_TESTS
XCTAssertEqual("\(path)/Contents/MacOS/DarwinCompatibilityTests", bundle.executablePath)
-#else
+ #else
XCTAssertEqual("\(path)/Contents/MacOS/TestFoundation", bundle.executablePath)
-#endif
+ #endif
XCTAssertEqual("\(path)/Contents/Frameworks", bundle.privateFrameworksPath)
XCTAssertEqual("\(path)/Contents/SharedFrameworks", bundle.sharedFrameworksPath)
XCTAssertEqual("\(path)/Contents/SharedSupport", bundle.sharedSupportPath)
#endif
XCTAssertNil(bundle.path(forAuxiliaryExecutable: "no_such_file"))
-#if !DARWIN_COMPATIBILITY_TESTS
+ #if !DARWIN_COMPATIBILITY_TESTS
XCTAssertNil(bundle.appStoreReceiptURL)
-#endif
+ #endif
}
func test_resources() {
let bundle = testBundle()
-
+
// bad resources
XCTAssertNil(bundle.url(forResource: nil, withExtension: nil, subdirectory: nil))
XCTAssertNil(bundle.url(forResource: "", withExtension: "", subdirectory: nil))
@@ -290,37 +300,38 @@
let bundle = testBundle()
// bundleIdentifier
-#if DARWIN_COMPATIBILITY_TESTS
+ #if DARWIN_COMPATIBILITY_TESTS
XCTAssertEqual("org.swift.DarwinCompatibilityTests", bundle.bundleIdentifier)
-#else
+ #else
XCTAssertEqual("org.swift.TestFoundation", bundle.bundleIdentifier)
-#endif
-
+ #endif
+
// infoDictionary
let info = bundle.infoDictionary
XCTAssertNotNil(info)
-
-#if DARWIN_COMPATIBILITY_TESTS
+
+ #if DARWIN_COMPATIBILITY_TESTS
XCTAssert("DarwinCompatibilityTests" == info!["CFBundleName"] as! String)
XCTAssert("org.swift.DarwinCompatibilityTests" == info!["CFBundleIdentifier"] as! String)
-#else
+ #else
XCTAssert("TestFoundation" == info!["CFBundleName"] as! String)
XCTAssert("org.swift.TestFoundation" == info!["CFBundleIdentifier"] as! String)
-#endif
-
+ #endif
+
// localizedInfoDictionary
XCTAssertNil(bundle.localizedInfoDictionary) // FIXME: Add a localized Info.plist for testing
}
func test_localizations() {
let bundle = testBundle()
-
+
XCTAssertEqual(["en"], bundle.localizations)
XCTAssertEqual(["en"], bundle.preferredLocalizations)
XCTAssertEqual(["en"], Bundle.preferredLocalizations(from: ["en", "pl", "es"]))
}
- private let _bundleName = "MyBundle.bundle"
+ private let _bundleName = "MyBundle"
+ private let _bundleExtension = "bundle"
private let _bundleResourceNames = ["hello.world", "goodbye.world", "swift.org"]
private let _subDirectory = "Sources"
private let _main = "main"
@@ -329,6 +340,7 @@
private func _setupPlayground(layout: BundlePlayground.Layout) -> BundlePlayground? {
return BundlePlayground(bundleName: _bundleName,
+ bundleExtension: _bundleExtension,
resourceFilenames: _bundleResourceNames,
resourceSubdirectory: _subDirectory,
subdirectoryResourcesFilenames: [ "\(_main).\(_type)" ],
@@ -352,58 +364,67 @@
// Oh well
}
}
-
+
func test_URLsForResourcesWithExtension() {
_withEachPlaygroundLayout { (playground) in
- let bundle = Bundle(path: playground.bundlePath)
+ let bundle = Bundle(path: playground.bundlePath)!
XCTAssertNotNil(bundle)
- let worldResources = bundle?.urls(forResourcesWithExtension: "world", subdirectory: nil)
+ let worldResources = bundle.urls(forResourcesWithExtension: "world", subdirectory: nil)
XCTAssertNotNil(worldResources)
XCTAssertEqual(worldResources?.count, 2)
- let path = bundle?.path(forResource: _main, ofType: _type, inDirectory: _subDirectory)
+ let path = bundle.path(forResource: _main, ofType: _type, inDirectory: _subDirectory)
XCTAssertNotNil(path)
}
}
- func test_bundleLoad(){
+ func test_bundleLoad() {
let bundle = testBundle()
let _ = bundle.load()
XCTAssertTrue(bundle.isLoaded)
}
- func test_bundleLoadWithError(){
+ func test_bundleLoadWithError() {
let bundleValid = testBundle()
- //test valid load using loadAndReturnError
- do{
+
+ // Test valid load using loadAndReturnError
+ do {
try bundleValid.loadAndReturnError()
- }catch{
+ }
+ catch{
XCTFail("should not fail to load")
}
- // executable cannot be located
+
+ // Executable cannot be located
try! _withEachPlaygroundLayout { (playground) in
let bundle = Bundle(path: playground.bundlePath)
XCTAssertThrowsError(try bundle!.loadAndReturnError())
}
}
- func test_bundleWithInvalidPath(){
+ func test_bundleWithInvalidPath() {
let bundleInvalid = Bundle(path: NSTemporaryDirectory() + "test.playground")
XCTAssertNil(bundleInvalid)
}
- func test_bundlePreflight(){
- let bundleValid = testBundle()
- do{
- try bundleValid.preflight()
- }catch{
- XCTFail("should not fail to load")
- }
- // executable cannot be located ..preflight should report error
+ func test_bundlePreflight() {
+ XCTAssertNoThrow(try testBundle().preflight())
+
try! _withEachPlaygroundLayout { (playground) in
- let bundle = Bundle(path: playground.bundlePath)
- XCTAssertThrowsError(try bundle!.preflight())
+ let bundle = Bundle(path: playground.bundlePath)!
+
+ // Must throw as the main executable is a dummy empty file.
+ XCTAssertThrowsError(try bundle.preflight())
+ }
+ }
+
+ func test_bundleFindExecutable() {
+ XCTAssertNotNil(testBundle().executableURL)
+
+ _withEachPlaygroundLayout { (playground) in
+ let bundle = Bundle(path: playground.bundlePath)!
+ XCTAssertNotNil(bundle.executableURL)
}
}
diff --git a/TestFoundation/TestIndexSet.swift b/TestFoundation/TestIndexSet.swift
index 95c0fe5..9eff873 100644
--- a/TestFoundation/TestIndexSet.swift
+++ b/TestFoundation/TestIndexSet.swift
@@ -57,6 +57,13 @@
("test_AnyHashableContainingIndexSet", test_AnyHashableContainingIndexSet),
("test_AnyHashableCreatedFromNSIndexSet", test_AnyHashableCreatedFromNSIndexSet),
("test_unconditionallyBridgeFromObjectiveC", test_unconditionallyBridgeFromObjectiveC),
+ ("testInsertNonOverlapping", testInsertNonOverlapping),
+ ("testInsertOverlapping", testInsertOverlapping),
+ ("testInsertOverlappingExtend", testInsertOverlappingExtend),
+ ("testInsertOverlappingMultiple", testInsertOverlappingMultiple),
+ ("testRemoveNonOverlapping", testRemoveNonOverlapping),
+ ("testRemoveOverlapping", testRemoveOverlapping),
+ ("testRemoveSplitting", testRemoveSplitting),
]
}
@@ -1082,4 +1089,120 @@
func test_unconditionallyBridgeFromObjectiveC() {
XCTAssertEqual(IndexSet(), IndexSet._unconditionallyBridgeFromObjectiveC(nil))
}
+
+ func testInsertNonOverlapping() {
+ var tested = IndexSet()
+ tested.insert(integersIn: 1..<2)
+ tested.insert(integersIn: 100..<200)
+ tested.insert(integersIn: 1000..<2000)
+
+ tested.insert(200)
+
+ var expected = IndexSet()
+ expected.insert(integersIn: 1..<2)
+ expected.insert(integersIn: 100..<201)
+ expected.insert(integersIn: 1000..<2000)
+
+ XCTAssertEqual(tested, expected)
+ }
+
+ func testInsertOverlapping() {
+ var tested = IndexSet()
+ tested.insert(integersIn: 1..<2)
+ tested.insert(integersIn: 100..<200)
+ tested.insert(integersIn: 1000..<2000)
+ tested.insert(integersIn: 10000..<20000)
+
+ tested.insert(integersIn: 150..<1500)
+
+ var expected = IndexSet()
+ expected.insert(integersIn: 1..<2)
+ expected.insert(integersIn: 100..<2000)
+ expected.insert(integersIn: 10000..<20000)
+
+ XCTAssertEqual(tested, expected)
+ }
+
+ func testInsertOverlappingExtend() {
+ var tested = IndexSet()
+ tested.insert(integersIn: 1..<2)
+ tested.insert(integersIn: 100..<200)
+ tested.insert(integersIn: 1000..<2000)
+
+ tested.insert(integersIn: 50..<500)
+
+ var expected = IndexSet()
+ expected.insert(integersIn: 1..<2)
+ expected.insert(integersIn: 50..<500)
+ expected.insert(integersIn: 1000..<2000)
+
+ XCTAssertEqual(tested, expected)
+ }
+
+ func testInsertOverlappingMultiple() {
+ var tested = IndexSet()
+ tested.insert(integersIn: 1..<2)
+ tested.insert(integersIn: 100..<200)
+ tested.insert(integersIn: 1000..<2000)
+ tested.insert(integersIn: 10000..<20000)
+
+ tested.insert(integersIn: 150..<3000)
+
+ var expected = IndexSet()
+ expected.insert(integersIn: 1..<2)
+ expected.insert(integersIn: 100..<3000)
+ expected.insert(integersIn: 10000..<20000)
+
+ XCTAssertEqual(tested, expected)
+ }
+
+ func testRemoveNonOverlapping() {
+ var tested = IndexSet()
+ tested.insert(integersIn: 1..<2)
+ tested.insert(integersIn: 100..<200)
+ tested.insert(integersIn: 1000..<2000)
+
+ tested.remove(199)
+
+ var expected = IndexSet()
+ expected.insert(integersIn: 1..<2)
+ expected.insert(integersIn: 100..<199)
+ expected.insert(integersIn: 1000..<2000)
+
+ XCTAssertEqual(tested, expected)
+ }
+
+ func testRemoveOverlapping() {
+ var tested = IndexSet()
+ tested.insert(integersIn: 1..<2)
+ tested.insert(integersIn: 100..<200)
+ tested.insert(integersIn: 1000..<2000)
+
+ tested.remove(integersIn: 150..<1500)
+
+ var expected = IndexSet()
+ expected.insert(integersIn: 1..<2)
+ expected.insert(integersIn: 100..<150)
+ expected.insert(integersIn: 1500..<2000)
+
+ XCTAssertEqual(tested, expected)
+ }
+
+ func testRemoveSplitting() {
+ var tested = IndexSet()
+ tested.insert(integersIn: 1..<2)
+ tested.insert(integersIn: 100..<200)
+ tested.insert(integersIn: 1000..<2000)
+
+ tested.remove(integersIn: 150..<160)
+
+ var expected = IndexSet()
+ expected.insert(integersIn: 1..<2)
+ expected.insert(integersIn: 100..<150)
+ expected.insert(integersIn: 160..<200)
+ expected.insert(integersIn: 1000..<2000)
+
+ XCTAssertEqual(tested, expected)
+ }
+
}
diff --git a/TestFoundation/TestNSDictionary.swift b/TestFoundation/TestNSDictionary.swift
index b1435d4..a5380de 100644
--- a/TestFoundation/TestNSDictionary.swift
+++ b/TestFoundation/TestNSDictionary.swift
@@ -32,6 +32,7 @@
("test_mutableCopying", test_mutableCopying),
("test_writeToFile", test_writeToFile),
("test_initWithContentsOfFile", test_initWithContentsOfFile),
+ ("test_settingWithStringKey", test_settingWithStringKey),
]
}
@@ -222,6 +223,12 @@
}
}
+ func test_settingWithStringKey() {
+ let dict = NSMutableDictionary()
+ // has crashed in the past
+ dict["stringKey"] = "value"
+ }
+
private func createTestFile(_ path: String, _contents: Data) -> String? {
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
do {
diff --git a/TestFoundation/TestNSString.swift b/TestFoundation/TestNSString.swift
index 0266819..71efa58 100755
--- a/TestFoundation/TestNSString.swift
+++ b/TestFoundation/TestNSString.swift
@@ -1238,14 +1238,7 @@
// ASCII cases
ComparisonTest("t", "tt"),
ComparisonTest("t", "Tt"),
- ComparisonTest("\u{0}", "",
- reason: {
-#if _runtime(_ObjC)
- return ""
-#else
- return "https://bugs.swift.org/browse/SR-332"
-#endif
- }()),
+ ComparisonTest("\u{0}", ""),
ComparisonTest("\u{0}", "\u{0}",
reason: "https://bugs.swift.org/browse/SR-332"),
ComparisonTest("\r\n", "t"),
diff --git a/TestFoundation/TestProcessInfo.swift b/TestFoundation/TestProcessInfo.swift
index 58a5711..efd542c 100644
--- a/TestFoundation/TestProcessInfo.swift
+++ b/TestFoundation/TestProcessInfo.swift
@@ -24,6 +24,7 @@
("test_operatingSystemVersion", test_operatingSystemVersion ),
("test_processName", test_processName ),
("test_globallyUniqueString", test_globallyUniqueString ),
+ ("test_environment", test_environment),
]
}
@@ -66,5 +67,41 @@
XCTAssertEqual(parts[3].utf16.count, 4)
XCTAssertEqual(parts[4].utf16.count, 12)
}
-
+
+ func test_environment() {
+ let preset = ProcessInfo.processInfo.environment["test"]
+ setenv("test", "worked", 1)
+ let postset = ProcessInfo.processInfo.environment["test"]
+ XCTAssertNil(preset)
+ XCTAssertEqual(postset, "worked")
+
+ // Bad values that wont be stored
+ XCTAssertEqual(setenv("", "", 1), -1)
+ XCTAssertEqual(setenv("bad1=", "", 1), -1)
+ XCTAssertEqual(setenv("bad2=", "1", 1) ,-1)
+ XCTAssertEqual(setenv("bad3=", "=2", 1), -1)
+
+ // Good values that will be, check splitting on '='
+ XCTAssertEqual(setenv("var1", "",1 ), 0)
+ XCTAssertEqual(setenv("var2", "=", 1), 0)
+ XCTAssertEqual(setenv("var3", "=x", 1), 0)
+ XCTAssertEqual(setenv("var4", "x=", 1), 0)
+ XCTAssertEqual(setenv("var5", "=x=", 1), 0)
+
+ let env = ProcessInfo.processInfo.environment
+
+ XCTAssertNil(env[""])
+ XCTAssertNil(env["bad1"])
+ XCTAssertNil(env["bad1="])
+ XCTAssertNil(env["bad2"])
+ XCTAssertNil(env["bad2="])
+ XCTAssertNil(env["bad3"])
+ XCTAssertNil(env["bad3="])
+
+ XCTAssertEqual(env["var1"], "")
+ XCTAssertEqual(env["var2"], "=")
+ XCTAssertEqual(env["var3"], "=x")
+ XCTAssertEqual(env["var4"], "x=")
+ XCTAssertEqual(env["var5"], "=x=")
+ }
}
diff --git a/TestFoundation/TestRunLoop.swift b/TestFoundation/TestRunLoop.swift
index 27668a2..931af8d 100644
--- a/TestFoundation/TestRunLoop.swift
+++ b/TestFoundation/TestRunLoop.swift
@@ -21,6 +21,7 @@
return [
("test_constants", test_constants),
("test_runLoopInit", test_runLoopInit),
+ ("test_commonModes", test_commonModes),
// these tests do not work the same as Darwin https://bugs.swift.org/browse/SR-399
// ("test_runLoopRunMode", test_runLoopRunMode),
// ("test_runLoopLimitDate", test_runLoopLimitDate),
@@ -90,4 +91,16 @@
XCTAssertLessThan(abs(timerTickInterval - expectedTimeInterval), 0.01)
}
+
+ func test_commonModes() {
+ let runLoop = RunLoop.current
+ let done = expectation(description: "The timer has fired")
+ let timer = Timer(timeInterval: 1, repeats: false) { (_) in
+ done.fulfill()
+ }
+
+ runLoop.add(timer, forMode: .commonModes)
+
+ waitForExpectations(timeout: 10)
+ }
}