Skip to content

[stdlib] Fix UnsafeBufferPointer range subscript getter tests #3233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions validation-test/stdlib/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct SubscriptGetTest {
/// The values that should be expected by slicing the UBP, or `nil` if the
/// test is expected to crash.
let expectedValues: [Int]?
/// Same as `expectedValues`, but for closed ranges. `nil` if no difference.
let expectedClosedValues: [Int]?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is let subscriptRangeTests in StdlibCollectionUnittest, which is very similar in intent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests don't seem to be instrumented for use with the different types of ranges. Would a follow-up item to fix that be of interest to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, having more powerful shared datasets would make all tests stronger.

let loc: SourceLoc

static var elementCount = (end - start)
Expand All @@ -35,10 +37,12 @@ struct SubscriptGetTest {

init(
rangeSelection: RangeSelection, expectedValues: [Int]? = nil,
expectedClosedValues: [Int]? = nil,
file: String = #file, line: UInt = #line
) {
self.rangeSelection = rangeSelection
self.expectedValues = expectedValues
self.expectedClosedValues = expectedClosedValues ?? expectedValues
self.loc = SourceLoc(file, line, comment: "test data")
}
}
Expand All @@ -48,8 +52,12 @@ let subscriptGetTests : [SubscriptGetTest] = [
SubscriptGetTest(rangeSelection: .emptyRange, expectedValues: []),

// Valid, edges.
SubscriptGetTest(rangeSelection: .leftEdge, expectedValues: [0]),
SubscriptGetTest(rangeSelection: .rightEdge, expectedValues: [19]),
SubscriptGetTest(rangeSelection: .leftEdge,
expectedValues: [],
expectedClosedValues: [0]),
SubscriptGetTest(rangeSelection: .rightEdge,
expectedValues: [],
expectedClosedValues: [19]),

// Valid, internal.
SubscriptGetTest(rangeSelection: .leftHalf,
Expand All @@ -60,6 +68,12 @@ let subscriptGetTests : [SubscriptGetTest] = [
expectedValues: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]),
SubscriptGetTest(rangeSelection: .full,
expectedValues: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
SubscriptGetTest(rangeSelection: .offsets(2, 4),
expectedValues: [2, 3],
expectedClosedValues: [2, 3, 4]),
SubscriptGetTest(rangeSelection: .offsets(16, 19),
expectedValues: [16, 17, 18],
expectedClosedValues: [16, 17, 18, 19]),

// Invalid, bottom out of bounds.
SubscriptGetTest(rangeSelection: .offsets(-1, -1)),
Expand Down Expand Up @@ -181,13 +195,17 @@ ${SelfName}TestSuite.test("badNilCount")
}

% for RangeName in ['range', 'countableRange', 'closedRange', 'countableClosedRange']:
${SelfName}TestSuite.test("subscript/get").forEach(in: subscriptGetTests) {
${SelfName}TestSuite.test("subscript/${RangeName}/get").forEach(in: subscriptGetTests) {
(test) in

let expectedValues: [Int]?
% if 'closed' in RangeName.lower():
if test.rangeSelection.isEmpty {
return
}
expectedValues = test.expectedClosedValues
% else:
expectedValues = test.expectedValues
% end

let elementCount = SubscriptGetTest.elementCount
Expand All @@ -198,10 +216,10 @@ ${SelfName}TestSuite.test("subscript/get").forEach(in: subscriptGetTests) {

let range = test.rangeSelection.${RangeName}(in: buffer)

if test.expectedValues == nil { expectCrashLater() }
if expectedValues == nil { expectCrashLater() }
let slice = buffer[range]
expectEqual(
test.expectedValues!,
expectedValues!,
slice.map { $0.value },
stackTrace: SourceLocStack().with(test.loc)
)
Expand Down