...

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

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

     1import 'dart:typed_data';
     2
     3import 'package:flat_buffers/flex_buffers.dart' show Builder;
     4import 'package:test/test.dart';
     5
     6void main() {
     7  test('build with single value', () {
     8    {
     9      var flx = Builder();
    10      flx.addNull();
    11      expect(flx.finish(), [0, 0, 1]);
    12    }
    13    {
    14      var flx = Builder();
    15      flx.addBool(true);
    16      expect(flx.finish(), [1, 104, 1]);
    17    }
    18    {
    19      var flx = Builder();
    20      flx.addBool(false);
    21      expect(flx.finish(), [0, 104, 1]);
    22    }
    23    {
    24      var flx = Builder();
    25      flx.addInt(1);
    26      expect(flx.finish(), [1, 4, 1]);
    27    }
    28    {
    29      var flx = Builder();
    30      flx.addInt(230);
    31      expect(flx.finish(), [230, 0, 5, 2]);
    32    }
    33    {
    34      var flx = Builder();
    35      flx.addInt(1025);
    36      expect(flx.finish(), [1, 4, 5, 2]);
    37    }
    38    {
    39      var flx = Builder();
    40      flx.addInt(-1025);
    41      expect(flx.finish(), [255, 251, 5, 2]);
    42    }
    43    {
    44      var builder = Builder()..addDouble(1.0);
    45      expect(builder.finish(), [0, 0, 128, 63, 14, 4]);
    46    }
    47    {
    48      var flx = Builder();
    49      flx.addDouble(0.1);
    50      expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 15, 8]);
    51    }
    52    {
    53      var flx = Builder();
    54      flx.addDouble(0.5);
    55      expect(flx.finish(), [0, 0, 0, 63, 14, 4]);
    56    }
    57    {
    58      var flx = Builder();
    59      flx.addString('Maxim');
    60      expect(flx.finish(), [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]);
    61    }
    62    {
    63      var flx = Builder();
    64      flx.addString('hello 😱');
    65      expect(flx.finish(),
    66          [10, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1]);
    67    }
    68  });
    69
    70  test('build vector', () {
    71    {
    72      var flx = Builder()
    73        ..startVector()
    74        ..addInt(1)
    75        ..addInt(2)
    76        ..end();
    77      expect(flx.finish(), [1, 2, 2, 64, 1]);
    78    }
    79    {
    80      var flx = Builder()
    81        ..startVector()
    82        ..addInt(-1)
    83        ..addInt(256)
    84        ..end();
    85      expect(flx.finish(), [255, 255, 0, 1, 4, 65, 1]);
    86    }
    87    {
    88      var flx = Builder()
    89        ..startVector()
    90        ..addInt(-45)
    91        ..addInt(256000)
    92        ..end();
    93      expect(flx.finish(), [211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1]);
    94    }
    95    {
    96      var flx = Builder()
    97        ..startVector()
    98        ..addDouble(1.1)
    99        ..addDouble(-256)
   100        ..end();
   101      expect(flx.finish(), [
   102        154,
   103        153,
   104        153,
   105        153,
   106        153,
   107        153,
   108        241,
   109        63,
   110        0,
   111        0,
   112        0,
   113        0,
   114        0,
   115        0,
   116        112,
   117        192,
   118        16,
   119        75,
   120        1
   121      ]);
   122    }
   123    {
   124      var flx = Builder()
   125        ..startVector()
   126        ..addInt(1)
   127        ..addInt(2)
   128        ..addInt(4)
   129        ..end();
   130      expect(flx.finish(), [1, 2, 4, 3, 76, 1]);
   131    }
   132    {
   133      var flx = Builder()
   134        ..startVector()
   135        ..addInt(-1)
   136        ..addInt(256)
   137        ..addInt(4)
   138        ..end();
   139      expect(flx.finish(), [255, 255, 0, 1, 4, 0, 6, 77, 1]);
   140    }
   141    {
   142      var flx = Builder()
   143        ..startVector()
   144        ..startVector()
   145        ..addInt(61)
   146        ..end()
   147        ..addInt(64)
   148        ..end();
   149      expect(flx.finish(), [1, 61, 2, 2, 64, 44, 4, 4, 40, 1]);
   150    }
   151    {
   152      var flx = Builder()
   153        ..startVector()
   154        ..addString('foo')
   155        ..addString('bar')
   156        ..addString('baz')
   157        ..end();
   158      expect(flx.finish(), [
   159        3,
   160        102,
   161        111,
   162        111,
   163        0,
   164        3,
   165        98,
   166        97,
   167        114,
   168        0,
   169        3,
   170        98,
   171        97,
   172        122,
   173        0,
   174        3,
   175        15,
   176        11,
   177        7,
   178        3,
   179        60,
   180        1
   181      ]);
   182    }
   183    {
   184      var flx = Builder()
   185        ..startVector()
   186        ..addString('foo')
   187        ..addString('bar')
   188        ..addString('baz')
   189        ..addString('foo')
   190        ..addString('bar')
   191        ..addString('baz')
   192        ..end();
   193      expect(flx.finish(), [
   194        3,
   195        102,
   196        111,
   197        111,
   198        0,
   199        3,
   200        98,
   201        97,
   202        114,
   203        0,
   204        3,
   205        98,
   206        97,
   207        122,
   208        0,
   209        6,
   210        15,
   211        11,
   212        7,
   213        18,
   214        14,
   215        10,
   216        6,
   217        60,
   218        1
   219      ]);
   220    }
   221    {
   222      var flx = Builder()
   223        ..startVector()
   224        ..addBool(true)
   225        ..addBool(false)
   226        ..addBool(true)
   227        ..end();
   228      expect(flx.finish(), [3, 1, 0, 1, 3, 144, 1]);
   229    }
   230    {
   231      var flx = Builder()
   232        ..startVector()
   233        ..addString('foo')
   234        ..addInt(1)
   235        ..addInt(-5)
   236        ..addDouble(1.3)
   237        ..addBool(true)
   238        ..end();
   239      expect(flx.finish(), [
   240        3,
   241        102,
   242        111,
   243        111,
   244        0,
   245        0,
   246        0,
   247        0,
   248        5,
   249        0,
   250        0,
   251        0,
   252        0,
   253        0,
   254        0,
   255        0,
   256        15,
   257        0,
   258        0,
   259        0,
   260        0,
   261        0,
   262        0,
   263        0,
   264        1,
   265        0,
   266        0,
   267        0,
   268        0,
   269        0,
   270        0,
   271        0,
   272        251,
   273        255,
   274        255,
   275        255,
   276        255,
   277        255,
   278        255,
   279        255,
   280        205,
   281        204,
   282        204,
   283        204,
   284        204,
   285        204,
   286        244,
   287        63,
   288        1,
   289        0,
   290        0,
   291        0,
   292        0,
   293        0,
   294        0,
   295        0,
   296        20,
   297        4,
   298        4,
   299        15,
   300        104,
   301        45,
   302        43,
   303        1
   304      ]);
   305    }
   306  });
   307
   308  test('build map', () {
   309    {
   310      var flx = Builder()
   311        ..startMap()
   312        ..addKey('a')
   313        ..addInt(12)
   314        ..end();
   315      expect(flx.finish(), [97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1]);
   316    }
   317    {
   318      var flx = Builder()
   319        ..startMap()
   320        ..addKey('a')
   321        ..addInt(12)
   322        ..addKey('')
   323        ..addInt(45)
   324        ..end();
   325      expect(
   326          flx.finish(), [97, 0, 0, 2, 2, 5, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]);
   327    }
   328    {
   329      var flx = Builder()
   330        ..startVector()
   331        ..startMap()
   332        ..addKey('something')
   333        ..addInt(12)
   334        ..end()
   335        ..startMap()
   336        ..addKey('something')
   337        ..addInt(45)
   338        ..end()
   339        ..end();
   340      expect(flx.finish(), [
   341        115,
   342        111,
   343        109,
   344        101,
   345        116,
   346        104,
   347        105,
   348        110,
   349        103,
   350        0,
   351        1,
   352        11,
   353        1,
   354        1,
   355        1,
   356        12,
   357        4,
   358        6,
   359        1,
   360        1,
   361        45,
   362        4,
   363        2,
   364        8,
   365        4,
   366        36,
   367        36,
   368        4,
   369        40,
   370        1
   371      ]);
   372    }
   373  });
   374
   375  test('build blob', () {
   376    {
   377      var flx = Builder()..addBlob(Uint8List.fromList([1, 2, 3]).buffer);
   378      expect(flx.finish(), [3, 1, 2, 3, 3, 100, 1]);
   379    }
   380  });
   381
   382  test('build from object', () {
   383    expect(
   384        Builder.buildFromObject(Uint8List.fromList([1, 2, 3]).buffer)
   385            .asUint8List(),
   386        [3, 1, 2, 3, 3, 100, 1]);
   387    expect(Builder.buildFromObject(null).asUint8List(), [0, 0, 1]);
   388    expect(Builder.buildFromObject(true).asUint8List(), [1, 104, 1]);
   389    expect(Builder.buildFromObject(false).asUint8List(), [0, 104, 1]);
   390    expect(Builder.buildFromObject(25).asUint8List(), [25, 4, 1]);
   391    expect(Builder.buildFromObject(-250).asUint8List(), [6, 255, 5, 2]);
   392    expect(
   393        Builder.buildFromObject(-2.50).asUint8List(), [0, 0, 32, 192, 14, 4]);
   394    expect(Builder.buildFromObject('Maxim').asUint8List(),
   395        [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]);
   396    expect(
   397        Builder.buildFromObject([1, 3.3, 'max', true, null, false])
   398            .asUint8List(),
   399        [
   400          3,
   401          109,
   402          97,
   403          120,
   404          0,
   405          0,
   406          0,
   407          0,
   408          6,
   409          0,
   410          0,
   411          0,
   412          0,
   413          0,
   414          0,
   415          0,
   416          1,
   417          0,
   418          0,
   419          0,
   420          0,
   421          0,
   422          0,
   423          0,
   424          102,
   425          102,
   426          102,
   427          102,
   428          102,
   429          102,
   430          10,
   431          64,
   432          31,
   433          0,
   434          0,
   435          0,
   436          0,
   437          0,
   438          0,
   439          0,
   440          1,
   441          0,
   442          0,
   443          0,
   444          0,
   445          0,
   446          0,
   447          0,
   448          0,
   449          0,
   450          0,
   451          0,
   452          0,
   453          0,
   454          0,
   455          0,
   456          0,
   457          0,
   458          0,
   459          0,
   460          0,
   461          0,
   462          0,
   463          0,
   464          4,
   465          15,
   466          20,
   467          104,
   468          0,
   469          104,
   470          54,
   471          43,
   472          1
   473        ]);
   474    expect(
   475        Builder.buildFromObject([
   476          {'something': 12},
   477          {'something': 45}
   478        ]).asUint8List(),
   479        [
   480          115,
   481          111,
   482          109,
   483          101,
   484          116,
   485          104,
   486          105,
   487          110,
   488          103,
   489          0,
   490          1,
   491          11,
   492          1,
   493          1,
   494          1,
   495          12,
   496          4,
   497          6,
   498          1,
   499          1,
   500          45,
   501          4,
   502          2,
   503          8,
   504          4,
   505          36,
   506          36,
   507          4,
   508          40,
   509          1
   510        ]);
   511  });
   512
   513  test('add double indirectly', () {
   514    var flx = Builder()..addDoubleIndirectly(0.1);
   515    expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 8, 35, 1]);
   516  });
   517
   518  test('add double indirectly to vector with cache', () {
   519    var flx = Builder()
   520      ..startVector()
   521      ..addDoubleIndirectly(0.1, cache: true)
   522      ..addDoubleIndirectly(0.1, cache: true)
   523      ..addDoubleIndirectly(0.1, cache: true)
   524      ..addDoubleIndirectly(0.1, cache: true)
   525      ..end();
   526    expect(flx.finish(), [
   527      154,
   528      153,
   529      153,
   530      153,
   531      153,
   532      153,
   533      185,
   534      63,
   535      4,
   536      9,
   537      10,
   538      11,
   539      12,
   540      35,
   541      35,
   542      35,
   543      35,
   544      8,
   545      40,
   546      1
   547    ]);
   548  });
   549
   550  test('add int indirectly', () {
   551    var flx = Builder()..addIntIndirectly(2345234523452345);
   552    expect(flx.finish(), [185, 115, 175, 118, 250, 84, 8, 0, 8, 27, 1]);
   553  });
   554
   555  test('add int indirectly to vector with cache', () {
   556    var flx = Builder()
   557      ..startVector()
   558      ..addIntIndirectly(2345234523452345, cache: true)
   559      ..addIntIndirectly(2345234523452345, cache: true)
   560      ..addIntIndirectly(2345234523452345, cache: true)
   561      ..addIntIndirectly(2345234523452345, cache: true)
   562      ..end();
   563    expect(flx.finish(), [
   564      185,
   565      115,
   566      175,
   567      118,
   568      250,
   569      84,
   570      8,
   571      0,
   572      4,
   573      9,
   574      10,
   575      11,
   576      12,
   577      27,
   578      27,
   579      27,
   580      27,
   581      8,
   582      40,
   583      1
   584    ]);
   585  });
   586
   587  test('snapshot', () {
   588    var flx = Builder();
   589    flx.startVector();
   590    flx.addInt(12);
   591    expect(flx.snapshot().asUint8List(), [1, 12, 1, 44, 1]);
   592    flx.addInt(24);
   593    expect(flx.snapshot().asUint8List(), [12, 24, 2, 64, 1]);
   594    flx.addInt(45);
   595    expect(flx.snapshot().asUint8List(), [12, 24, 45, 3, 76, 1]);
   596  });
   597}

View as plain text