1 /* 2 * Copyright 2021 Google Inc. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import XCTest 18 @testable import FlatBuffers 19 20 final class FlatBuffersTests: XCTestCase { 21 22 let country = "Norway" 23 testEndiannull24 func testEndian() { XCTAssertEqual(isLitteEndian, true) } 25 testOffsetnull26 func testOffset() { 27 let o = Offset() 28 let b = Offset(offset: 1) 29 XCTAssertEqual(o.isEmpty, true) 30 XCTAssertEqual(b.isEmpty, false) 31 } 32 testCreateStringnull33 func testCreateString() { 34 let helloWorld = "Hello, world!" 35 var b = FlatBufferBuilder(initialSize: 16) 36 XCTAssertEqual(b.create(string: country).o, 12) 37 XCTAssertEqual(b.create(string: helloWorld).o, 32) 38 b.clear() 39 XCTAssertEqual(b.create(string: helloWorld).o, 20) 40 XCTAssertEqual(b.create(string: country).o, 32) 41 b.clear() 42 XCTAssertEqual(b.create(string: String(repeating: "a", count: 257)).o, 264) 43 } 44 testStartTablenull45 func testStartTable() { 46 var b = FlatBufferBuilder(initialSize: 16) 47 XCTAssertNoThrow(b.startTable(with: 0)) 48 b.clear() 49 XCTAssertEqual(b.create(string: country).o, 12) 50 XCTAssertEqual(b.startTable(with: 0), 12) 51 } 52 testCreateFinishnull53 func testCreateFinish() { 54 var b = FlatBufferBuilder(initialSize: 16) 55 let countryOff = Country.createCountry( 56 builder: &b, 57 name: country, 58 log: 200, 59 lan: 100) 60 b.finish(offset: countryOff) 61 // swiftformat:disable all 62 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] 63 // swiftformat:enable all 64 XCTAssertEqual(b.sizedByteArray, v) 65 } 66 testCreateFinishWithPrefixnull67 func testCreateFinishWithPrefix() { 68 var b = FlatBufferBuilder(initialSize: 16) 69 let countryOff = Country.createCountry( 70 builder: &b, 71 name: country, 72 log: 200, 73 lan: 100) 74 b.finish(offset: countryOff, addPrefix: true) 75 // swiftformat:disable all 76 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] 77 // swiftformat:enable all 78 XCTAssertEqual(b.sizedByteArray, v) 79 } 80 testReadCountrynull81 func testReadCountry() { 82 // swiftformat:disable all 83 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] 84 // swiftformat:enable all 85 let buffer = ByteBuffer(bytes: v) 86 let c = Country.getRootAsCountry(buffer) 87 XCTAssertEqual(c.lan, 100) 88 XCTAssertEqual(c.log, 200) 89 XCTAssertEqual(c.nameVector, [78, 111, 114, 119, 97, 121]) 90 XCTAssertEqual(c.name, country) 91 } 92 testWriteNullableStringsnull93 func testWriteNullableStrings() { 94 var b = FlatBufferBuilder() 95 XCTAssertTrue(b.create(string: nil).isEmpty) 96 XCTAssertTrue(b.createShared(string: nil).isEmpty) 97 } 98 testWriteOptionalValuesnull99 func testWriteOptionalValues() { 100 var b = FlatBufferBuilder() 101 let root = optional_scalars_ScalarStuff.createScalarStuff( 102 &b, 103 justI8: 80, 104 maybeI8: nil, 105 justU8: 100, 106 maybeU8: 10, 107 maybeBool: true, 108 justEnum: .one, 109 maybeEnum: nil) 110 b.finish(offset: root) 111 let scalarTable = optional_scalars_ScalarStuff 112 .getRootAsScalarStuff(bb: b.sizedBuffer) 113 XCTAssertEqual(scalarTable.justI8, 80) 114 XCTAssertNil(scalarTable.maybeI8) 115 XCTAssertEqual(scalarTable.maybeBool, true) 116 XCTAssertEqual(scalarTable.defaultI8, 42) 117 XCTAssertEqual(scalarTable.justU8, 100) 118 XCTAssertEqual(scalarTable.maybeU8, 10) 119 XCTAssertEqual(scalarTable.justEnum, .one) 120 XCTAssertNil(scalarTable.maybeEnum) 121 } 122 } 123 124 class Country { 125 126 static let offsets: (name: VOffset, lan: VOffset, lng: VOffset) = (4, 6, 8) 127 private var __t: Table 128 129 private init(_ t: Table) { 130 __t = t 131 } 132 133 var lan: Int32 { let o = __t.offset(6); return o == 0 ? 0 : __t.readBuffer( 134 of: Int32.self, 135 at: o) } 136 var log: Int32 { let o = __t.offset(8); return o == 0 ? 0 : __t.readBuffer( 137 of: Int32.self, 138 at: o) } 139 var nameVector: [UInt8]? { __t.getVector(at: 4) } 140 var name: String? { 141 let o = __t.offset(4); return o == 0 ? nil : __t.string(at: o) } 142 143 @inlinable getRootAsCountrynull144 static func getRootAsCountry(_ bb: ByteBuffer) -> Country { 145 Country(Table( 146 bb: bb, 147 position: Int32(bb.read(def: UOffset.self, position: 0)))) 148 } 149 150 @inlinable 151 static func createCountry( 152 builder: inout FlatBufferBuilder, 153 name: String, 154 log: Int32, 155 lan: Int32) -> Offset 156 { 157 createCountry( 158 builder: &builder, 159 offset: builder.create(string: name), 160 log: log, 161 lan: lan) 162 } 163 164 @inlinable 165 static func createCountry( 166 builder: inout FlatBufferBuilder, 167 offset: Offset, 168 log: Int32, 169 lan: Int32) -> Offset 170 { 171 let _start = builder.startTable(with: 3) 172 Country.add(builder: &builder, lng: log) 173 Country.add(builder: &builder, lan: lan) 174 Country.add(builder: &builder, name: offset) 175 return Country.end(builder: &builder, startOffset: _start) 176 } 177 178 @inlinable 179 static func end( 180 builder: inout FlatBufferBuilder, 181 startOffset: UOffset) -> Offset 182 { 183 Offset(offset: builder.endTable(at: startOffset)) 184 } 185 186 @inlinable addnull187 static func add(builder: inout FlatBufferBuilder, name: String) { 188 add(builder: &builder, name: builder.create(string: name)) 189 } 190 191 @inlinable addnull192 static func add(builder: inout FlatBufferBuilder, name: Offset) { 193 builder.add(offset: name, at: Country.offsets.name) 194 } 195 196 @inlinable addnull197 static func add(builder: inout FlatBufferBuilder, lan: Int32) { 198 builder.add(element: lan, def: 0, at: Country.offsets.lan) 199 } 200 201 @inlinable addnull202 static func add(builder: inout FlatBufferBuilder, lng: Int32) { 203 builder.add(element: lng, def: 0, at: Country.offsets.lng) 204 } 205 } 206