...

Text file src/github.com/google/flatbuffers/dart/test/flex_reader_test.dart

Documentation: github.com/google/flatbuffers/dart/test

     1import 'dart:typed_data';
     2
     3import 'package:flat_buffers/flex_buffers.dart' show Reference, Builder;
     4import 'package:test/test.dart';
     5
     6void main() {
     7  test('is null', () {
     8    expect(Reference.fromBuffer(b([0, 0, 1])).isNull, isTrue);
     9  });
    10
    11  test('bool value', () {
    12    expect(Reference.fromBuffer(b([1, 104, 1])).boolValue, isTrue);
    13    expect(Reference.fromBuffer(b([0, 104, 1])).boolValue, isFalse);
    14  });
    15  test('int value', () {
    16    expect(Reference.fromBuffer(b([25, 4, 1])).intValue, 25);
    17    expect(Reference.fromBuffer(b([231, 4, 1])).intValue, -25);
    18    expect(Reference.fromBuffer(b([230, 8, 1])).intValue, 230);
    19    expect(Reference.fromBuffer(b([230, 0, 5, 2])).intValue, 230);
    20    expect(Reference.fromBuffer(b([1, 4, 5, 2])).intValue, 1025);
    21    expect(Reference.fromBuffer(b([255, 251, 5, 2])).intValue, -1025);
    22    expect(Reference.fromBuffer(b([1, 4, 9, 2])).intValue, 1025);
    23    expect(Reference.fromBuffer(b([255, 255, 255, 127, 6, 4])).intValue,
    24        2147483647);
    25    expect(Reference.fromBuffer(b([0, 0, 0, 128, 6, 4])).intValue, -2147483648);
    26    expect(
    27        Reference.fromBuffer(b([255, 255, 255, 255, 0, 0, 0, 0, 7, 8]))
    28            .intValue,
    29        4294967295);
    30    expect(
    31        Reference.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 127, 7, 8]))
    32            .intValue,
    33        9223372036854775807);
    34    expect(Reference.fromBuffer(b([0, 0, 0, 0, 0, 0, 0, 128, 7, 8])).intValue,
    35        -9223372036854775808);
    36    // Dart does not really support UInt64
    37//      expect(FlxValue.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 255, 11, 8])).intValue, 18446744073709551615);
    38  });
    39  test('double value', () {
    40    expect(Reference.fromBuffer(b([0, 0, 128, 63, 14, 4])).doubleValue, 1.0);
    41    expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).doubleValue, 4.5);
    42    expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).doubleValue,
    43        closeTo(.1, .001));
    44    expect(
    45        Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8]))
    46            .doubleValue,
    47        .1);
    48  });
    49  test('num value', () {
    50    expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).numValue, 4.5);
    51    expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).numValue,
    52        closeTo(.1, .001));
    53    expect(
    54        Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8]))
    55            .numValue,
    56        .1);
    57    expect(Reference.fromBuffer(b([255, 251, 5, 2])).numValue, -1025);
    58  });
    59  test('string value', () {
    60    expect(
    61        Reference.fromBuffer(b([5, 77, 97, 120, 105, 109, 0, 6, 20, 1]))
    62            .stringValue,
    63        'Maxim');
    64    expect(
    65        Reference.fromBuffer(b([
    66          10,
    67          104,
    68          101,
    69          108,
    70          108,
    71          111,
    72          32,
    73          240,
    74          159,
    75          152,
    76          177,
    77          0,
    78          11,
    79          20,
    80          1
    81        ])).stringValue,
    82        'hello 😱');
    83  });
    84  test('blob value', () {
    85    expect(
    86        Reference.fromBuffer(b([3, 1, 2, 3, 3, 100, 1])).blobValue, [1, 2, 3]);
    87  });
    88  test('bool vector', () {
    89    var flx = Reference.fromBuffer(b([3, 1, 0, 1, 3, 144, 1]));
    90    expect(flx[0].boolValue, true);
    91    expect(flx[1].boolValue, false);
    92    expect(flx[2].boolValue, true);
    93  });
    94  test('number vector', () {
    95    testNumbers([3, 1, 2, 3, 3, 44, 1], [1, 2, 3]);
    96    testNumbers([3, 255, 2, 3, 3, 44, 1], [-1, 2, 3]);
    97    testNumbers([3, 0, 1, 0, 43, 2, 3, 0, 6, 45, 1], [1, 555, 3]);
    98    testNumbers([3, 0, 0, 0, 1, 0, 0, 0, 204, 216, 0, 0, 3, 0, 0, 0, 12, 46, 1],
    99        [1, 55500, 3]);
   100    testNumbers([
   101      3,
   102      0,
   103      0,
   104      0,
   105      0,
   106      0,
   107      0,
   108      0,
   109      1,
   110      0,
   111      0,
   112      0,
   113      0,
   114      0,
   115      0,
   116      0,
   117      172,
   118      128,
   119      94,
   120      239,
   121      12,
   122      0,
   123      0,
   124      0,
   125      3,
   126      0,
   127      0,
   128      0,
   129      0,
   130      0,
   131      0,
   132      0,
   133      24,
   134      47,
   135      1
   136    ], [
   137      1,
   138      55555555500,
   139      3
   140    ]);
   141    testNumbers(
   142        [3, 0, 0, 0, 0, 0, 192, 63, 0, 0, 32, 64, 0, 0, 96, 64, 12, 54, 1],
   143        [1.5, 2.5, 3.5]);
   144    testNumbers([
   145      3,
   146      0,
   147      0,
   148      0,
   149      0,
   150      0,
   151      0,
   152      0,
   153      154,
   154      153,
   155      153,
   156      153,
   157      153,
   158      153,
   159      241,
   160      63,
   161      154,
   162      153,
   163      153,
   164      153,
   165      153,
   166      153,
   167      1,
   168      64,
   169      102,
   170      102,
   171      102,
   172      102,
   173      102,
   174      102,
   175      10,
   176      64,
   177      24,
   178      55,
   179      1
   180    ], [
   181      1.1,
   182      2.2,
   183      3.3
   184    ]);
   185  });
   186  test('number vector, fixed type', () {
   187    testNumbers([1, 2, 2, 64, 1], [1, 2]);
   188    testNumbers([255, 255, 0, 1, 4, 65, 1], [-1, 256]);
   189    testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1], [-45, 256000]);
   190    testNumbers([
   191      211,
   192      255,
   193      255,
   194      255,
   195      255,
   196      255,
   197      255,
   198      255,
   199      255,
   200      255,
   201      255,
   202      255,
   203      255,
   204      255,
   205      255,
   206      127,
   207      16,
   208      67,
   209      1
   210    ], [
   211      -45,
   212      9223372036854775807
   213    ]);
   214
   215    testNumbers([1, 2, 2, 68, 1], [1, 2]);
   216    testNumbers([1, 0, 0, 1, 4, 69, 1], [1, 256]);
   217    testNumbers([45, 0, 0, 0, 0, 232, 3, 0, 8, 70, 1], [45, 256000]);
   218
   219    testNumbers([205, 204, 140, 63, 0, 0, 0, 192, 8, 74, 1], [1.1, -2]);
   220    testNumbers([
   221      154,
   222      153,
   223      153,
   224      153,
   225      153,
   226      153,
   227      241,
   228      63,
   229      0,
   230      0,
   231      0,
   232      0,
   233      0,
   234      0,
   235      112,
   236      192,
   237      16,
   238      75,
   239      1
   240    ], [
   241      1.1,
   242      -256
   243    ]);
   244
   245    testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 4, 0, 0, 0, 12, 78, 1],
   246        [-45, 256000, 4]);
   247
   248    testNumbers([
   249      211,
   250      255,
   251      255,
   252      255,
   253      255,
   254      255,
   255      255,
   256      255,
   257      255,
   258      255,
   259      255,
   260      255,
   261      255,
   262      255,
   263      255,
   264      127,
   265      4,
   266      0,
   267      0,
   268      0,
   269      0,
   270      0,
   271      0,
   272      0,
   273      9,
   274      0,
   275      0,
   276      0,
   277      0,
   278      0,
   279      0,
   280      0,
   281      32,
   282      91,
   283      1
   284    ], [
   285      -45,
   286      9223372036854775807,
   287      4,
   288      9
   289    ]);
   290
   291    testNumbers([
   292      45,
   293      0,
   294      0,
   295      0,
   296      0,
   297      0,
   298      0,
   299      0,
   300      255,
   301      255,
   302      255,
   303      255,
   304      255,
   305      255,
   306      255,
   307      127,
   308      4,
   309      0,
   310      0,
   311      0,
   312      0,
   313      0,
   314      0,
   315      0,
   316      9,
   317      0,
   318      0,
   319      0,
   320      0,
   321      0,
   322      0,
   323      0,
   324      32,
   325      95,
   326      1
   327    ], [
   328      45,
   329      9223372036854775807,
   330      4,
   331      9
   332    ]);
   333
   334    testNumbers([
   335      154,
   336      153,
   337      153,
   338      153,
   339      153,
   340      153,
   341      241,
   342      63,
   343      0,
   344      0,
   345      0,
   346      0,
   347      0,
   348      0,
   349      112,
   350      64,
   351      0,
   352      0,
   353      0,
   354      0,
   355      0,
   356      0,
   357      16,
   358      64,
   359      24,
   360      87,
   361      1
   362    ], [
   363      1.1,
   364      256,
   365      4
   366    ]);
   367
   368    testNumbers([
   369      154,
   370      153,
   371      153,
   372      153,
   373      153,
   374      153,
   375      241,
   376      63,
   377      0,
   378      0,
   379      0,
   380      0,
   381      0,
   382      0,
   383      112,
   384      64,
   385      0,
   386      0,
   387      0,
   388      0,
   389      0,
   390      0,
   391      16,
   392      64,
   393      0,
   394      0,
   395      0,
   396      0,
   397      0,
   398      0,
   399      34,
   400      64,
   401      32,
   402      99,
   403      1
   404    ], [
   405      1.1,
   406      256,
   407      4,
   408      9
   409    ]);
   410  });
   411  test('string vector', () {
   412    testStrings([
   413      3,
   414      102,
   415      111,
   416      111,
   417      0,
   418      3,
   419      98,
   420      97,
   421      114,
   422      0,
   423      3,
   424      98,
   425      97,
   426      122,
   427      0,
   428      3,
   429      15,
   430      11,
   431      7,
   432      3,
   433      60,
   434      1
   435    ], [
   436      'foo',
   437      'bar',
   438      'baz'
   439    ]);
   440    testStrings([
   441      3,
   442      102,
   443      111,
   444      111,
   445      0,
   446      3,
   447      98,
   448      97,
   449      114,
   450      0,
   451      3,
   452      98,
   453      97,
   454      122,
   455      0,
   456      6,
   457      15,
   458      11,
   459      7,
   460      18,
   461      14,
   462      10,
   463      6,
   464      60,
   465      1
   466    ], [
   467      'foo',
   468      'bar',
   469      'baz',
   470      'foo',
   471      'bar',
   472      'baz'
   473    ]);
   474  });
   475  test('mixed vector', () {
   476    var flx = Reference.fromBuffer(b([
   477      3,
   478      102,
   479      111,
   480      111,
   481      0,
   482      0,
   483      0,
   484      0,
   485      5,
   486      0,
   487      0,
   488      0,
   489      0,
   490      0,
   491      0,
   492      0,
   493      15,
   494      0,
   495      0,
   496      0,
   497      0,
   498      0,
   499      0,
   500      0,
   501      1,
   502      0,
   503      0,
   504      0,
   505      0,
   506      0,
   507      0,
   508      0,
   509      251,
   510      255,
   511      255,
   512      255,
   513      255,
   514      255,
   515      255,
   516      255,
   517      205,
   518      204,
   519      204,
   520      204,
   521      204,
   522      204,
   523      244,
   524      63,
   525      1,
   526      0,
   527      0,
   528      0,
   529      0,
   530      0,
   531      0,
   532      0,
   533      20,
   534      4,
   535      4,
   536      15,
   537      104,
   538      45,
   539      43,
   540      1
   541    ]));
   542    expect(flx.length, 5);
   543    expect(flx[0].stringValue, 'foo');
   544    expect(flx[1].numValue, 1);
   545    expect(flx[2].numValue, -5);
   546    expect(flx[3].numValue, 1.3);
   547    expect(flx[4].boolValue, true);
   548  });
   549
   550  test('single value map', () {
   551    var flx = Reference.fromBuffer(b([97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1]));
   552    expect(flx.length, 1);
   553    expect(flx['a'].numValue, 12);
   554  });
   555  test('two value map', () {
   556    var flx = Reference.fromBuffer(
   557        b([0, 97, 0, 2, 4, 4, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]));
   558    expect(flx.length, 2);
   559    expect(flx['a'].numValue, 12);
   560    expect(flx[''].numValue, 45);
   561  });
   562  test('complex map', () {
   563    var flx = complexMap();
   564    expect(flx.length, 5);
   565    expect(flx['age'].numValue, 35);
   566    expect(flx['weight'].numValue, 72.5);
   567    expect(flx['name'].stringValue, 'Maxim');
   568
   569    expect(flx['flags'].length, 4);
   570    expect(flx['flags'][0].boolValue, true);
   571    expect(flx['flags'][1].boolValue, false);
   572    expect(flx['flags'][2].boolValue, true);
   573    expect(flx['flags'][3].boolValue, true);
   574
   575    expect(flx['address'].length, 3);
   576    expect(flx['address']['city'].stringValue, 'Bla');
   577    expect(flx['address']['zip'].stringValue, '12345');
   578    expect(flx['address']['countryCode'].stringValue, 'XX');
   579
   580    expect(
   581        () => flx['address']['country'].stringValue,
   582        throwsA(predicate((dynamic e) =>
   583            e is ArgumentError &&
   584            e.message ==
   585                'Key: [country] is not applicable on: //address of: ValueType.Map')));
   586    expect(
   587        () => flx['address']['countryCode'][0],
   588        throwsA(predicate((dynamic e) =>
   589            e is ArgumentError &&
   590            e.message ==
   591                'Key: [0] is not applicable on: //address/countryCode of: ValueType.String')));
   592    expect(
   593        () => flx[1],
   594        throwsA(predicate((dynamic e) =>
   595            e is ArgumentError &&
   596            e.message ==
   597                'Key: [1] is not applicable on: / of: ValueType.Map')));
   598    expect(
   599        () => flx['flags'][4],
   600        throwsA(predicate((dynamic e) =>
   601            e is ArgumentError &&
   602            e.message ==
   603                'Key: [4] is not applicable on: //flags of: ValueType.VectorBool length: 4')));
   604    expect(
   605        () => flx['flags'][-1],
   606        throwsA(predicate((dynamic e) =>
   607            e is ArgumentError &&
   608            e.message ==
   609                'Key: [-1] is not applicable on: //flags of: ValueType.VectorBool length: 4')));
   610  });
   611  test('complex map to json', () {
   612    var flx = complexMap();
   613    expect(flx.json,
   614        '{"address":{"city":"Bla","countryCode":"XX","zip":"12345"},"age":35,"flags":[true,false,true,true],"name":"Maxim","weight":72.5}');
   615  });
   616
   617  test('complex map iterators', () {
   618    var flx = complexMap();
   619    expect(flx.mapKeyIterable.map((e) => e).toList(),
   620        ['address', 'age', 'flags', 'name', 'weight']);
   621    expect(flx.mapValueIterable.map((e) => e.json).toList(), [
   622      flx['address'].json,
   623      flx['age'].json,
   624      flx['flags'].json,
   625      flx['name'].json,
   626      flx['weight'].json
   627    ]);
   628    expect(flx['flags'].vectorIterable.map((e) => e.boolValue).toList(),
   629        [true, false, true, true]);
   630  });
   631
   632  test('bug where offest were stored as int instead of uint', () {
   633    const data = [
   634      99,
   635      104,
   636      97,
   637      110,
   638      110,
   639      101,
   640      108,
   641      115,
   642      95,
   643      105,
   644      110,
   645      0,
   646      100,
   647      105,
   648      108,
   649      97,
   650      116,
   651      105,
   652      111,
   653      110,
   654      95,
   655      104,
   656      101,
   657      105,
   658      103,
   659      104,
   660      116,
   661      95,
   662      102,
   663      97,
   664      99,
   665      116,
   666      111,
   667      114,
   668      0,
   669      100,
   670      105,
   671      108,
   672      97,
   673      116,
   674      105,
   675      111,
   676      110,
   677      95,
   678      119,
   679      105,
   680      100,
   681      116,
   682      104,
   683      95,
   684      102,
   685      97,
   686      99,
   687      116,
   688      111,
   689      114,
   690      0,
   691      102,
   692      117,
   693      115,
   694      101,
   695      100,
   696      95,
   697      97,
   698      99,
   699      116,
   700      105,
   701      118,
   702      97,
   703      116,
   704      105,
   705      111,
   706      110,
   707      95,
   708      102,
   709      117,
   710      110,
   711      99,
   712      116,
   713      105,
   714      111,
   715      110,
   716      0,
   717      112,
   718      97,
   719      100,
   720      95,
   721      118,
   722      97,
   723      108,
   724      117,
   725      101,
   726      115,
   727      0,
   728      112,
   729      97,
   730      100,
   731      100,
   732      105,
   733      110,
   734      103,
   735      0,
   736      115,
   737      116,
   738      114,
   739      105,
   740      100,
   741      101,
   742      95,
   743      104,
   744      101,
   745      105,
   746      103,
   747      104,
   748      116,
   749      0,
   750      115,
   751      116,
   752      114,
   753      105,
   754      100,
   755      101,
   756      95,
   757      119,
   758      105,
   759      100,
   760      116,
   761      104,
   762      0,
   763      8,
   764      130,
   765      119,
   766      97,
   767      76,
   768      51,
   769      41,
   770      34,
   771      21,
   772      8,
   773      1,
   774      8,
   775      64,
   776      1,
   777      1,
   778      1,
   779      1,
   780      0,
   781      1,
   782      1,
   783      4,
   784      4,
   785      4,
   786      4,
   787      4,
   788      4,
   789      4,
   790      4,
   791      16,
   792      36,
   793      1
   794    ];
   795    var flx = Reference.fromBuffer(b(data));
   796    expect(flx.json,
   797        '{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}');
   798    const object = {
   799      "channels_in": 64,
   800      "dilation_height_factor": 1,
   801      "dilation_width_factor": 1,
   802      "fused_activation_function": 1,
   803      "pad_values": 1,
   804      "padding": 0,
   805      "stride_height": 1,
   806      "stride_width": 1
   807    };
   808    var data1 = Builder.buildFromObject(object).asUint8List();
   809    expect(data1.length, data.length);
   810    var flx1 = Reference.fromBuffer(b(data1));
   811    expect(flx1.json,
   812        '{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}');
   813  });
   814}
   815
   816ByteBuffer b(List<int> values) {
   817  var data = Uint8List.fromList(values);
   818  return data.buffer;
   819}
   820
   821void testNumbers(List<int> buffer, List<num> numbers) {
   822  var flx = Reference.fromBuffer(b(buffer));
   823  expect(flx.length, numbers.length);
   824  for (var i = 0; i < flx.length; i++) {
   825    expect(flx[i].numValue, closeTo(numbers[i], 0.001));
   826  }
   827}
   828
   829void testStrings(List<int> buffer, List<String> numbers) {
   830  var flx = Reference.fromBuffer(b(buffer));
   831  expect(flx.length, numbers.length);
   832  for (var i = 0; i < flx.length; i++) {
   833    expect(flx[i].stringValue, numbers[i]);
   834  }
   835}
   836
   837Reference complexMap() {
   838//  {
   839//    "age": 35,
   840//    "flags": [True, False, True, True],
   841//    "weight": 72.5,
   842//    "name": "Maxim",
   843//    "address": {
   844//      "city": "Bla",
   845//      "zip": "12345",
   846//      "countryCode": "XX",
   847//    }
   848//  }
   849  return Reference.fromBuffer(b([
   850    97,
   851    100,
   852    100,
   853    114,
   854    101,
   855    115,
   856    115,
   857    0,
   858    99,
   859    105,
   860    116,
   861    121,
   862    0,
   863    3,
   864    66,
   865    108,
   866    97,
   867    0,
   868    99,
   869    111,
   870    117,
   871    110,
   872    116,
   873    114,
   874    121,
   875    67,
   876    111,
   877    100,
   878    101,
   879    0,
   880    2,
   881    88,
   882    88,
   883    0,
   884    122,
   885    105,
   886    112,
   887    0,
   888    5,
   889    49,
   890    50,
   891    51,
   892    52,
   893    53,
   894    0,
   895    3,
   896    38,
   897    29,
   898    14,
   899    3,
   900    1,
   901    3,
   902    38,
   903    22,
   904    15,
   905    20,
   906    20,
   907    20,
   908    97,
   909    103,
   910    101,
   911    0,
   912    102,
   913    108,
   914    97,
   915    103,
   916    115,
   917    0,
   918    4,
   919    1,
   920    0,
   921    1,
   922    1,
   923    110,
   924    97,
   925    109,
   926    101,
   927    0,
   928    5,
   929    77,
   930    97,
   931    120,
   932    105,
   933    109,
   934    0,
   935    119,
   936    101,
   937    105,
   938    103,
   939    104,
   940    116,
   941    0,
   942    5,
   943    93,
   944    36,
   945    33,
   946    23,
   947    12,
   948    0,
   949    0,
   950    7,
   951    0,
   952    0,
   953    0,
   954    1,
   955    0,
   956    0,
   957    0,
   958    5,
   959    0,
   960    0,
   961    0,
   962    60,
   963    0,
   964    0,
   965    0,
   966    35,
   967    0,
   968    0,
   969    0,
   970    51,
   971    0,
   972    0,
   973    0,
   974    45,
   975    0,
   976    0,
   977    0,
   978    0,
   979    0,
   980    145,
   981    66,
   982    36,
   983    4,
   984    144,
   985    20,
   986    14,
   987    25,
   988    38,
   989    1
   990  ]));
   991}

View as plain text