Skip to content

Commit 194a2fd

Browse files
committed
more test fixes
1 parent 6e3c4bf commit 194a2fd

File tree

4 files changed

+57
-81
lines changed

4 files changed

+57
-81
lines changed

test/1_stdlib/BridgeNonVerbatim.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
import Swift
2525
import SwiftShims
26-
import ObjectiveC
26+
import Foundation
2727
import StdlibUnittest
28+
import StdlibUnittestFoundationExtras
2829

2930
struct X : _ObjectiveCBridgeable {
3031
static func _isBridgedToObjectiveC() -> Bool {
@@ -69,20 +70,20 @@ print("testing...")
6970

7071
func testScope() {
7172
let a = [X(1), X(2), X(3)]
72-
let nsx = a._buffer._asCocoaArray()
73+
let nsx: NSArray = a._bridgeToObjectiveC()
7374

7475
// construction of these tracked objects is lazy
7576
// CHECK-NEXT: trackedCount = 0 .
7677
print("trackedCount = \(LifetimeTracked.instances) .")
7778

7879
// We can get a single element out
7980
// CHECK-NEXT: nsx[0]: 1 .
80-
let one = nsx.objectAt(0) as! LifetimeTracked
81+
let one = nsx.object(at: 0) as! LifetimeTracked
8182
print("nsx[0]: \(one.value) .")
8283

8384
// We can get the element again, but it may not have the same identity
8485
// CHECK-NEXT: object identity matches?
85-
let anotherOne = nsx.objectAt(0) as! LifetimeTracked
86+
let anotherOne = nsx.object(at: 0) as! LifetimeTracked
8687
print("object identity matches? \(one === anotherOne)")
8788

8889
// Because the elements come back at +0, we really don't want to
@@ -92,9 +93,10 @@ func testScope() {
9293
objects.withUnsafeMutableBufferPointer {
9394
// FIXME: Can't elide signature and use $0 here <rdar://problem/17770732>
9495
(buf: inout UnsafeMutableBufferPointer<Int>) -> () in
95-
nsx.getObjects(
96-
UnsafeMutablePointer<AnyObject>(buf.baseAddress!),
97-
range: _SwiftNSRange(location: 1, length: 2))
96+
nsx.available_getObjects(
97+
AutoreleasingUnsafeMutablePointer(buf.baseAddress!),
98+
range: NSRange(location: 1, length: 2))
99+
return
98100
}
99101

100102
// CHECK-NEXT: getObjects yields them at +0: true

validation-test/stdlib/ArrayNew.swift.gyb

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,14 @@ import StdlibCollectionUnittest
1919
all_array_types = ['ContiguousArray', 'ArraySlice', 'Array']
2020
}%
2121

22-
extension Array {
23-
var identity: UnsafePointer<Void> {
24-
return self._buffer.identity
25-
}
26-
}
27-
28-
extension ArraySlice {
29-
var identity: UnsafePointer<Void> {
30-
return self._buffer.identity
31-
}
32-
}
33-
34-
extension ContiguousArray {
35-
var identity: UnsafePointer<Void> {
36-
return self._buffer.identity
22+
%for Self in all_array_types:
23+
extension ${Self} {
24+
typealias _BufferID = UnsafePointer<Void>?
25+
var _bufferID: _BufferID {
26+
return unsafeBitCast(_owner, to: _BufferID.self)
3727
}
3828
}
29+
%end
3930

4031
var ArrayTestSuite = TestSuite("Array")
4132

@@ -64,21 +55,21 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Int)/COW") {
6455
${element_type}(40), ${element_type}(50), ${element_type}(60),
6556
${element_type}(70)
6657
]]
67-
let identityOuter = a.identity
68-
var identityInner = a[0].identity
58+
let identityOuter = a._bufferID
59+
var identityInner = a[0]._bufferID
6960

7061
func checkIdentity(_ stackTrace: SourceLocStack) {
7162
% if element_type == 'TestValueTy':
7263
// Does not reallocate storage because we changed a property based on a
7364
// reference; array storage was not changed. Writeback of the inner array
7465
// does not happen.
75-
expectEqual(identityOuter, a.identity, stackTrace: stackTrace)
76-
expectEqual(identityInner, a[0].identity, stackTrace: stackTrace)
66+
expectEqual(identityOuter, a._bufferID, stackTrace: stackTrace)
67+
expectEqual(identityInner, a[0]._bufferID, stackTrace: stackTrace)
7768
% else:
78-
expectEqual(identityOuter, a.identity, stackTrace: stackTrace)
69+
expectEqual(identityOuter, a._bufferID, stackTrace: stackTrace)
7970

8071
// Should not reallocate storage.
81-
expectEqual(identityInner, a[0].identity, stackTrace: stackTrace)
72+
expectEqual(identityInner, a[0]._bufferID, stackTrace: stackTrace)
8273
% end
8374
}
8475

@@ -134,33 +125,33 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Range<Int>)/COW
134125
${element_type}(40), ${element_type}(50), ${element_type}(60),
135126
${element_type}(70), ${element_type}(80), ${element_type}(90),
136127
]]
137-
let identityOuter = a.identity
138-
var identityInner = a[0].identity
128+
let identityOuter = a._bufferID
129+
var identityInner = a[0]._bufferID
139130

140131
func checkIdentity(_ stackTrace: SourceLocStack) {
141132
% if element_type == 'TestValueTy':
142133
// Does not reallocate storage because we changed a property based on a
143134
// reference; array storage was not changed.
144-
expectEqual(identityOuter, a.identity, stackTrace: stackTrace)
145-
expectEqual(identityInner, a[0].identity, stackTrace: stackTrace)
135+
expectEqual(identityOuter, a._bufferID, stackTrace: stackTrace)
136+
expectEqual(identityInner, a[0]._bufferID, stackTrace: stackTrace)
146137
% else:
147-
expectEqual(identityOuter, a.identity, stackTrace: stackTrace)
138+
expectEqual(identityOuter, a._bufferID, stackTrace: stackTrace)
148139
// Writeback happens in subscript(Range<Int>), but array notices that the new
149140
// value did not change.
150141
// Another writeback happens in Array.subscript(Int), but this is not what we
151142
// want.
152-
expectNotEqual(identityInner, a[0].identity, stackTrace: stackTrace)
153-
identityInner = a[0].identity
143+
expectNotEqual(identityInner, a[0]._bufferID, stackTrace: stackTrace)
144+
identityInner = a[0]._bufferID
154145
% end
155146
}
156147

157148
// Mutating through a subscript expression.
158149
a[0..<1][0][0] = ${element_type}(1010)
159150

160151
// Reallocates storage because of the writeback in Array.subscript(Int).
161-
expectEqual(identityOuter, a.identity)
162-
expectNotEqual(identityInner, a[0].identity)
163-
identityInner = a[0].identity
152+
expectEqual(identityOuter, a._bufferID)
153+
expectNotEqual(identityInner, a[0]._bufferID)
154+
identityInner = a[0]._bufferID
164155

165156
a[0..<1][0][1].value = 1020
166157

@@ -195,9 +186,9 @@ ArrayTestSuite.test("${array_type}<${element_type}>/subscript(_: Range<Int>)/COW
195186
// Reallocates storage because of the writeback in Array.subscript(Int)
196187
// (writeback is being requested for the array element even though it is not
197188
// needed).
198-
expectEqual(identityOuter, a.identity)
199-
expectNotEqual(identityInner, a[0].identity)
200-
identityInner = a[0].identity
189+
expectEqual(identityOuter, a._bufferID)
190+
expectNotEqual(identityInner, a[0]._bufferID)
191+
identityInner = a[0]._bufferID
201192

202193
withInoutT(&a[0..<1][0][6].value) {
203194
(x: inout Int) in

validation-test/stdlib/Arrays.swift.gyb

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,14 @@ CopyToNativeArrayBufferTests.test("Collection._copyToContiguousArray()") {
7373
all_array_types = ['ContiguousArray', 'ArraySlice', 'Array']
7474
}%
7575

76-
extension Array {
77-
var identity: UnsafePointer<Void> {
78-
return unsafeBitCast(self, to: UnsafePointer<Void>.self)
79-
}
80-
}
81-
82-
extension ArraySlice {
83-
typealias Identity = (
84-
UnsafePointer<Void>, UnsafePointer<Void>,
85-
UnsafePointer<Void>, UnsafePointer<Void>)
86-
var identity: Identity {
87-
return unsafeBitCast(self, to: Identity.self)
88-
}
89-
}
90-
91-
extension ContiguousArray {
92-
var identity: UnsafePointer<Void> {
93-
return unsafeBitCast(self, to: UnsafePointer<Void>.self)
76+
%for Self in all_array_types:
77+
extension ${Self} {
78+
typealias _BufferID = UnsafePointer<Void>?
79+
var _bufferID: _BufferID {
80+
return unsafeBitCast(_owner, to: _BufferID.self)
9481
}
9582
}
83+
%end
9684

9785
var ArrayTestSuite = TestSuite("Array")
9886

@@ -249,10 +237,10 @@ ArrayTestSuite.test("${array_type}/emptyAllocation") {
249237
let arr0 = ${array_type}<Int>()
250238
let arr1 = ${array_type}<LifetimeTracked>(repeating: LifetimeTracked(0), count: 0)
251239
// Empty arrays all use the same buffer
252-
expectEqual(arr0.identity, arr1.identity)
240+
expectEqual(arr0._bufferID, arr1._bufferID)
253241

254242
let arr2: ${array_type}<LifetimeTracked> = []
255-
let emptyLiteralsShareBuffer = arr0.identity == arr2.identity
243+
let emptyLiteralsShareBuffer = arr0._bufferID == arr2._bufferID
256244
expectTrue(emptyLiteralsShareBuffer)
257245
}
258246

validation-test/stdlib/NewArray.swift.gyb

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func printSequence<T : Sequence>(_ x: T) {
3232
print("]")
3333
}
3434

35-
typealias BufferID = UnsafePointer<Void>?
35+
typealias _BufferID = UnsafePointer<Void>?
3636

3737
protocol MyArrayProtocol
3838
: RandomAccessCollection,
@@ -53,23 +53,18 @@ protocol MyArrayProtocol
5353
>(lhs: inout Self, rhs: S)
5454
}
5555
extension MyArrayProtocol {
56-
var _bufferID: BufferID {
57-
return unsafeBitCast(_owner, to: BufferID.self)
56+
var _bufferID: _BufferID {
57+
return unsafeBitCast(_owner, to: _BufferID.self)
5858
}
5959
}
6060
extension Array : MyArrayProtocol {}
6161
extension ArraySlice : MyArrayProtocol {}
6262
extension ContiguousArray : MyArrayProtocol {}
6363

64-
65-
func bufferID<T : MyArrayProtocol>(_ x: T) -> BufferID {
66-
return x._bufferID
67-
}
68-
6964
func checkReallocation<T : MyArrayProtocol>(
70-
_ x: T, _ lastBuffer: BufferID, _ reallocationExpected: Bool
71-
) -> BufferID {
72-
let currentBuffer = bufferID(x)
65+
_ x: T, _ lastBuffer: _BufferID, _ reallocationExpected: Bool
66+
) -> _BufferID {
67+
let currentBuffer = x._bufferID
7368
if (currentBuffer != lastBuffer) != reallocationExpected {
7469
let message = reallocationExpected ? "lack of" : ""
7570
print("unexpected \(message) reallocation")
@@ -112,7 +107,7 @@ func test<
112107
x.reserveCapacity(x.count + 2)
113108
checkEqual(x, LifetimeTracked(1)...LifetimeTracked(5), true)
114109

115-
let bufferId0 = bufferID(x)
110+
let bufferId0 = x._bufferID
116111

117112
// Append a range of integers
118113
x += LifetimeTracked(0)..<LifetimeTracked(2)
@@ -140,9 +135,9 @@ func test<
140135
// has shown that using 1.5, the other popular growth factor,
141136
// slows things down.
142137
for _ in a.count..<(a.capacity * 4) {
143-
let oldId = bufferID(a)
138+
let oldId = a._bufferID
144139
growBy1(&a)
145-
if oldId != bufferID(a) {
140+
if oldId != a._bufferID {
146141
reallocations += 1
147142
}
148143
}
@@ -183,29 +178,29 @@ func testAsArray() {
183178
// CHECK: == AsArray ==
184179

185180
let x = ContiguousArray(w)
186-
print(bufferID(w) == bufferID(x))
181+
print(w._bufferID == x._bufferID)
187182
// CHECK-NEXT: true
188183

189184
let y = Array(x)
190-
print(bufferID(x) == bufferID(y))
185+
print(x._bufferID == y._bufferID)
191186
// CHECK-NEXT: true
192187

193188
// Because of their indirection, arrays of classes can share
194189
// buffers.
195190
let y1 = Array(y)
196-
print(bufferID(y1) == bufferID(y))
191+
print(y1._bufferID == y._bufferID)
197192
// CHECK-NEXT: true
198193

199194
let z = ArraySlice(y)
200-
print(bufferID(y) == bufferID(z))
195+
print(y._bufferID == z._bufferID)
201196
// CHECK-NEXT: true
202197

203198
w = ContiguousArray(z)
204-
print(bufferID(w) == bufferID(z))
199+
print(w._bufferID == z._bufferID)
205200
// CHECK-NEXT: true
206201

207202
let zz = y[0..<2]
208-
print(bufferID(zz))
203+
print(zz._bufferID)
209204
// CHECK-NEXT: 0x
210205
}
211206
testAsArray()

0 commit comments

Comments
 (0)