|
1
|
|
|
|
|
2
|
|
|
var assert = require('assert'); |
|
3
|
|
|
|
|
4
|
|
|
describe('16-bit from bytes', function() { |
|
5
|
|
|
|
|
6
|
|
|
let byteData = require('../../index.js'); |
|
7
|
|
|
|
|
8
|
|
|
it('should turn 2 bytes to a 16-bit int', function() { |
|
9
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
10
|
|
|
[0,0,0,0], 16, {"signed": false}), |
|
11
|
|
|
[0,0]); |
|
12
|
|
|
}); |
|
13
|
|
|
it('should turn 4 bytes bin to 2 16-bit ints (max range)', function() { |
|
14
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
15
|
|
|
["00000000", "10000000", "11111111", "01111111"], |
|
16
|
|
|
16, {"base": 2, "signed": true}), |
|
17
|
|
|
[-32768, 32767]); |
|
18
|
|
|
}); |
|
19
|
|
|
it('should turn 4 bytes bin to 2 16-bit uInts (max range)', function() { |
|
20
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
21
|
|
|
["00000000","00000000","11111111","11111111"], |
|
22
|
|
|
16, {"base": 2, "signed": false}), |
|
23
|
|
|
[0, 65535]); |
|
24
|
|
|
}); |
|
25
|
|
|
it('should turn 5 bytes bin to 2 16-bit uInts (ignore the extra byte) (max range)', function() { |
|
26
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
27
|
|
|
["00000000","00000000","11111111","11111111","11111111"], |
|
28
|
|
|
16, {"base": 2, "signed": false}), |
|
29
|
|
|
[0, 65535]); |
|
30
|
|
|
}); |
|
31
|
|
|
it('should turn 1 byte bin to 0 16-bit uInts (not enough bytes)', function() { |
|
32
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
33
|
|
|
["11111111"], |
|
34
|
|
|
16, {"base": 2, "signed": false}), |
|
35
|
|
|
[]); |
|
36
|
|
|
}); |
|
37
|
|
|
it('should turn 4 bytes hex to 2 16-bit ints (max range)', function() { |
|
38
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
39
|
|
|
["0","80","ff","7f"], 16, {"base": 16, "signed": true}), |
|
40
|
|
|
[-32768, 32767]); |
|
41
|
|
|
}); |
|
42
|
|
|
it('should turn 4 bytes hex to 2 16-bit uInts (max range)', function() { |
|
43
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
44
|
|
|
["0","0","ff","ff"], 16, {"base": 16, "signed": false}), |
|
45
|
|
|
[0, 65535]); |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
// 16-bit floats: 0s |
|
49
|
|
|
it('should turn 2 bytes to 1 16-bit float (0)', function() { |
|
50
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
51
|
|
|
[0, 0], 16, {"base": 10, "float": true}), |
|
52
|
|
|
[0]); |
|
53
|
|
|
}); |
|
54
|
|
|
it('should turn 2 bytes hex to 1 16-bit float (0)', function() { |
|
55
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
56
|
|
|
["0", "0"], 16, {"base": 16, "float": true}), |
|
57
|
|
|
[0]); |
|
58
|
|
|
}); |
|
59
|
|
|
it('should turn 2 bytes bin to 1 16-bit float (0)', function() { |
|
60
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
61
|
|
|
["00000000", "00000000"], 16, {"base": 2, "float": true}), |
|
62
|
|
|
[0]); |
|
63
|
|
|
}); |
|
64
|
|
|
it('should turn 2 bytes bin to 1 16-bit float (0)', function() { |
|
65
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
66
|
|
|
["c0", "00"], 16, {"base": 16, "float": true}), |
|
67
|
|
|
[-2]); |
|
68
|
|
|
}); |
|
69
|
|
|
it('should turn 2 bytes hex to 1 16-bit float (1)', function() { |
|
70
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
71
|
|
|
["3c", "00"], 16, {"base": 16, "float": true}), |
|
72
|
|
|
[1]); |
|
73
|
|
|
}); |
|
74
|
|
|
it('should turn 2 bytes hex to 1 16-bit float (1/3)', function() { |
|
75
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
76
|
|
|
["35", "55"], 16, {"base": 16, "float": true})[0].toFixed(5), |
|
77
|
|
|
0.33325); |
|
78
|
|
|
}); |
|
79
|
|
|
it('should turn 2 bytes hex to 1 16-bit float (0.00006)', function() { |
|
80
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
81
|
|
|
["4", "0"], 16, {"base": 16, "float": true})[0].toFixed(5), |
|
82
|
|
|
0.00006); |
|
83
|
|
|
}); |
|
84
|
|
|
it('should turn 2 bytes hex to 1 16-bit float (65504)', function() { |
|
85
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
86
|
|
|
["7b", "ff"], 16, {"base": 16, "float": true})[0], |
|
87
|
|
|
65504); |
|
88
|
|
|
}); |
|
89
|
|
|
it('should turn 4 bytes hex to 2 16-bit float (65504, 0.33325)', function() { |
|
90
|
|
|
let halfs = byteData.fromBytes(["7b", "ff", "35", "55"], |
|
91
|
|
|
16, {"base": 16, "float": true}); |
|
92
|
|
|
assert.equal(halfs[0], 65504); |
|
93
|
|
|
assert.equal(halfs[1].toFixed(5), 0.33325); |
|
94
|
|
|
}); |
|
95
|
|
|
it('should turn 5 bytes hex to 2 16-bit float (65504, 0.33325, extra byte)', function() { |
|
96
|
|
|
let halfs = byteData.fromBytes(["7b", "ff", "35", "55","80"], |
|
97
|
|
|
16, {"base": 16, "float": true}); |
|
98
|
|
|
assert.equal(halfs[0], 65504); |
|
99
|
|
|
assert.equal(halfs[1].toFixed(5), 0.33325); |
|
100
|
|
|
}); |
|
101
|
|
|
}); |
|
102
|
|
|
|