1 import XCTest 2 @testable import FlatBuffers 3 4 final class FlatBuffersTests: XCTestCase { 5 6 let country = "Norway" 7 testEndiannull8 func testEndian() { XCTAssertEqual(isLitteEndian, true) } 9 testOffsetnull10 func testOffset() { 11 let o = Offset<Int>() 12 let b = Offset<Int>(offset: 1) 13 XCTAssertEqual(o.isEmpty, true) 14 XCTAssertEqual(b.isEmpty, false) 15 } 16 testCreateStringnull17 func testCreateString() { 18 let helloWorld = "Hello, world!" 19 let b = FlatBufferBuilder(initialSize: 16) 20 XCTAssertEqual(b.create(string: country).o, 12) 21 XCTAssertEqual(b.create(string: helloWorld).o, 32) 22 b.clear() 23 XCTAssertEqual(b.create(string: helloWorld).o, 20) 24 XCTAssertEqual(b.create(string: country).o, 32) 25 b.clear() 26 XCTAssertEqual(b.create(string: String(repeating: "a", count: 257)).o, 264) 27 } 28 testStartTablenull29 func testStartTable() { 30 let b = FlatBufferBuilder(initialSize: 16) 31 XCTAssertNoThrow(b.startTable(with: 0)) 32 b.clear() 33 XCTAssertEqual(b.create(string: country).o, 12) 34 XCTAssertEqual(b.startTable(with: 0), 12) 35 } 36 testCreatenull37 func testCreate() { 38 var b = FlatBufferBuilder(initialSize: 16) 39 _ = Country.createCountry(builder: &b, name: country, log: 200, lan: 100) 40 let v: [UInt8] = [10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 12, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0] 41 XCTAssertEqual(b.sizedByteArray, v) 42 } 43 testCreateFinishnull44 func testCreateFinish() { 45 var b = FlatBufferBuilder(initialSize: 16) 46 let countryOff = Country.createCountry(builder: &b, name: country, log: 200, lan: 100) 47 b.finish(offset: countryOff) 48 let v: [UInt8] = [16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 12, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0] 49 XCTAssertEqual(b.sizedByteArray, v) 50 } 51 testCreateFinishWithPrefixnull52 func testCreateFinishWithPrefix() { 53 var b = FlatBufferBuilder(initialSize: 16) 54 let countryOff = Country.createCountry(builder: &b, name: country, log: 200, lan: 100) 55 b.finish(offset: countryOff, addPrefix: true) 56 let v: [UInt8] = [44, 0, 0, 0, 16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 12, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0] 57 XCTAssertEqual(b.sizedByteArray, v) 58 } 59 testReadCountrynull60 func testReadCountry() { 61 let v: [UInt8] = [16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 12, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0] 62 let buffer = ByteBuffer(bytes: v) 63 let c = Country.getRootAsCountry(buffer) 64 XCTAssertEqual(c.lan, 100) 65 XCTAssertEqual(c.log, 200) 66 XCTAssertEqual(c.nameVector, [78, 111, 114, 119, 97, 121]) 67 XCTAssertEqual(c.name, country) 68 } 69 } 70 71 class Country { 72 73 static let offsets: (name: VOffset, lan: VOffset, lng: VOffset) = (0, 1, 2) 74 private var __t: Table 75 76 private init(_ t: Table) { 77 __t = t 78 } 79 80 var lan: Int32 { let o = __t.offset(6); return o == 0 ? 0 : __t.readBuffer(of: Int32.self, at: o) } 81 var log: Int32 { let o = __t.offset(8); return o == 0 ? 0 : __t.readBuffer(of: Int32.self, at: o) } 82 var nameVector: [UInt8]? { return __t.getVector(at: 4) } 83 var name: String? { let o = __t.offset(4); return o == 0 ? nil : __t.string(at: o) } 84 getRootAsCountrynull85 @inlinable static func getRootAsCountry(_ bb: ByteBuffer) -> Country { 86 return Country(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0)))) 87 } 88 createCountrynull89 @inlinable static func createCountry(builder: inout FlatBufferBuilder, name: String, log: Int32, lan: Int32) -> Offset<Country> { 90 return createCountry(builder: &builder, offset: builder.create(string: name), log: log, lan: lan) 91 } 92 createCountrynull93 @inlinable static func createCountry(builder: inout FlatBufferBuilder, offset: Offset<String>, log: Int32, lan: Int32) -> Offset<Country> { 94 let _start = builder.startTable(with: 3) 95 Country.add(builder: &builder, lng: log) 96 Country.add(builder: &builder, lan: lan) 97 Country.add(builder: &builder, name: offset) 98 return Country.end(builder: &builder, startOffset: _start) 99 } 100 endnull101 @inlinable static func end(builder: inout FlatBufferBuilder, startOffset: UOffset) -> Offset<Country> { 102 return Offset(offset: builder.endTable(at: startOffset)) 103 } 104 addnull105 @inlinable static func add(builder: inout FlatBufferBuilder, name: String) { 106 add(builder: &builder, name: builder.create(string: name)) 107 } 108 addnull109 @inlinable static func add(builder: inout FlatBufferBuilder, name: Offset<String>) { 110 builder.add(offset: name, at: Country.offsets.name) 111 } 112 addnull113 @inlinable static func add(builder: inout FlatBufferBuilder, lan: Int32) { 114 builder.add(element: lan, def: 0, at: Country.offsets.lan) 115 } 116 addnull117 @inlinable static func add(builder: inout FlatBufferBuilder, lng: Int32) { 118 builder.add(element: lng, def: 0, at: Country.offsets.lng) 119 } 120 } 121