Skip to content

Commit d777f20

Browse files
committed
[test] read/modify UnsafeBufferPointer through Slice
1 parent 68a4b43 commit d777f20

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

validation-test/stdlib/UnsafeBufferPointer.swift.gyb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,91 @@ UnsafeMutableBufferPointerTestSuite.test("changeElementViaBuffer") {
326326
expectEqual(-1.0, allocated[count-1])
327327
}
328328

329+
let bufCount = 4
330+
let sliceRange: Range = 1..<bufCount-1
331+
% for mutable in [True, False]:
332+
% for raw in [True, False]:
333+
% for action in ['read', 'change']:
334+
% if action != 'change' or mutable:
335+
for testIndex in (0..<bufCount) {
336+
% Type = 'Unsafe' + ('Mutable' if mutable else '') + ('Raw' if raw else '') + 'BufferPointer'
337+
${Type}TestSuite.test("${action}Element\(testIndex)ViaSlice") {
338+
% if raw:
339+
let allocated = UnsafeMutableRawPointer.allocate(bytes: bufCount, alignedTo: 8)
340+
for i in 0..<bufCount {
341+
allocated.storeBytes(of: UInt8(i), toByteOffset: i, as: UInt8.self)
342+
}
343+
% else:
344+
let allocated = UnsafeMutablePointer<Int>.allocate(capacity: bufCount)
345+
for i in 0..<bufCount { allocated[i] = i }
346+
% end # if mutable
347+
defer { allocated.deallocate() }
348+
349+
let buffer = ${Type}(start: allocated, count: bufCount)
350+
${'var' if mutable else 'let'} slice = buffer[sliceRange]
351+
352+
if _isDebugAssertConfiguration(),
353+
testIndex < sliceRange.lowerBound ||
354+
testIndex >= sliceRange.upperBound {
355+
expectCrashLater()
356+
}
357+
% if action == 'read':
358+
let value = slice[testIndex]
359+
expectEqual(buffer[testIndex], value)
360+
% else:
361+
slice[testIndex] = 103
362+
expectEqual(buffer[testIndex], 103)
363+
% end # if action
364+
}
365+
}
366+
% end # if action or mutable
367+
% end # for action
368+
% end # for raw
369+
% end # for mutable
370+
371+
let testRanges = (0..<bufCount-1).map({ i -> Range<Int> in i..<i+2 })
372+
% for mutable in [True, False]:
373+
% for raw in [True, False]:
374+
% for action in ['read', 'change']:
375+
% if action != 'change' or mutable:
376+
for testRange in testRanges {
377+
% Type = 'Unsafe' + ('Mutable' if mutable else '') + ('Raw' if raw else '') + 'BufferPointer'
378+
${Type}TestSuite.test("${action}Slice\(testRange)ViaSlice") {
379+
% if raw:
380+
let allocated = UnsafeMutableRawPointer.allocate(bytes: bufCount+2, alignedTo: 8)
381+
for i in 0..<bufCount+2 {
382+
allocated.storeBytes(of: UInt8(i), toByteOffset: i, as: UInt8.self)
383+
}
384+
% else:
385+
let allocated = UnsafeMutablePointer<Int>.allocate(capacity: bufCount+2)
386+
for i in 0..<bufCount+2 { allocated[i] = i }
387+
% end # if mutable
388+
defer { allocated.deallocate() }
389+
390+
let buffer = ${Type}(start: allocated, count: bufCount+2)
391+
${'var' if mutable else 'let'} slice = buffer[sliceRange]
392+
393+
if _isDebugAssertConfiguration(),
394+
testRange.lowerBound < sliceRange.lowerBound ||
395+
testRange.upperBound > sliceRange.upperBound {
396+
expectCrashLater()
397+
}
398+
399+
% if action == 'read':
400+
let value = slice[testRange]
401+
expectEqualSequence(buffer[testRange], value)
402+
% else:
403+
let sourceSlice = buffer[bufCount..<bufCount+2]
404+
slice[testRange] = sourceSlice
405+
expectEqualSequence(buffer[testRange], sourceSlice)
406+
% end # if action
407+
}
408+
}
409+
% end # if action or mutable
410+
% end # for action
411+
% end # for raw
412+
% end # for mutable
413+
329414
protocol SubscriptTest {
330415
static var start: Int { get }
331416
static var end: Int { get }

0 commit comments

Comments
 (0)