1 import XCTest 2 @testable import FlatBuffers 3 4 final class FlatBuffersDoubleTests: XCTestCase { 5 6 let country = "Norway" 7 testCreateCountrynull8 func testCreateCountry() { 9 var b = FlatBufferBuilder(initialSize: 16) 10 _ = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100) 11 let v: [UInt8] = [10, 0, 28, 0, 4, 0, 8, 0, 16, 0, 10, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 64, 0, 0, 0, 0, 0, 0, 105, 64, 0, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0] 12 XCTAssertEqual(b.sizedByteArray, v) 13 } 14 testCreateFinishnull15 func testCreateFinish() { 16 var b = FlatBufferBuilder(initialSize: 16) 17 let countryOff = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100) 18 b.finish(offset: countryOff) 19 let v: [UInt8] = [16, 0, 0, 0, 0, 0, 10, 0, 28, 0, 4, 0, 8, 0, 16, 0, 10, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 64, 0, 0, 0, 0, 0, 0, 105, 64, 0, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0] 20 XCTAssertEqual(b.sizedByteArray, v) 21 } 22 testCreateFinishWithPrefixnull23 func testCreateFinishWithPrefix() { 24 var b = FlatBufferBuilder(initialSize: 16) 25 let countryOff = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100) 26 b.finish(offset: countryOff, addPrefix: true) 27 let v: [UInt8] = [60, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 28, 0, 4, 0, 8, 0, 16, 0, 10, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 64, 0, 0, 0, 0, 0, 0, 105, 64, 0, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0] 28 XCTAssertEqual(b.sizedByteArray, v) 29 } 30 } 31 32 class CountryDouble { 33 34 static let offsets: (name: VOffset, lan: VOffset, lng: VOffset) = (4,6,8) 35 36 private var table: Table 37 38 private init(table t: Table) { table = t } 39 getRootAsCountrynull40 static func getRootAsCountry(_ bb: ByteBuffer) -> CountryDouble { 41 let pos = bb.read(def: Int32.self, position: Int(bb.size)) 42 return CountryDouble(table: Table(bb: bb, position: Int32(pos))) 43 } 44 createCountrynull45 static func createCountry(builder: inout FlatBufferBuilder, name: String, log: Double, lan: Double) -> Offset<Country> { 46 return createCountry(builder: &builder, offset: builder.create(string: name), log: log, lan: lan) 47 } 48 createCountrynull49 static func createCountry(builder: inout FlatBufferBuilder, offset: Offset<String>, log: Double, lan: Double) -> Offset<Country> { 50 let _start = builder.startTable(with: 3) 51 CountryDouble.add(builder: &builder, lng: log) 52 CountryDouble.add(builder: &builder, lan: lan) 53 CountryDouble.add(builder: &builder, name: offset) 54 return CountryDouble.end(builder: &builder, startOffset: _start) 55 } 56 endnull57 static func end(builder: inout FlatBufferBuilder, startOffset: UOffset) -> Offset<Country> { 58 return Offset(offset: builder.endTable(at: startOffset)) 59 } 60 addnull61 static func add(builder: inout FlatBufferBuilder, name: String) { 62 add(builder: &builder, name: builder.create(string: name)) 63 } 64 addnull65 static func add(builder: inout FlatBufferBuilder, name: Offset<String>) { 66 builder.add(offset: name, at: Country.offsets.name) 67 } 68 addnull69 static func add(builder: inout FlatBufferBuilder, lan: Double) { 70 builder.add(element: lan, def: 0, at: Country.offsets.lan) 71 } 72 addnull73 static func add(builder: inout FlatBufferBuilder, lng: Double) { 74 builder.add(element: lng, def: 0, at: Country.offsets.lng) 75 } 76 } 77