mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
The `_trim(first:)` and `_trim(last:)` helpers reassign `self` to the
remaining slice and then build the trimmed result from `self`'s now-updated
`baseAddress`/`count`, so they hand back a region that points into the wrong
place in the buffer.
`UnsafeMutableBufferPointer._trim(first:)` had this exact bug fixed earlier (it
captures `oldStart` before the reassignment), but three sibling helpers were
left untouched:
* `UnsafeBufferPointer._trim(first:)` reads `baseAddress` after advancing
`self`, so the returned prefix starts at the remainder instead of at the
original head.
* `UnsafeBufferPointer._trim(last:)` and
`UnsafeMutableBufferPointer._trim(last:)` recompute `count &- cut` after
shrinking `self`, so the returned suffix starts `cut` elements too early.
For a five-element buffer, `_trim(first: 2)` returned the bytes of `[30, 40]`
rather than `[10, 20]`, and `_trim(last: 2)` returned `[20, 30]` rather than
`[40, 50]`; trimming more than the count walked off the end of the buffer.
Capture the original start (and, for the suffix case, the post-trim count)
before mutating `self`, matching the already-correct `_trim(first:)` on the
mutable buffer. Adds direct tests covering both overloads on both buffer types.
111 lines
3.5 KiB
Swift
111 lines
3.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2026 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
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import XCTest
|
|
#if COLLECTIONS_SINGLE_MODULE
|
|
@_spi(Testing) import Collections
|
|
#else
|
|
import _CollectionsTestSupport
|
|
import InternalCollectionsUtilities
|
|
#endif
|
|
|
|
/// Tests for the `_trim(first:)` and `_trim(last:)` helpers on the unsafe
|
|
/// buffer pointer types in `InternalCollectionsUtilities`. These are used by
|
|
/// the deque consumption/segment machinery, so a wrong result here corrupts
|
|
/// the spans that get handed out to clients.
|
|
final class UnsafeBufferTrimTests: CollectionTestCase {
|
|
func test_mutableBuffer_trimFirst() {
|
|
let values = [10, 20, 30, 40, 50]
|
|
values.withUnsafeBufferPointer { source in
|
|
let storage = UnsafeMutableBufferPointer<Int>.allocate(capacity: source.count)
|
|
defer { storage.deallocate() }
|
|
_ = storage.initialize(fromContentsOf: source)
|
|
defer { storage.deinitialize() }
|
|
|
|
var rest = storage
|
|
let head = rest._trim(first: 2)
|
|
expectEqual(Array(head), [10, 20])
|
|
expectEqual(Array(rest), [30, 40, 50])
|
|
|
|
// Trimming more than the count yields everything.
|
|
var all = storage
|
|
let everything = all._trim(first: 99)
|
|
expectEqual(Array(everything), [10, 20, 30, 40, 50])
|
|
expectEqual(all.count, 0)
|
|
}
|
|
}
|
|
|
|
func test_mutableBuffer_trimLast() {
|
|
let values = [10, 20, 30, 40, 50]
|
|
values.withUnsafeBufferPointer { source in
|
|
let storage = UnsafeMutableBufferPointer<Int>.allocate(capacity: source.count)
|
|
defer { storage.deallocate() }
|
|
_ = storage.initialize(fromContentsOf: source)
|
|
defer { storage.deinitialize() }
|
|
|
|
var rest = storage
|
|
let tail = rest._trim(last: 2)
|
|
expectEqual(Array(tail), [40, 50])
|
|
expectEqual(Array(rest), [10, 20, 30])
|
|
|
|
var all = storage
|
|
let everything = all._trim(last: 99)
|
|
expectEqual(Array(everything), [10, 20, 30, 40, 50])
|
|
expectEqual(all.count, 0)
|
|
}
|
|
}
|
|
|
|
func test_immutableBuffer_trimFirst() {
|
|
let values = [10, 20, 30, 40, 50]
|
|
values.withUnsafeBufferPointer { source in
|
|
var rest = source
|
|
let head = rest._trim(first: 2)
|
|
expectEqual(Array(head), [10, 20])
|
|
expectEqual(Array(rest), [30, 40, 50])
|
|
|
|
var all = source
|
|
let everything = all._trim(first: 99)
|
|
expectEqual(Array(everything), [10, 20, 30, 40, 50])
|
|
expectEqual(all.count, 0)
|
|
}
|
|
}
|
|
|
|
func test_immutableBuffer_trimLast() {
|
|
let values = [10, 20, 30, 40, 50]
|
|
values.withUnsafeBufferPointer { source in
|
|
var rest = source
|
|
let tail = rest._trim(last: 2)
|
|
expectEqual(Array(tail), [40, 50])
|
|
expectEqual(Array(rest), [10, 20, 30])
|
|
|
|
var all = source
|
|
let everything = all._trim(last: 99)
|
|
expectEqual(Array(everything), [10, 20, 30, 40, 50])
|
|
expectEqual(all.count, 0)
|
|
}
|
|
}
|
|
|
|
func test_trim_zeroIsNoOp() {
|
|
let values = [1, 2, 3]
|
|
values.withUnsafeBufferPointer { source in
|
|
var a = source
|
|
expectEqual(a._trim(first: 0).count, 0)
|
|
expectEqual(Array(a), [1, 2, 3])
|
|
|
|
var b = source
|
|
expectEqual(b._trim(last: 0).count, 0)
|
|
expectEqual(Array(b), [1, 2, 3])
|
|
}
|
|
}
|
|
}
|