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 FlatBuffersVectors: XCTestCase { 21 testCreatingTwoCountriesnull22 func testCreatingTwoCountries() { 23 let norway = "Norway" 24 let denmark = "Denmark" 25 var b = FlatBufferBuilder(initialSize: 20) 26 let noStr = b.create(string: norway) 27 let deStr = b.create(string: denmark) 28 let n = Country.createCountry(builder: &b, offset: noStr, log: 888, lan: 700) 29 let d = Country.createCountry(builder: &b, offset: deStr, log: 200, lan: 100) 30 let vector = [n, d] 31 let vectorOffset = b.createVector(ofOffsets: vector) 32 b.finish(offset: vectorOffset) 33 XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 2, 0, 0, 0, 48, 0, 0, 0, 16, 0, 0, 0, 0, 0, 10, 0, 18, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 40, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 24, 0, 0, 0, 188, 2, 0, 0, 120, 3, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]) 34 } 35 testCreateIntArraynull36 func testCreateIntArray() { 37 let numbers: [Int32] = [1, 2, 3, 4, 5] 38 var b = FlatBufferBuilder(initialSize: 20) 39 let o = b.createVector(numbers, size: numbers.count) 40 b.finish(offset: o) 41 XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0]) 42 } 43 testCreateEmptyIntArraynull44 func testCreateEmptyIntArray() { 45 let numbers: [Int32] = [] 46 var b = FlatBufferBuilder(initialSize: 20) 47 let o = b.createVector(numbers, size: numbers.count) 48 b.finish(offset: o) 49 XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 0, 0, 0, 0]) 50 } 51 testCreateVectorOfStringsnull52 func testCreateVectorOfStrings() { 53 let strs = ["Denmark", "Norway"] 54 var b = FlatBufferBuilder(initialSize: 20) 55 let o = b.createVector(ofStrings: strs) 56 b.finish(offset: o) 57 XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 2, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0]) 58 } testCreateSharedStringVectornull59 func testCreateSharedStringVector() { 60 let norway = "Norway" 61 let denmark = "Denmark" 62 var b = FlatBufferBuilder(initialSize: 20) 63 let noStr = b.createShared(string: norway) 64 let deStr = b.createShared(string: denmark) 65 let _noStr = b.createShared(string: norway) 66 let _deStr = b.createShared(string: denmark) 67 let v = [noStr, deStr, _noStr, _deStr] 68 let end = b.createVector(ofOffsets: v) 69 b.finish(offset: end) 70 XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 4, 0, 0, 0, 28, 0, 0, 0, 12, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]) 71 } 72 testReadInt32Arraynull73 func testReadInt32Array() { 74 let data: [Int32] = [1, 2, 3, 4, 5] 75 var b = FlatBufferBuilder(initialSize: 20) 76 let v = Numbers.createNumbersVector(b: &b, array: data) 77 let end = Numbers.createNumbers(b: &b, o: v) 78 b.finish(offset: end) 79 let number = Numbers.getRootAsNumbers(ByteBuffer(bytes: b.sizedByteArray)) 80 XCTAssertEqual(number.vArrayInt32, [1, 2, 3, 4, 5]) 81 } 82 testReadDoubleArraynull83 func testReadDoubleArray() { 84 let data: [Double] = [1, 2, 3, 4, 5] 85 var b = FlatBufferBuilder(initialSize: 20) 86 let v = Numbers.createNumbersVector(b: &b, array: data) 87 let end = Numbers.createNumbers(b: &b, o: v) 88 b.finish(offset: end) 89 let number = Numbers.getRootAsNumbers(ByteBuffer(bytes: b.sizedByteArray)) 90 XCTAssertEqual(number.vArrayDouble, [1, 2, 3, 4, 5]) 91 } 92 } 93 94 struct Numbers { 95 96 private var __t: Table 97 98 private init(_ t: Table) { 99 __t = t 100 } 101 102 @inlinable getRootAsNumbersnull103 static func getRootAsNumbers(_ bb: ByteBuffer) -> Numbers { 104 Numbers(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0)))) 105 } 106 107 var vArrayInt: [Int]? { __t.getVector(at: 4) } 108 var vArrayInt32: [Int32]? { __t.getVector(at: 4) } 109 var vArrayDouble: [Double]? { __t.getVector(at: 4) } 110 var vArrayFloat: [Float32]? { __t.getVector(at: 4) } 111 createNumbersVectornull112 static func createNumbersVector(b: inout FlatBufferBuilder, array: [Int]) -> Offset { 113 b.createVector(array, size: array.count) 114 } 115 createNumbersVectornull116 static func createNumbersVector(b: inout FlatBufferBuilder, array: [Int32]) -> Offset { 117 b.createVector(array, size: array.count) 118 } 119 createNumbersVectornull120 static func createNumbersVector(b: inout FlatBufferBuilder, array: [Double]) -> Offset { 121 b.createVector(array, size: array.count) 122 } 123 createNumbersVectornull124 static func createNumbersVector(b: inout FlatBufferBuilder, array: [Float32]) -> Offset { 125 b.createVector(array, size: array.count) 126 } 127 createNumbersnull128 static func createNumbers(b: inout FlatBufferBuilder, o: Offset) -> Offset { 129 let start = b.startTable(with: 1) 130 b.add(offset: o, at: 4) 131 return Offset(offset: b.endTable(at: start)) 132 } 133 } 134