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 FlatBuffersDoubleTests: XCTestCase { 21 22 let country = "Norway" 23 testCreateFinishnull24 func testCreateFinish() { 25 var b = FlatBufferBuilder(initialSize: 16) 26 let countryOff = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100) 27 b.finish(offset: countryOff) 28 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] 29 XCTAssertEqual(b.sizedByteArray, v) 30 } 31 testCreateFinishWithPrefixnull32 func testCreateFinishWithPrefix() { 33 var b = FlatBufferBuilder(initialSize: 16) 34 let countryOff = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100) 35 b.finish(offset: countryOff, addPrefix: true) 36 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] 37 XCTAssertEqual(b.sizedByteArray, v) 38 } 39 } 40 41 class CountryDouble { 42 43 static let offsets: (name: VOffset, lan: VOffset, lng: VOffset) = (4, 6, 8) 44 45 private var table: Table 46 47 private init(table t: Table) { table = t } 48 getRootAsCountrynull49 static func getRootAsCountry(_ bb: ByteBuffer) -> CountryDouble { 50 let pos = bb.read(def: Int32.self, position: Int(bb.size)) 51 return CountryDouble(table: Table(bb: bb, position: Int32(pos))) 52 } 53 54 static func createCountry( 55 builder: inout FlatBufferBuilder, 56 name: String, 57 log: Double, 58 lan: Double) -> Offset 59 { 60 createCountry(builder: &builder, offset: builder.create(string: name), log: log, lan: lan) 61 } 62 63 static func createCountry( 64 builder: inout FlatBufferBuilder, 65 offset: Offset, 66 log: Double, 67 lan: Double) -> Offset 68 { 69 let _start = builder.startTable(with: 3) 70 CountryDouble.add(builder: &builder, lng: log) 71 CountryDouble.add(builder: &builder, lan: lan) 72 CountryDouble.add(builder: &builder, name: offset) 73 return CountryDouble.end(builder: &builder, startOffset: _start) 74 } 75 endnull76 static func end(builder: inout FlatBufferBuilder, startOffset: UOffset) -> Offset { 77 Offset(offset: builder.endTable(at: startOffset)) 78 } 79 addnull80 static func add(builder: inout FlatBufferBuilder, name: String) { 81 add(builder: &builder, name: builder.create(string: name)) 82 } 83 addnull84 static func add(builder: inout FlatBufferBuilder, name: Offset) { 85 builder.add(offset: name, at: Country.offsets.name) 86 } 87 addnull88 static func add(builder: inout FlatBufferBuilder, lan: Double) { 89 builder.add(element: lan, def: 0, at: Country.offsets.lan) 90 } 91 addnull92 static func add(builder: inout FlatBufferBuilder, lng: Double) { 93 builder.add(element: lng, def: 0, at: Country.offsets.lng) 94 } 95 } 96