1 /* 2 * Copyright 2021 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 17 import Foundation 18 import XCTest 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 { 27 testDatanull28 func testData() { 29 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]) 30 let _data = ByteBuffer(data: data) 31 readMonster(fb: _data) 32 } 33 testReadFromOtherLanguagesnull34 func testReadFromOtherLanguages() { 35 let path = FileManager.default.currentDirectoryPath 36 let url = URL(fileURLWithPath: path, isDirectory: true).appendingPathComponent("monsterdata_test").appendingPathExtension("mon") 37 guard let data = try? Data(contentsOf: url) else { return } 38 let _data = ByteBuffer(data: data) 39 readMonster(fb: _data) 40 } 41 testCreateMonsternull42 func testCreateMonster() { 43 let bytes = createMonster(withPrefix: false) 44 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]) 45 readMonster(fb: bytes.buffer) 46 mutateMonster(fb: bytes.buffer) 47 readMonster(fb: bytes.buffer) 48 } 49 testCreateMonsterResizedBuffernull50 func testCreateMonsterResizedBuffer() { 51 let bytes = createMonster(withPrefix: false) 52 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]) 53 readMonster(fb: bytes.sizedBuffer) 54 } 55 testCreateMonsterPrefixednull56 func testCreateMonsterPrefixed() { 57 let bytes = createMonster(withPrefix: true) 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]) 59 60 let newBuf = FlatBuffersUtils.removeSizePrefix(bb: bytes.buffer) 61 readMonster(fb: newBuf) 62 } 63 testCreateMonsterUsingCreateMonsterMethodWithNilPosnull64 func testCreateMonsterUsingCreateMonsterMethodWithNilPos() { 65 var fbb = FlatBufferBuilder(initialSize: 1) 66 let name = fbb.create(string: "Frodo") 67 let mStart = Monster.startMonster(&fbb) 68 Monster.add(name: name, &fbb) 69 let root = Monster.endMonster(&fbb, start: mStart) 70 fbb.finish(offset: root) 71 let newMonster = Monster.getRootAsMonster(bb: fbb.sizedBuffer) 72 XCTAssertNil(newMonster.pos) 73 XCTAssertEqual(newMonster.name, "Frodo") 74 } 75 testCreateMonsterUsingCreateMonsterMethodWithPosXnull76 func testCreateMonsterUsingCreateMonsterMethodWithPosX() { 77 var fbb = FlatBufferBuilder(initialSize: 1) 78 let name = fbb.create(string: "Barney") 79 let mStart = Monster.startMonster(&fbb) 80 Monster.add(pos: MyGame_Example_Vec3(x: 10, y: 0, z: 0, test1: 0, test2: .blue, test3: .init()), &fbb) 81 Monster.add(name: name, &fbb) 82 let root = Monster.endMonster(&fbb, start: mStart) 83 fbb.finish(offset: root) 84 85 let newMonster = Monster.getRootAsMonster(bb: fbb.sizedBuffer) 86 XCTAssertEqual(newMonster.pos!.x, 10) 87 XCTAssertEqual(newMonster.name, "Barney") 88 } 89 testReadMonsterFromUnsafePointerWithoutCopyingnull90 func testReadMonsterFromUnsafePointerWithoutCopying() { 91 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] 92 let unpacked = array.withUnsafeMutableBytes { (memory) -> MyGame_Example_MonsterT in 93 let bytes = ByteBuffer(assumingMemoryBound: memory.baseAddress!, capacity: memory.count) 94 var monster = Monster.getRootAsMonster(bb: bytes) 95 readFlatbufferMonster(monster: &monster) 96 let unpacked = monster.unpack() 97 return unpacked 98 } 99 readObjectApi(monster: unpacked) 100 } 101 testArrayOfBoolsnull102 func testArrayOfBools() { 103 let boolArray = [false, true, false, true, false, true, false] 104 var fbb = FlatBufferBuilder(initialSize: 1) 105 let name = fbb.create(string: "Frodo") 106 let bools = fbb.createVector(boolArray) 107 let root = Monster.createMonster(&fbb, nameOffset: name, 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 readMonsternull120 func readMonster(fb: ByteBuffer) { 121 var monster = Monster.getRootAsMonster(bb: fb) 122 readFlatbufferMonster(monster: &monster) 123 let unpacked: MyGame_Example_MonsterT? = monster.unpack() 124 readObjectApi(monster: unpacked!) 125 guard let buffer = unpacked?.serialize() else { fatalError("Couldnt generate bytebuffer") } 126 var newMonster = Monster.getRootAsMonster(bb: buffer) 127 readFlatbufferMonster(monster: &newMonster) 128 } 129 createMonsternull130 func createMonster(withPrefix prefix: Bool) -> FlatBufferBuilder { 131 var fbb = FlatBufferBuilder(initialSize: 1) 132 let names = [fbb.create(string: "Frodo"), fbb.create(string: "Barney"), fbb.create(string: "Wilma")] 133 var offsets: [Offset] = [] 134 let start1 = Monster.startMonster(&fbb) 135 Monster.add(name: names[0], &fbb) 136 offsets.append(Monster.endMonster(&fbb, start: start1)) 137 let start2 = Monster.startMonster(&fbb) 138 Monster.add(name: names[1], &fbb) 139 offsets.append(Monster.endMonster(&fbb, start: start2)) 140 let start3 = Monster.startMonster(&fbb) 141 Monster.add(name: names[2], &fbb) 142 offsets.append(Monster.endMonster(&fbb, start: start3)) 143 144 let sortedArray = Monster.sortVectorOfMonster(offsets: offsets, &fbb) 145 146 let str = fbb.create(string: "MyMonster") 147 let test1 = fbb.create(string: "test1") 148 let test2 = fbb.create(string: "test2") 149 let _inv: [Byte] = [0, 1, 2, 3, 4] 150 let inv = fbb.createVector(_inv) 151 152 let fred = fbb.create(string: "Fred") 153 let mon1Start = Monster.startMonster(&fbb) 154 Monster.add(name: fred, &fbb) 155 let mon2 = Monster.endMonster(&fbb, start: mon1Start) 156 157 let test4 = fbb.createVector(ofStructs: [ 158 MyGame_Example_Test(a: 30, b: 40), 159 MyGame_Example_Test(a: 10, b: 20), 160 ]) 161 162 let stringTestVector = fbb.createVector(ofOffsets: [test1, test2]) 163 let mStart = Monster.startMonster(&fbb) 164 Monster.add(pos: MyGame_Example_Vec3(x: 1, y: 2, z: 3, test1: 3, test2: .green, test3: .init(a: 5, b: 6)), &fbb) 165 Monster.add(hp: 80, &fbb) 166 Monster.add(name: str, &fbb) 167 Monster.addVectorOf(inventory: inv, &fbb) 168 Monster.add(testType: .monster, &fbb) 169 Monster.add(test: mon2, &fbb) 170 Monster.addVectorOf(test4: test4, &fbb) 171 Monster.addVectorOf(testarrayofstring: stringTestVector, &fbb) 172 Monster.add(testbool: true, &fbb) 173 Monster.addVectorOf(testarrayoftables: sortedArray, &fbb) 174 let end = Monster.endMonster(&fbb, start: mStart) 175 Monster.finish(&fbb, end: end, prefix: prefix) 176 return fbb 177 } 178 mutateMonsternull179 func mutateMonster(fb: ByteBuffer) { 180 let monster = Monster.getRootAsMonster(bb: fb) 181 XCTAssertFalse(monster.mutate(mana: 10)) 182 XCTAssertEqual(monster.testarrayoftables(at: 0)?.name, "Barney") 183 XCTAssertEqual(monster.testarrayoftables(at: 1)?.name, "Frodo") 184 XCTAssertEqual(monster.testarrayoftables(at: 2)?.name, "Wilma") 185 186 // Example of searching for a table by the key 187 XCTAssertNotNil(monster.testarrayoftablesBy(key: "Frodo")) 188 XCTAssertNotNil(monster.testarrayoftablesBy(key: "Barney")) 189 XCTAssertNotNil(monster.testarrayoftablesBy(key: "Wilma")) 190 191 XCTAssertEqual(monster.testType, .monster) 192 193 XCTAssertEqual(monster.mutate(inventory: 1, at: 0), true) 194 XCTAssertEqual(monster.mutate(inventory: 2, at: 1), true) 195 XCTAssertEqual(monster.mutate(inventory: 3, at: 2), true) 196 XCTAssertEqual(monster.mutate(inventory: 4, at: 3), true) 197 XCTAssertEqual(monster.mutate(inventory: 5, at: 4), true) 198 199 for i in 0..<monster.inventoryCount { 200 XCTAssertEqual(monster.inventory(at: i), Byte(i + 1)) 201 } 202 203 XCTAssertEqual(monster.mutate(inventory: 0, at: 0), true) 204 XCTAssertEqual(monster.mutate(inventory: 1, at: 1), true) 205 XCTAssertEqual(monster.mutate(inventory: 2, at: 2), true) 206 XCTAssertEqual(monster.mutate(inventory: 3, at: 3), true) 207 XCTAssertEqual(monster.mutate(inventory: 4, at: 4), true) 208 209 let vec = monster.mutablePos 210 XCTAssertEqual(vec?.x, 1) 211 XCTAssertTrue(vec?.mutate(x: 55.0) ?? false) 212 XCTAssertTrue(vec?.mutate(test1: 55) ?? false) 213 XCTAssertEqual(vec?.x, 55.0) 214 XCTAssertEqual(vec?.test1, 55.0) 215 XCTAssertTrue(vec?.mutate(x: 1) ?? false) 216 XCTAssertEqual(vec?.x, 1) 217 XCTAssertTrue(vec?.mutate(test1: 3) ?? false) 218 } 219 readFlatbufferMonsternull220 func readFlatbufferMonster(monster: inout MyGame_Example_Monster) { 221 XCTAssertEqual(monster.hp, 80) 222 XCTAssertEqual(monster.mana, 150) 223 XCTAssertEqual(monster.name, "MyMonster") 224 let pos = monster.pos 225 XCTAssertEqual(pos?.x, 1) 226 XCTAssertEqual(pos?.y, 2) 227 XCTAssertEqual(pos?.z, 3) 228 XCTAssertEqual(pos?.test1, 3) 229 XCTAssertEqual(pos?.test2, .green) 230 let test = pos?.test3 231 XCTAssertEqual(test?.a, 5) 232 XCTAssertEqual(test?.b, 6) 233 XCTAssertEqual(monster.testType, .monster) 234 let monster2 = monster.test(type: Monster.self) 235 XCTAssertEqual(monster2?.name, "Fred") 236 237 XCTAssertEqual(monster.mutate(mana: 10), false) 238 239 XCTAssertEqual(monster.mana, 150) 240 XCTAssertEqual(monster.inventoryCount, 5) 241 var sum: Byte = 0 242 for i in 0...monster.inventoryCount { 243 sum += monster.inventory(at: i) 244 } 245 XCTAssertEqual(sum, 10) 246 XCTAssertEqual(monster.test4Count, 2) 247 248 let test0 = monster.test4(at: 0) 249 let test1 = monster.test4(at: 1) 250 var sum0 = 0 251 var sum1 = 0 252 if let a = test0?.a, let b = test0?.b { 253 sum0 = Int(a) + Int(b) 254 } 255 if let a = test1?.a, let b = test1?.b { 256 sum1 = Int(a) + Int(b) 257 } 258 XCTAssertEqual(sum0 + sum1, 100) 259 260 let mutableTest0 = monster.mutableTest4(at: 0) 261 let mutableTest1 = monster.mutableTest4(at: 1) 262 var sum2 = 0 263 var sum3 = 0 264 if let a = mutableTest0?.a, let b = mutableTest0?.b { 265 sum2 = Int(a) + Int(b) 266 } 267 if let a = mutableTest1?.a, let b = mutableTest1?.b { 268 sum3 = Int(a) + Int(b) 269 } 270 XCTAssertEqual(sum2 + sum3, 100) 271 272 XCTAssertEqual(monster.testarrayofstringCount, 2) 273 XCTAssertEqual(monster.testarrayofstring(at: 0), "test1") 274 XCTAssertEqual(monster.testarrayofstring(at: 1), "test2") 275 XCTAssertEqual(monster.testbool, true) 276 277 let array = monster.nameSegmentArray 278 XCTAssertEqual(String(bytes: array ?? [], encoding: .utf8), "MyMonster") 279 280 if 0 == monster.testarrayofboolsCount { 281 XCTAssertEqual(monster.testarrayofbools.isEmpty, true) 282 } else { 283 XCTAssertEqual(monster.testarrayofbools.isEmpty, false) 284 } 285 } 286 readObjectApinull287 func readObjectApi(monster: MyGame_Example_MonsterT) { 288 XCTAssertEqual(monster.hp, 80) 289 XCTAssertEqual(monster.mana, 150) 290 XCTAssertEqual(monster.name, "MyMonster") 291 let pos = monster.pos 292 XCTAssertEqual(pos?.x, 1) 293 XCTAssertEqual(pos?.y, 2) 294 XCTAssertEqual(pos?.z, 3) 295 XCTAssertEqual(pos?.test1, 3) 296 XCTAssertEqual(pos?.test2, .green) 297 let test = pos?.test3 298 XCTAssertEqual(test?.a, 5) 299 XCTAssertEqual(test?.b, 6) 300 let monster2 = monster.test?.value as? MyGame_Example_MonsterT 301 XCTAssertEqual(monster2?.name, "Fred") 302 XCTAssertEqual(monster.mana, 150) 303 monster.mana = 10 304 XCTAssertEqual(monster.mana, 10) 305 monster.mana = 150 306 XCTAssertEqual(monster.mana, 150) 307 308 XCTAssertEqual(monster.inventory.count, 5) 309 var sum: Byte = 0 310 for i in monster.inventory { 311 sum += i 312 } 313 XCTAssertEqual(sum, 10) 314 XCTAssertEqual(monster.test4.count, 2) 315 let test0 = monster.test4[0] 316 let test1 = monster.test4[1] 317 var sum0 = 0 318 var sum1 = 0 319 if let a = test0?.a, let b = test0?.b { 320 sum0 = Int(a) + Int(b) 321 } 322 if let a = test1?.a, let b = test1?.b { 323 sum1 = Int(a) + Int(b) 324 } 325 XCTAssertEqual(sum0 + sum1, 100) 326 XCTAssertEqual(monster.testbool, true) 327 } 328 } 329