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( 27 builder: &b, 28 name: country, 29 log: 200, 30 lan: 100) 31 b.finish(offset: countryOff) 32 // swiftformat:disable all 33 let v: [UInt8] = [ 34 16, 0, 0, 0, 0, 0, 10, 0, 28, 0, 4, 0, 8, 0, 16, 0, 10, 35 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 64, 0, 0, 0, 36 0, 0, 0, 105, 64, 0, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 37 97, 121, 0, 0 38 ] 39 // swiftformat:enable all 40 XCTAssertEqual(b.sizedByteArray, v) 41 } 42 testCreateFinishWithPrefixnull43 func testCreateFinishWithPrefix() { 44 var b = FlatBufferBuilder(initialSize: 16) 45 let countryOff = CountryDouble.createCountry( 46 builder: &b, 47 name: country, 48 log: 200, 49 lan: 100) 50 b.finish(offset: countryOff, addPrefix: true) 51 // swiftformat:disable all 52 let v: [UInt8] = [ 53 60, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 28 54 , 0, 4, 0, 8, 0, 16, 0, 10, 0, 0, 0, 24, 0, 0, 0, 0, 0, 55 0, 0, 0, 0, 89, 64, 0, 0, 0, 0, 0, 0, 105, 64, 0, 0, 0, 56 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0 57 ] 58 // swiftformat:enable all 59 XCTAssertEqual(b.sizedByteArray, v) 60 } 61 } 62 63 class CountryDouble { 64 65 static let offsets: (name: VOffset, lan: VOffset, lng: VOffset) = (4, 6, 8) 66 67 private var table: Table 68 69 private init(table t: Table) { table = t } 70 getRootAsCountrynull71 static func getRootAsCountry(_ bb: ByteBuffer) -> CountryDouble { 72 let pos = bb.read(def: Int32.self, position: Int(bb.size)) 73 return CountryDouble(table: Table(bb: bb, position: Int32(pos))) 74 } 75 76 static func createCountry( 77 builder: inout FlatBufferBuilder, 78 name: String, 79 log: Double, 80 lan: Double) -> Offset 81 { 82 createCountry( 83 builder: &builder, 84 offset: builder.create(string: name), 85 log: log, 86 lan: lan) 87 } 88 89 static func createCountry( 90 builder: inout FlatBufferBuilder, 91 offset: Offset, 92 log: Double, 93 lan: Double) -> Offset 94 { 95 let _start = builder.startTable(with: 3) 96 CountryDouble.add(builder: &builder, lng: log) 97 CountryDouble.add(builder: &builder, lan: lan) 98 CountryDouble.add(builder: &builder, name: offset) 99 return CountryDouble.end(builder: &builder, startOffset: _start) 100 } 101 102 static func end( 103 builder: inout FlatBufferBuilder, 104 startOffset: UOffset) -> Offset 105 { 106 Offset(offset: builder.endTable(at: startOffset)) 107 } 108 addnull109 static func add(builder: inout FlatBufferBuilder, name: String) { 110 add(builder: &builder, name: builder.create(string: name)) 111 } 112 addnull113 static func add(builder: inout FlatBufferBuilder, name: Offset) { 114 builder.add(offset: name, at: Country.offsets.name) 115 } 116 addnull117 static func add(builder: inout FlatBufferBuilder, lan: Double) { 118 builder.add(element: lan, def: 0, at: Country.offsets.lan) 119 } 120 addnull121 static func add(builder: inout FlatBufferBuilder, lng: Double) { 122 builder.add(element: lng, def: 0, at: Country.offsets.lng) 123 } 124 } 125