summaryrefslogtreecommitdiff
path: root/tests/KotlinTest.kt
blob: cfb7056f655119fb35e52056715745d91a032eab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import MyGame.Example.*
import optional_scalars.*
import com.google.flatbuffers.ByteBufferUtil
import com.google.flatbuffers.FlatBufferBuilder
import NamespaceA.*
import NamespaceA.NamespaceB.*
import NamespaceA.NamespaceB.TableInNestedNS
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.RandomAccessFile
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.channels.FileChannel

import com.google.flatbuffers.Constants.SIZE_PREFIX_LENGTH

@kotlin.ExperimentalUnsignedTypes
class KotlinTest {

  companion object {
    @JvmStatic
    fun main(args: Array<String>) {

        // First, let's test reading a FlatBuffer generated by C++ code:
        // This file was generated from monsterdata_test.json

        val data = RandomAccessFile(File("monsterdata_test.mon"), "r").use {
            val temp = ByteArray(it.length().toInt())
            it.readFully(temp)
            temp
        }

        // Now test it:

        val bb = ByteBuffer.wrap(data)
        TestBuffer(bb)

        // Second, let's create a FlatBuffer from scratch in Java, and test it also.
        // We use an initial size of 1 to exercise the reallocation algorithm,
        // normally a size larger than the typical FlatBuffer you generate would be
        // better for performance.
        val fbb = FlatBufferBuilder(1)

        TestBuilderBasics(fbb, true)
        TestBuilderBasics(fbb, false)

        TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer())

        TestNamespaceNesting()

        TestNestedFlatBuffer()

        TestCreateByteVector()

        TestCreateUninitializedVector()

        TestByteBufferFactory()

        TestSizedInputStream()

        TestVectorOfUnions()

        TestSharedStringPool()
        TestScalarOptional()
        println("FlatBuffers test: completed successfully")
    }

    fun TestEnums() {
        assert(Color.name(Color.Red.toInt()) == "Red")
        assert(Color.name(Color.Blue.toInt()) == "Blue")
        assert(Any_.name(Any_.NONE.toInt()) == "NONE")
        assert(Any_.name(Any_.Monster.toInt()) == "Monster")
    }

    fun TestBuffer(bb: ByteBuffer) {
        assert(Monster.MonsterBufferHasIdentifier(bb) == true)

        val monster = Monster.getRootAsMonster(bb)

        assert(monster.hp == 80.toShort())
        assert(monster.mana == 150.toShort())  // default

        assert(monster.name == "MyMonster")
        // monster.friendly() // can't access, deprecated

        val pos = monster.pos!!
        assert(pos.x == 1.0f)
        assert(pos.y == 2.0f)
        assert(pos.z == 3.0f)
        assert(pos.test1 == 3.0)
        // issue: int != byte
        assert(pos.test2 == Color.Green)
        val t = pos.test3!!
        assert(t.a == 5.toShort())
        assert(t.b == 6.toByte())

        assert(monster.testType == Any_.Monster)
        val monster2 = Monster()
        assert(monster.test(monster2) != null == true)
        assert(monster2.name == "Fred")

        assert(monster.inventoryLength == 5)
        var invsum = 0u
        for (i in 0 until monster.inventoryLength)
            invsum += monster.inventory(i)
        assert(invsum == 10u)

        // Alternative way of accessing a vector:
        val ibb = monster.inventoryAsByteBuffer
        invsum = 0u
        while (ibb.position() < ibb.limit())
            invsum += ibb.get().toUInt()
        assert(invsum == 10u)


        val test_0 = monster.test4(0)!!
        val test_1 = monster.test4(1)!!
        assert(monster.test4Length == 2)
        assert(test_0.a + test_0.b + test_1.a + test_1.b == 100)

        assert(monster.testarrayofstringLength == 2)
        assert(monster.testarrayofstring(0) == "test1")
        assert(monster.testarrayofstring(1) == "test2")

        assert(monster.testbool == true)
    }

    // this method checks additional fields not present in the binary buffer read from file
    // these new tests are performed on top of the regular tests
    fun TestExtendedBuffer(bb: ByteBuffer) {
        TestBuffer(bb)

        val monster = Monster.getRootAsMonster(bb)

        assert(monster.testhashu32Fnv1 == (Integer.MAX_VALUE + 1L).toUInt())
    }

    fun TestNamespaceNesting() {
        // reference / manipulate these to verify compilation
        val fbb = FlatBufferBuilder(1)

        TableInNestedNS.startTableInNestedNS(fbb)
        TableInNestedNS.addFoo(fbb, 1234)
        val nestedTableOff = TableInNestedNS.endTableInNestedNS(fbb)

        TableInFirstNS.startTableInFirstNS(fbb)
        TableInFirstNS.addFooTable(fbb, nestedTableOff)
    }

    fun TestNestedFlatBuffer() {
        val nestedMonsterName = "NestedMonsterName"
        val nestedMonsterHp: Short = 600
        val nestedMonsterMana: Short = 1024

        var fbb1: FlatBufferBuilder? = FlatBufferBuilder(16)
        val str1 = fbb1!!.createString(nestedMonsterName)
        Monster.startMonster(fbb1)
        Monster.addName(fbb1, str1)
        Monster.addHp(fbb1, nestedMonsterHp)
        Monster.addMana(fbb1, nestedMonsterMana)
        val monster1 = Monster.endMonster(fbb1)
        Monster.finishMonsterBuffer(fbb1, monster1)
        val fbb1Bytes = fbb1.sizedByteArray()
        
        val fbb2 = FlatBufferBuilder(16)
        val str2 = fbb2.createString("My Monster")
        val nestedBuffer = Monster.createTestnestedflatbufferVector(fbb2, fbb1Bytes.asUByteArray())
        Monster.startMonster(fbb2)
        Monster.addName(fbb2, str2)
        Monster.addHp(fbb2, 50.toShort())
        Monster.addMana(fbb2, 32.toShort())
        Monster.addTestnestedflatbuffer(fbb2, nestedBuffer)
        val monster = Monster.endMonster(fbb2)
        Monster.finishMonsterBuffer(fbb2, monster)

        // Now test the data extracted from the nested buffer
        val mons = Monster.getRootAsMonster(fbb2.dataBuffer())
        val nestedMonster = mons.testnestedflatbufferAsMonster!!

        assert(nestedMonsterMana == nestedMonster.mana)
        assert(nestedMonsterHp == nestedMonster.hp)
        assert(nestedMonsterName == nestedMonster.name)
    }

    fun TestCreateByteVector() {
        val fbb = FlatBufferBuilder(16)
        val str = fbb.createString("MyMonster")
        val inventory = byteArrayOf(0, 1, 2, 3, 4)
        val vec = fbb.createByteVector(inventory)
        Monster.startMonster(fbb)
        Monster.addInventory(fbb, vec)
        Monster.addName(fbb, str)
        val monster1 = Monster.endMonster(fbb)
        Monster.finishMonsterBuffer(fbb, monster1)
        val monsterObject = Monster.getRootAsMonster(fbb.dataBuffer())

        assert(monsterObject.inventory(1) == inventory[1].toUByte())
        assert(monsterObject.inventoryLength == inventory.size)
        assert(ByteBuffer.wrap(inventory) == monsterObject.inventoryAsByteBuffer)
    }

    fun TestCreateUninitializedVector() {
        val fbb = FlatBufferBuilder(16)
        val str = fbb.createString("MyMonster")
        val inventory = byteArrayOf(0, 1, 2, 3, 4)
        val bb = fbb.createUnintializedVector(1, inventory.size, 1)
        for (i in inventory) {
            bb.put(i)
        }
        val vec = fbb.endVector()
        Monster.startMonster(fbb)
        Monster.addInventory(fbb, vec)
        Monster.addName(fbb, str)
        val monster1 = Monster.endMonster(fbb)
        Monster.finishMonsterBuffer(fbb, monster1)
        val monsterObject = Monster.getRootAsMonster(fbb.dataBuffer())

        assert(monsterObject.inventory(1) == inventory[1].toUByte())
        assert(monsterObject.inventoryLength == inventory.size)
        assert(ByteBuffer.wrap(inventory) == monsterObject.inventoryAsByteBuffer)
    }

    fun TestByteBufferFactory() {
        class MappedByteBufferFactory : FlatBufferBuilder.ByteBufferFactory() {
            override fun newByteBuffer(capacity: Int): ByteBuffer? {
                var bb: ByteBuffer?
                try {
                    bb = RandomAccessFile("javatest.bin", "rw").channel.map(
                        FileChannel.MapMode.READ_WRITE,
                        0,
                        capacity.toLong()
                    ).order(ByteOrder.LITTLE_ENDIAN)
                } catch (e: Throwable) {
                    println("FlatBuffers test: couldn't map ByteBuffer to a file")
                    bb = null
                }

                return bb
            }
        }

        val fbb = FlatBufferBuilder(1, MappedByteBufferFactory())

        TestBuilderBasics(fbb, false)
    }

    fun TestSizedInputStream() {
        // Test on default FlatBufferBuilder that uses HeapByteBuffer
        val fbb = FlatBufferBuilder(1)

        TestBuilderBasics(fbb, false)

        val `in` = fbb.sizedInputStream()
        val array = fbb.sizedByteArray()
        var count = 0
        var currentVal = 0

        while (currentVal != -1 && count < array.size) {
            try {
                currentVal = `in`.read()
            } catch (e: java.io.IOException) {
                println("FlatBuffers test: couldn't read from InputStream")
                return
            }

            assert(currentVal.toByte() == array[count])
            count++
        }
        assert(count == array.size)
    }

    fun TestBuilderBasics(fbb: FlatBufferBuilder, sizePrefix: Boolean) {
        val names = intArrayOf(fbb.createString("Frodo"), fbb.createString("Barney"), fbb.createString("Wilma"))
        val off = IntArray(3)
        Monster.startMonster(fbb)
        Monster.addName(fbb, names[0])
        off[0] = Monster.endMonster(fbb)
        Monster.startMonster(fbb)
        Monster.addName(fbb, names[1])
        off[1] = Monster.endMonster(fbb)
        Monster.startMonster(fbb)
        Monster.addName(fbb, names[2])
        off[2] = Monster.endMonster(fbb)
        val sortMons = fbb.createSortedVectorOfTables(Monster(), off)

        // We set up the same values as monsterdata.json:

        val str = fbb.createString("MyMonster")

        val inv = Monster.createInventoryVector(fbb, byteArrayOf(0, 1, 2, 3, 4).asUByteArray())

        val fred = fbb.createString("Fred")
        Monster.startMonster(fbb)
        Monster.addName(fbb, fred)
        val mon2 = Monster.endMonster(fbb)

        Monster.startTest4Vector(fbb, 2)
        Test.createTest(fbb, 10.toShort(), 20.toByte())
        Test.createTest(fbb, 30.toShort(), 40.toByte())
        val test4 = fbb.endVector()

        val testArrayOfString =
            Monster.createTestarrayofstringVector(fbb, intArrayOf(fbb.createString("test1"), fbb.createString("test2")))

        Monster.startMonster(fbb)
        Monster.addPos(
            fbb, Vec3.createVec3(
                fbb, 1.0f, 2.0f, 3.0f, 3.0,
                Color.Green, 5.toShort(), 6.toByte()
            )
        )
        Monster.addHp(fbb, 80.toShort())
        Monster.addName(fbb, str)
        Monster.addInventory(fbb, inv)
        Monster.addTestType(fbb, Any_.Monster)
        Monster.addTest(fbb, mon2)
        Monster.addTest4(fbb, test4)
        Monster.addTestarrayofstring(fbb, testArrayOfString)
        Monster.addTestbool(fbb, true)
        Monster.addTesthashu32Fnv1(fbb, (Integer.MAX_VALUE + 1L).toUInt())
        Monster.addTestarrayoftables(fbb, sortMons)
        val mon = Monster.endMonster(fbb)

        if (sizePrefix) {
            Monster.finishSizePrefixedMonsterBuffer(fbb, mon)
        } else {
            Monster.finishMonsterBuffer(fbb, mon)
        }

        // Write the result to a file for debugging purposes:
        // Note that the binaries are not necessarily identical, since the JSON
        // parser may serialize in a slightly different order than the above
        // Java code. They are functionally equivalent though.

        try {
            val filename = "monsterdata_java_wire" + (if (sizePrefix) "_sp" else "") + ".mon"
            val fc = FileOutputStream(filename).channel
            fc.write(fbb.dataBuffer().duplicate())
            fc.close()
        } catch (e: java.io.IOException) {
            println("FlatBuffers test: couldn't write file")
            return
        }

        // Test it:
        var dataBuffer = fbb.dataBuffer()
        if (sizePrefix) {
            assert(
                ByteBufferUtil.getSizePrefix(dataBuffer) + SIZE_PREFIX_LENGTH ==
                dataBuffer.remaining()
            )
            dataBuffer = ByteBufferUtil.removeSizePrefix(dataBuffer)
        }
        TestExtendedBuffer(dataBuffer)

        // Make sure it also works with read only ByteBuffers. This is slower,
        // since creating strings incurs an additional copy
        // (see Table.__string).
        TestExtendedBuffer(dataBuffer.asReadOnlyBuffer())

        TestEnums()

        //Attempt to mutate Monster fields and check whether the buffer has been mutated properly
        // revert to original values after testing
        val monster = Monster.getRootAsMonster(dataBuffer)

        // mana is optional and does not exist in the buffer so the mutation should fail
        // the mana field should retain its default value
        assert(monster.mutateMana(10.toShort()) == false)
        assert(monster.mana == 150.toShort())

        // Accessing a vector of sorted by the key tables
        assert(monster.testarrayoftables(0)!!.name == "Barney")
        assert(monster.testarrayoftables(1)!!.name == "Frodo")
        assert(monster.testarrayoftables(2)!!.name == "Wilma")

        // Example of searching for a table by the key
        assert(monster.testarrayoftablesByKey("Frodo")!!.name == "Frodo")
        assert(monster.testarrayoftablesByKey("Barney")!!.name == "Barney")
        assert(monster.testarrayoftablesByKey("Wilma")!!.name == "Wilma")

        // testType is an existing field and mutating it should succeed
        assert(monster.testType == Any_.Monster)
        assert(monster.mutateTestType(Any_.NONE) == true)
        assert(monster.testType == Any_.NONE)
        assert(monster.mutateTestType(Any_.Monster) == true)
        assert(monster.testType == Any_.Monster)

        //mutate the inventory vector
        assert(monster.mutateInventory(0, 1u) == true)
        assert(monster.mutateInventory(1, 2u) == true)
        assert(monster.mutateInventory(2, 3u) == true)
        assert(monster.mutateInventory(3, 4u) == true)
        assert(monster.mutateInventory(4, 5u) == true)

        for (i in 0 until monster.inventoryLength) {
            assert(monster.inventory(i) == (i.toUByte() + 1u).toUByte())
        }

        //reverse mutation
        assert(monster.mutateInventory(0, 0u) == true)
        assert(monster.mutateInventory(1, 1u) == true)
        assert(monster.mutateInventory(2, 2u) == true)
        assert(monster.mutateInventory(3, 3u) == true)
        assert(monster.mutateInventory(4, 4u) == true)

        // get a struct field and edit one of its fields
        val pos = monster.pos!!
        assert(pos.x == 1.0f)
        pos.mutateX(55.0f)
        assert(pos.x == 55.0f)
        pos.mutateX(1.0f)
        assert(pos.x == 1.0f)
    }

    fun TestVectorOfUnions() {
        val fbb = FlatBufferBuilder()

        val swordAttackDamage = 1

        val characterVector = intArrayOf(Attacker.createAttacker(fbb, swordAttackDamage))

        val characterTypeVector = ubyteArrayOf(Character_.MuLan)

        Movie.finishMovieBuffer(
            fbb,
            Movie.createMovie(
                fbb,
                0u,
                0,
                Movie.createCharactersTypeVector(fbb, characterTypeVector),
                Movie.createCharactersVector(fbb, characterVector)
            )
        )

        val movie = Movie.getRootAsMovie(fbb.dataBuffer())

        assert(movie.charactersTypeLength == characterTypeVector.size)
        assert(movie.charactersLength == characterVector.size)

        assert(movie.charactersType(0) == characterTypeVector[0])

        assert((movie.characters(Attacker(), 0) as Attacker).swordAttackDamage == swordAttackDamage)
    }

    fun TestSharedStringPool() {
        val fb = FlatBufferBuilder(1);
        val testString = "My string";
        val offset = fb.createSharedString(testString);
        for (i in 0..10) {
            assert(offset == fb.createSharedString(testString));
        }
    }

    fun TestScalarOptional() {
        val fbb = FlatBufferBuilder(1)
        ScalarStuff.startScalarStuff(fbb)
        var pos = ScalarStuff.endScalarStuff(fbb)
        fbb.finish(pos)

        var scalarStuff = ScalarStuff.getRootAsScalarStuff(fbb.dataBuffer())

        assert(scalarStuff.justI8  == 0.toByte())
        assert(scalarStuff.maybeI8 == null)
        assert(scalarStuff.defaultI8 == 42.toByte())
        assert(scalarStuff.justU8 == 0.toUByte())
        assert(scalarStuff.maybeU8 == null)
        assert(scalarStuff.defaultU8 == 42.toUByte())
        assert(scalarStuff.justI16 == 0.toShort())
        assert(scalarStuff.maybeI16 == null)
        assert(scalarStuff.defaultI16 == 42.toShort())
        assert(scalarStuff.justU16 == 0.toUShort())
        assert(scalarStuff.maybeU16 == null)
        assert(scalarStuff.defaultU16 == 42.toUShort())
        assert(scalarStuff.justI32 == 0)
        assert(scalarStuff.maybeI32 == null)
        assert(scalarStuff.defaultI32 == 42)
        assert(scalarStuff.justU32 == 0.toUInt())
        assert(scalarStuff.maybeU32 == null)
        assert(scalarStuff.defaultU32 == 42U)
        assert(scalarStuff.justI64 == 0L)
        assert(scalarStuff.maybeI64 == null)
        assert(scalarStuff.defaultI64 == 42L)
        assert(scalarStuff.justU64 == 0UL)
        assert(scalarStuff.maybeU64 == null)
        assert(scalarStuff.defaultU64 == 42UL)
        assert(scalarStuff.justF32 == 0.0f)
        assert(scalarStuff.maybeF32 == null)
        assert(scalarStuff.defaultF32 == 42.0f)
        assert(scalarStuff.justF64 == 0.0)
        assert(scalarStuff.maybeF64 == null)
        assert(scalarStuff.defaultF64 == 42.0)
        assert(scalarStuff.justBool == false)
        assert(scalarStuff.maybeBool == null)
        assert(scalarStuff.defaultBool == true)
        assert(scalarStuff.justEnum == OptionalByte.None)
        assert(scalarStuff.maybeEnum == null)
        assert(scalarStuff.defaultEnum == OptionalByte.One)

        fbb.clear()
 
        ScalarStuff.startScalarStuff(fbb)
        ScalarStuff.addJustI8(fbb, 5.toByte())
        ScalarStuff.addMaybeI8(fbb, 5.toByte())
        ScalarStuff.addDefaultI8(fbb, 5.toByte())
        ScalarStuff.addJustU8(fbb, 6.toUByte())
        ScalarStuff.addMaybeU8(fbb, 6.toUByte())
        ScalarStuff.addDefaultU8(fbb, 6.toUByte())
        ScalarStuff.addJustI16(fbb, 7.toShort())
        ScalarStuff.addMaybeI16(fbb, 7.toShort())
        ScalarStuff.addDefaultI16(fbb, 7.toShort())
        ScalarStuff.addJustU16(fbb, 8.toUShort())
        ScalarStuff.addMaybeU16(fbb, 8.toUShort())
        ScalarStuff.addDefaultU16(fbb, 8.toUShort())
        ScalarStuff.addJustI32(fbb, 9)
        ScalarStuff.addMaybeI32(fbb, 9)
        ScalarStuff.addDefaultI32(fbb, 9)
        ScalarStuff.addJustU32(fbb, 10.toUInt())
        ScalarStuff.addMaybeU32(fbb, 10.toUInt())
        ScalarStuff.addDefaultU32(fbb, 10.toUInt())
        ScalarStuff.addJustI64(fbb, 11L)
        ScalarStuff.addMaybeI64(fbb, 11L)
        ScalarStuff.addDefaultI64(fbb, 11L)
        ScalarStuff.addJustU64(fbb, 12UL)
        ScalarStuff.addMaybeU64(fbb, 12UL)
        ScalarStuff.addDefaultU64(fbb, 12UL)
        ScalarStuff.addJustF32(fbb, 13.0f)
        ScalarStuff.addMaybeF32(fbb, 13.0f)
        ScalarStuff.addDefaultF32(fbb, 13.0f)
        ScalarStuff.addJustF64(fbb, 14.0)
        ScalarStuff.addMaybeF64(fbb, 14.0)
        ScalarStuff.addDefaultF64(fbb, 14.0)
        ScalarStuff.addJustBool(fbb, true)
        ScalarStuff.addMaybeBool(fbb, true)
        ScalarStuff.addDefaultBool(fbb, true)
        ScalarStuff.addJustEnum(fbb, OptionalByte.Two)
        ScalarStuff.addMaybeEnum(fbb, OptionalByte.Two)
        ScalarStuff.addDefaultEnum(fbb, OptionalByte.Two)

        pos = ScalarStuff.endScalarStuff(fbb)

        fbb.finish(pos)

        scalarStuff = ScalarStuff.getRootAsScalarStuff(fbb.dataBuffer())

        assert(scalarStuff.justI8  == 5.toByte())
        assert(scalarStuff.maybeI8 == 5.toByte())
        assert(scalarStuff.defaultI8 == 5.toByte())
        assert(scalarStuff.justU8 == 6.toUByte())
        assert(scalarStuff.maybeU8 == 6.toUByte())
        assert(scalarStuff.defaultU8 == 6.toUByte())
        assert(scalarStuff.justI16 == 7.toShort())
        assert(scalarStuff.maybeI16 == 7.toShort())
        assert(scalarStuff.defaultI16 == 7.toShort())
        assert(scalarStuff.justU16 == 8.toUShort())
        assert(scalarStuff.maybeU16 == 8.toUShort())
        assert(scalarStuff.defaultU16 == 8.toUShort())
        assert(scalarStuff.justI32 == 9)
        assert(scalarStuff.maybeI32 == 9)
        assert(scalarStuff.defaultI32 == 9)
        assert(scalarStuff.justU32 == 10u)
        assert(scalarStuff.maybeU32 == 10u)
        assert(scalarStuff.defaultU32 == 10u)
        assert(scalarStuff.justI64 == 11L)
        assert(scalarStuff.maybeI64 == 11L)
        assert(scalarStuff.defaultI64 == 11L)
        assert(scalarStuff.justU64 == 12UL)
        assert(scalarStuff.maybeU64 == 12UL)
        assert(scalarStuff.defaultU64 == 12UL)
        assert(scalarStuff.justF32 == 13.0f)
        assert(scalarStuff.maybeF32 == 13.0f)
        assert(scalarStuff.defaultF32 == 13.0f)
        assert(scalarStuff.justF64 == 14.0)
        assert(scalarStuff.maybeF64 == 14.0)
        assert(scalarStuff.defaultF64 == 14.0)
        assert(scalarStuff.justBool == true)
        assert(scalarStuff.maybeBool == true)
        assert(scalarStuff.defaultBool == true)
        assert(scalarStuff.justEnum == OptionalByte.Two)
        assert(scalarStuff.maybeEnum == OptionalByte.Two)
        assert(scalarStuff.defaultEnum == OptionalByte.Two)
    }
  }
}