blob: 4d6d724fe3fbaf8df70c80fad22aebe19b8f975d [file] [log] [blame]
Austin Schuh58b9b472020-11-25 19:12:44 -08001/*
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 Schuh272c6132020-11-14 16:37:52 -080017import XCTest
18@testable import FlatBuffers
19
20final class FlatBuffersDoubleTests: XCTestCase {
Austin Schuh58b9b472020-11-25 19:12:44 -080021
22 let country = "Norway"
23
24 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
32 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 }
Austin Schuh272c6132020-11-14 16:37:52 -080039}
40
41class CountryDouble {
Austin Schuh58b9b472020-11-25 19:12:44 -080042
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
49 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<Country>
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<String>,
66 log: Double,
67 lan: Double) -> Offset<Country>
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
76 static func end(builder: inout FlatBufferBuilder, startOffset: UOffset) -> Offset<Country> {
77 Offset(offset: builder.endTable(at: startOffset))
78 }
79
80 static func add(builder: inout FlatBufferBuilder, name: String) {
81 add(builder: &builder, name: builder.create(string: name))
82 }
83
84 static func add(builder: inout FlatBufferBuilder, name: Offset<String>) {
85 builder.add(offset: name, at: Country.offsets.name)
86 }
87
88 static func add(builder: inout FlatBufferBuilder, lan: Double) {
89 builder.add(element: lan, def: 0, at: Country.offsets.lan)
90 }
91
92 static func add(builder: inout FlatBufferBuilder, lng: Double) {
93 builder.add(element: lng, def: 0, at: Country.offsets.lng)
94 }
Austin Schuh272c6132020-11-14 16:37:52 -080095}