-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Add new tests for pointers with explicit nullability.
- Loading branch information
1 parent
bc83940
commit a79a893
Showing
8 changed files
with
384 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// RUN: rm -rf %t && mkdir -p %t && %S/../../utils/gyb %s -o %t/UnsafeBufferPointer.swift | ||
// RUN: %S/../../utils/line-directive %t/UnsafeBufferPointer.swift -- %target-build-swift %t/UnsafeBufferPointer.swift -o %t/a.out | ||
// RUN: %S/../../utils/line-directive %t/UnsafeBufferPointer.swift -- %target-run %t/a.out | ||
// REQUIRES: executable_test | ||
|
||
import StdlibUnittest | ||
|
||
// Also import modules which are used by StdlibUnittest internally. This | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jrose-apple
Author
Contributor
|
||
// workaround is needed to link all required libraries in case we compile | ||
// StdlibUnittest with -sil-serialize-all. | ||
import SwiftPrivate | ||
#if _runtime(_ObjC) | ||
import ObjectiveC | ||
#endif | ||
|
||
var UnsafeBufferPointerTestSuite = TestSuite("UnsafeBufferPointer") | ||
var UnsafeMutableBufferPointerTestSuite = TestSuite("UnsafeMutableBufferPointer") | ||
|
||
% for (SelfName, SelfType, PointerType) in [ | ||
% ('UnsafeBufferPointer', 'UnsafeBufferPointer<Float>', 'UnsafePointer<Float>'), | ||
% ('UnsafeMutableBufferPointer', 'UnsafeMutableBufferPointer<Float>', 'UnsafeMutablePointer<Float>')]: | ||
|
||
${SelfName}TestSuite.test("nilBaseAddress") { | ||
let emptyBuffer = ${SelfType}(start: nil, count: 0) | ||
expectEmpty(emptyBuffer.baseAddress) | ||
expectEqual(0, emptyBuffer.count) | ||
expectTrue(emptyBuffer.startIndex == emptyBuffer.endIndex) | ||
|
||
var iter = emptyBuffer.makeIterator() | ||
expectEmpty(iter.next()) | ||
|
||
expectEqual([], Array(emptyBuffer)) | ||
} | ||
|
||
${SelfName}TestSuite.test("nonNilButEmpty") { | ||
let emptyAllocated = UnsafeMutablePointer<Float>(allocatingCapacity: 0) | ||
defer { emptyAllocated.deallocateCapacity(0) } | ||
|
||
let emptyBuffer = ${SelfType}(start: ${PointerType}(emptyAllocated), count: 0) | ||
expectEqual(emptyAllocated, emptyBuffer.baseAddress) | ||
expectEqual(0, emptyBuffer.count) | ||
expectTrue(emptyBuffer.startIndex == emptyBuffer.endIndex) | ||
|
||
var iter = emptyBuffer.makeIterator() | ||
expectEmpty(iter.next()) | ||
|
||
expectEqual([], Array(emptyBuffer)) | ||
} | ||
|
||
${SelfName}TestSuite.test("nonNilNonEmpty") { | ||
let count = 4 | ||
let allocated = UnsafeMutablePointer<Float>(allocatingCapacity: count) | ||
defer { allocated.deallocateCapacity(count) } | ||
allocated.initialize(with: 1.0, count: count) | ||
allocated[count - 1] = 2.0 | ||
|
||
let buffer = ${SelfType}(start: ${PointerType}(allocated), count: count - 1) | ||
expectEqual(allocated, buffer.baseAddress) | ||
expectEqual(count - 1, buffer.count) | ||
expectEqual(count - 1, buffer.endIndex - buffer.startIndex) | ||
|
||
allocated[1] = 0.0 | ||
expectEqual(1.0, buffer[0]) | ||
expectEqual(0.0, buffer[1]) | ||
expectEqual(1.0, buffer[2]) | ||
|
||
var iter = buffer.makeIterator() | ||
expectEqual(1.0, iter.next()) | ||
expectEqual(0.0, iter.next()) | ||
expectEqual(1.0, iter.next()) | ||
expectEmpty(iter.next()) | ||
|
||
expectEqual([1.0, 0.0, 1.0], Array(buffer)) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
expectEqual(2.0, allocated[count-1]) | ||
} | ||
|
||
${SelfName}TestSuite.test("badCount") | ||
.skip(.custom( | ||
{ _isFastAssertConfiguration() }, | ||
reason: "this trap is not guaranteed to happen in -Ounchecked")) | ||
.code { | ||
expectCrashLater() | ||
|
||
let emptyAllocated = UnsafeMutablePointer<Float>(allocatingCapacity: 0) | ||
defer { emptyAllocated.deallocateCapacity(0) } | ||
|
||
let buffer = ${SelfType}(start: ${PointerType}(emptyAllocated), count: -1) | ||
_ = buffer | ||
} | ||
|
||
${SelfName}TestSuite.test("badNilCount") | ||
.skip(.custom( | ||
{ _isFastAssertConfiguration() }, | ||
reason: "this trap is not guaranteed to happen in -Ounchecked")) | ||
.code { | ||
expectCrashLater() | ||
|
||
let buffer = ${SelfType}(start: nil, count: 1) | ||
_ = buffer | ||
} | ||
|
||
% end | ||
|
||
UnsafeMutableBufferPointerTestSuite.test("changeElementViaBuffer") { | ||
let count = 4 | ||
let allocated = UnsafeMutablePointer<Float>(allocatingCapacity: count) | ||
defer { allocated.deallocateCapacity(count) } | ||
allocated.initialize(with: 1.0, count: count) | ||
allocated[count-1] = -1.0 | ||
|
||
var buffer = UnsafeMutableBufferPointer(start: allocated, count: count - 1) | ||
|
||
buffer[1] = 0.0 | ||
expectEqual(1.0, buffer[0]) | ||
expectEqual(0.0, buffer[1]) | ||
expectEqual(1.0, buffer[2]) | ||
|
||
expectEqual(1.0, allocated[0]) | ||
expectEqual(0.0, allocated[1]) | ||
expectEqual(1.0, allocated[2]) | ||
expectEqual(-1.0, allocated[count-1]) | ||
|
||
buffer.sort() | ||
expectEqual(0.0, buffer[0]) | ||
expectEqual(1.0, buffer[1]) | ||
expectEqual(1.0, buffer[2]) | ||
|
||
expectEqual(0.0, allocated[0]) | ||
expectEqual(1.0, allocated[1]) | ||
expectEqual(1.0, allocated[2]) | ||
expectEqual(-1.0, allocated[count-1]) | ||
} | ||
|
||
runAllTests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
I thought Slava fixed this issue. (These imports also appear in
test/Interpreter/SDK/errors.swift
.)