bu.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var binary = require('../');
  2. var test = require('tap').test;
  3. test('bu', function (t) {
  4. t.plan(8);
  5. // note: can't store -12667700813876161 exactly in an ieee float
  6. var buf = new Buffer([
  7. 44, // a == 44
  8. 2, 43, // b == 555
  9. 164, 213, 37, 37, // c == 2765432101
  10. 29, 81, 180, 20, 155, 115, 203, 193, // d == 2112667700813876161
  11. ]);
  12. binary.parse(buf)
  13. .word8bu('a')
  14. .word16bu('b')
  15. .word32bu('c')
  16. .word64bu('d')
  17. .tap(function (vars) {
  18. t.same(vars.a, 44);
  19. t.same(vars.b, 555);
  20. t.same(vars.c, 2765432101);
  21. t.ok(
  22. Math.abs(vars.d - 2112667700813876161) < 1500
  23. );
  24. })
  25. ;
  26. // also check aliases here:
  27. binary.parse(buf)
  28. .word8be('a')
  29. .word16be('b')
  30. .word32be('c')
  31. .word64be('d')
  32. .tap(function (vars) {
  33. t.same(vars.a, 44);
  34. t.same(vars.b, 555);
  35. t.same(vars.c, 2765432101);
  36. t.ok(
  37. Math.abs(vars.d - 2112667700813876161) < 1500
  38. );
  39. })
  40. ;
  41. });