-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbinarypack.nodeunit.js
77 lines (48 loc) · 1.61 KB
/
binarypack.nodeunit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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();
}
};