Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 1 | /* |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 2 | * Copyright 2021 Google Inc. All rights reserved. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 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 Foundation |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 18 | import XCTest |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 19 | @testable import FlatBuffers |
| 20 | |
| 21 | typealias Test = MyGame_Example_Test |
| 22 | typealias Monster = MyGame_Example_Monster |
| 23 | typealias Vec3 = MyGame_Example_Vec3 |
| 24 | typealias Stat = MyGame_Example_Stat |
| 25 | |
| 26 | class FlatBuffersMonsterWriterTests: XCTestCase { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 27 | |
| 28 | func testData() { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 29 | // swiftformat:disable all |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 30 | let data = Data([48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0]) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 31 | // swiftformat:enable all |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 32 | let _data = ByteBuffer(data: data) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 33 | readVerifiedMonster(fb: _data) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func testReadFromOtherLanguages() { |
| 37 | let path = FileManager.default.currentDirectoryPath |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 38 | let url = URL(fileURLWithPath: path, isDirectory: true) |
| 39 | .appendingPathComponent("monsterdata_test").appendingPathExtension("mon") |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 40 | guard let data = try? Data(contentsOf: url) else { return } |
| 41 | let _data = ByteBuffer(data: data) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 42 | readVerifiedMonster(fb: _data) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func testCreateMonster() { |
| 46 | let bytes = createMonster(withPrefix: false) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 47 | // swiftformat:disable all |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 48 | XCTAssertEqual(bytes.sizedByteArray, [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0]) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 49 | // swiftformat:enable all |
| 50 | let monster = MyGame_Example_Monster.getRootAsMonster(bb: bytes.buffer) |
| 51 | readMonster(monster: monster) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 52 | mutateMonster(fb: bytes.buffer) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 53 | readMonster(monster: monster) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | func testCreateMonsterResizedBuffer() { |
| 57 | let bytes = createMonster(withPrefix: false) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 58 | // swiftformat:disable all |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 59 | XCTAssertEqual(bytes.sizedByteArray, [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0]) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 60 | // swiftformat:enable all |
| 61 | readVerifiedMonster(fb: bytes.sizedBuffer) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func testCreateMonsterPrefixed() { |
| 65 | let bytes = createMonster(withPrefix: true) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 66 | // swiftformat:disable all |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 67 | XCTAssertEqual(bytes.sizedByteArray, [44, 1, 0, 0, 44, 0, 0, 0, 77, 79, 78, 83, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0]) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 68 | // swiftformat:enable all |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 69 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 70 | var buffer = bytes.buffer |
| 71 | readMonster(monster: getPrefixedSizeRoot(byteBuffer: &buffer)) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func testCreateMonsterUsingCreateMonsterMethodWithNilPos() { |
| 75 | var fbb = FlatBufferBuilder(initialSize: 1) |
| 76 | let name = fbb.create(string: "Frodo") |
| 77 | let mStart = Monster.startMonster(&fbb) |
| 78 | Monster.add(name: name, &fbb) |
| 79 | let root = Monster.endMonster(&fbb, start: mStart) |
| 80 | fbb.finish(offset: root) |
| 81 | let newMonster = Monster.getRootAsMonster(bb: fbb.sizedBuffer) |
| 82 | XCTAssertNil(newMonster.pos) |
| 83 | XCTAssertEqual(newMonster.name, "Frodo") |
| 84 | } |
| 85 | |
| 86 | func testCreateMonsterUsingCreateMonsterMethodWithPosX() { |
| 87 | var fbb = FlatBufferBuilder(initialSize: 1) |
| 88 | let name = fbb.create(string: "Barney") |
| 89 | let mStart = Monster.startMonster(&fbb) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 90 | Monster.add( |
| 91 | pos: MyGame_Example_Vec3( |
| 92 | x: 10, |
| 93 | y: 0, |
| 94 | z: 0, |
| 95 | test1: 0, |
| 96 | test2: .blue, |
| 97 | test3: .init()), |
| 98 | &fbb) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 99 | Monster.add(name: name, &fbb) |
| 100 | let root = Monster.endMonster(&fbb, start: mStart) |
| 101 | fbb.finish(offset: root) |
| 102 | |
| 103 | let newMonster = Monster.getRootAsMonster(bb: fbb.sizedBuffer) |
| 104 | XCTAssertEqual(newMonster.pos!.x, 10) |
| 105 | XCTAssertEqual(newMonster.name, "Barney") |
| 106 | } |
| 107 | |
| 108 | func testReadMonsterFromUnsafePointerWithoutCopying() { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 109 | // swiftformat:disable all |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 110 | var array: [UInt8] = [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0] |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 111 | // swiftformat:enable all |
| 112 | let unpacked = array |
| 113 | .withUnsafeMutableBytes { (memory) -> MyGame_Example_MonsterT in |
| 114 | let bytes = ByteBuffer( |
| 115 | assumingMemoryBound: memory.baseAddress!, |
| 116 | capacity: memory.count) |
| 117 | var monster = Monster.getRootAsMonster(bb: bytes) |
| 118 | readFlatbufferMonster(monster: &monster) |
| 119 | let unpacked = monster.unpack() |
| 120 | return unpacked |
| 121 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 122 | readObjectApi(monster: unpacked) |
| 123 | } |
| 124 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 125 | func testArrayOfBools() { |
| 126 | let boolArray = [false, true, false, true, false, true, false] |
| 127 | var fbb = FlatBufferBuilder(initialSize: 1) |
| 128 | let name = fbb.create(string: "Frodo") |
| 129 | let bools = fbb.createVector(boolArray) |
| 130 | let root = Monster.createMonster( |
| 131 | &fbb, |
| 132 | nameOffset: name, |
| 133 | testarrayofboolsVectorOffset: bools) |
| 134 | fbb.finish(offset: root) |
| 135 | let monster = Monster.getRootAsMonster(bb: fbb.sizedBuffer) |
| 136 | |
| 137 | let values = monster.testarrayofbools |
| 138 | |
| 139 | XCTAssertEqual(boolArray, values) |
| 140 | |
| 141 | for i in 0..<monster.testarrayofboolsCount { |
| 142 | XCTAssertEqual(boolArray[Int(i)], monster.testarrayofbools(at: i)) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func readVerifiedMonster(fb: ByteBuffer) { |
| 147 | var byteBuffer = fb |
| 148 | XCTAssertNoThrow( |
| 149 | try readMonster( |
| 150 | monster: getCheckedRoot( |
| 151 | byteBuffer: &byteBuffer) as MyGame_Example_Monster)) |
| 152 | } |
| 153 | |
| 154 | func readMonster(monster: Monster) { |
| 155 | var monster = monster |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 156 | readFlatbufferMonster(monster: &monster) |
| 157 | let unpacked: MyGame_Example_MonsterT? = monster.unpack() |
| 158 | readObjectApi(monster: unpacked!) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 159 | guard let buffer = unpacked?.serialize() |
| 160 | else { fatalError("Couldnt generate bytebuffer") } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 161 | var newMonster = Monster.getRootAsMonster(bb: buffer) |
| 162 | readFlatbufferMonster(monster: &newMonster) |
| 163 | } |
| 164 | |
| 165 | func createMonster(withPrefix prefix: Bool) -> FlatBufferBuilder { |
| 166 | var fbb = FlatBufferBuilder(initialSize: 1) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 167 | let names = [ |
| 168 | fbb.create(string: "Frodo"), |
| 169 | fbb.create(string: "Barney"), |
| 170 | fbb.create(string: "Wilma"), |
| 171 | ] |
| 172 | var offsets: [Offset] = [] |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 173 | let start1 = Monster.startMonster(&fbb) |
| 174 | Monster.add(name: names[0], &fbb) |
| 175 | offsets.append(Monster.endMonster(&fbb, start: start1)) |
| 176 | let start2 = Monster.startMonster(&fbb) |
| 177 | Monster.add(name: names[1], &fbb) |
| 178 | offsets.append(Monster.endMonster(&fbb, start: start2)) |
| 179 | let start3 = Monster.startMonster(&fbb) |
| 180 | Monster.add(name: names[2], &fbb) |
| 181 | offsets.append(Monster.endMonster(&fbb, start: start3)) |
| 182 | |
| 183 | let sortedArray = Monster.sortVectorOfMonster(offsets: offsets, &fbb) |
| 184 | |
| 185 | let str = fbb.create(string: "MyMonster") |
| 186 | let test1 = fbb.create(string: "test1") |
| 187 | let test2 = fbb.create(string: "test2") |
| 188 | let _inv: [Byte] = [0, 1, 2, 3, 4] |
| 189 | let inv = fbb.createVector(_inv) |
| 190 | |
| 191 | let fred = fbb.create(string: "Fred") |
| 192 | let mon1Start = Monster.startMonster(&fbb) |
| 193 | Monster.add(name: fred, &fbb) |
| 194 | let mon2 = Monster.endMonster(&fbb, start: mon1Start) |
| 195 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 196 | let test4 = fbb.createVector(ofStructs: [ |
| 197 | MyGame_Example_Test(a: 30, b: 40), |
| 198 | MyGame_Example_Test(a: 10, b: 20), |
| 199 | ]) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 200 | |
| 201 | let stringTestVector = fbb.createVector(ofOffsets: [test1, test2]) |
| 202 | let mStart = Monster.startMonster(&fbb) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 203 | Monster.add( |
| 204 | pos: MyGame_Example_Vec3( |
| 205 | x: 1, |
| 206 | y: 2, |
| 207 | z: 3, |
| 208 | test1: 3, |
| 209 | test2: .green, |
| 210 | test3: .init(a: 5, b: 6)), |
| 211 | &fbb) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 212 | Monster.add(hp: 80, &fbb) |
| 213 | Monster.add(name: str, &fbb) |
| 214 | Monster.addVectorOf(inventory: inv, &fbb) |
| 215 | Monster.add(testType: .monster, &fbb) |
| 216 | Monster.add(test: mon2, &fbb) |
| 217 | Monster.addVectorOf(test4: test4, &fbb) |
| 218 | Monster.addVectorOf(testarrayofstring: stringTestVector, &fbb) |
| 219 | Monster.add(testbool: true, &fbb) |
| 220 | Monster.addVectorOf(testarrayoftables: sortedArray, &fbb) |
| 221 | let end = Monster.endMonster(&fbb, start: mStart) |
| 222 | Monster.finish(&fbb, end: end, prefix: prefix) |
| 223 | return fbb |
| 224 | } |
| 225 | |
| 226 | func mutateMonster(fb: ByteBuffer) { |
| 227 | let monster = Monster.getRootAsMonster(bb: fb) |
| 228 | XCTAssertFalse(monster.mutate(mana: 10)) |
| 229 | XCTAssertEqual(monster.testarrayoftables(at: 0)?.name, "Barney") |
| 230 | XCTAssertEqual(monster.testarrayoftables(at: 1)?.name, "Frodo") |
| 231 | XCTAssertEqual(monster.testarrayoftables(at: 2)?.name, "Wilma") |
| 232 | |
| 233 | // Example of searching for a table by the key |
| 234 | XCTAssertNotNil(monster.testarrayoftablesBy(key: "Frodo")) |
| 235 | XCTAssertNotNil(monster.testarrayoftablesBy(key: "Barney")) |
| 236 | XCTAssertNotNil(monster.testarrayoftablesBy(key: "Wilma")) |
| 237 | |
| 238 | XCTAssertEqual(monster.testType, .monster) |
| 239 | |
| 240 | XCTAssertEqual(monster.mutate(inventory: 1, at: 0), true) |
| 241 | XCTAssertEqual(monster.mutate(inventory: 2, at: 1), true) |
| 242 | XCTAssertEqual(monster.mutate(inventory: 3, at: 2), true) |
| 243 | XCTAssertEqual(monster.mutate(inventory: 4, at: 3), true) |
| 244 | XCTAssertEqual(monster.mutate(inventory: 5, at: 4), true) |
| 245 | |
| 246 | for i in 0..<monster.inventoryCount { |
| 247 | XCTAssertEqual(monster.inventory(at: i), Byte(i + 1)) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 250 | XCTAssertEqual(monster.mutate(inventory: 0, at: 0), true) |
| 251 | XCTAssertEqual(monster.mutate(inventory: 1, at: 1), true) |
| 252 | XCTAssertEqual(monster.mutate(inventory: 2, at: 2), true) |
| 253 | XCTAssertEqual(monster.mutate(inventory: 3, at: 3), true) |
| 254 | XCTAssertEqual(monster.mutate(inventory: 4, at: 4), true) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 255 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 256 | let vec = monster.mutablePos |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 257 | XCTAssertEqual(vec?.x, 1) |
| 258 | XCTAssertTrue(vec?.mutate(x: 55.0) ?? false) |
| 259 | XCTAssertTrue(vec?.mutate(test1: 55) ?? false) |
| 260 | XCTAssertEqual(vec?.x, 55.0) |
| 261 | XCTAssertEqual(vec?.test1, 55.0) |
| 262 | XCTAssertTrue(vec?.mutate(x: 1) ?? false) |
| 263 | XCTAssertEqual(vec?.x, 1) |
| 264 | XCTAssertTrue(vec?.mutate(test1: 3) ?? false) |
| 265 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 266 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 267 | func readFlatbufferMonster(monster: inout MyGame_Example_Monster) { |
| 268 | XCTAssertEqual(monster.hp, 80) |
| 269 | XCTAssertEqual(monster.mana, 150) |
| 270 | XCTAssertEqual(monster.name, "MyMonster") |
| 271 | let pos = monster.pos |
| 272 | XCTAssertEqual(pos?.x, 1) |
| 273 | XCTAssertEqual(pos?.y, 2) |
| 274 | XCTAssertEqual(pos?.z, 3) |
| 275 | XCTAssertEqual(pos?.test1, 3) |
| 276 | XCTAssertEqual(pos?.test2, .green) |
| 277 | let test = pos?.test3 |
| 278 | XCTAssertEqual(test?.a, 5) |
| 279 | XCTAssertEqual(test?.b, 6) |
| 280 | XCTAssertEqual(monster.testType, .monster) |
| 281 | let monster2 = monster.test(type: Monster.self) |
| 282 | XCTAssertEqual(monster2?.name, "Fred") |
| 283 | |
| 284 | XCTAssertEqual(monster.mutate(mana: 10), false) |
| 285 | |
| 286 | XCTAssertEqual(monster.mana, 150) |
| 287 | XCTAssertEqual(monster.inventoryCount, 5) |
| 288 | var sum: Byte = 0 |
| 289 | for i in 0...monster.inventoryCount { |
| 290 | sum += monster.inventory(at: i) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 291 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 292 | XCTAssertEqual(sum, 10) |
| 293 | XCTAssertEqual(monster.test4Count, 2) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 294 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 295 | let test0 = monster.test4(at: 0) |
| 296 | let test1 = monster.test4(at: 1) |
| 297 | var sum0 = 0 |
| 298 | var sum1 = 0 |
| 299 | if let a = test0?.a, let b = test0?.b { |
| 300 | sum0 = Int(a) + Int(b) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 301 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 302 | if let a = test1?.a, let b = test1?.b { |
| 303 | sum1 = Int(a) + Int(b) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 304 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 305 | XCTAssertEqual(sum0 + sum1, 100) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 306 | |
| 307 | let mutableTest0 = monster.mutableTest4(at: 0) |
| 308 | let mutableTest1 = monster.mutableTest4(at: 1) |
| 309 | var sum2 = 0 |
| 310 | var sum3 = 0 |
| 311 | if let a = mutableTest0?.a, let b = mutableTest0?.b { |
| 312 | sum2 = Int(a) + Int(b) |
| 313 | } |
| 314 | if let a = mutableTest1?.a, let b = mutableTest1?.b { |
| 315 | sum3 = Int(a) + Int(b) |
| 316 | } |
| 317 | XCTAssertEqual(sum2 + sum3, 100) |
| 318 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 319 | XCTAssertEqual(monster.testarrayofstringCount, 2) |
| 320 | XCTAssertEqual(monster.testarrayofstring(at: 0), "test1") |
| 321 | XCTAssertEqual(monster.testarrayofstring(at: 1), "test2") |
| 322 | XCTAssertEqual(monster.testbool, true) |
| 323 | |
| 324 | let array = monster.nameSegmentArray |
| 325 | XCTAssertEqual(String(bytes: array ?? [], encoding: .utf8), "MyMonster") |
| 326 | |
| 327 | if 0 == monster.testarrayofboolsCount { |
| 328 | XCTAssertEqual(monster.testarrayofbools.isEmpty, true) |
| 329 | } else { |
| 330 | XCTAssertEqual(monster.testarrayofbools.isEmpty, false) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 331 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | func readObjectApi(monster: MyGame_Example_MonsterT) { |
| 335 | XCTAssertEqual(monster.hp, 80) |
| 336 | XCTAssertEqual(monster.mana, 150) |
| 337 | XCTAssertEqual(monster.name, "MyMonster") |
| 338 | let pos = monster.pos |
| 339 | XCTAssertEqual(pos?.x, 1) |
| 340 | XCTAssertEqual(pos?.y, 2) |
| 341 | XCTAssertEqual(pos?.z, 3) |
| 342 | XCTAssertEqual(pos?.test1, 3) |
| 343 | XCTAssertEqual(pos?.test2, .green) |
| 344 | let test = pos?.test3 |
| 345 | XCTAssertEqual(test?.a, 5) |
| 346 | XCTAssertEqual(test?.b, 6) |
| 347 | let monster2 = monster.test?.value as? MyGame_Example_MonsterT |
| 348 | XCTAssertEqual(monster2?.name, "Fred") |
| 349 | XCTAssertEqual(monster.mana, 150) |
| 350 | monster.mana = 10 |
| 351 | XCTAssertEqual(monster.mana, 10) |
| 352 | monster.mana = 150 |
| 353 | XCTAssertEqual(monster.mana, 150) |
| 354 | |
| 355 | XCTAssertEqual(monster.inventory.count, 5) |
| 356 | var sum: Byte = 0 |
| 357 | for i in monster.inventory { |
| 358 | sum += i |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 359 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 360 | XCTAssertEqual(sum, 10) |
| 361 | XCTAssertEqual(monster.test4.count, 2) |
| 362 | let test0 = monster.test4[0] |
| 363 | let test1 = monster.test4[1] |
| 364 | var sum0 = 0 |
| 365 | var sum1 = 0 |
| 366 | if let a = test0?.a, let b = test0?.b { |
| 367 | sum0 = Int(a) + Int(b) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 368 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 369 | if let a = test1?.a, let b = test1?.b { |
| 370 | sum1 = Int(a) + Int(b) |
| 371 | } |
| 372 | XCTAssertEqual(sum0 + sum1, 100) |
| 373 | XCTAssertEqual(monster.testbool, true) |
| 374 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 375 | |
| 376 | func testEncoding() { |
| 377 | let fbb = createMonster(withPrefix: false) |
| 378 | var sizedBuffer = fbb.sizedBuffer |
| 379 | do { |
| 380 | let reader: Monster = try getCheckedRoot(byteBuffer: &sizedBuffer) |
| 381 | let encoder = JSONEncoder() |
| 382 | encoder.keyEncodingStrategy = .convertToSnakeCase |
| 383 | let data = try encoder.encode(reader) |
| 384 | XCTAssertEqual(data, jsonData.data(using: .utf8)) |
| 385 | } catch { |
| 386 | XCTFail(error.localizedDescription) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | var jsonData: String { |
| 391 | """ |
| 392 | {\"hp\":80,\"inventory\":[0,1,2,3,4],\"test\":{\"name\":\"Fred\"},\"testarrayofstring\":[\"test1\",\"test2\"],\"testarrayoftables\":[{\"name\":\"Barney\"},{\"name\":\"Frodo\"},{\"name\":\"Wilma\"}],\"test4\":[{\"a\":30,\"b\":40},{\"a\":10,\"b\":20}],\"testbool\":true,\"test_type\":\"Monster\",\"pos\":{\"y\":2,\"test3\":{\"a\":5,\"b\":6},\"z\":3,\"x\":1,\"test1\":3,\"test2\":\"Green\"},\"name\":\"MyMonster\"} |
| 393 | """ |
| 394 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 395 | } |