Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/binarypack.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ BinaryPack.Unpacker.prototype.unpack = function(){
}
BinaryPack.Unpacker.prototype.readUInt64 = function(){
var bytes = this.cursor.slice(8);
return ((((((bytes[0] * 256 +
bytes[1]) * 256 +
bytes[2]) * 256 +
bytes[3]) * 256 +
bytes[4]) * 256 +
bytes[5]) * 256 +
bytes[6]) * 256 +
bytes[7];
return ((((((bytes.buffer[0] * 256 +
bytes.buffer[1]) * 256 +
bytes.buffer[2]) * 256 +
bytes.buffer[3]) * 256 +
bytes.buffer[4]) * 256 +
bytes.buffer[5]) * 256 +
bytes.buffer[6]) * 256 +
bytes.buffer[7];
}

BinaryPack.Unpacker.prototype.readInt64 = function(){
var uint64 = this.readInt64();
var uint64 = this.readUInt64();
return (uint64 < Math.pow(2, 63) ) ? uint64 : uint64 - Math.pow(2, 64);
}

Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
"node": ">=0.6.0"
},
"dependencies" : {
"buffercursor" : ">=0.0.3"
"buffercursor" : "git+https://github.com/loughmiller/node-buffercursor.git#master"
},
"devDependencies": {
"nodeunit" : ">=0.8.0"
},
"scripts": {
"test": "nodeunit test/binarypack.nodeunit.js"
}
}

77 changes: 77 additions & 0 deletions test/binarypack.nodeunit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*jslint node: true */

"use strict";

var binarypack = require('../lib/binarypack.js');

function testPackUnpack(test, data) {
console.log('packing: ', data);
var buffer = binarypack.pack(data);
console.log('unpacking');
test.strictEqual(binarypack.unpack(buffer), data);
}


exports.integers = {
testInt32 : function (test) {

var i,
int32Max = Math.pow(2, 31) - 1;

for (i=1; i<=int32Max; i*=2) {

// test positive
testPackUnpack(test, i);

// test negative
testPackUnpack(test, i * -1);

// test + 1
testPackUnpack(test, i + 1);

// test - 1
testPackUnpack(test, i - 1);

// test negative + 1
testPackUnpack(test, (i + 1) * -1);

// test negative - 1
testPackUnpack(test, (i - 1) * -1);
}

testPackUnpack(test, int32Max);

testPackUnpack(test, int32Max * -1);

test.done();
},

testUInt32 : function (test) {

testPackUnpack(test, Math.pow(2, 31));

testPackUnpack(test, Math.pow(2, 32) - 1);

test.done();
},

testInt64 : function (test) {
testPackUnpack(test, Math.pow(2, 31) * -1);

testPackUnpack(test, Math.pow(2, 32));

testPackUnpack(test, Math.pow(2, 32) * -1);

testPackUnpack(test, Math.pow(2, 48));

testPackUnpack(test, Math.pow(2, 48) * -1);

// largest integer available in javascript
testPackUnpack(test, Math.pow(2, 53));

testPackUnpack(test, Math.pow(2, 53) * -1);

test.done();
}

};