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 @file:Suppress("UNCHECKED_CAST") 17 18 package com.google.flatbuffers.kotlin 19 20 import Attacker 21 import AttackerOffsetArray 22 import CharacterEArray 23 import dictionaryLookup.LongFloatEntry 24 import dictionaryLookup.LongFloatMap 25 import Movie 26 import dictionaryLookup.LongFloatEntryOffsetArray 27 import myGame.example.* 28 import myGame.example.Test.Companion.createTest 29 import optionalScalars.OptionalByte 30 import optionalScalars.ScalarStuff 31 import kotlin.test.Test 32 import kotlin.test.assertEquals 33 34 35 @ExperimentalUnsignedTypes 36 class FlatBufferBuilderTest { 37 38 @Test testSingleTablenull39 fun testSingleTable() { 40 val fbb = FlatBufferBuilder() 41 val name = fbb.createString("Frodo") 42 val invValues = ubyteArrayOf(10u, 11u, 12u, 13u, 14u) 43 val inv = Monster.createInventoryVector(fbb, invValues) 44 Monster.startMonster(fbb) 45 Monster.addPos( 46 fbb, Vec3.createVec3( 47 fbb, 1.0f, 2.0f, 3.0f, 3.0, 48 Color.Green, 5.toShort(), 6.toByte() 49 ) 50 ) 51 Monster.addHp(fbb, 80.toShort()) 52 Monster.addName(fbb, name) 53 Monster.addMana(fbb, 150) 54 Monster.addInventory(fbb, inv) 55 Monster.addTestType(fbb, AnyE.Monster) 56 Monster.addTestbool(fbb, true) 57 Monster.addTesthashu32Fnv1(fbb, (Int.MAX_VALUE + 1L).toUInt()) 58 val root = Monster.endMonster(fbb) 59 fbb.finish(root) 60 61 val monster = Monster.asRoot(fbb.dataBuffer()) 62 assertEquals(monster.name, "Frodo") 63 assertEquals(monster.mana, 150.toShort()) 64 assertEquals(monster.hp, 80) 65 66 val pos = monster.pos!! 67 assertEquals(monster.inventory(0), invValues[0]) 68 assertEquals(monster.inventory(1), invValues[1]) 69 assertEquals(monster.inventory(2), invValues[2]) 70 assertEquals(monster.inventory(3), invValues[3]) 71 assertEquals(pos.x, 1.0f) 72 assertEquals(pos.y, 2.0f) 73 assertEquals(pos.z, 3.0f) 74 assertEquals(pos.test1, 3.0) 75 assertEquals(pos.test2, Color.Green) 76 assertEquals(pos.test3!!.a, 5.toShort()) 77 assertEquals(pos.test3!!.b, 6.toByte()) 78 79 val inventoryBuffer = monster.inventoryAsBuffer() 80 assertEquals(invValues.size, inventoryBuffer.limit) 81 for (i in invValues.indices) { 82 assertEquals(invValues[i], inventoryBuffer.getUByte(i)) 83 } 84 } 85 86 @Test testSortedVectornull87 fun testSortedVector() { 88 val fbb = FlatBufferBuilder() 89 val names = arrayOf(fbb.createString("Frodo"), fbb.createString("Barney"), fbb.createString("Wilma")) 90 val monsters = MonsterOffsetArray(3) { 91 Monster.startMonster(fbb) 92 Monster.addName(fbb, names[it]) 93 Monster.endMonster(fbb) 94 } 95 val ary = Monster.createTestarrayoftablesVector(fbb, monsters) 96 Monster.startMonster(fbb) 97 Monster.addName(fbb, names[0]) 98 Monster.addTestarrayoftables(fbb, ary) 99 val root = Monster.endMonster(fbb) 100 fbb.finish(root) 101 val a = Monster.asRoot(fbb.dataBuffer()) 102 assertEquals(a.name, "Frodo") 103 assertEquals(a.testarrayoftablesLength, 3) 104 val monster0 = a.testarrayoftables(0)!! 105 val monster1 = a.testarrayoftables(1)!! 106 val monster2 = a.testarrayoftables(2)!! 107 assertEquals(monster0.name, "Frodo") 108 assertEquals(monster1.name, "Barney") 109 assertEquals(monster2.name, "Wilma") 110 111 // test AsBuffer feature 112 113 } 114 115 @Test testCreateBufferVectornull116 fun testCreateBufferVector() { 117 val fbb = FlatBufferBuilder(16) 118 val str = fbb.createString("MyMonster") 119 val inventory = ubyteArrayOf(0u, 1u, 2u, 3u, 4u, 5u, 6u, 88u, 99u, 122u, 1u) 120 val vec = Monster.createInventoryVector(fbb, inventory) 121 Monster.startMonster(fbb) 122 Monster.addInventory(fbb, vec) 123 Monster.addName(fbb, str) 124 val monster1 = Monster.endMonster(fbb) 125 Monster.finishMonsterBuffer(fbb, monster1) 126 val monsterObject: Monster = Monster.asRoot(fbb.dataBuffer()) 127 val iBuffer = monsterObject.inventoryAsBuffer() 128 129 assertEquals(monsterObject.inventoryLength, inventory.size) 130 assertEquals(iBuffer.limit, inventory.size) 131 132 for (i in inventory.indices) { 133 assertEquals(inventory[i], monsterObject.inventory(i)) 134 assertEquals(inventory[i], iBuffer.getUByte(i)) 135 } 136 } 137 138 @Test testCreateUninitializedVectornull139 fun testCreateUninitializedVector() { 140 val fbb = FlatBufferBuilder(16) 141 val str = fbb.createString("MyMonster") 142 val inventory = byteArrayOf(10, 11, 12, 13, 14) 143 val uninitializedBuffer = fbb.createUnintializedVector(1, inventory.size, 1) 144 for (i in inventory) { 145 uninitializedBuffer.put(i) 146 } 147 val vec = fbb.endVector<UByte>() 148 Monster.startMonster(fbb) 149 Monster.addInventory(fbb, vec) 150 Monster.addName(fbb, str) 151 val monster1 = Monster.endMonster(fbb) 152 Monster.finishMonsterBuffer(fbb, monster1) 153 val monsterObject: Monster = Monster.asRoot(fbb.dataBuffer()) 154 assertEquals(inventory[1].toUByte(), monsterObject.inventory(1)) 155 assertEquals(inventory.size, monsterObject.inventoryLength) 156 val inventoryBuffer = monsterObject.inventoryAsBuffer() 157 assertEquals(inventory[1].toInt().toUByte(), inventoryBuffer.getUByte(1)) 158 assertEquals(inventory.size, inventoryBuffer.limit) 159 } 160 161 @Test testBuilderBasicsnull162 fun testBuilderBasics() { 163 val fbb = FlatBufferBuilder() 164 val names = arrayOf(fbb.createString("Frodo"), fbb.createString("Barney"), fbb.createString("Wilma")) 165 val off = Array<Offset<Monster>>(3) { Offset(0) } 166 Monster.startMonster(fbb) 167 Monster.addName(fbb, names[0]) 168 off[0] = Monster.endMonster(fbb) 169 Monster.startMonster(fbb) 170 Monster.addName(fbb, names[1]) 171 off[1] = Monster.endMonster(fbb) 172 Monster.startMonster(fbb) 173 Monster.addName(fbb, names[2]) 174 off[2] = Monster.endMonster(fbb) 175 val sortMons = fbb.createSortedVectorOfTables(Monster(), off) 176 177 // We set up the same values as monsterdata.json: 178 179 val inv = Monster.createInventoryVector(fbb, byteArrayOf(0,1,2,3,4).toUByteArray()) 180 181 val fred = fbb.createString("Fred") 182 Monster.startMonster(fbb) 183 Monster.addName(fbb, fred) 184 val mon2 = Monster.endMonster(fbb) 185 186 Monster.startTest4Vector(fbb, 2) 187 createTest(fbb, 10.toShort(), 20.toByte()) 188 createTest(fbb, 30.toShort(), 40.toByte()) 189 val test4 = fbb.endVector<myGame.example.Test>() 190 191 val strings = StringOffsetArray(2) { fbb.createString("test$it") } 192 val testArrayOfString = 193 Monster.createTestarrayofstringVector(fbb, strings) 194 195 Monster.startMonster(fbb) 196 Monster.addName(fbb, names[0]) 197 Monster.addPos(fbb, Vec3.createVec3( 198 fbb, 1.0f, 2.0f, 3.0f, 3.0, 199 Color.Green, 5.toShort(), 6.toByte() 200 )) 201 Monster.addHp(fbb, 80) 202 Monster.addMana(fbb, 150) 203 Monster.addInventory(fbb, inv) 204 Monster.addTestType(fbb, AnyE.Monster) 205 Monster.addTest(fbb, mon2.toUnion()) 206 Monster.addTest4(fbb, test4) 207 Monster.addTestarrayofstring(fbb, testArrayOfString) 208 Monster.addTestbool(fbb, true) 209 Monster.addTesthashu32Fnv1(fbb, (Int.MAX_VALUE + 1L).toUInt()) 210 Monster.addTestarrayoftables(fbb, sortMons) 211 val mon = Monster.endMonster(fbb) 212 Monster.finishMonsterBuffer(fbb, mon) 213 //Attempt to mutate Monster fields and check whether the buffer has been mutated properly 214 // revert to original values after testing 215 val monster = Monster.asRoot(fbb.dataBuffer()) 216 217 // mana is optional and does not exist in the buffer so the mutation should fail 218 // the mana field should retain its default value 219 assertEquals(monster.mana, 150.toShort()) 220 assertEquals(monster.hp, 80) 221 222 // Accessing a vector of sorted by the key tables 223 assertEquals(monster.testarrayoftables(0)!!.name, "Barney") 224 assertEquals(monster.testarrayoftables(1)!!.name, "Frodo") 225 assertEquals(monster.testarrayoftables(2)!!.name, "Wilma") 226 227 // Example of searching for a table by the key 228 assertEquals(monster.testarrayoftablesByKey("Frodo")!!.name, "Frodo") 229 assertEquals(monster.testarrayoftablesByKey("Barney")!!.name, "Barney") 230 assertEquals(monster.testarrayoftablesByKey("Wilma")!!.name, "Wilma") 231 232 for (i in 0 until monster.inventoryLength) { 233 assertEquals(monster.inventory(i), (i).toUByte()) 234 } 235 236 // get a struct field and edit one of its fields 237 val pos2 = monster.pos!! 238 assertEquals(pos2.x, 1.0f) 239 assertEquals(pos2.test2, Color.Green) 240 } 241 242 @Test testVectorOfUnionsnull243 fun testVectorOfUnions() { 244 val fbb = FlatBufferBuilder() 245 val swordAttackDamage = 1 246 val attacker = Attacker.createAttacker(fbb, swordAttackDamage).toUnion() 247 val attackers = UnionOffsetArray(1) { attacker } 248 val characters = CharacterEArray(1) 249 characters[0] = CharacterE.MuLan.value 250 251 Movie.finishMovieBuffer( 252 fbb, 253 Movie.createMovie( 254 fbb, 255 CharacterE.MuLan, 256 attacker, 257 Movie.createCharactersTypeVector(fbb, characters), 258 Movie.createCharactersVector(fbb, attackers) 259 ) 260 ) 261 262 val movie: Movie = Movie.asRoot(fbb.dataBuffer()) 263 264 265 266 assertEquals(movie.charactersTypeLength, 1) 267 assertEquals(movie.charactersLength, 1) 268 269 assertEquals(movie.charactersType(0), CharacterE.MuLan) 270 assertEquals((movie.characters(Attacker(), 0) as Attacker).swordAttackDamage, swordAttackDamage) 271 } 272 273 @Test TestVectorOfBytesnull274 fun TestVectorOfBytes() { 275 val fbb = FlatBufferBuilder(16) 276 var str = fbb.createString("ByteMonster") 277 val data = ubyteArrayOf(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u) 278 var offset = Monster.createInventoryVector(fbb, data) 279 Monster.startMonster(fbb) 280 Monster.addName(fbb, str) 281 Monster.addInventory(fbb, offset) 282 var monster1 = Monster.endMonster(fbb) 283 Monster.finishMonsterBuffer(fbb, monster1) 284 285 val monsterObject = Monster.asRoot(fbb.dataBuffer()) 286 assertEquals("ByteMonster", monsterObject.name) 287 assertEquals(data.size, monsterObject.inventoryLength) 288 assertEquals(monsterObject.inventory(4), data[4]) 289 offset = fbb.createByteVector(data.toByteArray()) as VectorOffset<UByte> // TODO: fix me 290 str = fbb.createString("ByteMonster") 291 Monster.startMonster(fbb) 292 Monster.addName(fbb, str) 293 Monster.addInventory(fbb, offset) 294 monster1 = Monster.endMonster(fbb) 295 Monster.finishMonsterBuffer(fbb, monster1) 296 297 val monsterObject2 = Monster.asRoot(fbb.dataBuffer()) 298 assertEquals(monsterObject2.inventoryLength, data.size) 299 for (i in data.indices) { 300 assertEquals(monsterObject2.inventory(i), data[i]) 301 } 302 fbb.clear() 303 offset = fbb.createByteVector(data.toByteArray(), 3, 4) as VectorOffset<UByte> 304 str = fbb.createString("ByteMonster") 305 Monster.startMonster(fbb) 306 Monster.addName(fbb, str) 307 Monster.addInventory(fbb, offset) 308 monster1 = Monster.endMonster(fbb) 309 Monster.finishMonsterBuffer(fbb, monster1) 310 311 val monsterObject3 = Monster.asRoot(fbb.dataBuffer()) 312 assertEquals(monsterObject3.inventoryLength, 4) 313 assertEquals(monsterObject3.inventory(0), data[3]) 314 fbb.clear() 315 offset = Monster.createInventoryVector(fbb, data) 316 str = fbb.createString("ByteMonster") 317 Monster.startMonster(fbb) 318 Monster.addName(fbb, str) 319 Monster.addInventory(fbb, offset) 320 monster1 = Monster.endMonster(fbb) 321 Monster.finishMonsterBuffer(fbb, monster1) 322 323 val monsterObject4 = Monster.asRoot(fbb.dataBuffer()) 324 assertEquals(monsterObject4.inventoryLength, data.size) 325 assertEquals(monsterObject4.inventory(8), 8u) 326 fbb.clear() 327 328 val largeData = ByteArray(1024) 329 offset = fbb.createByteVector(largeData) as VectorOffset<UByte> //TODO: fix me 330 str = fbb.createString("ByteMonster") 331 Monster.startMonster(fbb) 332 Monster.addName(fbb, str) 333 Monster.addInventory(fbb, offset) 334 monster1 = Monster.endMonster(fbb) 335 Monster.finishMonsterBuffer(fbb, monster1) 336 337 val monsterObject5 = Monster.asRoot(fbb.dataBuffer()) 338 assertEquals(monsterObject5.inventoryLength, largeData.size) 339 assertEquals(monsterObject5.inventory(25), largeData[25].toUByte()) 340 fbb.clear() 341 342 var bb = ArrayReadBuffer(largeData, 512) 343 offset = fbb.createByteVector(bb) as VectorOffset<UByte> //TODO: fix me 344 str = fbb.createString("ByteMonster") 345 Monster.startMonster(fbb) 346 Monster.addName(fbb, str) 347 Monster.addInventory(fbb, offset) 348 monster1 = Monster.endMonster(fbb) 349 Monster.finishMonsterBuffer(fbb, monster1) 350 val monsterObject6 = Monster.asRoot(fbb.dataBuffer()) 351 assertEquals(monsterObject6.inventoryLength, 512) 352 assertEquals(monsterObject6.inventory(0), largeData[0].toUByte()) 353 fbb.clear() 354 355 bb = ArrayReadBuffer(largeData, largeData.size - 216) 356 val stringBuffer = ArrayReadBuffer("AlreadyBufferedString".encodeToByteArray()) 357 offset = fbb.createByteVector(bb) as VectorOffset<UByte> //TODO: fix me 358 str = fbb.createString(stringBuffer) 359 Monster.startMonster(fbb) 360 Monster.addName(fbb, str) 361 Monster.addInventory(fbb, offset) 362 monster1 = Monster.endMonster(fbb) 363 Monster.finishMonsterBuffer(fbb, monster1) 364 365 val monsterObject7 = Monster.asRoot(fbb.dataBuffer()) 366 assertEquals(monsterObject7.inventoryLength, 216) 367 assertEquals("AlreadyBufferedString", monsterObject7.name) 368 } 369 370 @Test testEnumsnull371 fun testEnums() { 372 assertEquals(Color.name(Color.Red), "Red") 373 assertEquals(Color.name(Color.Blue), "Blue") 374 assertEquals(AnyE.name(AnyE.None), "NONE") 375 assertEquals(AnyE.name(AnyE.Monster), "Monster") 376 } 377 378 @Test testSharedStringPoolnull379 fun testSharedStringPool() { 380 val fb = FlatBufferBuilder(1) 381 val testString = "My string" 382 val offset = fb.createSharedString(testString) 383 for (i in 0..9) { 384 assertEquals(offset, fb.createSharedString(testString)) 385 } 386 } 387 388 @Test testScalarOptionalnull389 fun testScalarOptional() { 390 val fbb = FlatBufferBuilder(1) 391 ScalarStuff.startScalarStuff(fbb) 392 var pos = ScalarStuff.endScalarStuff(fbb) 393 fbb.finish(pos) 394 var scalarStuff: ScalarStuff = ScalarStuff.asRoot(fbb.dataBuffer()) 395 assertEquals(scalarStuff.justI8, 0.toByte()) 396 assertEquals(scalarStuff.maybeI8, null) 397 assertEquals(scalarStuff.defaultI8, 42.toByte()) 398 assertEquals(scalarStuff.justU8, 0u) 399 assertEquals(scalarStuff.maybeU8, null) 400 assertEquals(scalarStuff.defaultU8, 42u) 401 assertEquals(scalarStuff.justI16, 0.toShort()) 402 assertEquals(scalarStuff.maybeI16, null) 403 assertEquals(scalarStuff.defaultI16, 42.toShort()) 404 assertEquals(scalarStuff.justU16, 0u) 405 assertEquals(scalarStuff.maybeU16, null) 406 assertEquals(scalarStuff.defaultU16, 42u) 407 assertEquals(scalarStuff.justI32, 0) 408 assertEquals(scalarStuff.maybeI32, null) 409 assertEquals(scalarStuff.defaultI32, 42) 410 assertEquals(scalarStuff.justU32, 0u) 411 assertEquals(scalarStuff.maybeU32, null) 412 assertEquals(scalarStuff.defaultU32, 42u) 413 assertEquals(scalarStuff.justI64, 0L) 414 assertEquals(scalarStuff.maybeI64, null) 415 assertEquals(scalarStuff.defaultI64, 42L) 416 assertEquals(scalarStuff.justU64, 0UL) 417 assertEquals(scalarStuff.maybeU64, null) 418 assertEquals(scalarStuff.defaultU64, 42UL) 419 assertEquals(scalarStuff.justF32, 0.0f) 420 assertEquals(scalarStuff.maybeF32, null) 421 assertEquals(scalarStuff.defaultF32, 42.0f) 422 assertEquals(scalarStuff.justF64, 0.0) 423 assertEquals(scalarStuff.maybeF64, null) 424 assertEquals(scalarStuff.defaultF64, 42.0) 425 assertEquals(scalarStuff.justBool, false) 426 assertEquals(scalarStuff.maybeBool, null) 427 assertEquals(scalarStuff.defaultBool, true) 428 assertEquals(scalarStuff.justEnum, OptionalByte.None) 429 assertEquals(scalarStuff.maybeEnum, null) 430 assertEquals(scalarStuff.defaultEnum, OptionalByte.One) 431 fbb.clear() 432 ScalarStuff.startScalarStuff(fbb) 433 ScalarStuff.addJustI8(fbb, 5.toByte()) 434 ScalarStuff.addMaybeI8(fbb, 5.toByte()) 435 ScalarStuff.addDefaultI8(fbb, 5.toByte()) 436 ScalarStuff.addJustU8(fbb, 6u) 437 ScalarStuff.addMaybeU8(fbb, 6u) 438 ScalarStuff.addDefaultU8(fbb, 6u) 439 ScalarStuff.addJustI16(fbb, 7.toShort()) 440 ScalarStuff.addMaybeI16(fbb, 7.toShort()) 441 ScalarStuff.addDefaultI16(fbb, 7.toShort()) 442 ScalarStuff.addJustU16(fbb, 8u) 443 ScalarStuff.addMaybeU16(fbb, 8u) 444 ScalarStuff.addDefaultU16(fbb, 8u) 445 ScalarStuff.addJustI32(fbb, 9) 446 ScalarStuff.addMaybeI32(fbb, 9) 447 ScalarStuff.addDefaultI32(fbb, 9) 448 ScalarStuff.addJustU32(fbb, 10u) 449 ScalarStuff.addMaybeU32(fbb, 10u) 450 ScalarStuff.addDefaultU32(fbb, 10u) 451 ScalarStuff.addJustI64(fbb, 11L) 452 ScalarStuff.addMaybeI64(fbb, 11L) 453 ScalarStuff.addDefaultI64(fbb, 11L) 454 ScalarStuff.addJustU64(fbb, 12UL) 455 ScalarStuff.addMaybeU64(fbb, 12UL) 456 ScalarStuff.addDefaultU64(fbb, 12UL) 457 ScalarStuff.addJustF32(fbb, 13.0f) 458 ScalarStuff.addMaybeF32(fbb, 13.0f) 459 ScalarStuff.addDefaultF32(fbb, 13.0f) 460 ScalarStuff.addJustF64(fbb, 14.0) 461 ScalarStuff.addMaybeF64(fbb, 14.0) 462 ScalarStuff.addDefaultF64(fbb, 14.0) 463 ScalarStuff.addJustBool(fbb, true) 464 ScalarStuff.addMaybeBool(fbb, true) 465 ScalarStuff.addDefaultBool(fbb, true) 466 ScalarStuff.addJustEnum(fbb, OptionalByte.Two) 467 ScalarStuff.addMaybeEnum(fbb, OptionalByte.Two) 468 ScalarStuff.addDefaultEnum(fbb, OptionalByte.Two) 469 pos = ScalarStuff.endScalarStuff(fbb) 470 fbb.finish(pos) 471 scalarStuff = ScalarStuff.asRoot(fbb.dataBuffer()) 472 assertEquals(scalarStuff.justI8, 5.toByte()) 473 assertEquals(scalarStuff.maybeI8, 5.toByte()) 474 assertEquals(scalarStuff.defaultI8, 5.toByte()) 475 assertEquals(scalarStuff.justU8, 6u) 476 assertEquals(scalarStuff.maybeU8, 6u) 477 assertEquals(scalarStuff.defaultU8, 6u) 478 assertEquals(scalarStuff.justI16, 7.toShort()) 479 assertEquals(scalarStuff.maybeI16, 7.toShort()) 480 assertEquals(scalarStuff.defaultI16, 7.toShort()) 481 assertEquals(scalarStuff.justU16, 8u) 482 assertEquals(scalarStuff.maybeU16, 8u) 483 assertEquals(scalarStuff.defaultU16, 8u) 484 assertEquals(scalarStuff.justI32, 9) 485 assertEquals(scalarStuff.maybeI32, 9) 486 assertEquals(scalarStuff.defaultI32, 9) 487 assertEquals(scalarStuff.justU32, 10u) 488 assertEquals(scalarStuff.maybeU32, 10u) 489 assertEquals(scalarStuff.defaultU32, 10u) 490 assertEquals(scalarStuff.justI64, 11L) 491 assertEquals(scalarStuff.maybeI64, 11L) 492 assertEquals(scalarStuff.defaultI64, 11L) 493 assertEquals(scalarStuff.justU64, 12UL) 494 assertEquals(scalarStuff.maybeU64, 12UL) 495 assertEquals(scalarStuff.defaultU64, 12UL) 496 assertEquals(scalarStuff.justF32, 13.0f) 497 assertEquals(scalarStuff.maybeF32, 13.0f) 498 assertEquals(scalarStuff.defaultF32, 13.0f) 499 assertEquals(scalarStuff.justF64, 14.0) 500 assertEquals(scalarStuff.maybeF64, 14.0) 501 assertEquals(scalarStuff.defaultF64, 14.0) 502 assertEquals(scalarStuff.justBool, true) 503 assertEquals(scalarStuff.maybeBool, true) 504 assertEquals(scalarStuff.defaultBool, true) 505 assertEquals(scalarStuff.justEnum, OptionalByte.Two) 506 assertEquals(scalarStuff.maybeEnum, OptionalByte.Two) 507 assertEquals(scalarStuff.defaultEnum, OptionalByte.Two) 508 } 509 510 // @todo Seems like nesting code generation is broken for all generators. 511 // disabling test for now. 512 // @Test 513 // fun testNamespaceNesting() { 514 // // reference / manipulate these to verify compilation 515 // val fbb = FlatBufferBuilder(1) 516 // TableInNestedNS.startTableInNestedNS(fbb) 517 // TableInNestedNS.addFoo(fbb, 1234) 518 // val nestedTableOff = TableInNestedNS.endTableInNestedNs(fbb) 519 // TableInFirstNS.startTableInFirstNS(fbb) 520 // TableInFirstNS.addFooTable(fbb, nestedTableOff) 521 // TableInFirstNS.endTableInFirstNs(fbb) 522 // } 523 524 @Test testNestedFlatBuffernull525 fun testNestedFlatBuffer() { 526 val nestedMonsterName = "NestedMonsterName" 527 val nestedMonsterHp: Short = 600 528 val nestedMonsterMana: Short = 1024 529 val fbb1 = FlatBufferBuilder(16) 530 val str1 = fbb1.createString(nestedMonsterName) 531 Monster.startMonster(fbb1) 532 Monster.addName(fbb1, str1) 533 Monster.addHp(fbb1, nestedMonsterHp) 534 Monster.addMana(fbb1, nestedMonsterMana) 535 val monster1 = Monster.endMonster(fbb1) 536 Monster.finishMonsterBuffer(fbb1, monster1) 537 val fbb1Bytes: ByteArray = fbb1.sizedByteArray() 538 val fbb2 = FlatBufferBuilder(16) 539 val str2 = fbb2.createString("My Monster") 540 val nestedBuffer = Monster.createTestnestedflatbufferVector(fbb2, fbb1Bytes.toUByteArray()) 541 Monster.startMonster(fbb2) 542 Monster.addName(fbb2, str2) 543 Monster.addHp(fbb2, 50.toShort()) 544 Monster.addMana(fbb2, 32.toShort()) 545 Monster.addTestnestedflatbuffer(fbb2, nestedBuffer) 546 val monster = Monster.endMonster(fbb2) 547 Monster.finishMonsterBuffer(fbb2, monster) 548 549 // Now test the data extracted from the nested buffer 550 val mons = Monster.asRoot(fbb2.dataBuffer()) 551 val nestedMonster = mons.testnestedflatbufferAsMonster 552 assertEquals(nestedMonsterMana, nestedMonster!!.mana) 553 assertEquals(nestedMonsterHp, nestedMonster.hp) 554 assertEquals(nestedMonsterName, nestedMonster.name) 555 } 556 557 @Test testDictionaryLookupnull558 fun testDictionaryLookup() { 559 val fbb = FlatBufferBuilder(16) 560 val lfIndex = LongFloatEntry.createLongFloatEntry(fbb, 0, 99.0f) 561 val vectorEntriesIdx = LongFloatMap.createEntriesVector(fbb, LongFloatEntryOffsetArray(1) { lfIndex }) 562 val rootIdx = LongFloatMap.createLongFloatMap(fbb, vectorEntriesIdx) 563 LongFloatMap.finishLongFloatMapBuffer(fbb, rootIdx) 564 val map: LongFloatMap = LongFloatMap.asRoot(fbb.dataBuffer()) 565 566 assertEquals(1, map.entriesLength) 567 568 val e: LongFloatEntry = map.entries(0)!! 569 assertEquals(0L, e.key) 570 assertEquals(99.0f, e.value) 571 val e2: LongFloatEntry = map.entriesByKey(0)!! 572 assertEquals(0L, e2.key) 573 assertEquals(99.0f, e2.value) 574 } 575 } 576