1 import XCTest 2 import Foundation 3 @testable import FlatBuffers 4 5 typealias Test1 = MyGame.Example.Test 6 typealias Monster1 = MyGame.Example.Monster 7 typealias Vec3 = MyGame.Example.Vec3 8 9 class FlatBuffersMonsterWriterTests: XCTestCase { 10 testDatanull11 func testData() { 12 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]) 13 let _data = ByteBuffer(data: data) 14 readMonster(fb: _data) 15 } 16 testReadFromOtherLangaguesnull17 func testReadFromOtherLangagues() { 18 let path = FileManager.default.currentDirectoryPath 19 let url = URL(fileURLWithPath: path, isDirectory: true).appendingPathComponent("monsterdata_test").appendingPathExtension("mon") 20 guard let data = try? Data(contentsOf: url) else { return } 21 let _data = ByteBuffer(data: data) 22 readMonster(fb: _data) 23 } 24 testCreateMonsternull25 func testCreateMonster() { 26 let bytes = createMonster(withPrefix: false) 27 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]) 28 readMonster(fb: bytes.buffer) 29 mutateMonster(fb: bytes.buffer) 30 readMonster(fb: bytes.buffer) 31 } 32 testCreateMonsterResizedBuffernull33 func testCreateMonsterResizedBuffer() { 34 let bytes = createMonster(withPrefix: false) 35 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]) 36 readMonster(fb: ByteBuffer(data: bytes.data)) 37 } 38 testCreateMonsterPrefixednull39 func testCreateMonsterPrefixed() { 40 let bytes = createMonster(withPrefix: true) 41 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]) 42 43 let newBuf = FlatBuffersUtils.removeSizePrefix(bb: bytes.buffer) 44 readMonster(fb: newBuf) 45 } 46 createMonsternull47 func createMonster(withPrefix prefix: Bool) -> FlatBufferBuilder { 48 let fbb = FlatBufferBuilder(initialSize: 1) 49 let names = [fbb.create(string: "Frodo"), fbb.create(string: "Barney"), fbb.create(string: "Wilma")] 50 var offsets: [Offset<UOffset>] = [] 51 let start1 = Monster1.startMonster(fbb) 52 Monster1.add(name: names[0], fbb) 53 offsets.append(Monster1.endMonster(fbb, start: start1)) 54 let start2 = Monster1.startMonster(fbb) 55 Monster1.add(name: names[1], fbb) 56 offsets.append(Monster1.endMonster(fbb, start: start2)) 57 let start3 = Monster1.startMonster(fbb) 58 Monster1.add(name: names[2], fbb) 59 offsets.append(Monster1.endMonster(fbb, start: start3)) 60 61 let sortedArray = Monster1.sortVectorOfMonster(offsets: offsets, fbb) 62 63 let str = fbb.create(string: "MyMonster") 64 let test1 = fbb.create(string: "test1") 65 let test2 = fbb.create(string: "test2") 66 let _inv: [Byte] = [0, 1, 2, 3, 4] 67 let inv = fbb.createVector(_inv) 68 69 let fred = fbb.create(string: "Fred") 70 let mon1Start = Monster1.startMonster(fbb) 71 Monster1.add(name: fred, fbb) 72 let mon2 = Monster1.endMonster(fbb, start: mon1Start) 73 let test4 = fbb.createVector(structs: [MyGame.Example.createTest(a: 30, b: 40), 74 MyGame.Example.createTest(a: 10, b: 20)], 75 type: Test1.self) 76 77 let stringTestVector = fbb.createVector(ofOffsets: [test1, test2]) 78 79 let mStart = Monster1.startMonster(fbb) 80 let posOffset = fbb.create(struct: MyGame.Example.createVec3(x: 1, y: 2, z: 3, test1: 3, test2: .green, test3a: 5, test3b: 6), type: Vec3.self) 81 Monster1.add(pos: posOffset, fbb) 82 Monster1.add(hp: 80, fbb) 83 Monster1.add(name: str, fbb) 84 Monster1.addVectorOf(inventory: inv, fbb) 85 Monster1.add(testType: .monster, fbb) 86 Monster1.add(test: mon2, fbb) 87 Monster1.addVectorOf(test4: test4, fbb) 88 Monster1.addVectorOf(testarrayofstring: stringTestVector, fbb) 89 Monster1.add(testbool: true, fbb) 90 Monster1.addVectorOf(testarrayoftables: sortedArray, fbb) 91 let end = Monster1.endMonster(fbb, start: mStart) 92 Monster1.finish(fbb, end: end, prefix: prefix) 93 return fbb 94 } 95 mutateMonsternull96 func mutateMonster(fb: ByteBuffer) { 97 let monster = Monster1.getRootAsMonster(bb: fb) 98 XCTAssertFalse(monster.mutate(mana: 10)) 99 XCTAssertEqual(monster.testarrayoftables(at: 0)?.name, "Barney") 100 XCTAssertEqual(monster.testarrayoftables(at: 1)?.name, "Frodo") 101 XCTAssertEqual(monster.testarrayoftables(at: 2)?.name, "Wilma") 102 103 // Example of searching for a table by the key 104 XCTAssertNotNil(monster.testarrayoftablesBy(key: "Frodo")) 105 XCTAssertNotNil(monster.testarrayoftablesBy(key: "Barney")) 106 XCTAssertNotNil(monster.testarrayoftablesBy(key: "Wilma")) 107 108 XCTAssertEqual(monster.testType, .monster) 109 110 XCTAssertEqual(monster.mutate(inventory: 1, at: 0), true) 111 XCTAssertEqual(monster.mutate(inventory: 2, at: 1), true) 112 XCTAssertEqual(monster.mutate(inventory: 3, at: 2), true) 113 XCTAssertEqual(monster.mutate(inventory: 4, at: 3), true) 114 XCTAssertEqual(monster.mutate(inventory: 5, at: 4), true) 115 116 for i in 0..<monster.inventoryCount { 117 XCTAssertEqual(monster.inventory(at: i), Byte(i + 1)) 118 } 119 120 XCTAssertEqual(monster.mutate(inventory: 0, at: 0), true) 121 XCTAssertEqual(monster.mutate(inventory: 1, at: 1), true) 122 XCTAssertEqual(monster.mutate(inventory: 2, at: 2), true) 123 XCTAssertEqual(monster.mutate(inventory: 3, at: 3), true) 124 XCTAssertEqual(monster.mutate(inventory: 4, at: 4), true) 125 126 let vec = monster.pos 127 XCTAssertEqual(vec?.x, 1) 128 XCTAssertTrue(vec?.mutate(x: 55.0) ?? false) 129 XCTAssertTrue(vec?.mutate(test1: 55) ?? false) 130 XCTAssertEqual(vec?.x, 55.0) 131 XCTAssertEqual(vec?.test1, 55.0) 132 XCTAssertTrue(vec?.mutate(x: 1) ?? false) 133 XCTAssertEqual(vec?.x, 1) 134 XCTAssertTrue(vec?.mutate(test1: 3) ?? false) 135 } 136 readMonsternull137 func readMonster(fb: ByteBuffer) { 138 let monster = Monster1.getRootAsMonster(bb: fb) 139 XCTAssertEqual(monster.hp, 80) 140 XCTAssertEqual(monster.mana, 150) 141 XCTAssertEqual(monster.name, "MyMonster") 142 let pos = monster.pos 143 XCTAssertEqual(pos?.x, 1) 144 XCTAssertEqual(pos?.y, 2) 145 XCTAssertEqual(pos?.z, 3) 146 XCTAssertEqual(pos?.test1, 3) 147 XCTAssertEqual(pos?.test2, .green) 148 let test = pos?.test3 149 XCTAssertEqual(test?.a, 5) 150 XCTAssertEqual(test?.b, 6) 151 XCTAssertEqual(monster.testType, .monster) 152 let monster2 = monster.test(type: Monster1.self) 153 XCTAssertEqual(monster2?.name, "Fred") 154 155 XCTAssertEqual(monster.mutate(mana: 10), false) 156 157 XCTAssertEqual(monster.mana, 150) 158 XCTAssertEqual(monster.inventoryCount, 5) 159 var sum: Byte = 0 160 for i in 0...monster.inventoryCount { 161 sum += monster.inventory(at: i) 162 } 163 XCTAssertEqual(sum, 10) 164 XCTAssertEqual(monster.test4Count, 2) 165 let test0 = monster.test4(at: 0) 166 let test1 = monster.test4(at: 1) 167 var sum0 = 0 168 var sum1 = 0 169 if let a = test0?.a, let b = test0?.b { 170 sum0 = Int(a) + Int(b) 171 } 172 if let a = test1?.a, let b = test1?.b { 173 sum1 = Int(a) + Int(b) 174 } 175 XCTAssertEqual(sum0 + sum1, 100) 176 XCTAssertEqual(monster.testarrayofstringCount, 2) 177 XCTAssertEqual(monster.testarrayofstring(at: 0), "test1") 178 XCTAssertEqual(monster.testarrayofstring(at: 1), "test2") 179 XCTAssertEqual(monster.testbool, true) 180 181 let array = monster.nameSegmentArray 182 XCTAssertEqual(String(bytes: array ?? [], encoding: .utf8), "MyMonster") 183 184 if 0 == monster.testarrayofboolsCount { 185 XCTAssertEqual(monster.testarrayofbools.isEmpty, true) 186 } else { 187 XCTAssertEqual(monster.testarrayofbools.isEmpty, false) 188 } 189 } 190 } 191