Files
swift-mirror/test/1_stdlib/ArrayTraps.swift.gyb
Dave Abrahams 61d7f0a0a4 [stdlib] Downcasted arrays stay native
We used to handle deferred type-checking by treating down-casted native
array buffers as NSArrays (which they are, but we know more).  Instead,
we now save a bit that indicates deferred type-checking is needed and
remember that the buffer is native, which saves dispatching through
objc_MsgSend.

Fixes <rdar://problem/19302286> down-casted Arrays are inefficient

Swift SVN r25472
2015-02-21 19:12:45 +00:00

153 lines
3.5 KiB
Plaintext

// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: %S/../../utils/gyb %s -o %t/ArrayTraps.swift
// RUN: %S/../../utils/line-directive %t/ArrayTraps.swift -- %target-build-swift %t/ArrayTraps.swift -o %t/a.out_Debug
// RUN: %S/../../utils/line-directive %t/ArrayTraps.swift -- %target-build-swift %t/ArrayTraps.swift -o %t/a.out_Release -O
//
// RUN: %S/../../utils/line-directive %t/ArrayTraps.swift -- %target-run %t/a.out_Debug
// RUN: %S/../../utils/line-directive %t/ArrayTraps.swift -- %target-run %t/a.out_Release
// XFAIL: linux
import StdlibUnittest
%{
# We test for bounds-checking traps for both reading and writing
# both single elements and slices of all three different array
# types.
array_types = ('Array', 'ContiguousArray', 'Slice', '_UnitTestArray')
def bounds_trap(index, expr_to_write):
global io
return 'expectCrashLater()\n' + (
'let x = a[%s]' % index
if io == 'read' else
'a[%s] = %s' % (index, expr_to_write))
}%
% for ArrayTy in array_types:
var ${ArrayTy}Traps = TestSuite("${ArrayTy}Traps")
% for io in ['read', 'write']:
${ArrayTy}Traps.test("bounds1/${io}") {
var a: ${ArrayTy}<Int> = []
${bounds_trap(index='0', expr_to_write='1')}
}
${ArrayTy}Traps.test("bounds2/${io}") {
var a: ${ArrayTy}<Int> = []
${bounds_trap(index='100', expr_to_write='1')}
}
${ArrayTy}Traps.test("bounds3/${io}") {
var a: ${ArrayTy} = [ 10, 20, 30 ]
${bounds_trap(index='3', expr_to_write='1')}
}
${ArrayTy}Traps.test("sliceBounds0/${io}") {
var a: ${ArrayTy}<Int> = []
${bounds_trap(index='-1..<1', expr_to_write='Slice()')}
}
${ArrayTy}Traps.test("sliceBounds1/${io}") {
var a: ${ArrayTy} = [ 1 ]
${bounds_trap(index='-1..<1', expr_to_write='Slice()')}
}
${ArrayTy}Traps.test("sliceBounds2/${io}") {
var a: ${ArrayTy} = [ 1 ]
${bounds_trap(index='0..<2', expr_to_write='Slice()')}
}
${ArrayTy}Traps.test("sliceBounds3/${io}") {
var a: ${ArrayTy} = [ 1 ]
${bounds_trap(index='1..<2', expr_to_write='Slice()')}
}
% end
${ArrayTy}Traps.test("PopFromEmpty") {
var a: ${ArrayTy}<Int> = []
expectCrashLater()
a.removeLast()
}
% for index in -1, 2:
${ArrayTy}Traps.test("${'insert(_:atIndex:)/%s' % index}") {
var a: ${ArrayTy}<Int> = [42]
expectCrashLater()
a.insert(3, atIndex: ${index})
}
% end
% for index in -1, 1, 2:
${ArrayTy}Traps.test("${'removeAtIndex(_:)/%s' % index}") {
var a: ${ArrayTy}<Int> = [42]
expectCrashLater()
a.removeAtIndex(${index})
}
% end
% end
class Base { }
class Derived : Base { }
class Derived2 : Derived { }
ArrayTraps.test("downcast1") {
let ba: [Base] = [ Derived(), Base() ]
let da = ba as! [Derived]
let d0 = da[0]
expectCrashLater()
da[1]
}
import Foundation
ArrayTraps.test("downcast2") {
let a: [AnyObject] = [ "String", 1 ]
let sa = a as! [NSString]
let s0 = sa[0]
expectCrashLater()
sa[1]
}
ArrayTraps.test("downcast3") {
let ba: [Base] = [ Derived2(), Derived(), Base() ]
let d2a = ba as! [Derived2]
let d2a0 = d2a[0]
let d1a = d2a as [Derived]
let d1a0 = d1a[0]
let d1a1 = d1a[1]
expectCrashLater()
d1a[2]
}
@objc protocol ObjCProto { }
class ObjCBase : NSObject, ObjCProto { }
class ObjCDerived : ObjCBase { }
ArrayTraps.test("downcast4") {
let ba: [ObjCProto] = [ ObjCDerived(), ObjCBase() ]
let da = ba as! [ObjCDerived]
let d0 = da[0]
expectCrashLater()
da[1]
}
ArrayTraps.test("unsafeLength") {
var a = [ 42, 77, 88 ]
expectEqual(42, a.withUnsafeBufferPointer({ $0[0] }))
expectCrashLater()
a.withUnsafeBufferPointer {
UnsafeBufferPointer(start: $0.baseAddress, count: -1)
}
}
runAllTests()