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