mirror of
https://github.com/apple/swift-certificates
synced 2026-11-22 04:20:39 +00:00
Implement TinyArray (#98)
* Implement TinyArray * update cmake * “Hide” enum and support storing arrays of exactly one element in `.arbitrary` * Add allocation tests for TinyArray * make arrays of different sizes * Remove array optimisation * run append in a loop * Move _TinyArray into a separate module and apply review feedback * update cmake files * remove unused Foundation import * Allocations are lower by “1” on Linux
This commit is contained in:
1 parent
3bf87fd9ee
commit
41dff30819
13 files changed
+709
-7
No files matched your search
+11
-2
@@ -12,10 +12,19 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import X509
|
||||
import _CertificateInternals
|
||||
|
||||
func run(identifier: String) {
|
||||
measure(identifier: identifier) {
|
||||
return 0
|
||||
var count = 0
|
||||
for _ in 0..<1000 {
|
||||
var tinyArray = _TinyArray<Int>()
|
||||
for i in 0..<1000 {
|
||||
tinyArray.append(i)
|
||||
}
|
||||
count += tinyArray.count
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCertificates open source project
|
||||
//
|
||||
// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import _CertificateInternals
|
||||
|
||||
func run(identifier: String) {
|
||||
measure(identifier: identifier) {
|
||||
var counts = 0
|
||||
for _ in 0..<1000 {
|
||||
counts += _TinyArray(CollectionOfOne(1)).count
|
||||
|
||||
do {
|
||||
var array = _TinyArray<Int>()
|
||||
array.append(contentsOf: CollectionOfOne(1))
|
||||
counts += array.count
|
||||
}
|
||||
}
|
||||
|
||||
return counts
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ let package = Package(
|
||||
.target(
|
||||
name: "X509",
|
||||
dependencies: [
|
||||
"_CertificateInternals",
|
||||
.product(name: "SwiftASN1", package: "swift-asn1"),
|
||||
.product(name: "Crypto", package: "swift-crypto"),
|
||||
.product(name: "_CryptoExtras", package: "swift-crypto"),
|
||||
@@ -55,6 +56,16 @@ let package = Package(
|
||||
.copy("PEMTestRSACertificate.pem"),
|
||||
.copy("CSR Vectors/"),
|
||||
]),
|
||||
.target(
|
||||
name: "_CertificateInternals",
|
||||
exclude: [
|
||||
"CMakeLists.txt",
|
||||
]),
|
||||
.testTarget(
|
||||
name: "CertificateInternalsTests",
|
||||
dependencies: [
|
||||
"_CertificateInternals",
|
||||
]),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@@ -13,3 +13,4 @@
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
add_subdirectory(X509)
|
||||
add_subdirectory(_CertificateInternals)
|
||||
@@ -109,7 +109,8 @@ target_link_libraries(X509 PUBLIC
|
||||
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>
|
||||
Crypto
|
||||
_CryptoExtras
|
||||
SwiftASN1)
|
||||
SwiftASN1
|
||||
_CertificateInternals)
|
||||
set_target_properties(X509 PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
##===----------------------------------------------------------------------===##
|
||||
##
|
||||
## This source file is part of the SwiftCertificates open source project
|
||||
##
|
||||
## Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors
|
||||
## Licensed under Apache License v2.0
|
||||
##
|
||||
## See LICENSE.txt for license information
|
||||
## See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
|
||||
##
|
||||
## SPDX-License-Identifier: Apache-2.0
|
||||
##
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
add_library(_CertificateInternals
|
||||
"_TinyArray.swift")
|
||||
|
||||
set_target_properties(_CertificateInternals PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
|
||||
_install_target(_CertificateInternals)
|
||||
set_property(GLOBAL APPEND PROPERTY SWIFT_CERTIFICATES_EXPORTS _CertificateInternals)
|
||||
@@ -0,0 +1,313 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCertificates open source project
|
||||
//
|
||||
// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// ``TinyArray`` is a ``RandomAccessCollection`` optimised to store zero or one ``Element``.
|
||||
/// It supports arbitrary many elements but if only up to one ``Element`` is stored it does **not** allocate separate storage on the heap
|
||||
/// and instead stores the ``Element`` inline.
|
||||
public struct _TinyArray<Element> {
|
||||
@usableFromInline
|
||||
enum Storage {
|
||||
case one(Element)
|
||||
case arbitrary([Element])
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
var storage: Storage
|
||||
}
|
||||
|
||||
// MARK: - TinyArray "public" interface
|
||||
|
||||
extension _TinyArray: Equatable where Element: Equatable {}
|
||||
extension _TinyArray: Hashable where Element: Hashable {}
|
||||
extension _TinyArray: Sendable where Element: Sendable {}
|
||||
|
||||
extension _TinyArray: RandomAccessCollection {
|
||||
public typealias Element = Element
|
||||
|
||||
public typealias Index = Int
|
||||
|
||||
@inlinable
|
||||
public subscript(position: Int) -> Element {
|
||||
get {
|
||||
self.storage[position]
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var startIndex: Int {
|
||||
self.storage.startIndex
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var endIndex: Int {
|
||||
self.storage.endIndex
|
||||
}
|
||||
}
|
||||
|
||||
extension _TinyArray {
|
||||
@inlinable
|
||||
public init(_ elements: some Sequence<Element>) {
|
||||
self.storage = .init(elements)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public init(_ elements: some Sequence<Result<Element, some Error>>) throws {
|
||||
self.storage = try .init(elements)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public init() {
|
||||
self.storage = .init()
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func append(_ newElement: Element) {
|
||||
self.storage.append(newElement)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func append(contentsOf newElements: some Sequence<Element>){
|
||||
self.storage.append(contentsOf: newElements)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inlinable
|
||||
public mutating func remove(at index: Int) -> Element {
|
||||
self.storage.remove(at: index)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows {
|
||||
try self.storage.removeAll(where: shouldBeRemoved)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func sort(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows {
|
||||
try self.storage.sort(by: areInIncreasingOrder)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - TinyArray.Storage "private" implementation
|
||||
|
||||
extension _TinyArray.Storage: Equatable where Element: Equatable {
|
||||
@inlinable
|
||||
static func ==(lhs: Self, rhs: Self) -> Bool {
|
||||
switch (lhs, rhs) {
|
||||
case (.one(let lhs), .one(let rhs)):
|
||||
return lhs == rhs
|
||||
case (.arbitrary(let lhs), .arbitrary(let rhs)):
|
||||
// we don't use lhs.elementsEqual(rhs) so we can hit the fast path from Array
|
||||
// if both arrays share the same underlying storage: https://github.com/apple/swift/blob/b42019005988b2d13398025883e285a81d323efa/stdlib/public/core/Array.swift#L1775
|
||||
return lhs == rhs
|
||||
|
||||
case (.one(let element), .arbitrary(let array)),
|
||||
(.arbitrary(let array), .one(let element)):
|
||||
guard array.count == 1 else {
|
||||
return false
|
||||
}
|
||||
return element == array[0]
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
extension _TinyArray.Storage: Hashable where Element: Hashable {
|
||||
@inlinable
|
||||
func hash(into hasher: inout Hasher) {
|
||||
// same strategy as Array: https://github.com/apple/swift/blob/b42019005988b2d13398025883e285a81d323efa/stdlib/public/core/Array.swift#L1801
|
||||
hasher.combine(count)
|
||||
for element in self {
|
||||
hasher.combine(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
extension _TinyArray.Storage: Sendable where Element: Sendable {}
|
||||
|
||||
extension _TinyArray.Storage: RandomAccessCollection {
|
||||
@inlinable
|
||||
subscript(position: Int) -> Element {
|
||||
get {
|
||||
switch self {
|
||||
case .one(let element):
|
||||
guard position == 0 else {
|
||||
fatalError("index \(position) out of bounds")
|
||||
}
|
||||
return element
|
||||
case .arbitrary(let elements):
|
||||
return elements[position]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
var startIndex: Int {
|
||||
0
|
||||
}
|
||||
|
||||
@inlinable
|
||||
var endIndex: Int {
|
||||
switch self {
|
||||
case .one: return 1
|
||||
case .arbitrary(let elements): return elements.endIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension _TinyArray.Storage {
|
||||
@inlinable
|
||||
init(_ elements: some Sequence<Element>) {
|
||||
self = .arbitrary([])
|
||||
self.append(contentsOf: elements)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
init(_ newElements: some Sequence<Result<Element, some Error>>) throws {
|
||||
var iterator = newElements.makeIterator()
|
||||
guard let firstElement = try iterator.next()?.get() else {
|
||||
self = .arbitrary([])
|
||||
return
|
||||
}
|
||||
guard let secondElement = try iterator.next()?.get() else {
|
||||
// newElements just contains a single element
|
||||
// and we hit the fast path
|
||||
self = .one(firstElement)
|
||||
return
|
||||
}
|
||||
|
||||
var elements: [Element] = []
|
||||
elements.reserveCapacity(newElements.underestimatedCount)
|
||||
elements.append(firstElement)
|
||||
elements.append(secondElement)
|
||||
while let nextElement = try iterator.next()?.get() {
|
||||
elements.append(nextElement)
|
||||
}
|
||||
self = .arbitrary(elements)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
init() {
|
||||
self = .arbitrary([])
|
||||
}
|
||||
|
||||
@inlinable
|
||||
mutating func append(_ newElement: Element) {
|
||||
self.append(contentsOf: CollectionOfOne(newElement))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
mutating func append(contentsOf newElements: some Sequence<Element>){
|
||||
switch self {
|
||||
case .one(let firstElement):
|
||||
var iterator = newElements.makeIterator()
|
||||
guard let secondElement = iterator.next() else {
|
||||
// newElements is empty, nothing to do
|
||||
return
|
||||
}
|
||||
var elements: [Element] = []
|
||||
elements.reserveCapacity(1 + newElements.underestimatedCount)
|
||||
elements.append(firstElement)
|
||||
elements.append(secondElement)
|
||||
elements.appendRemainingElements(from: &iterator)
|
||||
self = .arbitrary(elements)
|
||||
|
||||
case .arbitrary(var elements):
|
||||
if elements.isEmpty {
|
||||
// if `self` is currently empty and `newElements` just contains a single
|
||||
// element, we skip allocating an array and set `self` to `.one(firstElement)`
|
||||
var iterator = newElements.makeIterator()
|
||||
guard let firstElement = iterator.next() else {
|
||||
// newElements is empty, nothing to do
|
||||
return
|
||||
}
|
||||
guard let secondElement = iterator.next() else {
|
||||
// newElements just contains a single element
|
||||
// and we hit the fast path
|
||||
self = .one(firstElement)
|
||||
return
|
||||
}
|
||||
elements.reserveCapacity(elements.count + newElements.underestimatedCount)
|
||||
elements.append(firstElement)
|
||||
elements.append(secondElement)
|
||||
elements.appendRemainingElements(from: &iterator)
|
||||
self = .arbitrary(elements)
|
||||
|
||||
} else {
|
||||
elements.append(contentsOf: newElements)
|
||||
self = .arbitrary(elements)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inlinable
|
||||
mutating func remove(at index: Int) -> Element {
|
||||
switch self {
|
||||
case .one(let oldElement):
|
||||
guard index == 0 else {
|
||||
fatalError("index \(index) out of bounds")
|
||||
}
|
||||
self = .arbitrary([])
|
||||
return oldElement
|
||||
|
||||
case .arbitrary(var elements):
|
||||
defer {
|
||||
self = .arbitrary(elements)
|
||||
}
|
||||
return elements.remove(at: index)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows {
|
||||
switch self {
|
||||
case .one(let oldElement):
|
||||
if try shouldBeRemoved(oldElement) {
|
||||
self = .arbitrary([])
|
||||
}
|
||||
|
||||
case .arbitrary(var elements):
|
||||
defer {
|
||||
self = .arbitrary(elements)
|
||||
}
|
||||
return try elements.removeAll(where: shouldBeRemoved)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
mutating func sort(by areInIncreasingOrder: (Element, Element) throws -> Bool) rethrows {
|
||||
switch self {
|
||||
case .one:
|
||||
// a collection of just one element is always sorted, nothing to do
|
||||
break
|
||||
case .arbitrary(var elements):
|
||||
defer {
|
||||
self = .arbitrary(elements)
|
||||
}
|
||||
|
||||
try elements.sort(by: areInIncreasingOrder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension Array {
|
||||
@inlinable
|
||||
mutating func appendRemainingElements(from iterator: inout some IteratorProtocol<Element>) {
|
||||
while let nextElement = iterator.next() {
|
||||
append(nextElement)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCertificates open source project
|
||||
//
|
||||
// Copyright (c) 2023 Apple Inc. and the SwiftCertificates project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftCertificates project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import _CertificateInternals
|
||||
|
||||
final class TinyArrayTests: XCTestCase {
|
||||
private func _assertEqual(
|
||||
_ expected: @autoclosure () -> some Sequence<Int>,
|
||||
initial: @autoclosure () -> _TinyArray<Int>,
|
||||
_ mutate: (inout _TinyArray<Int>) -> (),
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
var actual = initial()
|
||||
mutate(&actual)
|
||||
XCTAssertEqual(Array(actual), Array(expected()), file: file, line: line)
|
||||
let expected = _TinyArray(expected())
|
||||
XCTAssertEqual(actual, expected, file: file, line: line)
|
||||
var acutalHasher = Hasher()
|
||||
acutalHasher.combine(actual)
|
||||
var expectedHasher = Hasher()
|
||||
expectedHasher.combine(expected)
|
||||
XCTAssertEqual(acutalHasher.finalize(), expectedHasher.finalize(), "\(actual) does not have the same hash as \(expected)", file: file, line: line)
|
||||
}
|
||||
|
||||
private func assertEqual(
|
||||
_ expected: [Int],
|
||||
initial: @autoclosure () -> _TinyArray<Int> = _TinyArray(),
|
||||
_ mutate: (inout _TinyArray<Int>) -> (),
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
_assertEqual(expected, initial: initial(), mutate, file: file, line: line)
|
||||
// get a sequence that is not an `Array` to hit the slow path as well
|
||||
_assertEqual(expected.lazy.map { $0 }, initial: initial(), mutate, file: file, line: line)
|
||||
}
|
||||
|
||||
func testInit() {
|
||||
XCTAssertEqual(Array(_TinyArray([Int]())), [])
|
||||
XCTAssertEqual(Array(_TinyArray<Int>()), [])
|
||||
XCTAssertEqual(_TinyArray<Int>(), _TinyArray<Int>([]))
|
||||
|
||||
XCTAssertEqual(Array(_TinyArray<Int>(CollectionOfOne(1))), [1])
|
||||
XCTAssertEqual(Array(_TinyArray<Int>([1])), [1])
|
||||
XCTAssertEqual(Array(_TinyArray<Int>([1, 2])), [1, 2])
|
||||
|
||||
XCTAssertEqual(Array(_TinyArray<Int>([1, 2, 3])), [1, 2, 3])
|
||||
XCTAssertEqual(Array(_TinyArray<Int>([1, 2, 3, 4])), [1, 2, 3, 4])
|
||||
XCTAssertEqual(Array(_TinyArray<Int>([1, 2, 3, 4, 5])), [1, 2, 3, 4, 5])
|
||||
}
|
||||
|
||||
func testAppend() {
|
||||
assertEqual([1]) { array in
|
||||
array.append(1)
|
||||
}
|
||||
assertEqual([1, 2]) { array in
|
||||
array.append(1)
|
||||
array.append(2)
|
||||
}
|
||||
assertEqual([1, 2, 3]) { array in
|
||||
array.append(1)
|
||||
array.append(2)
|
||||
array.append(3)
|
||||
}
|
||||
assertEqual([1, 2, 3, 4]) { array in
|
||||
array.append(1)
|
||||
array.append(2)
|
||||
array.append(3)
|
||||
array.append(4)
|
||||
}
|
||||
}
|
||||
|
||||
func testAppendContentsOf() {
|
||||
assertEqual([]) { array in
|
||||
array.append(contentsOf: [])
|
||||
}
|
||||
assertEqual([]) { array in
|
||||
array.append(contentsOf: [])
|
||||
array.append(contentsOf: [])
|
||||
}
|
||||
assertEqual([1]) { array in
|
||||
array.append(contentsOf: [1])
|
||||
}
|
||||
assertEqual([1]) { array in
|
||||
array.append(contentsOf: [1])
|
||||
array.append(contentsOf: [])
|
||||
}
|
||||
assertEqual([1, 2]) { array in
|
||||
array.append(contentsOf: [1, 2])
|
||||
}
|
||||
assertEqual([1, 2]) { array in
|
||||
array.append(contentsOf: [1, 2])
|
||||
array.append(contentsOf: [])
|
||||
}
|
||||
assertEqual([1, 2, 3]) { array in
|
||||
array.append(contentsOf: [1, 2, 3])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4]) { array in
|
||||
array.append(contentsOf: [1, 2, 3, 4])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4, 5]) { array in
|
||||
array.append(contentsOf: [1, 2, 3, 4, 5])
|
||||
}
|
||||
|
||||
assertEqual([1, 2]) { array in
|
||||
array.append(contentsOf: [1])
|
||||
array.append(contentsOf: [2])
|
||||
}
|
||||
assertEqual([1, 2, 3]) { array in
|
||||
array.append(contentsOf: [1])
|
||||
array.append(contentsOf: [2, 3])
|
||||
}
|
||||
assertEqual([1, 2, 3]) { array in
|
||||
array.append(contentsOf: [1, 2])
|
||||
array.append(contentsOf: [3])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4]) { array in
|
||||
array.append(contentsOf: [1, 2])
|
||||
array.append(contentsOf: [3, 4])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4]) { array in
|
||||
array.append(contentsOf: [1])
|
||||
array.append(contentsOf: [2, 3, 4])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4]) { array in
|
||||
array.append(contentsOf: [1, 2, 3])
|
||||
array.append(contentsOf: [4])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4, 5]) { array in
|
||||
array.append(contentsOf: [1, 2, 3, 4])
|
||||
array.append(contentsOf: [5])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4, 5]) { array in
|
||||
array.append(contentsOf: [1, 2, 3])
|
||||
array.append(contentsOf: [4, 5])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4, 5]) { array in
|
||||
array.append(contentsOf: [1, 2])
|
||||
array.append(contentsOf: [3, 4, 5])
|
||||
}
|
||||
assertEqual([1, 2, 3, 4, 5]) { array in
|
||||
array.append(contentsOf: [1])
|
||||
array.append(contentsOf: [2, 3, 4, 5])
|
||||
}
|
||||
}
|
||||
|
||||
func testRemoveAt() {
|
||||
assertEqual([], initial: [1]) { array in
|
||||
array.remove(at: 0)
|
||||
}
|
||||
assertEqual([1], initial: [1, 2]) { array in
|
||||
array.remove(at: 1)
|
||||
}
|
||||
assertEqual([2], initial: [1, 2]) { array in
|
||||
array.remove(at: 0)
|
||||
}
|
||||
assertEqual([], initial: [1, 2]) { array in
|
||||
array.remove(at: 1)
|
||||
array.remove(at: 0)
|
||||
}
|
||||
assertEqual([], initial: [1, 2]) { array in
|
||||
array.remove(at: 0)
|
||||
array.remove(at: 0)
|
||||
}
|
||||
assertEqual([1, 2], initial: [1, 2, 3]) { array in
|
||||
array.remove(at: 2)
|
||||
}
|
||||
assertEqual([1, 3], initial: [1, 2, 3]) { array in
|
||||
array.remove(at: 1)
|
||||
}
|
||||
assertEqual([2, 3], initial: [1, 2, 3]) { array in
|
||||
array.remove(at: 0)
|
||||
}
|
||||
assertEqual([1], initial: [1, 2, 3]) { array in
|
||||
array.remove(at: 1)
|
||||
array.remove(at: 1)
|
||||
}
|
||||
assertEqual([2], initial: [1, 2, 3]) { array in
|
||||
array.remove(at: 0)
|
||||
array.remove(at: 1)
|
||||
}
|
||||
assertEqual([3], initial: [1, 2, 3]) { array in
|
||||
array.remove(at: 1)
|
||||
array.remove(at: 0)
|
||||
}
|
||||
assertEqual([], initial: [1, 2, 3]) { array in
|
||||
array.remove(at: 2)
|
||||
array.remove(at: 1)
|
||||
array.remove(at: 0)
|
||||
}
|
||||
assertEqual([], initial: [1, 2, 3]) { array in
|
||||
array.remove(at: 0)
|
||||
array.remove(at: 0)
|
||||
array.remove(at: 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testRemoveAll() {
|
||||
assertEqual([], initial: []) { array in
|
||||
array.removeAll(where: { _ in true })
|
||||
}
|
||||
assertEqual([], initial: [1]) { array in
|
||||
array.removeAll(where: { _ in true })
|
||||
}
|
||||
assertEqual([], initial: [1, 2]) { array in
|
||||
array.removeAll(where: { _ in true })
|
||||
}
|
||||
assertEqual([], initial: [1, 2, 3]) { array in
|
||||
array.removeAll(where: { _ in true })
|
||||
}
|
||||
assertEqual([], initial: [1, 2, 3, 4]) { array in
|
||||
array.removeAll(where: { _ in true })
|
||||
}
|
||||
assertEqual([], initial: [1, 2, 3, 4, 5]) { array in
|
||||
array.removeAll(where: { _ in true })
|
||||
}
|
||||
|
||||
assertEqual([1], initial: [1]) { array in
|
||||
array.removeAll(where: { _ in false })
|
||||
}
|
||||
assertEqual([1, 2], initial: [1, 2]) { array in
|
||||
array.removeAll(where: { _ in false })
|
||||
}
|
||||
assertEqual([1, 2, 3], initial: [1, 2, 3]) { array in
|
||||
array.removeAll(where: { _ in false })
|
||||
}
|
||||
assertEqual([1, 2, 3, 4], initial: [1, 2, 3, 4]) { array in
|
||||
array.removeAll(where: { _ in false })
|
||||
}
|
||||
assertEqual([1, 2, 3, 4, 5], initial: [1, 2, 3, 4, 5]) { array in
|
||||
array.removeAll(where: { _ in false })
|
||||
}
|
||||
|
||||
assertEqual([], initial: [1]) { array in
|
||||
array.removeAll(where: { Set([1]).contains($0) })
|
||||
}
|
||||
assertEqual([2], initial: [1, 2]) { array in
|
||||
array.removeAll(where: { Set([1]).contains($0) })
|
||||
}
|
||||
assertEqual([2, 3], initial: [1, 2, 3]) { array in
|
||||
array.removeAll(where: { Set([1]).contains($0) })
|
||||
}
|
||||
assertEqual([2], initial: [1, 2, 3]) { array in
|
||||
array.removeAll(where: { Set([1, 3]).contains($0) })
|
||||
}
|
||||
}
|
||||
|
||||
func testSort() {
|
||||
assertEqual([], initial: []) { array in
|
||||
array.sort(by: { lhs, rhs in
|
||||
XCTFail("should never be called")
|
||||
return lhs < rhs
|
||||
})
|
||||
}
|
||||
assertEqual([1], initial: [1]) { array in
|
||||
array.sort(by: { lhs, rhs in
|
||||
XCTFail("should never be called")
|
||||
return lhs < rhs
|
||||
})
|
||||
}
|
||||
assertEqual([2, 1], initial: [1, 2]) { array in
|
||||
array.sort(by: >)
|
||||
}
|
||||
assertEqual([3, 2, 1], initial: [1, 2, 3]) { array in
|
||||
array.sort(by: >)
|
||||
}
|
||||
}
|
||||
|
||||
func testThrowingInitFromResult() {
|
||||
XCTAssertEqual(Array(try _TinyArray<Int>(CollectionOfOne(Result<_, Error>.success(1)))), [1])
|
||||
XCTAssertEqual(Array(try _TinyArray([Result<_, Error>.success(1)])), [1])
|
||||
XCTAssertEqual(Array(try _TinyArray([Result<_, Error>.success(1), .success(2)])), [1, 2])
|
||||
XCTAssertEqual(Array(try _TinyArray([Result<_, Error>.success(1), .success(2), .success(3)])), [1, 2, 3])
|
||||
XCTAssertEqual(Array(try _TinyArray([Result<_, Error>.success(1), .success(2), .success(3), .success(4)])), [1, 2, 3, 4])
|
||||
XCTAssertEqual(Array(try _TinyArray([Result<_, Error>.success(1), .success(2), .success(3), .success(4), .success(5)])), [1, 2, 3, 4, 5])
|
||||
|
||||
struct MyError: Error {}
|
||||
|
||||
XCTAssertThrowsError(Array(try _TinyArray<Int>([Result.failure(MyError())])))
|
||||
XCTAssertThrowsError(Array(try _TinyArray<Int>([Result.failure(MyError()), Result.failure(MyError())])))
|
||||
XCTAssertThrowsError(Array(try _TinyArray<Int>([.success(1), Result.failure(MyError())])))
|
||||
XCTAssertThrowsError(Array(try _TinyArray<Int>([.success(1), Result.failure(MyError()), .success(2)])))
|
||||
XCTAssertThrowsError(Array(try _TinyArray<Int>([.success(1), .success(2), Result.failure(MyError())])))
|
||||
XCTAssertThrowsError(Array(try _TinyArray<Int>([.success(1), .success(2), Result.failure(MyError()), .success(4)])))
|
||||
XCTAssertThrowsError(Array(try _TinyArray<Int>([.success(1), .success(2), .success(3), Result.failure(MyError())])))
|
||||
XCTAssertThrowsError(Array(try _TinyArray<Int>([.success(1), .success(2), .success(3), .success(4), Result.failure(MyError())])))
|
||||
}
|
||||
}
|
||||
|
||||
extension _TinyArray: ExpressibleByArrayLiteral {
|
||||
public typealias ArrayLiteralElement = Element
|
||||
|
||||
public init(arrayLiteral elements: Element...) {
|
||||
self.init(elements)
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,8 @@ services:
|
||||
image: swift-certificates:22.04-5.7
|
||||
environment:
|
||||
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
|
||||
- MAX_ALLOCS_ALLOWED_empty_1=0
|
||||
- MAX_ALLOCS_ALLOWED_tiny_array_cow_append_contents_of=9000
|
||||
- MAX_ALLOCS_ALLOWED_tiny_array_non_allocating_operations=0
|
||||
# - SANITIZER_ARG=--sanitize=thread # TSan broken still
|
||||
|
||||
shell:
|
||||
|
||||
@@ -14,7 +14,8 @@ services:
|
||||
environment:
|
||||
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
|
||||
- IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error
|
||||
- MAX_ALLOCS_ALLOWED_empty_1=0
|
||||
- MAX_ALLOCS_ALLOWED_tiny_array_cow_append_contents_of=9000
|
||||
- MAX_ALLOCS_ALLOWED_tiny_array_non_allocating_operations=0
|
||||
# - SANITIZER_ARG=--sanitize=thread # TSan broken still
|
||||
|
||||
shell:
|
||||
|
||||
@@ -13,7 +13,8 @@ services:
|
||||
environment:
|
||||
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
|
||||
- IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error
|
||||
- MAX_ALLOCS_ALLOWED_empty_1=0
|
||||
- MAX_ALLOCS_ALLOWED_tiny_array_cow_append_contents_of=9000
|
||||
- MAX_ALLOCS_ALLOWED_tiny_array_non_allocating_operations=0
|
||||
# - SANITIZER_ARG=--sanitize=thread # TSan broken still
|
||||
|
||||
shell:
|
||||
|
||||
@@ -13,7 +13,8 @@ services:
|
||||
environment:
|
||||
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
|
||||
- IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error
|
||||
- MAX_ALLOCS_ALLOWED_empty_1=0
|
||||
- MAX_ALLOCS_ALLOWED_tiny_array_cow_append_contents_of=9000
|
||||
- MAX_ALLOCS_ALLOWED_tiny_array_non_allocating_operations=0
|
||||
# - SANITIZER_ARG=--sanitize=thread # TSan broken still
|
||||
|
||||
shell:
|
||||
|
||||
@@ -56,4 +56,5 @@ function update_cmakelists_source() {
|
||||
}
|
||||
|
||||
update_cmakelists_source "X509" "*.swift"
|
||||
update_cmakelists_source "_CertificateInternals" "*.swift"
|
||||
|
||||
Reference in new issue
Block a user