1 import XCTest
2 @testable import FlatBuffers
3
4 final class FlatBuffersStructsTests: XCTestCase {
5
testCreatingStructnull6 func testCreatingStruct() {
7 let v = createVecWrite(x: 1.0, y: 2.0, z: 3.0)
8 let b = FlatBufferBuilder(initialSize: 20)
9 let o = b.create(struct: v, type: Vec.self)
10 let end = VPointerVec.createVPointer(b: b, o: o)
11 b.finish(offset: end)
12 XCTAssertEqual(b.sizedByteArray, [12, 0, 0, 0, 0, 0, 6, 0, 4, 0, 4, 0, 6, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64])
13 }
14
testReadingStructnull15 func testReadingStruct() {
16 let v = createVecWrite(x: 1.0, y: 2.0, z: 3.0)
17 let b = FlatBufferBuilder(initialSize: 20)
18 let o = b.create(struct: v, type: Vec.self)
19 let end = VPointerVec.createVPointer(b: b, o: o)
20 b.finish(offset: end)
21 let buffer = b.sizedByteArray
22 XCTAssertEqual(buffer, [12, 0, 0, 0, 0, 0, 6, 0, 4, 0, 4, 0, 6, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64])
23 let point = VPointerVec.getRootAsCountry(ByteBuffer(bytes: buffer))
24 XCTAssertEqual(point.vec?.z, 3.0)
25 }
26
testCreatingVectorStructnull27 func testCreatingVectorStruct() {
28 let b = FlatBufferBuilder(initialSize: 20)
29 let path = b.createVector(structs: [createVecWrite(x: 1, y: 2, z: 3), createVecWrite(x: 4.0, y: 5.0, z: 6)], type: Vec.self)
30 let end = VPointerVectorVec.createVPointer(b: b, v: path)
31 b.finish(offset: end)
32 XCTAssertEqual(b.sizedByteArray, [12, 0, 0, 0, 8, 0, 8, 0, 0, 0, 4, 0, 8, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64])
33 }
34
testCreatingVectorStructWithForcedDefaultsnull35 func testCreatingVectorStructWithForcedDefaults() {
36 let b = FlatBufferBuilder(initialSize: 20, serializeDefaults: true)
37 let path = b.createVector(structs: [createVecWrite(x: 1, y: 2, z: 3), createVecWrite(x: 4.0, y: 5.0, z: 6)], type: Vec.self)
38 let end = VPointerVectorVec.createVPointer(b: b, v: path)
39 b.finish(offset: end)
40 XCTAssertEqual(b.sizedByteArray, [12, 0, 0, 0, 8, 0, 12, 0, 4, 0, 8, 0, 8, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64])
41 }
42
testCreatingEnumsnull43 func testCreatingEnums() {
44 let b = FlatBufferBuilder(initialSize: 20)
45 let path = b.createVector(structs: [createVecWrite(x: 1, y: 2, z: 3), createVecWrite(x: 4, y: 5, z: 6)], type: Vec.self)
46 let end = VPointerVectorVec.createVPointer(b: b, color: .blue, v: path)
47 b.finish(offset: end)
48 XCTAssertEqual(b.sizedByteArray, [12, 0, 0, 0, 8, 0, 12, 0, 4, 0, 8, 0, 8, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64])
49 }
50
testReadingStructWithEnumsnull51 func testReadingStructWithEnums() {
52 let b = FlatBufferBuilder(initialSize: 20)
53 let vec = createVec2(x: 1, y: 2, z: 3, color: .red)
54 let o = b.create(struct: vec, type: Vec2.self)
55 let end = VPointerVec2.createVPointer(b: b, o: o, type: .vec)
56 b.finish(offset: end)
57 let buffer = b.sizedByteArray
58 XCTAssertEqual(buffer, [16, 0, 0, 0, 0, 0, 10, 0, 12, 0, 12, 0, 11, 0, 4, 0, 10, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 1, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0])
59 let point = VPointerVec2.getRootAsCountry(ByteBuffer(bytes: buffer))
60 XCTAssertEqual(point.vec?.c, Color2.red)
61 XCTAssertEqual(point.vec?.x, 1.0)
62 XCTAssertEqual(point.vec?.y, 2.0)
63 XCTAssertEqual(point.vec?.z, 3.0)
64 XCTAssertEqual(point.UType, Test.vec)
65 }
66
67 }
68
createVecWritenull69 func createVecWrite(x: Float32, y: Float32, z: Float32) -> UnsafeMutableRawPointer{
70 let memory = UnsafeMutableRawPointer.allocate(byteCount: Vec.size, alignment: Vec.alignment)
71 memory.initializeMemory(as: UInt8.self, repeating: 0, count: Vec.size)
72 memory.storeBytes(of: x, toByteOffset: 0, as: Float32.self)
73 memory.storeBytes(of: y, toByteOffset: 4, as: Float32.self)
74 memory.storeBytes(of: z, toByteOffset: 8, as: Float32.self)
75 return memory
76 }
77
78 struct Vec: Readable {
79 var __buffer: ByteBuffer! { __p.bb }
80
81 static var size = 12
82 static var alignment = 4
83 private var __p: Struct
84 init(_ fb: ByteBuffer, o: Int32) { __p = Struct(bb: fb, position: o) }
85 var x: Float32 { return __p.readBuffer(of: Float32.self, at: 0)}
86 var y: Float32 { return __p.readBuffer(of: Float32.self, at: 4)}
87 var z: Float32 { return __p.readBuffer(of: Float32.self, at: 8)}
88 }
89
90 struct VPointerVec {
91
92 private var __t: Table
93
94 private init(_ t: Table) {
95 __t = t
96 }
97
98 var vec: Vec? { let o = __t.offset(4); return o == 0 ? nil : Vec(__t.bb, o: o + __t.postion) }
99
getRootAsCountrynull100 @inlinable static func getRootAsCountry(_ bb: ByteBuffer) -> VPointerVec {
101 return VPointerVec(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0))))
102 }
103
startVPointernull104 static func startVPointer(b: FlatBufferBuilder) -> UOffset { b.startTable(with: 1) }
finishnull105 static func finish(b: FlatBufferBuilder, s: UOffset) -> Offset<UOffset> { return Offset(offset: b.endTable(at: s)) }
106
createVPointernull107 static func createVPointer(b: FlatBufferBuilder, o: Offset<UOffset>) -> Offset<UOffset> {
108 let s = VPointerVec.startVPointer(b: b)
109 b.add(structOffset: 0)
110 return VPointerVec.finish(b: b, s: s)
111 }
112 }
113
114 enum Color: UInt32 { case red = 0, green = 1, blue = 2 }
115
116 private let VPointerVectorVecOffsets: (color: VOffset, vector: VOffset) = (0, 1)
117
118 struct VPointerVectorVec {
119
startVPointernull120 static func startVPointer(b: FlatBufferBuilder) -> UOffset { b.startTable(with: 2) }
121
addVectornull122 static func addVector(b: FlatBufferBuilder, v: Offset<UOffset>) { b.add(offset: v, at: VPointerVectorVecOffsets.vector) }
123
addColornull124 static func addColor(b: FlatBufferBuilder, color: Color) { b.add(element: color.rawValue, def: 1, at: VPointerVectorVecOffsets.color) }
125
finishnull126 static func finish(b: FlatBufferBuilder, s: UOffset) -> Offset<UOffset> { return Offset(offset: b.endTable(at: s)) }
127
createVPointernull128 static func createVPointer(b: FlatBufferBuilder, color: Color = .green, v: Offset<UOffset>) -> Offset<UOffset> {
129 let s = VPointerVectorVec.startVPointer(b: b)
130 VPointerVectorVec.addVector(b: b, v: v)
131 VPointerVectorVec.addColor(b: b, color: color)
132 return VPointerVectorVec.finish(b: b, s: s)
133 }
134 }
135
136 enum Color2: Int32 { case red = 0, green = 1, blue = 2 }
137 enum Test: Byte { case none = 0, vec = 1 }
138
createVec2null139 func createVec2(x: Float32 = 0, y: Float32 = 0, z: Float32 = 0, color: Color2) -> UnsafeMutableRawPointer {
140 let memory = UnsafeMutableRawPointer.allocate(byteCount: Vec2.size, alignment: Vec2.alignment)
141 memory.initializeMemory(as: UInt8.self, repeating: 0, count: Vec2.size)
142 memory.storeBytes(of: x, toByteOffset: 0, as: Float32.self)
143 memory.storeBytes(of: y, toByteOffset: 4, as: Float32.self)
144 memory.storeBytes(of: z, toByteOffset: 8, as: Float32.self)
145 return memory
146 }
147
148 struct Vec2: Readable {
149 var __buffer: ByteBuffer! { __p.bb }
150
151 static var size = 13
152 static var alignment = 4
153 private var __p: Struct
154
155 init(_ fb: ByteBuffer, o: Int32) { __p = Struct(bb: fb, position: o) }
156 var c: Color2 { return Color2(rawValue: __p.readBuffer(of: Int32.self, at: 12)) ?? .red }
157 var x: Float32 { return __p.readBuffer(of: Float32.self, at: 0)}
158 var y: Float32 { return __p.readBuffer(of: Float32.self, at: 4)}
159 var z: Float32 { return __p.readBuffer(of: Float32.self, at: 8)}
160 }
161
162 struct VPointerVec2 {
163
164 private var __t: Table
165
166 private init(_ t: Table) {
167 __t = t
168 }
169
170 var vec: Vec2? { let o = __t.offset(4); return o == 0 ? nil : Vec2( __t.bb, o: o + __t.postion) }
171 var UType: Test? { let o = __t.offset(6); return o == 0 ? Test.none : Test(rawValue: __t.readBuffer(of: Byte.self, at: o)) }
172
getRootAsCountrynull173 @inlinable static func getRootAsCountry(_ bb: ByteBuffer) -> VPointerVec2 {
174 return VPointerVec2(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0))))
175 }
176
startVPointernull177 static func startVPointer(b: FlatBufferBuilder) -> UOffset { b.startTable(with: 3) }
finishnull178 static func finish(b: FlatBufferBuilder, s: UOffset) -> Offset<UOffset> { return Offset(offset: b.endTable(at: s)) }
179
createVPointernull180 static func createVPointer(b: FlatBufferBuilder, o: Offset<UOffset>, type: Test) -> Offset<UOffset> {
181 let s = VPointerVec2.startVPointer(b: b)
182 b.add(structOffset: 0)
183 b.add(element: type.rawValue, def: Test.none.rawValue, at: 1)
184 b.add(offset: o, at: 2)
185 return VPointerVec2.finish(b: b, s: s)
186 }
187 }
188