-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathArrayTests.swift
318 lines (281 loc) · 13.1 KB
/
ArrayTests.swift
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import WinSDK
import XCTest
import test_component
import Foundation
class StringableInt: IStringable {
let value: Int32
init(value: Int32) {
self.value = value
}
public func toString() -> String {
return "\(value)"
}
}
class ArrayScenarios: IArrayScenarios {
var array: [Int32] = []
func inArray(_ value: [Int32]) throws {
array = value
}
func outArray(_ value: inout [Int32]) throws {
value = array
}
func refArray(_ value: inout [Int32]) throws {
for i in 0..<value.count-1 {
value[i] = Int32(i)
}
}
func returnArray() throws -> [Int32] {
array
}
func doubleIn(_ value1: [Int32], _ value2: [Int32]) throws {}
func inAndOut(_ value: [Int32], _ results: inout [Int32]) throws {}
func inAndRef(_ value: [Int32], _ results: inout [Int32]) throws {}
func inAndReturn(_ value: [Int32]) throws -> [Int32] { value }
func inAndRefNonBlittable(_ value: [Int32], _ results: inout [Bool]) throws {}
var arrayProperty: [Int32] = []
}
class ArrayInputTests : XCTestCase {
public func testInputIntArray() throws {
let input: [Int32] = [1, 2, 3, 4, 5]
let result = try ArrayMethods.inInt32Array(input)
XCTAssertEqual("12345", result)
}
public func testInStringArray() throws {
let input: [String] = ["1", "2", "3", "4", "5"]
let result = try ArrayMethods.inStringArray(input)
XCTAssertEqual("12345", result)
}
public func testInObjectArray() throws {
let input: [StringableInt] = [.init(value: 1), .init(value: 2), .init(value: 3), .init(value: 4), .init(value: 5)]
let result = try ArrayMethods.inObjectArray(input)
XCTAssertEqual("12345", result)
}
public func testInStringableArray() throws {
let input: [StringableInt] = [.init(value: 1), .init(value: 2), .init(value: 3), .init(value: 4), .init(value: 5)]
let result = try ArrayMethods.inStringableArray(input)
XCTAssertEqual("12345", result)
}
public func testInStructArray() throws {
let input: [BlittableStruct] = [BlittableStruct(first: 1, second: 2), BlittableStruct(first: 3, second: 4) ]
let result = try ArrayMethods.inStructArray(input)
XCTAssertEqual("1234", result)
}
public func testInNonBlittableStructArray() throws {
let input: [NonBlittableStruct] = [NonBlittableStruct(first: "1", second: "2", third: 3, fourth: "4"), NonBlittableStruct(first: "5", second: "6", third: 7, fourth: "8")]
let result = try ArrayMethods.inNonBlittableStructArray(input)
XCTAssertEqual("12345678", result)
}
public func testInEnumArray() throws {
let input: [Signed] = [.first, .second, .third]
let result = try ArrayMethods.inEnumArray(input)
XCTAssertEqual("FirstSecondThird", result)
}
public func testThroughSwiftImplementation() throws {
let arrayScenario = ArrayScenarios()
try ArrayMethods.testInArrayThroughSwiftImplementation(arrayScenario, [1, 2, 3])
XCTAssertEqual([1, 2, 3], arrayScenario.array)
}
}
public class ArrayOutputTests: XCTestCase {
public func testOutIntArray() throws {
var intArray = [Int32]()
try ArrayMethods.outInt32Array(&intArray)
XCTAssertEqual([1, 2, 3], intArray)
}
public func testOutStringArray() throws {
var stringArray = [String]()
try ArrayMethods.outStringArray(&stringArray)
XCTAssertEqual(["1", "2", "3"], stringArray)
}
public func testOutObjectArray() throws {
var objectArray = [Any?]()
try ArrayMethods.outObjectArray(&objectArray)
let mapped = try objectArray.compactMap { try XCTUnwrap($0 as? IStringable).toString() }
XCTAssertEqual(["1", "2", "3"], mapped)
}
public func testOutStringableArray() throws {
var stringableArray = [AnyIStringable?]()
try ArrayMethods.outStringableArray(&stringableArray)
let mapped = try stringableArray.compactMap { try $0?.toString() }
XCTAssertEqual(["1", "2", "3"], mapped)
}
public func testOutStructArray() throws {
var structArray = [BlittableStruct]()
try ArrayMethods.outStructArray(&structArray)
XCTAssertEqual([
BlittableStruct(first: 1, second: 2),
BlittableStruct(first: 3, second: 4),
BlittableStruct(first: 5, second: 6)
], structArray)
}
public func testOutNonBlittableStructArray() throws {
var structArray = [NonBlittableStruct]()
try ArrayMethods.outNonBlittableStructArray(&structArray)
XCTAssertEqual([
NonBlittableStruct(first: "1", second: "2", third: 3, fourth: "4"),
NonBlittableStruct(first: "5", second: "6", third: 7, fourth: "8"),
NonBlittableStruct(first: "9", second: "10", third: 11, fourth: "12")
], structArray)
}
public func testOutEnumArry() throws {
var enumArray = [Signed]()
try ArrayMethods.outEnumArray(&enumArray)
XCTAssertEqual([.first, .second], enumArray)
}
public func testThroughSwiftImplementation() throws {
let arrayScenario = ArrayScenarios()
arrayScenario.array = [1, 3, 1, 10]
try ArrayMethods.testOutArrayThroughSwiftImplementation(arrayScenario) { array in
XCTAssertEqual([1, 3, 1, 10], array)
}
}
}
public class ArrayByReferenceTests: XCTestCase {
public func testInIntArrayByReference() throws {
var input = [Int32](repeating: 8675309, count: 5)
try ArrayMethods.refInt32Array(&input)
XCTAssertEqual([1, 2, 3, 4, 8675309], input)
}
public func testInStringArrayByReference() throws {
var input = [String](repeating: "SWIFTRULES", count: 5)
try ArrayMethods.refStringArray(&input)
XCTAssertEqual(["1", "2", "3", "4", ""], input)
}
public func testInObjectArrayByReference() throws {
var input = [Any?](repeating: StringableInt(value: 42), count: 5)
try ArrayMethods.refObjectArray(&input)
let mapped = try input.map { try ($0 as? IStringable)?.toString() }
XCTAssertEqual(["1", "2", "3", "4", nil], mapped)
}
public func testInStringableArrayByReference() throws {
var input = [AnyIStringable?](repeating: StringableInt(value: 42), count: 5)
try ArrayMethods.refStringableArray(&input)
let mapped = try input.map { try $0?.toString() }
XCTAssertEqual(["1", "2", "3", "4", nil], mapped)
}
public func testInStructArrayByReference() throws {
var input = [BlittableStruct](repeating: BlittableStruct(first: 10, second: 10), count: 3)
try ArrayMethods.refStructArray(&input)
XCTAssertEqual([
BlittableStruct(first: 1, second: 2),
BlittableStruct(first: 3, second: 4),
BlittableStruct(first: 10, second: 10)], input)
}
public func testInNonBlittableStructArrayByReference() throws {
var input = [NonBlittableStruct](repeating: NonBlittableStruct(first: "H", second: "E", third: 1, fourth: "P"), count: 3)
try ArrayMethods.refNonBlittableStructArray(&input)
XCTAssertEqual([
NonBlittableStruct(first: "1", second: "2", third: 3, fourth: "4"),
NonBlittableStruct(first: "5", second: "6", third: 7, fourth: "8"),
NonBlittableStruct(first: "", second: "", third: 0, fourth: "")], input)
}
public func testInEnumArrayByReference() throws {
var input = [Signed](repeating: .third, count: 3)
try ArrayMethods.refEnumArray(&input)
XCTAssertEqual([Signed.first, Signed.second, Signed.third], input)
}
public func testThroughSwiftImplementation() throws {
let arrayScenario = ArrayScenarios()
var testArray: [Int32] = [5, 4, 3, 2, 1]
try ArrayMethods.testRefArrayThroughSwiftImplementation(arrayScenario, &testArray) { array in
XCTAssertEqual([0, 1, 2, 3, 1], array)
}
XCTAssertEqual([0, 1, 2, 3, 1], testArray)
}
}
public class ReturnArrayInputTests: XCTestCase {
public func testReturnIntArray() throws {
let result = try ArrayMethods.returnInt32Array()
XCTAssertEqual([1, 2, 3], result)
}
public func testReturnStringArray() throws {
let result = try ArrayMethods.returnStringArray()
XCTAssertEqual(["1", "2", "3"], result)
}
public func testReturnObjectArray() throws {
let result = try ArrayMethods.returnObjectArray()
let mapped = try result.compactMap { try XCTUnwrap($0 as? IStringable).toString() }
XCTAssertEqual(["1", "2", "3"], mapped)
}
public func testReturnStringableArray() throws {
let result = try ArrayMethods.returnStringableArray()
let mapped = try result.compactMap { try $0?.toString() }
XCTAssertEqual(["1", "2", "3"], mapped)
}
public func testReturnStructArray() throws {
let result = try ArrayMethods.returnStructArray()
XCTAssertEqual([
BlittableStruct(first: 1, second: 2),
BlittableStruct(first: 3, second: 4),
BlittableStruct(first: 5, second: 6)
], result)
}
public func testReturnNonBlittableStructArray() throws {
let result = try ArrayMethods.returnNonBlittableStructArray()
XCTAssertEqual([
NonBlittableStruct(first: "1", second: "2", third: 3, fourth: "4"),
NonBlittableStruct(first: "5", second: "6", third: 7, fourth: "8"),
NonBlittableStruct(first: "9", second: "10", third: 11, fourth: "12")
], result)
}
public func testReturnEnumArray() throws {
let result = try ArrayMethods.returnEnumArray()
XCTAssertEqual([Signed.first, Signed.second], result)
}
public func testThroughSwiftImplementation() throws {
let arrayScenario = ArrayScenarios()
arrayScenario.array = [1, 3, 1, 10]
try ArrayMethods.testReturnArrayThroughSwiftImplementation(arrayScenario) { array in
XCTAssertEqual([1, 3, 1, 10], array)
}
}
}
private let inputArrayTests: [XCTestCaseEntry] = [
testCase([
("testInputIntArray", ArrayInputTests.testInputIntArray),
("testInStringArray", ArrayInputTests.testInStringArray),
("testInObjectArray", ArrayInputTests.testInObjectArray),
("testInStringableArray", ArrayInputTests.testInStringableArray),
("testInStructArray", ArrayInputTests.testInStructArray),
("testInNonBlittableStructArray", ArrayInputTests.testInNonBlittableStructArray),
("testInEnumArray", ArrayInputTests.testInEnumArray),
("testThroughSwiftImplementation", ArrayInputTests.testThroughSwiftImplementation)
])
]
private let outputArrayTests: [XCTestCaseEntry] = [
testCase([
("testOutIntArray", ArrayOutputTests.testOutIntArray),
("testOutStringArray", ArrayOutputTests.testOutStringArray),
("testOutObjectArray", ArrayOutputTests.testOutObjectArray),
("testOutStringableArray", ArrayOutputTests.testOutStringableArray),
("testOutStructArray", ArrayOutputTests.testOutStructArray),
("testOutNonBlittableStructArray", ArrayOutputTests.testOutNonBlittableStructArray),
("testOutEnumArry", ArrayOutputTests.testOutEnumArry),
("testThroughSwiftImplementation", ArrayOutputTests.testThroughSwiftImplementation)
])
]
private let referenceArrayTests: [XCTestCaseEntry] = [
testCase([
("testInIntArrayByReference", ArrayByReferenceTests.testInIntArrayByReference),
("testInStringArrayByReference", ArrayByReferenceTests.testInStringArrayByReference),
("testInObjectArrayByReference", ArrayByReferenceTests.testInObjectArrayByReference),
("testInStringableArrayByReference", ArrayByReferenceTests.testInStringableArrayByReference),
("testInStructArrayByReference", ArrayByReferenceTests.testInStructArrayByReference),
("testInNonBlittableStructArrayByReference", ArrayByReferenceTests.testInNonBlittableStructArrayByReference),
("testInEnumArrayByReference", ArrayByReferenceTests.testInEnumArrayByReference),
("testThroughSwiftImplementation", ArrayByReferenceTests.testThroughSwiftImplementation)
])
]
private let returnArrayTests: [XCTestCaseEntry] = [
testCase([
("testReturnIntArray", ReturnArrayInputTests.testReturnIntArray),
("testReturnStringArray", ReturnArrayInputTests.testReturnStringArray),
("testReturnObjectArray", ReturnArrayInputTests.testReturnObjectArray),
("testReturnStringableArray", ReturnArrayInputTests.testReturnStringableArray),
("testReturnStructArray", ReturnArrayInputTests.testReturnStructArray),
("testReturnNonBlittableStructArray", ReturnArrayInputTests.testReturnNonBlittableStructArray),
("testReturnEnumArray", ReturnArrayInputTests.testReturnEnumArray),
("testThroughSwiftImplementation", ReturnArrayInputTests.testThroughSwiftImplementation)
])
]
let arrayTests = inputArrayTests + outputArrayTests + referenceArrayTests + returnArrayTests