• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 flx = Builder();
45      flx.addDouble(0.1);
46      expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 15, 8]);
47    }
48    {
49      var flx = Builder();
50      flx.addDouble(0.5);
51      expect(flx.finish(), [0, 0, 0, 63, 14, 4]);
52    }
53    {
54      var flx = Builder();
55      flx.addString('Maxim');
56      expect(flx.finish(), [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]);
57    }
58    {
59      var flx = Builder();
60      flx.addString('hello ��');
61      expect(flx.finish(), [10, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1]);
62    }
63  });
64
65  test('build vector', (){
66    {
67      var flx = Builder()
68        ..startVector()
69        ..addInt(1)
70        ..addInt(2)
71        ..end()
72      ;
73      expect(flx.finish(), [1, 2, 2, 64, 1]);
74    }
75    {
76      var flx = Builder()
77        ..startVector()
78        ..addInt(-1)
79        ..addInt(256)
80        ..end()
81      ;
82      expect(flx.finish(), [255, 255, 0, 1, 4, 65, 1]);
83    }
84    {
85      var flx = Builder()
86        ..startVector()
87        ..addInt(-45)
88        ..addInt(256000)
89        ..end()
90      ;
91      expect(flx.finish(), [211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1]);
92    }
93    {
94      var flx = Builder()
95        ..startVector()
96        ..addDouble(1.1)
97        ..addDouble(-256)
98        ..end()
99      ;
100      expect(flx.finish(), [154, 153, 153, 153, 153, 153, 241, 63, 0, 0, 0, 0, 0, 0, 112, 192, 16, 75, 1]);
101    }
102    {
103      var flx = Builder()
104        ..startVector()
105        ..addInt(1)
106        ..addInt(2)
107        ..addInt(4)
108        ..end()
109      ;
110      expect(flx.finish(), [1, 2, 4, 3, 76, 1]);
111    }
112    {
113      var flx = Builder()
114        ..startVector()
115        ..addInt(-1)
116        ..addInt(256)
117        ..addInt(4)
118        ..end()
119      ;
120      expect(flx.finish(), [255, 255, 0, 1, 4, 0, 6, 77, 1]);
121    }
122    {
123      var flx = Builder()
124        ..startVector()
125          ..startVector()
126          ..addInt(61)
127          ..end()
128        ..addInt(64)
129        ..end()
130      ;
131      expect(flx.finish(), [1, 61, 2, 2, 64, 44, 4, 4, 40, 1]);
132    }
133    {
134      var flx = Builder()
135        ..startVector()
136        ..addString('foo')
137        ..addString('bar')
138        ..addString('baz')
139        ..end()
140      ;
141      expect(flx.finish(), [3, 102, 111, 111, 0, 3, 98, 97, 114, 0, 3, 98, 97, 122, 0, 3, 15, 11, 7, 3, 60, 1]);
142    }
143    {
144      var flx = Builder()
145        ..startVector()
146        ..addString('foo')
147        ..addString('bar')
148        ..addString('baz')
149        ..addString('foo')
150        ..addString('bar')
151        ..addString('baz')
152        ..end()
153      ;
154      expect(flx.finish(), [3, 102, 111, 111, 0, 3, 98, 97, 114, 0, 3, 98, 97, 122, 0, 6, 15, 11, 7, 18, 14, 10, 6, 60, 1]);
155    }
156    {
157      var flx = Builder()
158        ..startVector()
159        ..addBool(true)
160        ..addBool(false)
161        ..addBool(true)
162        ..end()
163      ;
164      expect(flx.finish(), [3, 1, 0, 1, 3, 144, 1]);
165    }
166    {
167      var flx = Builder()
168        ..startVector()
169        ..addString('foo')
170        ..addInt(1)
171        ..addInt(-5)
172        ..addDouble(1.3)
173        ..addBool(true)
174        ..end()
175      ;
176      expect(flx.finish(), [
177        3, 102, 111, 111, 0, 0, 0, 0,
178        5, 0, 0, 0, 0, 0, 0, 0,
179        15, 0, 0, 0, 0, 0, 0, 0,
180        1, 0, 0, 0, 0, 0, 0, 0,
181        251, 255, 255, 255, 255, 255, 255, 255,
182        205, 204, 204, 204, 204, 204, 244, 63,
183        1, 0, 0, 0, 0, 0, 0, 0,
184        20, 4, 4, 15, 104, 45, 43, 1]);
185    }
186  });
187
188  test('build map', ()
189  {
190    {
191      var flx = Builder()
192        ..startMap()
193        ..addKey('a')
194        ..addInt(12)
195        ..end()
196      ;
197      expect(flx.finish(), [97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1]);
198    }
199    {
200      var flx = Builder()
201        ..startMap()
202        ..addKey('a')
203        ..addInt(12)
204        ..addKey('')
205        ..addInt(45)
206        ..end()
207      ;
208      expect(flx.finish(), [97, 0, 0, 2, 2, 5, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]);
209    }
210    {
211      var flx = Builder()
212        ..startVector()
213          ..startMap()
214            ..addKey('something')
215            ..addInt(12)
216          ..end()
217          ..startMap()
218            ..addKey('something')
219            ..addInt(45)
220          ..end()
221        ..end()
222      ;
223      expect(flx.finish(), [115, 111, 109, 101, 116, 104, 105, 110, 103, 0,
224        1, 11, 1, 1, 1, 12, 4, 6, 1, 1, 45, 4, 2, 8, 4, 36, 36, 4, 40, 1]);
225    }
226  });
227
228  test('build blob', ()
229  {
230    {
231      var flx = Builder()
232        ..addBlob(Uint8List.fromList([1, 2, 3]).buffer)
233      ;
234      expect(flx.finish(), [3, 1, 2, 3, 3, 100, 1]);
235    }
236  });
237
238  test('build from object', (){
239    expect(Builder.buildFromObject(Uint8List.fromList([1, 2, 3]).buffer).asUint8List(), [3, 1, 2, 3, 3, 100, 1]);
240    expect(Builder.buildFromObject(null).asUint8List(), [0, 0, 1]);
241    expect(Builder.buildFromObject(true).asUint8List(), [1, 104, 1]);
242    expect(Builder.buildFromObject(false).asUint8List(), [0, 104, 1]);
243    expect(Builder.buildFromObject(25).asUint8List(), [25, 4, 1]);
244    expect(Builder.buildFromObject(-250).asUint8List(), [6, 255, 5, 2]);
245    expect(Builder.buildFromObject(-2.50).asUint8List(), [0, 0, 32, 192, 14, 4]);
246    expect(Builder.buildFromObject('Maxim').asUint8List(), [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]);
247    expect(Builder.buildFromObject([1, 3.3, 'max', true, null, false]).asUint8List(), [
248      3, 109, 97, 120, 0, 0, 0, 0,
249      6, 0, 0, 0, 0, 0, 0, 0,
250      1, 0, 0, 0, 0, 0, 0, 0,
251      102, 102, 102, 102, 102, 102, 10, 64,
252      31, 0, 0, 0, 0, 0, 0, 0,
253      1, 0, 0, 0, 0, 0, 0, 0,
254      0, 0, 0, 0, 0, 0, 0, 0,
255      0, 0, 0, 0, 0, 0, 0, 0,
256      4, 15, 20, 104, 0, 104, 54, 43, 1
257    ]);
258    expect(Builder.buildFromObject([{'something':12}, {'something': 45}]).asUint8List(), [
259      115, 111, 109, 101, 116, 104, 105, 110, 103, 0,
260      1, 11, 1, 1, 1, 12, 4, 6, 1, 1, 45, 4, 2, 8, 4, 36, 36, 4, 40, 1
261    ]);
262  });
263
264  test('add double indirectly', (){
265    var flx = Builder()
266      ..addDoubleIndirectly(0.1)
267    ;
268    expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 8, 35, 1]);
269  });
270
271  test('add double indirectly to vector with cache', (){
272    var flx = Builder()
273      ..startVector()
274      ..addDoubleIndirectly(0.1, cache: true)
275      ..addDoubleIndirectly(0.1, cache: true)
276      ..addDoubleIndirectly(0.1, cache: true)
277      ..addDoubleIndirectly(0.1, cache: true)
278      ..end()
279    ;
280    expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63,
281      4, 9, 10, 11, 12, 35, 35, 35, 35, 8, 40, 1]);
282  });
283
284  test('add int indirectly', (){
285    var flx = Builder()
286      ..addIntIndirectly(2345234523452345)
287    ;
288    expect(flx.finish(), [185, 115, 175, 118, 250, 84, 8, 0, 8, 27, 1]);
289  });
290
291  test('add int indirectly to vector with cache', (){
292    var flx = Builder()
293      ..startVector()
294      ..addIntIndirectly(2345234523452345, cache: true)
295      ..addIntIndirectly(2345234523452345, cache: true)
296      ..addIntIndirectly(2345234523452345, cache: true)
297      ..addIntIndirectly(2345234523452345, cache: true)
298      ..end()
299    ;
300    expect(flx.finish(), [185, 115, 175, 118, 250, 84, 8, 0,
301      4, 9, 10, 11, 12, 27, 27, 27, 27, 8, 40, 1]);
302  });
303
304  test('snapshot', (){
305    var flx = Builder();
306    flx.startVector();
307    flx.addInt(12);
308    expect(flx.snapshot().asUint8List(), [1, 12, 1, 44, 1]);
309    flx.addInt(24);
310    expect(flx.snapshot().asUint8List(), [12, 24, 2, 64, 1]);
311    flx.addInt(45);
312    expect(flx.snapshot().asUint8List(), [12, 24, 45, 3, 76, 1]);
313  });
314}
315
316