Skip to content

Commit

Permalink
No parens in pack if not necessary.
Browse files Browse the repository at this point in the history
Closes #321.
  • Loading branch information
flatheadmill committed Mar 15, 2014
1 parent f350f68 commit b6365d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions release.md
@@ -1,5 +1,6 @@
### Issue by Issue

* No parens in pack if not necessary. #321.
* Rename Programmatic function to `$`. #320.
* Convert pack sketch to a test. #319.
* Convert signage sketch to a test. #318.
Expand Down
21 changes: 13 additions & 8 deletions t/serialize/pack.t.js
@@ -1,4 +1,4 @@
require('proof')(3, function (equal) {
require('proof')(8, function (equal) {
require('../..') // satisfy coverage

function pack (bits, offset, size) {
Expand All @@ -7,18 +7,23 @@ require('proof')(3, function (equal) {
mask = mask >>> bits - size
shift = bits - offset - size
mask = mask << shift >>> 0
if (shift) {
return '(value << ' + shift + ' & 0x' + mask.toString(16) + ') >>> 0'
} else {
return '(value & 0x' + mask.toString(16) + ') >>> 0'
}
var source = shift
? 'value << ' + shift + ' & 0x' + mask.toString(16)
: 'value & 0x' + mask.toString(16)
return !offset && bits == 32 ? '(' + source + ') >>> 0' : source
}

var f = new Function('value', 'return ' + pack(16, 5, 7))
equal(f(0x7f).toString(2), '11111110000', 'short')
var f = new Function('value', 'return ' + pack(32, 0, 2))
equal(f(0x3).toString(2), '11000000000000000000000000000000', 'short')
console.log(pack(16, 13, 3))
equal(f(0x3).toString(2), '11000000000000000000000000000000', 'word left most')
equal(f(-1).toString(2), '11000000000000000000000000000000', 'word left most -1')
var f = new Function('value', 'return ' + pack(32, 1, 2))
equal(f(0x3).toString(2), '1100000000000000000000000000000', 'word')
var f = new Function('value', 'return ' + pack(16, 13, 3))
equal(f(0xff).toString(2), '111', 'no left shift')
var f = new Function('value', 'return ' + pack(32, 29, 3))
equal(f(0xff).toString(2), '111', 'no left shift 32')
equal(f(-1).toString(2), '111', 'no left shift 32 -1')
equal(f(-4).toString(2), '100', 'no left shift 32 -4')
})

0 comments on commit b6365d5

Please sign in to comment.