Files
swift-mirror/test/stdlib/ArrayBuffer_CopyContents.swift
Karoy Lorentey 466e26a872 [stdlib] Implement _copyContents on internal Array types
`_copyContents(initializing:)` is a core method of Sequence, and it is used surprisingly often to copy stuff out of sequences. Array’s internal types currently have explicit implementations of it that trap (to prevent a performance bug due to the default iterator-based implementation. This has proved a bad idea, as not all code paths that end up calling `_copyContents` have actually been expunged — so we replaced a performance bug with a catastrophic correctness bug. 😥

Rather than trying to play whack-a-mole with code paths that end up in `_copyContents`, replace the traps with (relatively) efficient implementations, based on the ancient `_copyContents(subRange:initializing)` methods that have already been there all this time.

This resolves https://bugs.swift.org/browse/SR-14663.

I expect specialization will make this fix deploy back to earlier OSes in most (but unfortunately not all) cases.
2021-06-16 13:47:00 -07:00

80 lines
2.1 KiB
Swift

//===--- ArrayBuffer_CopyContents.swift -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 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: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: swift_stdlib_asserts
import Foundation
import StdlibUnittest
let suite = TestSuite("ArrayBuffer_CopyContents")
defer { runAllTests() }
var trackedCount = 0
var nextBaseSerialNumber = 0
/// A type that will be bridged verbatim to Objective-C
class Thing: NSObject {
var value: Int
var serialNumber: Int
func foo() { }
required init(_ value: Int) {
trackedCount += 1
nextBaseSerialNumber += 1
serialNumber = nextBaseSerialNumber
self.value = value
}
deinit {
assert(serialNumber > 0, "double destruction!")
trackedCount -= 1
serialNumber = -serialNumber
}
override func isEqual(_ other: Any?) -> Bool {
return (other as? Thing)?.value == self.value
}
override var hash: Int { value }
}
suite.test("nativeArray/_copyContents") {
let array = [Thing(0), Thing(1), Thing(2), Thing(3)]
expectEqualSequence(array._copyToNewArray(), array)
}
suite.test("nativeArraySlice/_copyContents") {
let array = (0 ..< 100).map { Thing($0) }
expectEqualSequence(
array[20 ..< 30]._copyToNewArray(),
(20 ..< 30).map { Thing($0) })
}
suite.test("bridgedArray/_copyContents") {
let array = NSArray(array: (0 ..< 5).map { Thing($0) }) as! [Thing]
expectEqualSequence(
array._copyToNewArray(),
(0 ..< 5).map { Thing($0) })
}
suite.test("bridgedArraySlice/_copyContents") {
let array = NSArray(array: (0 ..< 100).map { Thing($0) }) as! [Thing]
expectEqualSequence(
array[20 ..< 30]._copyToNewArray(),
(20 ..< 30).map { Thing($0) })
}