forked from pgriess/node-jspack
-
Notifications
You must be signed in to change notification settings - Fork 12
/
int64.js
239 lines (197 loc) · 8.16 KB
/
int64.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var should = require('should');
var jspack = require('../jspack.js').jspack;
var Long = require('long');
describe('Test long integration (examples):', function () {
// Demonstrating the use together with Long.js (https://github.com/dcodeIO/Long.js)
//
// Packing a long requires the input of a 2 part array containing the [low, high] bits
// of the specific long value.
// Unpacking a long results in a 3 part array containing [low, high, unsigned] bits and flag.
// The decoded value can be applied directly to Long.fromBits()
//
// Test number u 228290380562207 (BE: 0x00, 0x00, 0xcf, 0xa0, 0xff, 0x09, 0xff, 0x1f)
// (LE: 0x1f, 0xff, 0x09, 0xff, 0xa0, 0xcf, 0x00, 0x00)
// Test number s -228290380562207 (BE: 0xff, 0xff, 0x30, 0x5f, 0x00, 0xf6, 0x00, 0xe1)
// (LE: 0xe1, 0x00, 0xf6, 0x00, 0x5f, 0x30, 0xff, 0xff)
it('pack <Q', function () {
//var num = new Long(0xff09ff1f, 0x0000cfa0, true);
var num = Long.fromNumber(228290380562207, true);
// Pass long representation to jspack
var buf = jspack.Pack('<Q', [[num.getLowBitsUnsigned(), num.getHighBitsUnsigned()]]);
buf.should.be.eql([0x1f, 0xff, 0x09, 0xff, 0xa0, 0xcf, 0x00, 0x00]);
});
it('unpack <Q', function () {
var testNum = new Long(0xff09ff1f, 0x0000cfa0, true); // unsigned
var buf = jspack.Unpack('<Q', [0x1f, 0xff, 0x09, 0xff, 0xa0, 0xcf, 0x00, 0x00]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
// Create long by passing unpacked buffer to it (either way works)
var res = Long.fromBits.apply(undefined, buf[0]);
var res2 = new Long(buf[0][0], buf[0][1], buf[0][2]);
testNum.equals(res).should.be.true;
testNum.equals(res2).should.be.true;
res.toNumber().should.be.eql(228290380562207);
});
it('pack <q', function () {
//var num = new Long(0x00f600e1, 0xffff305f, true);
var num = Long.fromNumber(-228290380562207); // signed
// Pass long representation to jspack
var buf = jspack.Pack('<q', [[num.getLowBitsUnsigned(), num.getHighBitsUnsigned()]]);
buf.should.be.eql([0xe1, 0x00, 0xf6, 0x00, 0x5f, 0x30, 0xff, 0xff]);
});
it('unpack <q', function () {
var testNum = new Long(0x00f600e1, 0xffff305f); // signed
var buf = jspack.Unpack('<q', [0xe1, 0x00, 0xf6, 0x00, 0x5f, 0x30, 0xff, 0xff]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
// Create long by passing unpacked buffer to it (either way works)
var res = Long.fromBits.apply(undefined, buf[0]);
var res2 = new Long(buf[0][0], buf[0][1], buf[0][2]);
testNum.equals(res).should.be.true;
testNum.equals(res2).should.be.true;
res.toNumber().should.be.eql(-228290380562207);
});
});
describe('Test signed/unsigned int64:', function () {
// Number 0xffa0ffe1ffff, packed with Python struct:
// little endian:
// 0xff, 0xff, 0xe1, 0xff, 0xa0, 0xff, 0x00, 0x00
// big endian:
// 0x00, 0x00, 0xff, 0xa0, 0xff, 0xe1, 0xff, 0xff
it('pack <Q', function () {
var buf = jspack.Pack('<Q', [[0xffe1ffff, 0xffa0]]);
buf.should.be.eql([0xff, 0xff, 0xe1, 0xff, 0xa0, 0xff, 0x00, 0x00]);
});
it('pack >Q', function () {
var buf = jspack.Pack('>Q', [[0xffe1ffff, 0xffa0]]);
buf.should.be.eql([0x00, 0x00, 0xff, 0xa0, 0xff, 0xe1, 0xff, 0xff]);
});
it('unpack <Q', function () {
var buf = jspack.Unpack('<Q', [0xff, 0xff, 0xe1, 0xff, 0xa0, 0xff, 0x00, 0x00]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0xffe1ffff);
buf[0][1].should.be.eql(0xffa0);
buf[0][2].should.be.true;
});
it('unpack >Q', function () {
var buf = jspack.Unpack('>Q', [0x00, 0x00, 0xff, 0xa0, 0xff, 0xe1, 0xff, 0xff]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0xffe1ffff);
buf[0][1].should.be.eql(0xffa0);
buf[0][2].should.be.true;
});
// Test lower-case q as well. This only test the matching of the caracter and the unsigned bit,
// the parsing is the same as for upper-case Q (since we don't actually convert to a number).
it('pack >q (signed)', function () {
var buf = jspack.Pack('>q', [[0xffe1ffff, 0xffa0]]);
buf.should.be.eql([0x00, 0x00, 0xff, 0xa0, 0xff, 0xe1, 0xff, 0xff]);
});
it('unpack <q (signed)', function () {
var buf = jspack.Unpack('<q', [0xff, 0xff, 0xe1, 0xff, 0xa0, 0xff, 0x00, 0x00]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0xffe1ffff);
buf[0][1].should.be.eql(0xffa0);
buf[0][2].should.be.false;
});
});
describe('Boundary tests:', function () {
it('unpack >Q full', function () {
var buf = jspack.Unpack('>Q', [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0xffffffff);
buf[0][1].should.be.eql(0xffffffff);
buf[0][2].should.be.true;
});
it('pack >Q full', function () {
var buf = jspack.Pack('>Q', [[0xffffffff, 0xffffffff]]);
buf.should.be.eql([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
});
it('unpack <Q full', function () {
var buf = jspack.Unpack('<Q', [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0xffffffff);
buf[0][1].should.be.eql(0xffffffff);
buf[0][2].should.be.true;
});
it('pack <Q full', function () {
var buf = jspack.Pack('<Q', [[0xffffffff, 0xffffffff]]);
buf.should.be.eql([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
});
it('unpack >Q zero', function () {
var buf = jspack.Unpack('>Q', [0, 0, 0, 0, 0, 0, 0, 0]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0);
buf[0][1].should.be.eql(0);
buf[0][2].should.be.true;
});
it('pack >Q zero', function () {
var buf = jspack.Pack('>Q', [[0, 0]]);
buf.should.be.eql([0, 0, 0, 0, 0, 0, 0, 0]);
});
it('unpack <Q zero', function () {
var buf = jspack.Unpack('<Q', [0, 0, 0, 0, 0, 0, 0, 0]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0);
buf[0][1].should.be.eql(0);
buf[0][2].should.be.true;
});
it('pack <Q zero', function () {
var buf = jspack.Pack('<Q', [[0, 0]]);
buf.should.be.eql([0, 0, 0, 0, 0, 0, 0, 0]);
});
it('unpack >Q one', function () {
var buf = jspack.Unpack('>Q', [1, 1, 1, 1, 1, 1, 1, 1]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0x01010101);
buf[0][1].should.be.eql(0x01010101);
buf[0][2].should.be.true;
});
it('pack >Q one', function () {
var buf = jspack.Pack('>Q', [[0x01010101, 0x01010101]]);
buf.should.be.eql([1, 1, 1, 1, 1, 1, 1, 1]);
});
it('unpack <Q one', function () {
var buf = jspack.Unpack('<Q', [1, 1, 1, 1, 1, 1, 1, 1]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0x01010101);
buf[0][1].should.be.eql(0x01010101);
buf[0][2].should.be.true;
});
it('pack <Q one', function () {
var buf = jspack.Pack('<Q', [[0x01010101, 0x01010101]]);
buf.should.be.eql([1, 1, 1, 1, 1, 1, 1, 1]);
});
it('unpack >Q 0xfe', function () {
var buf = jspack.Unpack('>Q', [0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0xfefefefe);
buf[0][1].should.be.eql(0xfefefefe);
buf[0][2].should.be.true;
});
it('pack >Q 0xfe', function () {
var buf = jspack.Pack('>Q', [[0xfefefefe, 0xfefefefe]]);
buf.should.be.eql([0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe]);
});
it('unpack <Q 0xfe', function () {
var buf = jspack.Unpack('<Q', [0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe]);
buf.length.should.be.eql(1);
buf[0].length.should.be.eql(3);
buf[0][0].should.be.eql(0xfefefefe);
buf[0][1].should.be.eql(0xfefefefe);
buf[0][2].should.be.true;
});
it('pack <Q 0xfe', function () {
var buf = jspack.Pack('<Q', [[0xfefefefe, 0xfefefefe]]);
buf.should.be.eql([0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe]);
});
});