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( 26 Reference.fromBuffer(b([0, 0, 0, 128, 6, 4])).intValue, -2147483648); 27 expect( 28 Reference.fromBuffer(b([255, 255, 255, 255, 0, 0, 0, 0, 7, 8])) 29 .intValue, 30 4294967295); 31 expect( 32 Reference.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 127, 7, 8])) 33 .intValue, 34 9223372036854775807); 35 expect(Reference.fromBuffer(b([0, 0, 0, 0, 0, 0, 0, 128, 7, 8])).intValue, 36 -9223372036854775808); 37 // Dart does not really support UInt64 38// expect(FlxValue.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 255, 11, 8])).intValue, 18446744073709551615); 39 }); 40 test('double value', () { 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, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1 67 ])).stringValue, 68 'hello '); 69 }); 70 test('blob value', () { 71 expect( 72 Reference.fromBuffer(b([3, 1, 2, 3, 3, 100, 1])).blobValue, [1, 2, 3]); 73 }); 74 test('bool vector', () { 75 var flx = Reference.fromBuffer(b([3, 1, 0, 1, 3, 144, 1])); 76 expect(flx[0].boolValue, true); 77 expect(flx[1].boolValue, false); 78 expect(flx[2].boolValue, true); 79 }); 80 test('number vector', () { 81 testNumbers([3, 1, 2, 3, 3, 44, 1], [1, 2, 3]); 82 testNumbers([3, 255, 2, 3, 3, 44, 1], [-1, 2, 3]); 83 testNumbers([3, 0, 1, 0, 43, 2, 3, 0, 6, 45, 1], [1, 555, 3]); 84 testNumbers( 85 [3, 0, 0, 0, 1, 0, 0, 0, 204, 216, 0, 0, 3, 0, 0, 0, 12, 46, 1], 86 [1, 55500, 3]); 87 testNumbers([ 88 3, 0, 0, 0, 0, 0, 0, 0, 89 1, 0, 0, 0, 0, 0, 0, 0, 90 172, 128, 94, 239, 12, 0, 0, 0, 91 3, 0, 0, 0, 0, 0, 0, 0, 92 24, 47, 1 93 ], [1, 55555555500, 3 94 ]); 95 testNumbers( 96 [3, 0, 0, 0, 0, 0, 192, 63, 0, 0, 32, 64, 0, 0, 96, 64, 12, 54, 1], 97 [1.5, 2.5, 3.5]); 98 testNumbers([ 99 3, 0, 0, 0, 0, 0, 0, 0, 100 154, 153, 153, 153, 153, 153, 241, 63, 101 154, 153, 153, 153, 153, 153, 1, 64, 102 102, 102, 102, 102, 102, 102, 10, 64, 103 24, 55, 1 104 ], [1.1, 2.2, 3.3 105 ]); 106 }); 107 test('number vector, fixed type', () { 108 testNumbers([1, 2, 2, 64, 1], [1, 2]); 109 testNumbers([255, 255, 0, 1, 4, 65, 1], [-1, 256]); 110 testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1], [-45, 256000]); 111 testNumbers([ 112 211, 255, 255, 255, 255, 255, 255, 255, 113 255, 255, 255, 255, 255, 255, 255, 127, 114 16, 67, 1 115 ], [ 116 -45, 9223372036854775807 117 ]); 118 119 testNumbers([1, 2, 2, 68, 1], [1, 2]); 120 testNumbers([1, 0, 0, 1, 4, 69, 1], [1, 256]); 121 testNumbers([45, 0, 0, 0, 0, 232, 3, 0, 8, 70, 1], [45, 256000]); 122 123 testNumbers([205, 204, 140, 63, 0, 0, 0, 192, 8, 74, 1], [1.1, -2]); 124 testNumbers([ 125 154, 153, 153, 153, 153, 153, 241, 63, 126 0, 0, 0, 0, 0, 0, 112, 192, 127 16, 75, 1 128 ], [ 129 1.1, -256 130 ]); 131 132 testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 4, 0, 0, 0, 12, 78, 1], 133 [-45, 256000, 4]); 134 135 testNumbers([ 136 211, 255, 255, 255, 255, 255, 255, 255, 137 255, 255, 255, 255, 255, 255, 255, 127, 138 4, 0, 0, 0, 0, 0, 0, 0, 139 9, 0, 0, 0, 0, 0, 0, 0, 140 32, 91, 1 141 ], [ 142 -45, 9223372036854775807, 4, 9 143 ]); 144 145 testNumbers([ 146 45, 0, 0, 0, 0, 0, 0, 0, 147 255, 255, 255, 255, 255, 255, 255, 127, 148 4, 0, 0, 0, 0, 0, 0, 0, 149 9, 0, 0, 0, 0, 0, 0, 0, 150 32, 95, 1 151 ], [ 152 45, 9223372036854775807, 4, 9 153 ]); 154 155 testNumbers([ 156 154, 153, 153, 153, 153, 153, 241, 63, 157 0, 0, 0, 0, 0, 0, 112, 64, 158 0, 0, 0, 0, 0, 0, 16, 64, 159 24, 87, 1 160 ], [ 161 1.1, 256, 4 162 ]); 163 164 testNumbers([ 165 154, 153, 153, 153, 153, 153, 241, 63, 166 0, 0, 0, 0, 0, 0, 112, 64, 167 0, 0, 0, 0, 0, 0, 16, 64, 168 0, 0, 0, 0, 0, 0, 34, 64, 169 32, 99, 1 170 ], [ 171 1.1, 256, 4, 9 172 ]); 173 }); 174 test('string vector', () { 175 testStrings([ 176 3, 102, 111, 111, 0, 177 3, 98, 97, 114, 0, 178 3, 98, 97, 122, 0, 179 3, 15, 11, 7, 180 3, 60, 1 181 ], [ 182 'foo', 'bar', 'baz' 183 ]); 184 testStrings([ 185 3, 102, 111, 111, 0, 186 3, 98, 97, 114, 0, 187 3, 98, 97, 122, 0, 188 6, 15, 11, 7, 18, 14, 10, 189 6, 60, 1 190 ], [ 191 'foo', 'bar', 'baz', 'foo', 'bar', 'baz' 192 ]); 193 }); 194 test('mixed vector', () { 195 var flx = Reference.fromBuffer(b([ 196 3, 102, 111, 111, 0, 0, 0, 0, 197 5, 0, 0, 0, 0, 0, 0, 0, 198 15, 0, 0, 0, 0, 0, 0, 0, 199 1, 0, 0, 0, 0, 0, 0, 0, 200 251, 255, 255, 255, 255, 255, 255, 255, 201 205, 204, 204, 204, 204, 204, 244, 63, 202 1, 0, 0, 0, 0, 0, 0, 0, 203 20, 4, 4, 15, 104, 45, 43, 1 204 ])); 205 expect(flx.length, 5); 206 expect(flx[0].stringValue, 'foo'); 207 expect(flx[1].numValue, 1); 208 expect(flx[2].numValue, -5); 209 expect(flx[3].numValue, 1.3); 210 expect(flx[4].boolValue, true); 211 }); 212 213 test('single value map', () { 214 var flx = Reference.fromBuffer(b([97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1])); 215 expect(flx.length, 1); 216 expect(flx['a'].numValue, 12); 217 }); 218 test('two value map', () { 219 var flx = Reference.fromBuffer(b([0, 97, 0, 2, 4, 4, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1])); 220 expect(flx.length, 2); 221 expect(flx['a'].numValue, 12); 222 expect(flx[''].numValue, 45); 223 }); 224 test('complex map', () { 225 var flx = complexMap(); 226 expect(flx.length, 5); 227 expect(flx['age'].numValue, 35); 228 expect(flx['weight'].numValue, 72.5); 229 expect(flx['name'].stringValue, 'Maxim'); 230 231 expect(flx['flags'].length, 4); 232 expect(flx['flags'][0].boolValue, true); 233 expect(flx['flags'][1].boolValue, false); 234 expect(flx['flags'][2].boolValue, true); 235 expect(flx['flags'][3].boolValue, true); 236 237 expect(flx['address'].length, 3); 238 expect(flx['address']['city'].stringValue, 'Bla'); 239 expect(flx['address']['zip'].stringValue, '12345'); 240 expect(flx['address']['countryCode'].stringValue, 'XX'); 241 242 expect(() => flx['address']['country'].stringValue, 243 throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [country] is not applicable on: //address of: ValueType.Map'))); 244 expect(() => flx['address']['countryCode'][0], 245 throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [0] is not applicable on: //address/countryCode of: ValueType.String'))); 246 expect(() => flx[1], 247 throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [1] is not applicable on: / of: ValueType.Map'))); 248 expect(() => flx['flags'][4], 249 throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [4] is not applicable on: //flags of: ValueType.VectorBool length: 4'))); 250 expect(() => flx['flags'][-1], 251 throwsA(predicate((e) => e is ArgumentError && e.message == 'Key: [-1] is not applicable on: //flags of: ValueType.VectorBool length: 4'))); 252 }); 253 test('complex map to json', () { 254 var flx = complexMap(); 255 expect(flx.json, '{"address":{"city":"Bla","countryCode":"XX","zip":"12345"},"age":35,"flags":[true,false,true,true],"name":"Maxim","weight":72.5}'); 256 }); 257 258 test('complex map iterators', () { 259 var flx = complexMap(); 260 expect(flx.mapKeyIterable.map((e) => e).toList(), ['address', 'age', 'flags', 'name', 'weight']); 261 expect(flx.mapValueIterable.map((e) => e.json).toList(), [flx['address'].json, flx['age'].json, flx['flags'].json, flx['name'].json, flx['weight'].json]); 262 expect(flx['flags'].vectorIterable.map((e) => e.boolValue).toList(), [true, false, true, true]); 263 }); 264 265 test('bug where offest were stored as int instead of uint', (){ 266 const data = [99, 104, 97, 110, 110, 101, 108, 115, 95, 105, 110, 0, 267 100, 105, 108, 97, 116, 105, 111, 110, 95, 104, 101, 105, 103, 104, 116, 95, 102, 97, 99, 116, 111, 114, 0, 268 100, 105, 108, 97, 116, 105, 111, 110, 95, 119, 105, 100, 116, 104, 95, 102, 97, 99, 116, 111, 114, 0, 269 102, 117, 115, 101, 100, 95, 97, 99, 116, 105, 118, 97, 116, 105, 111, 110, 95, 102, 117, 110, 99, 116, 105, 111, 110, 0, 270 112, 97, 100, 95, 118, 97, 108, 117, 101, 115, 0, 112, 97, 100, 100, 105, 110, 103, 0, 271 115, 116, 114, 105, 100, 101, 95, 104, 101, 105, 103, 104, 116, 0, 272 115, 116, 114, 105, 100, 101, 95, 119, 105, 100, 116, 104, 0, 273 8, 130, 119, 97, 76, 51, 41, 34, 21, 8, 1, 8, 64, 1, 1, 1, 1, 0, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 16, 36, 1]; 274 var flx = Reference.fromBuffer(b(data)); 275 expect(flx.json, '{"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}'); 276 const object = {"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}; 277 var data1 = Builder.buildFromObject(object).asUint8List(); 278 expect(data1.length, data.length); 279 var flx1 = Reference.fromBuffer(b(data1)); 280 expect(flx1.json, '{"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}'); 281 }); 282} 283 284ByteBuffer b(List<int> values) { 285 var data = Uint8List.fromList(values); 286 return data.buffer; 287} 288 289void testNumbers(List<int> buffer, List<num> numbers) { 290 var flx = Reference.fromBuffer(b(buffer)); 291 expect(flx.length, numbers.length); 292 for (var i = 0; i < flx.length; i++) { 293 expect(flx[i].numValue, closeTo(numbers[i], 0.001)); 294 } 295} 296 297void testStrings(List<int> buffer, List<String> numbers) { 298 var flx = Reference.fromBuffer(b(buffer)); 299 expect(flx.length, numbers.length); 300 for (var i = 0; i < flx.length; i++) { 301 expect(flx[i].stringValue, numbers[i]); 302 } 303} 304 305Reference complexMap(){ 306// { 307// "age": 35, 308// "flags": [True, False, True, True], 309// "weight": 72.5, 310// "name": "Maxim", 311// "address": { 312// "city": "Bla", 313// "zip": "12345", 314// "countryCode": "XX", 315// } 316// } 317 return Reference.fromBuffer(b([ 318 97, 100, 100, 114, 101, 115, 115, 0, 319 99, 105, 116, 121, 0, 3, 66, 108, 97, 0, 320 99, 111, 117, 110, 116, 114, 121, 67, 111, 100, 101, 0, 321 2, 88, 88, 0, 322 122, 105, 112, 0, 323 5, 49, 50, 51, 52, 53, 0, 324 3, 38, 29, 14, 3, 1, 3, 38, 22, 15, 20, 20, 20, 325 97, 103, 101, 0, 326 102, 108, 97, 103, 115, 0, 327 4, 1, 0, 1, 1, 328 110, 97, 109, 101, 0, 329 5, 77, 97, 120, 105, 109, 0, 330 119, 101, 105, 103, 104, 116, 0, 331 5, 93, 36, 33, 23, 12, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 60, 0, 0, 0, 35, 0, 0, 0, 51, 0, 0, 0, 45, 332 0, 0, 0, 0, 0, 145, 66, 36, 4, 144, 20, 14, 25, 38, 1 333 ])); 334} 335