utils.bin.test.js 981 B

1234567891011121314151617181920212223242526272829303132
  1. var
  2. QUnit = require('qunit'),
  3. toUnsigned = require('../lib/utils/bin').toUnsigned;
  4. QUnit.module('Binary Utils');
  5. QUnit.test('converts values to unsigned integers after bitwise operations', function(assert) {
  6. var bytes;
  7. bytes = [0, 0, 124, 129];
  8. assert.equal(toUnsigned(bytes[0] << 24 |
  9. bytes[1] << 16 |
  10. bytes[2] << 8 |
  11. bytes[3]),
  12. 31873, 'positive signed result stays positive');
  13. bytes = [150, 234, 221, 192];
  14. // sanity check
  15. assert.equal(bytes[0] << 24 |
  16. bytes[1] << 16 |
  17. bytes[2] << 8 |
  18. bytes[3],
  19. -1762992704, 'bitwise operation produces negative signed result');
  20. assert.equal(toUnsigned(bytes[0] << 24 |
  21. bytes[1] << 16 |
  22. bytes[2] << 8 |
  23. bytes[3]),
  24. 2531974592, 'negative signed result becomes unsigned positive');
  25. });