|
1
|
|
|
|
|
2
|
|
|
var assert = require('assert'); |
|
3
|
|
|
|
|
4
|
|
|
describe('32-bit from bytes', function() { |
|
5
|
|
|
|
|
6
|
|
|
let byteData = require('../../index.js'); |
|
7
|
|
|
|
|
8
|
|
|
// 32 |
|
9
|
|
|
it('should turn 4 bytes bin to 1 32-bit float', function() { |
|
10
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
11
|
|
|
["01011111","01110000","00001001","01000000"], 32, {"base": 2, "float": true})[0].toFixed(7), |
|
12
|
|
|
2.1474836); |
|
13
|
|
|
}); |
|
14
|
|
|
|
|
15
|
|
|
it('should turn 4 bytes hex to 1 32-bit float', function() { |
|
16
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
17
|
|
|
["5f","70","9","40"], 32, {"base": 16, "float": true})[0].toFixed(7), |
|
18
|
|
|
2.1474836); |
|
19
|
|
|
}); |
|
20
|
|
|
it('should turn 3 bytes hex to 0 32-bit float (not enough bytes)', function() { |
|
21
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
22
|
|
|
["5f","70","9"], 32, {"base": 16, "float": true}), |
|
23
|
|
|
[]); |
|
24
|
|
|
}); |
|
25
|
|
|
it('should turn 1 32-bit float to 4 bytes', function() { |
|
26
|
|
|
assert.deepEqual( |
|
27
|
|
|
byteData.fromBytes([95,112,9,64], 32, {"float": true})[0].toFixed(7), |
|
28
|
|
|
2.1474836); |
|
29
|
|
|
}); |
|
30
|
|
|
it('should turn 8 bytes to 2 32-bit floats', function() { |
|
31
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
32
|
|
|
[0,0,0,0,0,0,0,0], 32, {"float": true}), |
|
33
|
|
|
[0,0]); |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
// 32 bit ints |
|
37
|
|
|
it('should turn 8 bytes to 2 32-bit ints (0s)', function() { |
|
38
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
39
|
|
|
[0,0,0,0,0,0,0,0], 32, {"base": 10, "signed": false}), |
|
40
|
|
|
[0,0]); |
|
41
|
|
|
}); |
|
42
|
|
|
|
|
43
|
|
|
it('should turn 8 bytes bin to 2 32-bit ints (max range)', function() { |
|
44
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
45
|
|
|
["00000000","00000000","00000000","10000000", |
|
46
|
|
|
"11111111","11111111","11111111","01111111"], |
|
47
|
|
|
32, {"base": 2, "signed": true}), |
|
48
|
|
|
[-2147483648,2147483647]); |
|
49
|
|
|
}); |
|
50
|
|
|
it('should turn 8 bytes hex to 2 32-bit ints (max range)', function() { |
|
51
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
52
|
|
|
["0","0","0","80", "ff","ff","ff","7f"], |
|
53
|
|
|
32, {"base": 16, "signed": true}), |
|
54
|
|
|
[-2147483648,2147483647]); |
|
55
|
|
|
}); |
|
56
|
|
|
it('should turn 10 bytes hex to 2 32-bit ints (ignore extra bytes) (max range)', function() { |
|
57
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
58
|
|
|
["0","0","0","80", "ff","ff","ff","7f","ff","80"], |
|
59
|
|
|
32, {"base": 16, "signed": true}), |
|
60
|
|
|
[-2147483648,2147483647]); |
|
61
|
|
|
}); |
|
62
|
|
|
it('should turn 4 bytes hex to 1 32-bit ints (random negative)', function() { |
|
63
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
64
|
|
|
["e8","3","0","80"], |
|
65
|
|
|
32, {"base": 16, "signed": true}), |
|
66
|
|
|
[-2147482648]); |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
it('should turn 4 bytes hex to 1 32-bit ints (random negative)', function() { |
|
70
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
71
|
|
|
["0","80","ff","ff"], 32, {"base": 16, "signed": true}), |
|
72
|
|
|
[-32768]); |
|
73
|
|
|
}); |
|
74
|
|
|
it('should turn 8 bytes bin to 2 32-bit uInts (max range)', function() { |
|
75
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
76
|
|
|
["00000000","00000000","00000000","00000000", |
|
77
|
|
|
"11111111","11111111","11111111","11111111"], 32, {"base": 2, "signed": false}), |
|
78
|
|
|
[0,4294967295]); |
|
79
|
|
|
}); |
|
80
|
|
|
it('should turn 8 bytes hex to 2 32-bit uInts (max range)', function() { |
|
81
|
|
|
assert.deepEqual(byteData.fromBytes( |
|
82
|
|
|
["0","0","0","00", "ff","ff","ff","ff"], 32, {"base": 16, "signed": false}), |
|
83
|
|
|
[0,4294967295]); |
|
84
|
|
|
}); |
|
85
|
|
|
}); |
|
86
|
|
|
|