Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020 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 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 17 | import XCTest |
| 18 | @testable import FlatBuffers |
| 19 | |
| 20 | final class FlatBuffersTests: XCTestCase { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame^] | 21 | |
| 22 | let country = "Norway" |
| 23 | |
| 24 | func testEndian() { XCTAssertEqual(isLitteEndian, true) } |
| 25 | |
| 26 | func testOffset() { |
| 27 | let o = Offset<Int>() |
| 28 | let b = Offset<Int>(offset: 1) |
| 29 | XCTAssertEqual(o.isEmpty, true) |
| 30 | XCTAssertEqual(b.isEmpty, false) |
| 31 | } |
| 32 | |
| 33 | 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 | |
| 45 | 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 | |
| 53 | func testCreateFinish() { |
| 54 | var b = FlatBufferBuilder(initialSize: 16) |
| 55 | let countryOff = Country.createCountry(builder: &b, name: country, log: 200, lan: 100) |
| 56 | b.finish(offset: countryOff) |
| 57 | 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] |
| 58 | XCTAssertEqual(b.sizedByteArray, v) |
| 59 | } |
| 60 | |
| 61 | func testCreateFinishWithPrefix() { |
| 62 | var b = FlatBufferBuilder(initialSize: 16) |
| 63 | let countryOff = Country.createCountry(builder: &b, name: country, log: 200, lan: 100) |
| 64 | b.finish(offset: countryOff, addPrefix: true) |
| 65 | 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] |
| 66 | XCTAssertEqual(b.sizedByteArray, v) |
| 67 | } |
| 68 | |
| 69 | func testReadCountry() { |
| 70 | 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] |
| 71 | let buffer = ByteBuffer(bytes: v) |
| 72 | let c = Country.getRootAsCountry(buffer) |
| 73 | XCTAssertEqual(c.lan, 100) |
| 74 | XCTAssertEqual(c.log, 200) |
| 75 | XCTAssertEqual(c.nameVector, [78, 111, 114, 119, 97, 121]) |
| 76 | XCTAssertEqual(c.name, country) |
| 77 | } |
| 78 | |
| 79 | func testWriteNullableStrings() { |
| 80 | var b = FlatBufferBuilder() |
| 81 | XCTAssertTrue(b.create(string: nil).isEmpty) |
| 82 | XCTAssertTrue(b.createShared(string: nil).isEmpty) |
| 83 | } |
| 84 | |
| 85 | func testWriteOptionalValues() { |
| 86 | var b = FlatBufferBuilder() |
| 87 | let root = optional_scalars_ScalarStuff.createScalarStuff( |
| 88 | &b, |
| 89 | justI8: 80, |
| 90 | maybeI8: nil, |
| 91 | justU8: 100, |
| 92 | maybeU8: 10, |
| 93 | maybeBool: true, |
| 94 | justEnum: .one, |
| 95 | maybeEnum: nil) |
| 96 | b.finish(offset: root) |
| 97 | let scalarTable = optional_scalars_ScalarStuff.getRootAsScalarStuff(bb: b.sizedBuffer) |
| 98 | XCTAssertEqual(scalarTable.justI8, 80) |
| 99 | XCTAssertNil(scalarTable.maybeI8) |
| 100 | XCTAssertEqual(scalarTable.maybeBool, true) |
| 101 | XCTAssertEqual(scalarTable.defaultI8, 42) |
| 102 | XCTAssertEqual(scalarTable.justU8, 100) |
| 103 | XCTAssertEqual(scalarTable.maybeU8, 10) |
| 104 | XCTAssertEqual(scalarTable.justEnum, .one) |
| 105 | XCTAssertNil(scalarTable.maybeEnum) |
| 106 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | class Country { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame^] | 110 | |
| 111 | static let offsets: (name: VOffset, lan: VOffset, lng: VOffset) = (4, 6, 8) |
| 112 | private var __t: Table |
| 113 | |
| 114 | private init(_ t: Table) { |
| 115 | __t = t |
| 116 | } |
| 117 | |
| 118 | var lan: Int32 { let o = __t.offset(6); return o == 0 ? 0 : __t.readBuffer(of: Int32.self, at: o) } |
| 119 | var log: Int32 { let o = __t.offset(8); return o == 0 ? 0 : __t.readBuffer(of: Int32.self, at: o) } |
| 120 | var nameVector: [UInt8]? { __t.getVector(at: 4) } |
| 121 | var name: String? { let o = __t.offset(4); return o == 0 ? nil : __t.string(at: o) } |
| 122 | |
| 123 | @inlinable |
| 124 | static func getRootAsCountry(_ bb: ByteBuffer) -> Country { |
| 125 | Country(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0)))) |
| 126 | } |
| 127 | |
| 128 | @inlinable |
| 129 | static func createCountry( |
| 130 | builder: inout FlatBufferBuilder, |
| 131 | name: String, |
| 132 | log: Int32, |
| 133 | lan: Int32) -> Offset<Country> |
| 134 | { |
| 135 | createCountry(builder: &builder, offset: builder.create(string: name), log: log, lan: lan) |
| 136 | } |
| 137 | |
| 138 | @inlinable |
| 139 | static func createCountry( |
| 140 | builder: inout FlatBufferBuilder, |
| 141 | offset: Offset<String>, |
| 142 | log: Int32, |
| 143 | lan: Int32) -> Offset<Country> |
| 144 | { |
| 145 | let _start = builder.startTable(with: 3) |
| 146 | Country.add(builder: &builder, lng: log) |
| 147 | Country.add(builder: &builder, lan: lan) |
| 148 | Country.add(builder: &builder, name: offset) |
| 149 | return Country.end(builder: &builder, startOffset: _start) |
| 150 | } |
| 151 | |
| 152 | @inlinable |
| 153 | static func end(builder: inout FlatBufferBuilder, startOffset: UOffset) -> Offset<Country> { |
| 154 | Offset(offset: builder.endTable(at: startOffset)) |
| 155 | } |
| 156 | |
| 157 | @inlinable |
| 158 | static func add(builder: inout FlatBufferBuilder, name: String) { |
| 159 | add(builder: &builder, name: builder.create(string: name)) |
| 160 | } |
| 161 | |
| 162 | @inlinable |
| 163 | static func add(builder: inout FlatBufferBuilder, name: Offset<String>) { |
| 164 | builder.add(offset: name, at: Country.offsets.name) |
| 165 | } |
| 166 | |
| 167 | @inlinable |
| 168 | static func add(builder: inout FlatBufferBuilder, lan: Int32) { |
| 169 | builder.add(element: lan, def: 0, at: Country.offsets.lan) |
| 170 | } |
| 171 | |
| 172 | @inlinable |
| 173 | static func add(builder: inout FlatBufferBuilder, lng: Int32) { |
| 174 | builder.add(element: lng, def: 0, at: Country.offsets.lng) |
| 175 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 176 | } |