forked from BigfootDev/flatbuffers
* Migrating from Xctests to swift testing This migrates to the new Swift testing framework, which would allow us to always use the latest tech from swift moving forward. * Updates flag to make sure that Wasm testing works
137 lines
3.7 KiB
Swift
137 lines
3.7 KiB
Swift
/*
|
|
* Copyright 2024 Google Inc. All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import Foundation
|
|
import Testing
|
|
|
|
@testable import FlatBuffers
|
|
|
|
struct ByteBufferTests {
|
|
|
|
@Test
|
|
func testCopyingMemory() {
|
|
let count = 100
|
|
let ptr = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
|
|
let byteBuffer = ByteBuffer(copyingMemoryBound: ptr, capacity: count)
|
|
byteBuffer.withUnsafeBytes { memory in
|
|
#expect(memory.baseAddress! != ptr)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func testSamePointer() {
|
|
let count = 100
|
|
let ptr = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
|
|
let byteBuffer = ByteBuffer(assumingMemoryBound: ptr, capacity: count)
|
|
byteBuffer.withUnsafeBytes { memory in
|
|
#expect(memory.baseAddress! == ptr)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func testSameDataPtr() {
|
|
let count = 100
|
|
let ptr = Data(repeating: 0, count: count)
|
|
let byteBuffer = ByteBuffer(data: ptr)
|
|
byteBuffer.withUnsafeBytes { memory in
|
|
ptr.withUnsafeBytes { ptr in
|
|
#expect(memory.baseAddress! == ptr.baseAddress!)
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func testSameArrayPtr() {
|
|
let count = 100
|
|
let ptr: [UInt8] = Array(repeating: 0, count: count)
|
|
let byteBuffer = ByteBuffer(bytes: ptr)
|
|
ptr.withUnsafeBytes { ptr in
|
|
byteBuffer.withUnsafeBytes { memory in
|
|
#expect(memory.baseAddress == ptr.baseAddress)
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func testReadingDoubleBuffer() {
|
|
let count = 8
|
|
let array: [Double] = Array(repeating: 8.8, count: count)
|
|
var oldBuffer = _InternalByteBuffer(initialSize: 16)
|
|
oldBuffer.push(elements: array)
|
|
let bytes: [Byte] = oldBuffer.withUnsafeBytes { bytes in
|
|
Array(bytes)
|
|
}
|
|
let byteBuffer = ByteBuffer(bytes: bytes)
|
|
byteBuffer.withUnsafePointerToSlice(index: 0, count: count) { ptr in
|
|
#expect(ptr.count == count)
|
|
bytes.withUnsafeBufferPointer {
|
|
#expect(
|
|
UnsafeRawPointer($0.baseAddress) ==
|
|
UnsafeRawPointer(ptr.baseAddress))
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func testReadingNativeStructs() {
|
|
let array = [
|
|
MyGame_Example_Vec3(
|
|
x: 3.2,
|
|
y: 3.2,
|
|
z: 3.2,
|
|
test1: 8,
|
|
test2: .red,
|
|
test3: MyGame_Example_Test(a: 8, b: 8)),
|
|
MyGame_Example_Vec3(
|
|
x: 3.2,
|
|
y: 3.2,
|
|
z: 3.2,
|
|
test1: 8,
|
|
test2: .green,
|
|
test3: MyGame_Example_Test(a: 16, b: 16)),
|
|
MyGame_Example_Vec3(
|
|
x: 3.2,
|
|
y: 3.2,
|
|
z: 3.2,
|
|
test1: 8,
|
|
test2: .blue,
|
|
test3: MyGame_Example_Test(a: 32, b: 32)),
|
|
]
|
|
let count = array.count
|
|
var oldBuffer = _InternalByteBuffer(initialSize: 16)
|
|
oldBuffer.push(elements: array)
|
|
let bytes: [Byte] = oldBuffer.withUnsafeBytes { bytes in
|
|
Array(bytes)
|
|
}
|
|
let byteBuffer = ByteBuffer(bytes: bytes)
|
|
byteBuffer
|
|
.withUnsafePointerToSlice(index: 0, count: count) { bufferPointer in
|
|
#expect(bufferPointer.count == count)
|
|
bytes.withUnsafeBufferPointer { ptr in
|
|
#expect(
|
|
UnsafeRawPointer(ptr.baseAddress) ==
|
|
UnsafeRawPointer(bufferPointer.baseAddress))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct TestNativeStructs: NativeStruct {
|
|
let x: Double
|
|
let y: Double
|
|
let z: Int
|
|
}
|