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