1 import XCTest 2 @testable import FlatBuffers 3 4 final class FlatBuffersUnionTests: XCTestCase { 5 testCreateMonstornull6 func testCreateMonstor() { 7 8 var b = FlatBufferBuilder(initialSize: 20) 9 let dmg: Int16 = 5 10 let str = "Axe" 11 let axe = b.create(string: str) 12 let weapon = Weapon.createWeapon(builder: &b, offset: axe, dmg: dmg) 13 let weapons = b.createVector(ofOffsets: [weapon]) 14 let root = Monster.createMonster(builder: &b, 15 offset: weapons, 16 equipment: .Weapon, 17 equippedOffset: weapon.o) 18 b.finish(offset: root) 19 let buffer = b.sizedByteArray 20 XCTAssertEqual(buffer, [16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 8, 0, 7, 0, 12, 0, 10, 0, 0, 0, 0, 0, 0, 1, 8, 0, 0, 0, 20, 0, 0, 0, 1, 0, 0, 0, 12, 0, 0, 0, 8, 0, 12, 0, 8, 0, 6, 0, 8, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 3, 0, 0, 0, 65, 120, 101, 0]) 21 let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buffer)) 22 XCTAssertEqual(monster.weapon(at: 0)?.dmg, dmg) 23 XCTAssertEqual(monster.weapon(at: 0)?.name, str) 24 XCTAssertEqual(monster.weapon(at: 0)?.nameVector, [65, 120, 101]) 25 let p: Weapon? = monster.equiped() 26 XCTAssertEqual(p?.dmg, dmg) 27 XCTAssertEqual(p?.name, str) 28 XCTAssertEqual(p?.nameVector, [65, 120, 101]) 29 } 30 testEndTableFinishnull31 func testEndTableFinish() { 32 var builder = FlatBufferBuilder(initialSize: 20) 33 let sword = builder.create(string: "Sword") 34 let axe = builder.create(string: "Axe") 35 let weaponOne = Weapon.createWeapon(builder: &builder, offset: sword, dmg: 3) 36 let weaponTwo = Weapon.createWeapon(builder: &builder, offset: axe, dmg: 5) 37 let name = builder.create(string: "Orc") 38 let inventory: [UInt8] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 39 let inv = builder.createVector(inventory, size: 10) 40 let weapons = builder.createVector(ofOffsets: [weaponOne, weaponTwo]) 41 var vecArray: [UnsafeMutableRawPointer] = [] 42 vecArray.append(createVecWrite(x: 4.0, y: 5.0, z: 6.0)) 43 vecArray.append(createVecWrite(x: 1.0, y: 2.0, z: 3.0)) 44 let path = builder.createVector(structs: vecArray, type: Vec.self) 45 let orc = FinalMonster.createMonster(builder: &builder, 46 position: builder.create(struct: createVecWrite(x: 1.0, y: 2.0, z: 3.0), type: Vec.self), 47 hp: 300, 48 name: name, 49 inventory: inv, 50 color: .red, 51 weapons: weapons, 52 equipment: .Weapon, 53 equippedOffset: weaponTwo, 54 path: path) 55 builder.finish(offset: orc) 56 XCTAssertEqual(builder.sizedByteArray, [32, 0, 0, 0, 0, 0, 26, 0, 36, 0, 36, 0, 0, 0, 34, 0, 28, 0, 0, 0, 24, 0, 23, 0, 16, 0, 15, 0, 8, 0, 4, 0, 26, 0, 0, 0, 44, 0, 0, 0, 104, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 76, 0, 0, 0, 0, 0, 44, 1, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 2, 0, 0, 0, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 2, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 10, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 3, 0, 0, 0, 79, 114, 99, 0, 244, 255, 255, 255, 0, 0, 5, 0, 24, 0, 0, 0, 8, 0, 12, 0, 8, 0, 6, 0, 8, 0, 0, 0, 0, 0, 3, 0, 12, 0, 0, 0, 3, 0, 0, 0, 65, 120, 101, 0, 5, 0, 0, 0, 83, 119, 111, 114, 100, 0, 0, 0]) 57 } 58 testEnumVectornull59 func testEnumVector() { 60 let vectorOfEnums: [ColorsNameSpace.RGB] = [.blue, .green] 61 62 let builder = FlatBufferBuilder(initialSize: 1) 63 let off = builder.createVector(vectorOfEnums) 64 let start = ColorsNameSpace.Monster.startMonster(builder) 65 ColorsNameSpace.Monster.add(colors: off, builder) 66 let end = ColorsNameSpace.Monster.endMonster(builder, start: start) 67 builder.finish(offset: end) 68 XCTAssertEqual(builder.sizedByteArray, [12, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0]) 69 let monster = ColorsNameSpace.Monster.getRootAsMonster(bb: builder.buffer) 70 XCTAssertEqual(monster.colorsCount, 2) 71 XCTAssertEqual(monster.colors(at: 0), .blue) 72 XCTAssertEqual(monster.colors(at: 1), .green) 73 } 74 testUnionVectornull75 func testUnionVector() { 76 let fb = FlatBufferBuilder() 77 78 let swordDmg: Int32 = 8 79 let attackStart = Attacker.startAttacker(fb) 80 Attacker.add(swordAttackDamage: swordDmg, fb) 81 let attack = Attacker.endAttacker(fb, start: attackStart) 82 83 let characterType: [Character] = [.belle, .mulan, .bookfan] 84 let characters = [ 85 fb.create(struct: createBookReader(booksRead: 7), type: BookReader.self), 86 attack, 87 fb.create(struct: createBookReader(booksRead: 2), type: BookReader.self), 88 ] 89 let types = fb.createVector(characterType) 90 let characterVector = fb.createVector(ofOffsets: characters) 91 let end = Movie.createMovie(fb, vectorOfCharactersType: types, vectorOfCharacters: characterVector) 92 Movie.finish(fb, end: end) 93 94 let movie = Movie.getRootAsMovie(bb: fb.buffer) 95 XCTAssertEqual(movie.charactersTypeCount, Int32(characterType.count)) 96 XCTAssertEqual(movie.charactersCount, Int32(characters.count)) 97 98 for i in 0..<movie.charactersTypeCount { 99 XCTAssertEqual(movie.charactersType(at: i), characterType[Int(i)]) 100 } 101 102 XCTAssertEqual(movie.characters(at: 0, type: BookReader.self)?.booksRead, 7) 103 XCTAssertEqual(movie.characters(at: 1, type: Attacker.self)?.swordAttackDamage, swordDmg) 104 XCTAssertEqual(movie.characters(at: 2, type: BookReader.self)?.booksRead, 2) 105 } 106 } 107 108 public enum ColorsNameSpace { 109 110 enum RGB: Int32, Enum { 111 typealias T = Int32 112 static var byteSize: Int { return MemoryLayout<Int32>.size } 113 var value: Int32 { return self.rawValue } 114 case red = 0, green = 1, blue = 2 115 } 116 117 struct Monster: FlatBufferObject { 118 var __buffer: ByteBuffer! { _accessor.bb } 119 120 private var _accessor: Table getRootAsMonsternull121 static func getRootAsMonster(bb: ByteBuffer) -> Monster { return Monster(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) } 122 123 init(_ t: Table) { _accessor = t } 124 init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) } 125 126 public var colorsCount: Int32 { let o = _accessor.offset(4); return o == 0 ? 0 : _accessor.vector(count: o) } colorsnull127 public func colors(at index: Int32) -> ColorsNameSpace.RGB? { let o = _accessor.offset(4); return o == 0 ? ColorsNameSpace.RGB(rawValue: 0)! : ColorsNameSpace.RGB(rawValue: _accessor.directRead(of: Int32.self, offset: _accessor.vector(at: o) + index * 4)) } startMonsternull128 static func startMonster(_ fbb: FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) } addnull129 static func add(colors: Offset<UOffset>, _ fbb: FlatBufferBuilder) { fbb.add(offset: colors, at: 0) } endMonsternull130 static func endMonster(_ fbb: FlatBufferBuilder, start: UOffset) -> Offset<UOffset> { let end = Offset<UOffset>(offset: fbb.endTable(at: start)); return end } 131 } 132 } 133 134 135 enum Equipment: Byte { case none, Weapon } 136 137 enum Color3: Int8 { case red = 0, green, blue } 138 139 struct FinalMonster { 140 141 @inlinable static func createMonster(builder: inout FlatBufferBuilder, 142 position: Offset<UOffset>, 143 hp: Int16, 144 name: Offset<String>, 145 inventory: Offset<UOffset>, 146 color: Color3, 147 weapons: Offset<UOffset>, 148 equipment: Equipment = .none, 149 equippedOffset: Offset<Weapon>, 150 path: Offset<UOffset>) -> Offset<Monster> { 151 let start = builder.startTable(with: 11) 152 builder.add(structOffset: 0) 153 builder.add(element: hp, def: 100, at: 2) 154 builder.add(offset: name, at: 3) 155 builder.add(offset: inventory, at: 5) 156 builder.add(element: color.rawValue, def: Color3.green.rawValue, at: 6) 157 builder.add(offset: weapons, at: 7) 158 builder.add(element: equipment.rawValue, def: Equipment.none.rawValue, at: 8) 159 builder.add(offset: equippedOffset, at: 9) 160 builder.add(offset: path, at: 10) 161 return Offset(offset: builder.endTable(at: start)) 162 } 163 } 164 165 struct Monster { 166 167 private var __t: Table 168 169 init(_ fb: ByteBuffer, o: Int32) { __t = Table(bb: fb, position: o) } 170 init(_ t: Table) { __t = t } 171 weaponnull172 func weapon(at index: Int32) -> Weapon? { let o = __t.offset(4); return o == 0 ? nil : Weapon.assign(__t.indirect(__t.vector(at: o) + (index * 4)), __t.bb) } 173 equiped<T: FlatBufferObject>null174 func equiped<T: FlatBufferObject>() -> T? { 175 let o = __t.offset(8); return o == 0 ? nil : __t.union(o) 176 } 177 getRootAsMonsternull178 static func getRootAsMonster(bb: ByteBuffer) -> Monster { 179 return Monster(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0)))) 180 } 181 182 @inlinable static func createMonster(builder: inout FlatBufferBuilder, 183 offset: Offset<UOffset>, 184 equipment: Equipment = .none, 185 equippedOffset: UOffset) -> Offset<Monster> { 186 let start = builder.startTable(with: 3) 187 builder.add(element: equippedOffset, def: 0, at: 2) 188 builder.add(offset: offset, at: 0) 189 builder.add(element: equipment.rawValue, def: Equipment.none.rawValue, at: 1) 190 return Offset(offset: builder.endTable(at: start)) 191 } 192 } 193 194 struct Weapon: FlatBufferObject { 195 196 var __buffer: ByteBuffer! { __t.bb } 197 198 static let offsets: (name: VOffset, dmg: VOffset) = (0, 1) 199 private var __t: Table 200 201 init(_ t: Table) { __t = t } 202 init(_ fb: ByteBuffer, o: Int32) { __t = Table(bb: fb, position: o)} 203 204 var dmg: Int16 { let o = __t.offset(6); return o == 0 ? 0 : __t.readBuffer(of: Int16.self, at: o) } 205 var nameVector: [UInt8]? { return __t.getVector(at: 4) } 206 var name: String? { let o = __t.offset(4); return o == 0 ? nil : __t.string(at: o) } 207 assignnull208 static func assign(_ i: Int32, _ bb: ByteBuffer) -> Weapon { return Weapon(Table(bb: bb, position: i)) } 209 createWeaponnull210 @inlinable static func createWeapon(builder: inout FlatBufferBuilder, offset: Offset<String>, dmg: Int16) -> Offset<Weapon> { 211 let _start = builder.startTable(with: 2) 212 Weapon.add(builder: &builder, name: offset) 213 Weapon.add(builder: &builder, dmg: dmg) 214 return Weapon.end(builder: &builder, startOffset: _start) 215 } 216 endnull217 @inlinable static func end(builder: inout FlatBufferBuilder, startOffset: UOffset) -> Offset<Weapon> { 218 return Offset(offset: builder.endTable(at: startOffset)) 219 } 220 addnull221 @inlinable static func add(builder: inout FlatBufferBuilder, name: Offset<String>) { 222 builder.add(offset: name, at: Weapon.offsets.name) 223 } 224 addnull225 @inlinable static func add(builder: inout FlatBufferBuilder, dmg: Int16) { 226 builder.add(element: dmg, def: 0, at: Weapon.offsets.dmg) 227 } 228 } 229