forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnsafePointer.swift.gyb
266 lines (233 loc) · 7.69 KB
/
UnsafePointer.swift.gyb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// RUN: rm -rf %t && mkdir -p %t && %S/../../utils/gyb %s -o %t/UnsafePointer.swift
// RUN: %S/../../utils/line-directive %t/UnsafePointer.swift -- %target-build-swift %t/UnsafePointer.swift -o %t/a.out
// RUN: %S/../../utils/line-directive %t/UnsafePointer.swift -- %target-run %t/a.out
// REQUIRES: executable_test
import StdlibUnittest
protocol TestProtocol1 {}
// Check that the generic parameter is called 'Memory'.
#if _runtime(_ObjC)
extension AutoreleasingUnsafeMutablePointer where Memory : TestProtocol1 {
var _memoryIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
#endif
extension UnsafePointer where Memory : TestProtocol1 {
var _memoryIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension UnsafeMutablePointer where Memory : TestProtocol1 {
var _memoryIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
// Check that the generic parameter is called 'Element'.
extension UnsafeBufferPointerGenerator where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension UnsafeBufferPointer where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension UnsafeMutableBufferPointer where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
var UnsafePointerTestSuite = TestSuite("UnsafePointer")
var UnsafeMutablePointerTestSuite = TestSuite("UnsafeMutablePointer")
var COpaquePointerTestSuite = TestSuite("COpaquePointer")
% for (SelfName, SelfType) in [
% ( 'UnsafePointer', 'UnsafePointer<Float>' ),
% ( 'UnsafeMutablePointer', 'UnsafeMutablePointer<Float>'),
% ( 'COpaquePointer', 'COpaquePointer' ) ]:
${SelfName}TestSuite.test("convertFromNil") {
let ptr: ${SelfType} = nil
expectEqual(0, unsafeBitCast(ptr, Int.self))
}
${SelfName}TestSuite.test("initNoArgs") {
let ptr = ${SelfType}()
expectEqual(0, unsafeBitCast(ptr, Int.self))
}
${SelfName}TestSuite.test("initFromCOpaquePointer") {
let other = COpaquePointer(bitPattern: 0x12345678)
let ptr = UnsafePointer<Float>(other)
expectEqual(0x12345678, unsafeBitCast(ptr, Int.self))
}
${SelfName}TestSuite.test("initFromUnsafePointer") {
let other = UnsafePointer<Double>(bitPattern: 0x12345678)
let ptr = ${SelfType}(other)
expectEqual(0x12345678, unsafeBitCast(ptr, Int.self))
}
${SelfName}TestSuite.test("initFromUnsafeMutablePointer") {
let other = UnsafeMutablePointer<Double>(bitPattern: 0x12345678)
let ptr = ${SelfType}(other)
expectEqual(0x12345678, unsafeBitCast(ptr, Int.self))
}
${SelfName}TestSuite.test("initFromInteger") {
if true {
let word: Int = 0x12345678
let ptr = ${SelfType}(bitPattern: word)
expectEqual(word, unsafeBitCast(ptr, Int.self))
}
if true {
let uword: UInt = 0x12345678
let ptr = ${SelfType}(bitPattern: uword)
expectEqual(uword, unsafeBitCast(ptr, UInt.self))
}
}
${SelfName}TestSuite.test("Hashable") {
let ptrs = [
${SelfType}(bitPattern: 0x0),
${SelfType}(bitPattern: 0x12345678),
${SelfType}(bitPattern: 0x87654321 as UInt),
]
for i in ptrs.indices {
for j in ptrs.indices {
var pi = ptrs[i]
var pj = ptrs[j]
checkHashable(i == j, pi, pj, "i=\(i), j=\(j)")
}
}
}
% end
enum Check {
case LeftOverlap
case RightOverlap
case Disjoint
}
class Missile {
static var missilesLaunched = 0
let number: Int
init(_ number: Int) { self.number = number }
deinit { Missile.missilesLaunched++ }
}
func checkPointerCorrectness(check: Check,
_ f: (UnsafeMutablePointer<Missile>) ->
(UnsafeMutablePointer<Missile>, count: Int) -> Void,
_ withMissiles: Bool = false) {
let ptr = UnsafeMutablePointer<Missile>.alloc(4)
switch check {
case .RightOverlap:
ptr.initialize(Missile(1))
(ptr + 1).initialize(Missile(2))
if withMissiles {
(ptr + 2).initialize(Missile(3))
}
f(ptr + 1)(ptr, count: 2)
expectEqual(1, ptr[1].number)
expectEqual(2, ptr[2].number)
case .LeftOverlap:
if withMissiles {
ptr.initialize(Missile(1))
}
(ptr + 1).initialize(Missile(2))
(ptr + 2).initialize(Missile(3))
f(ptr)(ptr + 1, count: 2)
expectEqual(2, ptr[0].number)
expectEqual(3, ptr[1].number)
case .Disjoint:
if withMissiles {
ptr.initialize(Missile(0))
(ptr + 1).initialize(Missile(1))
}
(ptr + 2).initialize(Missile(2))
(ptr + 3).initialize(Missile(3))
f(ptr)(ptr + 2, count: 2)
expectEqual(2, ptr[0].number)
expectEqual(3, ptr[1].number)
// backwards
let ptr2 = UnsafeMutablePointer<Missile>.alloc(4)
ptr2.initialize(Missile(0))
(ptr2 + 1).initialize(Missile(1))
if withMissiles {
(ptr2 + 2).initialize(Missile(2))
(ptr2 + 3).initialize(Missile(3))
}
f(ptr2 + 2)(ptr2, count: 2)
expectEqual(0, ptr2[2].number)
expectEqual(1, ptr2[3].number)
}
}
let checkPtr: ((UnsafeMutablePointer<Missile> ->
(UnsafeMutablePointer<Missile>, count: Int) -> Void), Bool) -> Check -> Void
= { (f, m) in return { checkPointerCorrectness($0, f, m) } }
UnsafeMutablePointerTestSuite.test("moveInitializeBackwardFrom") {
let check = checkPtr(UnsafeMutablePointer.moveInitializeBackwardFrom, false)
check(Check.RightOverlap)
check(Check.Disjoint)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.LeftOverlap)
}
}
UnsafeMutablePointerTestSuite.test("moveAssignFrom") {
let check = checkPtr(UnsafeMutablePointer.moveAssignFrom, true)
check(Check.Disjoint)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.LeftOverlap)
}
}
UnsafeMutablePointerTestSuite.test("moveAssignFrom.Right") {
let check = checkPtr(UnsafeMutablePointer.moveAssignFrom, true)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.RightOverlap)
}
}
UnsafeMutablePointerTestSuite.test("assignFrom") {
let check = checkPtr(UnsafeMutablePointer.assignFrom, true)
check(Check.LeftOverlap)
check(Check.Disjoint)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.RightOverlap)
}
}
UnsafeMutablePointerTestSuite.test("assignBackwardFrom") {
let check = checkPtr(UnsafeMutablePointer.assignBackwardFrom, true)
check(Check.RightOverlap)
check(Check.Disjoint)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.LeftOverlap)
}
}
UnsafeMutablePointerTestSuite.test("moveInitializeFrom") {
let check = checkPtr(UnsafeMutablePointer.moveInitializeFrom, false)
check(Check.LeftOverlap)
check(Check.Disjoint)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.RightOverlap)
}
}
UnsafeMutablePointerTestSuite.test("initializeFrom") {
let check = checkPtr(UnsafeMutablePointer.initializeFrom, false)
check(Check.Disjoint)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.LeftOverlap)
}
}
UnsafeMutablePointerTestSuite.test("initializeFrom.Right") {
let check = checkPtr(UnsafeMutablePointer.initializeFrom, false)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.RightOverlap)
}
}
runAllTests()