mirror of
https://github.com/google/flatbuffers.git
synced 2026-11-14 07:56:22 +00:00
Bigfoot
This commit is contained in:
@@ -14,58 +14,51 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
import XCTest
|
||||
|
||||
@testable import FlatBuffers
|
||||
|
||||
struct ByteBufferTests {
|
||||
|
||||
@Test
|
||||
final class ByteBufferTests: XCTestCase {
|
||||
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)
|
||||
XCTAssertNotEqual(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)
|
||||
XCTAssertEqual(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!)
|
||||
XCTAssertEqual(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)
|
||||
XCTAssertEqual(memory.baseAddress, ptr.baseAddress)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testReadingDoubleBuffer() {
|
||||
let count = 8
|
||||
let array: [Double] = Array(repeating: 8.8, count: count)
|
||||
@@ -76,16 +69,15 @@ struct ByteBufferTests {
|
||||
}
|
||||
let byteBuffer = ByteBuffer(bytes: bytes)
|
||||
byteBuffer.withUnsafePointerToSlice(index: 0, count: count) { ptr in
|
||||
#expect(ptr.count == count)
|
||||
XCTAssertEqual(ptr.count, count)
|
||||
bytes.withUnsafeBufferPointer {
|
||||
#expect(
|
||||
UnsafeRawPointer($0.baseAddress) ==
|
||||
UnsafeRawPointer(ptr.baseAddress))
|
||||
XCTAssertEqual(
|
||||
UnsafeRawPointer($0.baseAddress),
|
||||
UnsafeRawPointer(ptr.baseAddress))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testReadingNativeStructs() {
|
||||
let array = [
|
||||
MyGame_Example_Vec3(
|
||||
@@ -119,11 +111,11 @@ struct ByteBufferTests {
|
||||
let byteBuffer = ByteBuffer(bytes: bytes)
|
||||
byteBuffer
|
||||
.withUnsafePointerToSlice(index: 0, count: count) { bufferPointer in
|
||||
#expect(bufferPointer.count == count)
|
||||
XCTAssertEqual(bufferPointer.count, count)
|
||||
bytes.withUnsafeBufferPointer { ptr in
|
||||
#expect(
|
||||
UnsafeRawPointer(ptr.baseAddress) ==
|
||||
UnsafeRawPointer(bufferPointer.baseAddress))
|
||||
XCTAssertEqual(
|
||||
UnsafeRawPointer(ptr.baseAddress),
|
||||
UnsafeRawPointer(bufferPointer.baseAddress))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,3 +126,7 @@ private struct TestNativeStructs: NativeStruct {
|
||||
let y: Double
|
||||
let z: Int
|
||||
}
|
||||
|
||||
extension MyGame_Example_Color: CaseIterable {
|
||||
public static var allCases: [MyGame_Example_Color] = [.red, .blue, .green]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user