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
[stdlib] [WIP] Add UnsafeRawBufferPointer.initialize(as:from:) #5718
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,59 @@ UnsafeRawBufferPointerTestSuite.test("initFromArray") { | |
expectEqual(array2, array1) | ||
} | ||
|
||
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).underflow") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused by the underflow terminology here. This sequence still overflows the buffer right? Isn't it an overflow with underestimated count? I don't see a test case for actual underflow! |
||
let buffer = UnsafeMutableRawBufferPointer.allocate(count: 30) | ||
defer { buffer.deallocate() } | ||
let source = stride(from: 5 as Int64, to: 0, by: -1) | ||
var (it,idx) = buffer.initializeMemory(as: Int64.self, from: source) | ||
expectEqual(it.next()!, 2) | ||
expectEqual(idx, 24) | ||
([5, 4, 3] as [Int64]).withUnsafeBytes { | ||
expectEqualSequence($0,buffer[0..<idx]) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this testing the situation where the underEstimate is actually too small? (Not sure how accurate stride is) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but that's allowed (it is an underestimate after all). Your point about testing exactly-rightly-sized buffers too is taken tho. |
||
|
||
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).overflow") { | ||
let buffer = UnsafeMutableRawBufferPointer.allocate(count: 30) | ||
defer { buffer.deallocate() } | ||
let source: [Int64] = [5, 4, 3, 2, 1] | ||
if _isDebugAssertConfiguration() { | ||
expectCrashLater() | ||
} | ||
var (it, idx) = buffer.initializeMemory(as: Int64.self, from: source) | ||
expectEqual(it.next()!, 2) | ||
expectEqual(idx, 24) | ||
([5, 4, 3] as [Int64]).withUnsafeBytes { | ||
expectEqualSequence($0,buffer[0..<idx]) | ||
} | ||
} | ||
|
||
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).exact") { | ||
let buffer = UnsafeMutableRawBufferPointer.allocate(count: 24) | ||
defer { buffer.deallocate() } | ||
let source: [Int64] = [5, 4, 3] | ||
var (it, idx) = buffer.initializeMemory(as: Int64.self, from: source) | ||
expectNil(it.next()) | ||
expectEqual(idx, buffer.endIndex) | ||
source.withUnsafeBytes { expectEqualSequence($0,buffer) } | ||
} | ||
|
||
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).invalidNilPtr") { | ||
let buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) | ||
let source: [Int64] = [5, 4, 3, 2, 1] | ||
expectCrashLater() | ||
var (it, idx) = buffer.initializeMemory(as: Int64.self, from: source) | ||
} | ||
|
||
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).validNilPtr") { | ||
let buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) | ||
let source: [Int64] = [] | ||
var (it, idx) = buffer.initializeMemory(as: Int64.self, from: source) | ||
expectNil(it.next()) | ||
expectEqual(idx, source.endIndex) | ||
} | ||
|
||
|
||
// Directly test the byte Sequence produced by withUnsafeBytes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should have tests verifying both of the null pointer behaviours. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should have a test verifying the case where the iterator is exhausted with slack space. (the final sequence is empty) Possibly a test that verifies that the "exactly enough space" case works (off by ones will getcha!). |
||
UnsafeRawBufferPointerTestSuite.test("withUnsafeBytes.Sequence") { | ||
let array1: [Int32] = [0, 1, 2, 3] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it fair game to refer to initializedUpTo here?