Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | import 'dart:typed_data'; |
| 6 | import 'dart:io' as io; |
| 7 | |
| 8 | import 'package:path/path.dart' as path; |
| 9 | |
| 10 | import 'package:flat_buffers/flat_buffers.dart'; |
| 11 | import 'package:test/test.dart'; |
| 12 | import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 13 | |
| 14 | import './monster_test_my_game.example_generated.dart' as example; |
| 15 | |
| 16 | main() { |
| 17 | defineReflectiveSuite(() { |
| 18 | defineReflectiveTests(BuilderTest); |
| 19 | defineReflectiveTests(CheckOtherLangaugesData); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 20 | defineReflectiveTests(GeneratorTest); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 21 | }); |
| 22 | } |
| 23 | |
| 24 | int indexToField(int index) { |
| 25 | return (1 + 1 + index) * 2; |
| 26 | } |
| 27 | |
| 28 | @reflectiveTest |
| 29 | class CheckOtherLangaugesData { |
| 30 | test_cppData() async { |
| 31 | List<int> data = await new io.File(path.join( |
| 32 | path.dirname(io.Platform.script.path), |
| 33 | 'monsterdata_test.mon', |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 34 | )).readAsBytes(); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 35 | example.Monster mon = new example.Monster(data); |
| 36 | expect(mon.hp, 80); |
| 37 | expect(mon.mana, 150); |
| 38 | expect(mon.name, 'MyMonster'); |
| 39 | expect(mon.pos.x, 1.0); |
| 40 | expect(mon.pos.y, 2.0); |
| 41 | expect(mon.pos.z, 3.0); |
| 42 | expect(mon.pos.test1, 3.0); |
| 43 | expect(mon.pos.test2.value, 2.0); |
| 44 | expect(mon.pos.test3.a, 5); |
| 45 | expect(mon.pos.test3.b, 6); |
| 46 | expect(mon.testType.value, example.AnyTypeId.Monster.value); |
| 47 | expect(mon.test is example.Monster, true); |
| 48 | final monster2 = mon.test as example.Monster; |
| 49 | expect(monster2.name, "Fred"); |
| 50 | |
| 51 | expect(mon.inventory.length, 5); |
| 52 | expect(mon.inventory.reduce((cur, next) => cur + next), 10); |
| 53 | expect(mon.test4.length, 2); |
| 54 | expect( |
| 55 | mon.test4[0].a + mon.test4[0].b + mon.test4[1].a + mon.test4[1].b, 100); |
| 56 | expect(mon.testarrayofstring.length, 2); |
| 57 | expect(mon.testarrayofstring[0], "test1"); |
| 58 | expect(mon.testarrayofstring[1], "test2"); |
| 59 | |
| 60 | // this will fail if accessing any field fails. |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 61 | expect( |
| 62 | mon.toString(), |
| 63 | 'Monster{' |
| 64 | 'pos: Vec3{x: 1.0, y: 2.0, z: 3.0, test1: 3.0, test2: Color{value: 2}, test3: Test{a: 5, b: 6}}, ' |
| 65 | 'mana: 150, hp: 80, name: MyMonster, inventory: [0, 1, 2, 3, 4], ' |
| 66 | 'color: Color{value: 8}, testType: AnyTypeId{value: 1}, ' |
| 67 | 'test: Monster{pos: null, mana: 150, hp: 100, name: Fred, ' |
| 68 | 'inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, ' |
| 69 | 'test: null, test4: null, testarrayofstring: null, ' |
| 70 | 'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, ' |
| 71 | 'testempty: null, testbool: false, testhashs32Fnv1: 0, ' |
| 72 | 'testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, ' |
| 73 | 'testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, ' |
| 74 | 'testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, ' |
| 75 | 'testf2: 3.0, testf3: 0.0, testarrayofstring2: null, ' |
| 76 | 'testarrayofsortedstruct: null, flex: null, test5: null, ' |
| 77 | 'vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, ' |
| 78 | 'vectorOfReferrables: null, singleWeakReference: 0, ' |
| 79 | 'vectorOfWeakReferences: null, vectorOfStrongReferrables: null, ' |
| 80 | 'coOwningReference: 0, vectorOfCoOwningReferences: null, ' |
| 81 | 'nonOwningReference: 0, vectorOfNonOwningReferences: null, ' |
| 82 | 'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, ' |
| 83 | 'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, ' |
| 84 | 'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}}, ' |
| 85 | 'test4: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], ' |
| 86 | 'testarrayofstring: [test1, test2], testarrayoftables: null, ' |
| 87 | 'enemy: Monster{pos: null, mana: 150, hp: 100, name: Fred, ' |
| 88 | 'inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, ' |
| 89 | 'test: null, test4: null, testarrayofstring: null, ' |
| 90 | 'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, ' |
| 91 | 'testempty: null, testbool: false, testhashs32Fnv1: 0, ' |
| 92 | 'testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, ' |
| 93 | 'testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, ' |
| 94 | 'testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, ' |
| 95 | 'testf2: 3.0, testf3: 0.0, testarrayofstring2: null, ' |
| 96 | 'testarrayofsortedstruct: null, flex: null, test5: null, ' |
| 97 | 'vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, ' |
| 98 | 'vectorOfReferrables: null, singleWeakReference: 0, ' |
| 99 | 'vectorOfWeakReferences: null, vectorOfStrongReferrables: null, ' |
| 100 | 'coOwningReference: 0, vectorOfCoOwningReferences: null, ' |
| 101 | 'nonOwningReference: 0, vectorOfNonOwningReferences: null, ' |
| 102 | 'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, ' |
| 103 | 'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, ' |
| 104 | 'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}}, ' |
| 105 | 'testnestedflatbuffer: null, testempty: null, testbool: true, ' |
| 106 | 'testhashs32Fnv1: -579221183, testhashu32Fnv1: 3715746113, ' |
| 107 | 'testhashs64Fnv1: 7930699090847568257, ' |
| 108 | 'testhashu64Fnv1: 7930699090847568257, ' |
| 109 | 'testhashs32Fnv1a: -1904106383, testhashu32Fnv1a: 2390860913, ' |
| 110 | 'testhashs64Fnv1a: 4898026182817603057, ' |
| 111 | 'testhashu64Fnv1a: 4898026182817603057, ' |
| 112 | 'testarrayofbools: [true, false, true], testf: 3.14159, testf2: 3.0, ' |
| 113 | 'testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: null, ' |
| 114 | 'flex: null, test5: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], ' |
| 115 | 'vectorOfLongs: [1, 100, 10000, 1000000, 100000000], ' |
| 116 | 'vectorOfDoubles: [-1.7976931348623157e+308, 0.0, 1.7976931348623157e+308], ' |
| 117 | 'parentNamespaceTest: null, vectorOfReferrables: null, ' |
| 118 | 'singleWeakReference: 0, vectorOfWeakReferences: null, ' |
| 119 | 'vectorOfStrongReferrables: null, coOwningReference: 0, ' |
| 120 | 'vectorOfCoOwningReferences: null, nonOwningReference: 0, ' |
| 121 | 'vectorOfNonOwningReferences: null, ' |
| 122 | 'anyUniqueType: AnyUniqueAliasesTypeId{value: 0}, anyUnique: null, ' |
| 123 | 'anyAmbiguousType: AnyAmbiguousAliasesTypeId{value: 0}, ' |
| 124 | 'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}}', |
| 125 | ); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | @reflectiveTest |
| 130 | class BuilderTest { |
| 131 | void test_monsterBuilder() { |
| 132 | final fbBuilder = new Builder(); |
| 133 | final str = fbBuilder.writeString('MyMonster'); |
| 134 | |
| 135 | fbBuilder.writeString('test1'); |
| 136 | fbBuilder.writeString('test2'); |
| 137 | final testArrayOfString = fbBuilder.endStructVector(2); |
| 138 | |
| 139 | final fred = fbBuilder.writeString('Fred'); |
| 140 | |
| 141 | final List<int> treasure = [0, 1, 2, 3, 4]; |
| 142 | final inventory = fbBuilder.writeListUint8(treasure); |
| 143 | |
| 144 | final monBuilder = new example.MonsterBuilder(fbBuilder) |
| 145 | ..begin() |
| 146 | ..addNameOffset(fred); |
| 147 | final mon2 = monBuilder.finish(); |
| 148 | |
| 149 | final testBuilder = new example.TestBuilder(fbBuilder); |
| 150 | testBuilder.finish(10, 20); |
| 151 | testBuilder.finish(30, 40); |
| 152 | final test4 = fbBuilder.endStructVector(2); |
| 153 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 154 | monBuilder |
| 155 | ..begin() |
| 156 | ..addPos( |
| 157 | new example.Vec3Builder(fbBuilder).finish( |
| 158 | 1.0, |
| 159 | 2.0, |
| 160 | 3.0, |
| 161 | 3.0, |
| 162 | example.Color.Green, |
| 163 | () => testBuilder.finish(5, 6), |
| 164 | ), |
| 165 | ) |
| 166 | ..addHp(80) |
| 167 | ..addNameOffset(str) |
| 168 | ..addInventoryOffset(inventory) |
| 169 | ..addTestType(example.AnyTypeId.Monster) |
| 170 | ..addTestOffset(mon2) |
| 171 | ..addTest4Offset(test4) |
| 172 | ..addTestarrayofstringOffset(testArrayOfString); |
| 173 | final mon = monBuilder.finish(); |
| 174 | fbBuilder.finish(mon); |
| 175 | } |
| 176 | |
| 177 | void test_error_addInt32_withoutStartTable() { |
| 178 | Builder builder = new Builder(); |
| 179 | expect(() { |
| 180 | builder.addInt32(0, 0); |
| 181 | }, throwsStateError); |
| 182 | } |
| 183 | |
| 184 | void test_error_addOffset_withoutStartTable() { |
| 185 | Builder builder = new Builder(); |
| 186 | expect(() { |
| 187 | builder.addOffset(0, 0); |
| 188 | }, throwsStateError); |
| 189 | } |
| 190 | |
| 191 | void test_error_endTable_withoutStartTable() { |
| 192 | Builder builder = new Builder(); |
| 193 | expect(() { |
| 194 | builder.endTable(); |
| 195 | }, throwsStateError); |
| 196 | } |
| 197 | |
| 198 | void test_error_startTable_duringTable() { |
| 199 | Builder builder = new Builder(); |
| 200 | builder.startTable(); |
| 201 | expect(() { |
| 202 | builder.startTable(); |
| 203 | }, throwsStateError); |
| 204 | } |
| 205 | |
| 206 | void test_error_writeString_duringTable() { |
| 207 | Builder builder = new Builder(); |
| 208 | builder.startTable(); |
| 209 | expect(() { |
| 210 | builder.writeString('12345'); |
| 211 | }, throwsStateError); |
| 212 | } |
| 213 | |
| 214 | void test_file_identifier() { |
| 215 | Uint8List byteList; |
| 216 | { |
| 217 | Builder builder = new Builder(initialSize: 0); |
| 218 | builder.startTable(); |
| 219 | int offset = builder.endTable(); |
| 220 | byteList = builder.finish(offset, 'Az~ÿ'); |
| 221 | } |
| 222 | // Convert byteList to a ByteData so that we can read data from it. |
| 223 | ByteData byteData = byteList.buffer.asByteData(byteList.offsetInBytes); |
| 224 | // First 4 bytes are an offset to the table data. |
| 225 | int tableDataLoc = byteData.getUint32(0, Endian.little); |
| 226 | // Next 4 bytes are the file identifier. |
| 227 | expect(byteData.getUint8(4), 65); // 'a' |
| 228 | expect(byteData.getUint8(5), 122); // 'z' |
| 229 | expect(byteData.getUint8(6), 126); // '~' |
| 230 | expect(byteData.getUint8(7), 255); // 'ÿ' |
| 231 | // First 4 bytes of the table data are a backwards offset to the vtable. |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 232 | int vTableLoc = |
| 233 | tableDataLoc - byteData.getInt32(tableDataLoc, Endian.little); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 234 | // First 2 bytes of the vtable are the size of the vtable in bytes, which |
| 235 | // should be 4. |
| 236 | expect(byteData.getUint16(vTableLoc, Endian.little), 4); |
| 237 | // Next 2 bytes are the size of the object in bytes (including the vtable |
| 238 | // pointer), which should be 4. |
| 239 | expect(byteData.getUint16(vTableLoc + 2, Endian.little), 4); |
| 240 | } |
| 241 | |
| 242 | void test_low() { |
| 243 | Builder builder = new Builder(initialSize: 0); |
| 244 | expect((builder..putUint8(1)).lowFinish(), [1]); |
| 245 | expect((builder..putUint32(2)).lowFinish(), [2, 0, 0, 0, 0, 0, 0, 1]); |
| 246 | expect((builder..putUint8(3)).lowFinish(), |
| 247 | [0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 1]); |
| 248 | expect((builder..putUint8(4)).lowFinish(), |
| 249 | [0, 0, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]); |
| 250 | expect((builder..putUint8(5)).lowFinish(), |
| 251 | [0, 5, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]); |
| 252 | expect((builder..putUint32(6)).lowFinish(), |
| 253 | [6, 0, 0, 0, 0, 5, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]); |
| 254 | } |
| 255 | |
| 256 | void test_table_default() { |
| 257 | List<int> byteList; |
| 258 | { |
| 259 | Builder builder = new Builder(initialSize: 0); |
| 260 | builder.startTable(); |
| 261 | builder.addInt32(0, 10, 10); |
| 262 | builder.addInt32(1, 20, 10); |
| 263 | int offset = builder.endTable(); |
| 264 | byteList = builder.finish(offset); |
| 265 | } |
| 266 | // read and verify |
| 267 | BufferContext buffer = new BufferContext.fromBytes(byteList); |
| 268 | int objectOffset = buffer.derefObject(0); |
| 269 | // was not written, so uses the new default value |
| 270 | expect( |
| 271 | const Int32Reader() |
| 272 | .vTableGet(buffer, objectOffset, indexToField(0), 15), |
| 273 | 15); |
| 274 | // has the written value |
| 275 | expect( |
| 276 | const Int32Reader() |
| 277 | .vTableGet(buffer, objectOffset, indexToField(1), 15), |
| 278 | 20); |
| 279 | } |
| 280 | |
| 281 | void test_table_format() { |
| 282 | Uint8List byteList; |
| 283 | { |
| 284 | Builder builder = new Builder(initialSize: 0); |
| 285 | builder.startTable(); |
| 286 | builder.addInt32(0, 10); |
| 287 | builder.addInt32(1, 20); |
| 288 | builder.addInt32(2, 30); |
| 289 | byteList = builder.finish(builder.endTable()); |
| 290 | } |
| 291 | // Convert byteList to a ByteData so that we can read data from it. |
| 292 | ByteData byteData = byteList.buffer.asByteData(byteList.offsetInBytes); |
| 293 | // First 4 bytes are an offset to the table data. |
| 294 | int tableDataLoc = byteData.getUint32(0, Endian.little); |
| 295 | // First 4 bytes of the table data are a backwards offset to the vtable. |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 296 | int vTableLoc = |
| 297 | tableDataLoc - byteData.getInt32(tableDataLoc, Endian.little); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 298 | // First 2 bytes of the vtable are the size of the vtable in bytes, which |
| 299 | // should be 10. |
| 300 | expect(byteData.getUint16(vTableLoc, Endian.little), 10); |
| 301 | // Next 2 bytes are the size of the object in bytes (including the vtable |
| 302 | // pointer), which should be 16. |
| 303 | expect(byteData.getUint16(vTableLoc + 2, Endian.little), 16); |
| 304 | // Remaining 6 bytes are the offsets within the object where the ints are |
| 305 | // located. |
| 306 | for (int i = 0; i < 3; i++) { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 307 | int offset = byteData.getUint16(vTableLoc + 4 + 2 * i, Endian.little); |
| 308 | expect( |
| 309 | byteData.getInt32(tableDataLoc + offset, Endian.little), 10 + 10 * i); |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | void test_table_string() { |
| 314 | String latinString = 'test'; |
| 315 | String unicodeString = 'Проба пера'; |
| 316 | List<int> byteList; |
| 317 | { |
| 318 | Builder builder = new Builder(initialSize: 0); |
| 319 | int latinStringOffset = builder.writeString(latinString); |
| 320 | int unicodeStringOffset = builder.writeString(unicodeString); |
| 321 | builder.startTable(); |
| 322 | builder.addOffset(0, latinStringOffset); |
| 323 | builder.addOffset(1, unicodeStringOffset); |
| 324 | int offset = builder.endTable(); |
| 325 | byteList = builder.finish(offset); |
| 326 | } |
| 327 | // read and verify |
| 328 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 329 | int objectOffset = buf.derefObject(0); |
| 330 | expect(const StringReader().vTableGet(buf, objectOffset, indexToField(0)), |
| 331 | latinString); |
| 332 | expect(const StringReader().vTableGet(buf, objectOffset, indexToField(1)), |
| 333 | unicodeString); |
| 334 | } |
| 335 | |
| 336 | void test_table_types() { |
| 337 | List<int> byteList; |
| 338 | { |
| 339 | Builder builder = new Builder(initialSize: 0); |
| 340 | int stringOffset = builder.writeString('12345'); |
| 341 | builder.startTable(); |
| 342 | builder.addBool(0, true); |
| 343 | builder.addInt8(1, 10); |
| 344 | builder.addInt32(2, 20); |
| 345 | builder.addOffset(3, stringOffset); |
| 346 | builder.addInt32(4, 40); |
| 347 | builder.addUint32(5, 0x9ABCDEF0); |
| 348 | builder.addUint8(6, 0x9A); |
| 349 | int offset = builder.endTable(); |
| 350 | byteList = builder.finish(offset); |
| 351 | } |
| 352 | // read and verify |
| 353 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 354 | int objectOffset = buf.derefObject(0); |
| 355 | expect( |
| 356 | const BoolReader().vTableGet(buf, objectOffset, indexToField(0)), true); |
| 357 | expect( |
| 358 | const Int8Reader().vTableGet(buf, objectOffset, indexToField(1)), 10); |
| 359 | expect( |
| 360 | const Int32Reader().vTableGet(buf, objectOffset, indexToField(2)), 20); |
| 361 | expect(const StringReader().vTableGet(buf, objectOffset, indexToField(3)), |
| 362 | '12345'); |
| 363 | expect( |
| 364 | const Int32Reader().vTableGet(buf, objectOffset, indexToField(4)), 40); |
| 365 | expect(const Uint32Reader().vTableGet(buf, objectOffset, indexToField(5)), |
| 366 | 0x9ABCDEF0); |
| 367 | expect(const Uint8Reader().vTableGet(buf, objectOffset, indexToField(6)), |
| 368 | 0x9A); |
| 369 | } |
| 370 | |
| 371 | void test_writeList_of_Uint32() { |
| 372 | List<int> values = <int>[10, 100, 12345, 0x9abcdef0]; |
| 373 | // write |
| 374 | List<int> byteList; |
| 375 | { |
| 376 | Builder builder = new Builder(initialSize: 0); |
| 377 | int offset = builder.writeListUint32(values); |
| 378 | byteList = builder.finish(offset); |
| 379 | } |
| 380 | // read and verify |
| 381 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 382 | List<int> items = const Uint32ListReader().read(buf, 0); |
| 383 | expect(items, hasLength(4)); |
| 384 | expect(items, orderedEquals(values)); |
| 385 | } |
| 386 | |
| 387 | void test_writeList_ofBool() { |
| 388 | void verifyListBooleans(int len, List<int> trueBits) { |
| 389 | // write |
| 390 | List<int> byteList; |
| 391 | { |
| 392 | Builder builder = new Builder(initialSize: 0); |
| 393 | List<bool> values = new List<bool>.filled(len, false); |
| 394 | for (int bit in trueBits) { |
| 395 | values[bit] = true; |
| 396 | } |
| 397 | int offset = builder.writeListBool(values); |
| 398 | byteList = builder.finish(offset); |
| 399 | } |
| 400 | // read and verify |
| 401 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 402 | List<bool> items = const BoolListReader().read(buf, 0); |
| 403 | expect(items, hasLength(len)); |
| 404 | for (int i = 0; i < items.length; i++) { |
| 405 | expect(items[i], trueBits.contains(i), reason: 'bit $i of $len'); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | verifyListBooleans(0, <int>[]); |
| 410 | verifyListBooleans(1, <int>[]); |
| 411 | verifyListBooleans(1, <int>[0]); |
| 412 | verifyListBooleans(31, <int>[0, 1]); |
| 413 | verifyListBooleans(31, <int>[1, 2, 24, 25, 30]); |
| 414 | verifyListBooleans(31, <int>[0, 30]); |
| 415 | verifyListBooleans(32, <int>[1, 2, 24, 25, 31]); |
| 416 | verifyListBooleans(33, <int>[1, 2, 24, 25, 32]); |
| 417 | verifyListBooleans(33, <int>[1, 2, 24, 25, 31, 32]); |
| 418 | verifyListBooleans(63, <int>[]); |
| 419 | verifyListBooleans(63, <int>[0, 1, 2, 61, 62]); |
| 420 | verifyListBooleans(63, new List<int>.generate(63, (i) => i)); |
| 421 | verifyListBooleans(64, <int>[]); |
| 422 | verifyListBooleans(64, <int>[0, 1, 2, 61, 62, 63]); |
| 423 | verifyListBooleans(64, <int>[1, 2, 62]); |
| 424 | verifyListBooleans(64, <int>[0, 1, 2, 63]); |
| 425 | verifyListBooleans(64, new List<int>.generate(64, (i) => i)); |
| 426 | verifyListBooleans(100, <int>[0, 3, 30, 60, 90, 99]); |
| 427 | } |
| 428 | |
| 429 | void test_writeList_ofInt32() { |
| 430 | List<int> byteList; |
| 431 | { |
| 432 | Builder builder = new Builder(initialSize: 0); |
| 433 | int offset = builder.writeListInt32(<int>[1, 2, 3, 4, 5]); |
| 434 | byteList = builder.finish(offset); |
| 435 | } |
| 436 | // read and verify |
| 437 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 438 | List<int> items = const ListReader<int>(const Int32Reader()).read(buf, 0); |
| 439 | expect(items, hasLength(5)); |
| 440 | expect(items, orderedEquals(<int>[1, 2, 3, 4, 5])); |
| 441 | } |
| 442 | |
| 443 | void test_writeList_ofFloat64() { |
| 444 | List<double> values = <double>[-1.234567, 3.4E+9, -5.6E-13, 7.8, 12.13]; |
| 445 | // write |
| 446 | List<int> byteList; |
| 447 | { |
| 448 | Builder builder = new Builder(initialSize: 0); |
| 449 | int offset = builder.writeListFloat64(values); |
| 450 | byteList = builder.finish(offset); |
| 451 | } |
| 452 | |
| 453 | // read and verify |
| 454 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 455 | List<double> items = const Float64ListReader().read(buf, 0); |
| 456 | |
| 457 | expect(items, hasLength(values.length)); |
| 458 | for (int i = 0; i < values.length; i++) { |
| 459 | expect(values[i], closeTo(items[i], .001)); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void test_writeList_ofFloat32() { |
| 464 | List<double> values = [1.0, 2.23, -3.213, 7.8, 12.13]; |
| 465 | // write |
| 466 | List<int> byteList; |
| 467 | { |
| 468 | Builder builder = new Builder(initialSize: 0); |
| 469 | int offset = builder.writeListFloat32(values); |
| 470 | byteList = builder.finish(offset); |
| 471 | } |
| 472 | // read and verify |
| 473 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 474 | List<double> items = const Float32ListReader().read(buf, 0); |
| 475 | expect(items, hasLength(5)); |
| 476 | for (int i = 0; i < values.length; i++) { |
| 477 | expect(values[i], closeTo(items[i], .001)); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void test_writeList_ofObjects() { |
| 482 | List<int> byteList; |
| 483 | { |
| 484 | Builder builder = new Builder(initialSize: 0); |
| 485 | // write the object #1 |
| 486 | int object1; |
| 487 | { |
| 488 | builder.startTable(); |
| 489 | builder.addInt32(0, 10); |
| 490 | builder.addInt32(1, 20); |
| 491 | object1 = builder.endTable(); |
| 492 | } |
| 493 | // write the object #1 |
| 494 | int object2; |
| 495 | { |
| 496 | builder.startTable(); |
| 497 | builder.addInt32(0, 100); |
| 498 | builder.addInt32(1, 200); |
| 499 | object2 = builder.endTable(); |
| 500 | } |
| 501 | // write the list |
| 502 | int offset = builder.writeList([object1, object2]); |
| 503 | byteList = builder.finish(offset); |
| 504 | } |
| 505 | // read and verify |
| 506 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 507 | List<TestPointImpl> items = |
| 508 | const ListReader<TestPointImpl>(const TestPointReader()).read(buf, 0); |
| 509 | expect(items, hasLength(2)); |
| 510 | expect(items[0].x, 10); |
| 511 | expect(items[0].y, 20); |
| 512 | expect(items[1].x, 100); |
| 513 | expect(items[1].y, 200); |
| 514 | } |
| 515 | |
| 516 | void test_writeList_ofStrings_asRoot() { |
| 517 | List<int> byteList; |
| 518 | { |
| 519 | Builder builder = new Builder(initialSize: 0); |
| 520 | int str1 = builder.writeString('12345'); |
| 521 | int str2 = builder.writeString('ABC'); |
| 522 | int offset = builder.writeList([str1, str2]); |
| 523 | byteList = builder.finish(offset); |
| 524 | } |
| 525 | // read and verify |
| 526 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 527 | List<String> items = |
| 528 | const ListReader<String>(const StringReader()).read(buf, 0); |
| 529 | expect(items, hasLength(2)); |
| 530 | expect(items, contains('12345')); |
| 531 | expect(items, contains('ABC')); |
| 532 | } |
| 533 | |
| 534 | void test_writeList_ofStrings_inObject() { |
| 535 | List<int> byteList; |
| 536 | { |
| 537 | Builder builder = new Builder(initialSize: 0); |
| 538 | int listOffset = builder.writeList( |
| 539 | [builder.writeString('12345'), builder.writeString('ABC')]); |
| 540 | builder.startTable(); |
| 541 | builder.addOffset(0, listOffset); |
| 542 | int offset = builder.endTable(); |
| 543 | byteList = builder.finish(offset); |
| 544 | } |
| 545 | // read and verify |
| 546 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 547 | StringListWrapperImpl reader = new StringListWrapperReader().read(buf, 0); |
| 548 | List<String> items = reader.items; |
| 549 | expect(items, hasLength(2)); |
| 550 | expect(items, contains('12345')); |
| 551 | expect(items, contains('ABC')); |
| 552 | } |
| 553 | |
| 554 | void test_writeList_ofUint32() { |
| 555 | List<int> byteList; |
| 556 | { |
| 557 | Builder builder = new Builder(initialSize: 0); |
| 558 | int offset = builder.writeListUint32(<int>[1, 2, 0x9ABCDEF0]); |
| 559 | byteList = builder.finish(offset); |
| 560 | } |
| 561 | // read and verify |
| 562 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 563 | List<int> items = const Uint32ListReader().read(buf, 0); |
| 564 | expect(items, hasLength(3)); |
| 565 | expect(items, orderedEquals(<int>[1, 2, 0x9ABCDEF0])); |
| 566 | } |
| 567 | |
| 568 | void test_writeList_ofUint16() { |
| 569 | List<int> byteList; |
| 570 | { |
| 571 | Builder builder = new Builder(initialSize: 0); |
| 572 | int offset = builder.writeListUint16(<int>[1, 2, 60000]); |
| 573 | byteList = builder.finish(offset); |
| 574 | } |
| 575 | // read and verify |
| 576 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 577 | List<int> items = const Uint16ListReader().read(buf, 0); |
| 578 | expect(items, hasLength(3)); |
| 579 | expect(items, orderedEquals(<int>[1, 2, 60000])); |
| 580 | } |
| 581 | |
| 582 | void test_writeList_ofUint8() { |
| 583 | List<int> byteList; |
| 584 | { |
| 585 | Builder builder = new Builder(initialSize: 0); |
| 586 | int offset = builder.writeListUint8(<int>[1, 2, 3, 4, 0x9A]); |
| 587 | byteList = builder.finish(offset); |
| 588 | } |
| 589 | // read and verify |
| 590 | BufferContext buf = new BufferContext.fromBytes(byteList); |
| 591 | List<int> items = const Uint8ListReader().read(buf, 0); |
| 592 | expect(items, hasLength(5)); |
| 593 | expect(items, orderedEquals(<int>[1, 2, 3, 4, 0x9A])); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | class StringListWrapperImpl { |
| 598 | final BufferContext bp; |
| 599 | final int offset; |
| 600 | |
| 601 | StringListWrapperImpl(this.bp, this.offset); |
| 602 | |
| 603 | List<String> get items => const ListReader<String>(const StringReader()) |
| 604 | .vTableGet(bp, offset, indexToField(0)); |
| 605 | } |
| 606 | |
| 607 | class StringListWrapperReader extends TableReader<StringListWrapperImpl> { |
| 608 | const StringListWrapperReader(); |
| 609 | |
| 610 | @override |
| 611 | StringListWrapperImpl createObject(BufferContext object, int offset) { |
| 612 | return new StringListWrapperImpl(object, offset); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | class TestPointImpl { |
| 617 | final BufferContext bp; |
| 618 | final int offset; |
| 619 | |
| 620 | TestPointImpl(this.bp, this.offset); |
| 621 | |
| 622 | int get x => const Int32Reader().vTableGet(bp, offset, indexToField(0), 0); |
| 623 | |
| 624 | int get y => const Int32Reader().vTableGet(bp, offset, indexToField(1), 0); |
| 625 | } |
| 626 | |
| 627 | class TestPointReader extends TableReader<TestPointImpl> { |
| 628 | const TestPointReader(); |
| 629 | |
| 630 | @override |
| 631 | TestPointImpl createObject(BufferContext object, int offset) { |
| 632 | return new TestPointImpl(object, offset); |
| 633 | } |
| 634 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 635 | |
| 636 | @reflectiveTest |
| 637 | class GeneratorTest { |
| 638 | void test_constantEnumValues() async { |
| 639 | expect(example.Color.values, same(example.Color.values)); |
| 640 | expect(example.Race.values, same(example.Race.values)); |
| 641 | expect(example.AnyTypeId.values, same(example.AnyTypeId.values)); |
| 642 | expect(example.AnyUniqueAliasesTypeId.values, same(example.AnyUniqueAliasesTypeId.values)); |
| 643 | expect(example.AnyAmbiguousAliasesTypeId.values, same(example.AnyAmbiguousAliasesTypeId.values)); |
| 644 | } |
| 645 | } |