Skip to content

Commit 252d437

Browse files
authored
Merge pull request #3087 from austinzheng/az-s3c
2 parents bd2091f + dfcc13d commit 252d437

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

stdlib/private/StdlibCollectionUnittest/RangeSelection.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public enum RangeSelection {
1919
case middle
2020
case leftHalf
2121
case rightHalf
22+
case full
2223
case offsets(Int, Int)
2324

2425
public var isEmpty: Bool {
@@ -45,6 +46,8 @@ public enum RangeSelection {
4546
let start = c.index(c.startIndex, offsetBy: c.count / 2)
4647
let end = c.endIndex
4748
return start..<end
49+
case .full:
50+
return c.startIndex..<c.endIndex
4851
case let .offsets(lowerBound, upperBound):
4952
let start = c.index(c.startIndex, offsetBy: numericCast(lowerBound))
5053
let end = c.index(c.startIndex, offsetBy: numericCast(upperBound))
@@ -75,6 +78,9 @@ public enum RangeSelection {
7578
let start = c.index(c.startIndex, offsetBy: c.count / 2)
7679
let beforeEnd = c.index(c.startIndex, offsetBy: c.count - 1)
7780
return start...beforeEnd
81+
case .full:
82+
let beforeEnd = c.index(c.startIndex, offsetBy: c.count - 1)
83+
return c.startIndex...beforeEnd
7884
case let .offsets(lowerBound, upperBound):
7985
let start = c.index(c.startIndex, offsetBy: numericCast(lowerBound))
8086
let end = c.index(c.startIndex, offsetBy: numericCast(upperBound))

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,15 @@ public struct Unsafe${Mutable}BufferPointer<Element>
162162
-> ${Mutable}RandomAccessSlice<Unsafe${Mutable}BufferPointer<Element>>
163163
{
164164
get {
165-
// FIXME: swift-3-indexing-model: range check.
166-
// FIXME: swift-3-indexing-model: tests.
165+
_debugPrecondition(bounds.lowerBound >= startIndex)
166+
_debugPrecondition(bounds.upperBound <= endIndex)
167167
return ${Mutable}RandomAccessSlice(
168168
base: self, bounds: bounds)
169169
}
170170
% if Mutable:
171171
set {
172-
// FIXME: swift-3-indexing-model: range check.
172+
_debugPrecondition(bounds.lowerBound >= startIndex)
173+
_debugPrecondition(bounds.upperBound <= endIndex)
173174
// FIXME: swift-3-indexing-model: tests.
174175
_writeBackMutableSlice(&self, bounds: bounds, slice: newValue)
175176
}

validation-test/stdlib/UnsafeBufferPointer.swift.gyb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,81 @@
44
import StdlibUnittest
55
import StdlibCollectionUnittest
66

7+
// Tests
8+
9+
struct SubscriptGetTest {
10+
// SubscriptGetTest operates on a `(end - start)` sized buffer containing
11+
// monotonically increasing integers from `start` to `end - 1`.
12+
static let start = 0
13+
static let end = 20
14+
let rangeSelection: RangeSelection
15+
/// The values that should be expected by slicing the UBP, or `nil` if the
16+
/// test is expected to crash.
17+
let expectedValues: [Int]?
18+
let loc: SourceLoc
19+
20+
static var elementCount = (end - start)
21+
22+
% for SelfType in ['UnsafeBufferPointer', 'UnsafeMutableBufferPointer']:
23+
/// Create and populate an `${SelfType}` for use with unit tests.
24+
/// PRECONDITION: `memory` must be allocated with space for
25+
/// `SubscriptGetTest.elementCount` elements.
26+
func create${SelfType}(from memory: UnsafeMutablePointer<OpaqueValue<Int>>)
27+
-> ${SelfType}<OpaqueValue<Int>>
28+
{
29+
for i in SubscriptGetTest.start..<SubscriptGetTest.end {
30+
memory[i] = OpaqueValue(i)
31+
}
32+
return ${SelfType}(start: memory, count: SubscriptGetTest.elementCount)
33+
}
34+
% end
35+
36+
init(
37+
rangeSelection: RangeSelection, expectedValues: [Int]? = nil,
38+
file: String = #file, line: UInt = #line
39+
) {
40+
self.rangeSelection = rangeSelection
41+
self.expectedValues = expectedValues
42+
self.loc = SourceLoc(file, line, comment: "test data")
43+
}
44+
}
45+
46+
let subscriptGetTests : [SubscriptGetTest] = [
47+
// Valid, empty.
48+
SubscriptGetTest(rangeSelection: .emptyRange, expectedValues: []),
49+
50+
// Valid, edges.
51+
SubscriptGetTest(rangeSelection: .leftEdge, expectedValues: [0]),
52+
SubscriptGetTest(rangeSelection: .rightEdge, expectedValues: [19]),
53+
54+
// Valid, internal.
55+
SubscriptGetTest(rangeSelection: .leftHalf,
56+
expectedValues: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
57+
SubscriptGetTest(rangeSelection: .rightHalf,
58+
expectedValues: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
59+
SubscriptGetTest(rangeSelection: .middle,
60+
expectedValues: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]),
61+
SubscriptGetTest(rangeSelection: .full,
62+
expectedValues: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
63+
64+
// Invalid, bottom out of bounds.
65+
SubscriptGetTest(rangeSelection: .offsets(-1, -1)),
66+
SubscriptGetTest(rangeSelection: .offsets(-1, 0)),
67+
SubscriptGetTest(rangeSelection: .offsets(-100, 5)),
68+
69+
// Invalid, top out of bounds.
70+
SubscriptGetTest(rangeSelection: .offsets(20, 20)),
71+
SubscriptGetTest(rangeSelection: .offsets(19, 20)),
72+
SubscriptGetTest(rangeSelection: .offsets(5, 100)),
73+
74+
// Invalid, both out of bounds.
75+
SubscriptGetTest(rangeSelection: .offsets(-1, 20)),
76+
SubscriptGetTest(rangeSelection: .offsets(-100, 100)),
77+
]
78+
79+
80+
// Test Suites
81+
782
var UnsafeBufferPointerTestSuite = TestSuite("UnsafeBufferPointer")
883
var UnsafeMutableBufferPointerTestSuite = TestSuite("UnsafeMutableBufferPointer")
984

@@ -105,6 +180,34 @@ ${SelfName}TestSuite.test("badNilCount")
105180
_ = buffer
106181
}
107182

183+
% for RangeName in ['range', 'countableRange', 'closedRange', 'countableClosedRange']:
184+
${SelfName}TestSuite.test("subscript/get").forEach(in: subscriptGetTests) {
185+
(test) in
186+
187+
% if 'closed' in RangeName.lower():
188+
if test.rangeSelection.isEmpty {
189+
return
190+
}
191+
% end
192+
193+
let elementCount = SubscriptGetTest.elementCount
194+
195+
var memory = UnsafeMutablePointer<OpaqueValue<Int>>(allocatingCapacity: elementCount)
196+
let buffer = test.create${SelfName}(from: memory)
197+
defer { memory.deallocateCapacity(elementCount) }
198+
199+
let range = test.rangeSelection.${RangeName}(in: buffer)
200+
201+
if test.expectedValues == nil { expectCrashLater() }
202+
let slice = buffer[range]
203+
expectEqual(
204+
test.expectedValues!,
205+
slice.map { $0.value },
206+
stackTrace: SourceLocStack().with(test.loc)
207+
)
208+
}
209+
% end
210+
108211
% end
109212

110213
UnsafeMutableBufferPointerTestSuite.test("changeElementViaBuffer") {

0 commit comments

Comments
 (0)