Skip to content

Commit

Permalink
[test] Add new tests for pointers with explicit nullability.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrose-apple committed Apr 12, 2016
1 parent bc83940 commit a79a893
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 75 deletions.
135 changes: 135 additions & 0 deletions test/1_stdlib/UnsafeBufferPointer.swift.gyb
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.

Copy link
@gribozavr

gribozavr Apr 15, 2016

Contributor

I thought Slava fixed this issue. (These imports also appear in test/Interpreter/SDK/errors.swift.)

This comment has been minimized.

Copy link
@jrose-apple

jrose-apple Apr 15, 2016

Author Contributor

Ah, you're right! Didn't realize that had gone in while this was in progress.

// 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.

Copy link
@gribozavr

gribozavr Apr 15, 2016

Contributor

expectEqualSequence


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()
44 changes: 44 additions & 0 deletions test/1_stdlib/UnsafePointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,73 @@ ${SelfName}TestSuite.test("initFromOpaquePointer") {
let other = OpaquePointer(bitPattern: 0x12345678)!
let ptr = UnsafePointer<Float>(other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))

let optionalOther: Optional = other
let optionalPointer = UnsafePointer<Float>(optionalOther)
expectNotEmpty(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))

let nilOther: Optional<OpaquePointer> = nil
let nilPointer = UnsafePointer<Float>(nilOther)
expectEmpty(nilPointer)
}

${SelfName}TestSuite.test("initFromUnsafePointer") {
let other = UnsafePointer<Double>(bitPattern: 0x12345678)!
let ptr = ${SelfType}(other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))

let optionalOther: Optional = other
let optionalPointer = ${SelfType}(optionalOther)
expectNotEmpty(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))

let nilOther: Optional<UnsafePointer<Double>> = nil
let nilPointer = ${SelfType}(nilOther)
expectEmpty(nilPointer)
}

${SelfName}TestSuite.test("initFromUnsafeMutablePointer") {
let other = UnsafeMutablePointer<Double>(bitPattern: 0x12345678)!
let ptr = ${SelfType}(other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))

let optionalOther: Optional = other
let optionalPointer = ${SelfType}(optionalOther)
expectNotEmpty(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))

let nilOther: Optional<UnsafeMutablePointer<Double>> = nil
let nilPointer = ${SelfType}(nilOther)
expectEmpty(nilPointer)
}

${SelfName}TestSuite.test("initFromInteger") {
do {
let word: Int = 0x12345678
let ptr = ${SelfType}(bitPattern: word)
expectNotEmpty(ptr)
expectEqual(word, unsafeBitCast(ptr, to: Int.self))
}
do {
let uword: UInt = 0x12345678
let ptr = ${SelfType}(bitPattern: uword)
expectNotEmpty(ptr)
expectEqual(uword, unsafeBitCast(ptr, to: UInt.self))
}
}

${SelfName}TestSuite.test("initFromNilBitPattern") {
do {
let word = unsafeBitCast(nil as ${SelfType}?, to: Int.self)
let ptr = ${SelfType}(bitPattern: word)
expectEmpty(ptr)
expectEqual(word, unsafeBitCast(ptr, to: Int.self))
}
do {
let uword = unsafeBitCast(nil as ${SelfType}?, to: UInt.self)
let ptr = ${SelfType}(bitPattern: uword)
expectEmpty(ptr)
expectEqual(uword, unsafeBitCast(ptr, to: UInt.self))
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/ClangModules/cfuncs_parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,32 @@ func test_pointer() {
opaque_pointer_param(op)
}

func test_pointer_nonnull() {
var i: CInt = 0
var ia: [CInt] = [1, 2, 3]
var f: CFloat = 0
var fa: [CFloat] = [1, 2, 3]

nonnull_param_pointer(&i)
nonnull_param_pointer(&ia)

nonnull_param_const_pointer(&i)
nonnull_param_const_pointer(ia)
nonnull_param_const_pointer([1, 2, 3])

nonnull_param_void_pointer(&i)
nonnull_param_void_pointer(&ia)
nonnull_param_void_pointer(&f)
nonnull_param_void_pointer(&fa)

nonnull_param_const_void_pointer(&i)
nonnull_param_const_void_pointer(ia)
// FIXME: nonnull_param_const_void_pointer([1, 2, 3])
nonnull_param_const_void_pointer(&f)
nonnull_param_const_void_pointer(fa)
// FIXME: nonnull_param_const_void_pointer([1.0, 2.0, 3.0])
}

func test_decay() {
decay_param_array(nil as UnsafeMutablePointer<CInt>?)
var i: CInt = 0
Expand Down
6 changes: 6 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/cfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ void param_const_pointer(const int *p);
void param_void_pointer(void *p);
void param_const_void_pointer(const void *p);

void nonnull_param_pointer(int * _Nonnull p);
void nonnull_param_const_pointer(const int * _Nonnull p);

void nonnull_param_void_pointer(void * _Nonnull p);
void nonnull_param_const_void_pointer(const void * _Nonnull p);

void decay_param_array(int p[]);
void decay_param_const_array(const int p[]);

Expand Down
31 changes: 25 additions & 6 deletions test/Interpreter/process_arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,36 @@

// REQUIRES: swift_interpreter

print("Begin")
print("Begin arguments")
for arg in Process.arguments { print(arg) }
print("End")
print("End arguments")

// CHECK-NONE: Begin
// CHECK-NONE: Begin arguments
// CHECK-NONE-NEXT: {{.*}}process_arguments.swift
// CHECK-NONE-NEXT: End
// CHECK-NONE-NEXT: End arguments

// CHECK-THREE: Begin
// CHECK-THREE: Begin arguments
// CHECK-THREE-NEXT: {{.*}}process_arguments.swift
// CHECK-THREE-NEXT: a
// CHECK-THREE-NEXT: b
// CHECK-THREE-NEXT: c
// CHECK-THREE-NEXT: End
// CHECK-THREE-NEXT: End arguments

print("Begin unsafeArgv")
for i in 0...Int(Process.argc) {
print(Process.unsafeArgv[i].map { String(cString: $0) } ?? "(null)")
}
print("End unsafeArgv")

// CHECK-NONE: Begin unsafeArgv
// CHECK-NONE-NEXT: {{.*}}process_arguments.swift
// CHECK-NONE-NEXT: (null)
// CHECK-NONE-NEXT: End unsafeArgv

// CHECK-THREE: Begin unsafeArgv
// CHECK-THREE-NEXT: {{.*}}process_arguments.swift
// CHECK-THREE-NEXT: a
// CHECK-THREE-NEXT: b
// CHECK-THREE-NEXT: c
// CHECK-THREE-NEXT: (null)
// CHECK-THREE-NEXT: End unsafeArgv
Loading

0 comments on commit a79a893

Please sign in to comment.