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