Skip to content

Commit

Permalink
Convert pack sketch to a test.
Browse files Browse the repository at this point in the history
Closes #319.
  • Loading branch information
flatheadmill committed Mar 6, 2014
1 parent 16e267a commit 9cc86d6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
12 changes: 0 additions & 12 deletions pack.js

This file was deleted.

1 change: 1 addition & 0 deletions release.md
@@ -1,5 +1,6 @@
### Issue by Issue

* Convert pack sketch to a test. #319.
* Convert signage sketch to a test. #318.
* Convert unpack sketch to a test. #317.
* Use `signage` for signed whole integers. #316.
Expand Down
24 changes: 24 additions & 0 deletions t/serialize/pack.t.js
@@ -0,0 +1,24 @@
require('proof')(3, function (equal) {
require('../..') // satisfy coverage

function pack (bits, offset, size) {
var mask = 0xffffffff, shift
mask = mask >>> 32 - bits
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 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))
var f = new Function('value', 'return ' + pack(16, 13, 3))
equal(f(0xff).toString(2), '111', 'no left shift')
})

0 comments on commit 9cc86d6

Please sign in to comment.