blob: 379e7331833c71f714022b928fda79403be06d31 [file] [log] [blame]
Austin Schuh58b9b472020-11-25 19:12:44 -08001/*
James Kuszmaul8e62b022022-03-22 09:33:25 -07002 * Copyright 2021 Google Inc. All rights reserved.
Austin Schuh58b9b472020-11-25 19:12:44 -08003 *
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 Schuh272c6132020-11-14 16:37:52 -080017import XCTest
18@testable import FlatBuffers
19
20final class FlatBuffersTests: XCTestCase {
Austin Schuh58b9b472020-11-25 19:12:44 -080021
22 let country = "Norway"
23
24 func testEndian() { XCTAssertEqual(isLitteEndian, true) }
25
26 func testOffset() {
James Kuszmaul8e62b022022-03-22 09:33:25 -070027 let o = Offset()
28 let b = Offset(offset: 1)
Austin Schuh58b9b472020-11-25 19:12:44 -080029 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)
James Kuszmaul8e62b022022-03-22 09:33:25 -070055 let countryOff = Country.createCountry(
56 builder: &b,
57 name: country,
58 log: 200,
59 lan: 100)
Austin Schuh58b9b472020-11-25 19:12:44 -080060 b.finish(offset: countryOff)
James Kuszmaul8e62b022022-03-22 09:33:25 -070061 // swiftformat:disable all
Austin Schuh58b9b472020-11-25 19:12:44 -080062 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]
James Kuszmaul8e62b022022-03-22 09:33:25 -070063 // swiftformat:enable all
Austin Schuh58b9b472020-11-25 19:12:44 -080064 XCTAssertEqual(b.sizedByteArray, v)
65 }
66
67 func testCreateFinishWithPrefix() {
68 var b = FlatBufferBuilder(initialSize: 16)
James Kuszmaul8e62b022022-03-22 09:33:25 -070069 let countryOff = Country.createCountry(
70 builder: &b,
71 name: country,
72 log: 200,
73 lan: 100)
Austin Schuh58b9b472020-11-25 19:12:44 -080074 b.finish(offset: countryOff, addPrefix: true)
James Kuszmaul8e62b022022-03-22 09:33:25 -070075 // swiftformat:disable all
Austin Schuh58b9b472020-11-25 19:12:44 -080076 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]
James Kuszmaul8e62b022022-03-22 09:33:25 -070077 // swiftformat:enable all
Austin Schuh58b9b472020-11-25 19:12:44 -080078 XCTAssertEqual(b.sizedByteArray, v)
79 }
80
81 func testReadCountry() {
James Kuszmaul8e62b022022-03-22 09:33:25 -070082 // swiftformat:disable all
Austin Schuh58b9b472020-11-25 19:12:44 -080083 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]
James Kuszmaul8e62b022022-03-22 09:33:25 -070084 // swiftformat:enable all
Austin Schuh58b9b472020-11-25 19:12:44 -080085 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
93 func testWriteNullableStrings() {
94 var b = FlatBufferBuilder()
95 XCTAssertTrue(b.create(string: nil).isEmpty)
96 XCTAssertTrue(b.createShared(string: nil).isEmpty)
97 }
98
99 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)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700111 let scalarTable = optional_scalars_ScalarStuff
112 .getRootAsScalarStuff(bb: b.sizedBuffer)
Austin Schuh58b9b472020-11-25 19:12:44 -0800113 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 }
Austin Schuh272c6132020-11-14 16:37:52 -0800122}
123
124class Country {
Austin Schuh58b9b472020-11-25 19:12:44 -0800125
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
James Kuszmaul8e62b022022-03-22 09:33:25 -0700133 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) }
Austin Schuh58b9b472020-11-25 19:12:44 -0800139 var nameVector: [UInt8]? { __t.getVector(at: 4) }
James Kuszmaul8e62b022022-03-22 09:33:25 -0700140 var name: String? {
141 let o = __t.offset(4); return o == 0 ? nil : __t.string(at: o) }
Austin Schuh58b9b472020-11-25 19:12:44 -0800142
143 @inlinable
144 static func getRootAsCountry(_ bb: ByteBuffer) -> Country {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700145 Country(Table(
146 bb: bb,
147 position: Int32(bb.read(def: UOffset.self, position: 0))))
Austin Schuh58b9b472020-11-25 19:12:44 -0800148 }
149
150 @inlinable
151 static func createCountry(
152 builder: inout FlatBufferBuilder,
153 name: String,
154 log: Int32,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700155 lan: Int32) -> Offset
Austin Schuh58b9b472020-11-25 19:12:44 -0800156 {
James Kuszmaul8e62b022022-03-22 09:33:25 -0700157 createCountry(
158 builder: &builder,
159 offset: builder.create(string: name),
160 log: log,
161 lan: lan)
Austin Schuh58b9b472020-11-25 19:12:44 -0800162 }
163
164 @inlinable
165 static func createCountry(
166 builder: inout FlatBufferBuilder,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700167 offset: Offset,
Austin Schuh58b9b472020-11-25 19:12:44 -0800168 log: Int32,
James Kuszmaul8e62b022022-03-22 09:33:25 -0700169 lan: Int32) -> Offset
Austin Schuh58b9b472020-11-25 19:12:44 -0800170 {
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
James Kuszmaul8e62b022022-03-22 09:33:25 -0700179 static func end(
180 builder: inout FlatBufferBuilder,
181 startOffset: UOffset) -> Offset
182 {
Austin Schuh58b9b472020-11-25 19:12:44 -0800183 Offset(offset: builder.endTable(at: startOffset))
184 }
185
186 @inlinable
187 static func add(builder: inout FlatBufferBuilder, name: String) {
188 add(builder: &builder, name: builder.create(string: name))
189 }
190
191 @inlinable
James Kuszmaul8e62b022022-03-22 09:33:25 -0700192 static func add(builder: inout FlatBufferBuilder, name: Offset) {
Austin Schuh58b9b472020-11-25 19:12:44 -0800193 builder.add(offset: name, at: Country.offsets.name)
194 }
195
196 @inlinable
197 static func add(builder: inout FlatBufferBuilder, lan: Int32) {
198 builder.add(element: lan, def: 0, at: Country.offsets.lan)
199 }
200
201 @inlinable
202 static func add(builder: inout FlatBufferBuilder, lng: Int32) {
203 builder.add(element: lng, def: 0, at: Country.offsets.lng)
204 }
Austin Schuh272c6132020-11-14 16:37:52 -0800205}