| @@ 2-101 (lines=100) @@ | ||
| 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 | ||
| @@ 2-88 (lines=87) @@ | ||
| 1 | ||
| 2 | var assert = require('assert'); |
|
| 3 | ||
| 4 | describe('16-bit to bytes', function() { |
|
| 5 | ||
| 6 | let byteData = require('../../index.js'); |
|
| 7 | ||
| 8 | // 16-bit / 2 bytes |
|
| 9 | // signed |
|
| 10 | it('should turn 2 signed 16-bit ints to 4 bytes (0s)', function() { |
|
| 11 | assert.deepEqual(byteData.toBytes([0, 0], 16, {"base": 10}), |
|
| 12 | [0, 0, 0, 0]); |
|
| 13 | }); |
|
| 14 | it('should turn 1 signed 16-bit int to 2 bytes (0)', function() { |
|
| 15 | assert.deepEqual(byteData.toBytes([0], 16, {"base": 10}), |
|
| 16 | [0, 0]); |
|
| 17 | }); |
|
| 18 | it('should turn 2 signed 16-bit ints to 4 bytes (max range)', function() { |
|
| 19 | assert.deepEqual(byteData.toBytes([-32768, 32767], 16, {"base": 10}), |
|
| 20 | [0, 128, 255, 127] |
|
| 21 | ); |
|
| 22 | }); |
|
| 23 | it('should turn 1 16-bit signed int to 1 byte hex (-1)', function() { |
|
| 24 | assert.deepEqual(byteData.toBytes([-1], 16, {"base": 16}), |
|
| 25 | ['ff', 'ff']); |
|
| 26 | }); |
|
| 27 | ||
| 28 | // unsigned |
|
| 29 | it('should turn 2 unsigned 16-bit ints to 4 bytes (0s)', function() { |
|
| 30 | assert.deepEqual(byteData.toBytes([0, 0], 16, {"base": 10}), |
|
| 31 | [0, 0, 0, 0]); |
|
| 32 | }); |
|
| 33 | it('should turn 1 unsigned 16-bit int to 2 bytes (0)', function() { |
|
| 34 | assert.deepEqual(byteData.toBytes([0], 16, {"base": 10}), |
|
| 35 | [0, 0]); |
|
| 36 | }); |
|
| 37 | it('should turn 2 unsigned 16-bit ints to 4 bytes (max range)', function() { |
|
| 38 | assert.deepEqual(byteData.toBytes([0, 65535], 16, {"base": 10}), |
|
| 39 | [0, 0, 255, 255] |
|
| 40 | ); |
|
| 41 | }); |
|
| 42 | it('should turn 1 unsigned 16-bit int to 2 bytes (0)', function() { |
|
| 43 | assert.deepEqual(byteData.toBytes([765], 16, {"base": 16}), |
|
| 44 | ["fd", "02"]); |
|
| 45 | }); |
|
| 46 | ||
| 47 | // 16-bit floats: 0s |
|
| 48 | it('should turn 2 bytes to 1 16-bit float (0)', function() { |
|
| 49 | assert.deepEqual(byteData.toBytes( |
|
| 50 | [0], 16, {"base": 10, "float": true}), |
|
| 51 | [0, 0]); |
|
| 52 | }); |
|
| 53 | it('should turn 2 bytes hex to 1 16-bit float (0)', function() { |
|
| 54 | assert.deepEqual(byteData.toBytes( |
|
| 55 | [0], 16, {"base": 10, "float": true}), |
|
| 56 | ["00", "00"]); |
|
| 57 | }); |
|
| 58 | it('should turn 2 bytes bin to 1 16-bit float (0)', function() { |
|
| 59 | assert.deepEqual(byteData.toBytes( |
|
| 60 | [0], 16, {"base": 2, "float": true}), |
|
| 61 | ["00000000", "00000000"]); |
|
| 62 | }); |
|
| 63 | it('should turn 2 bytes hex to 1 16-bit float (1)', function() { |
|
| 64 | assert.deepEqual(byteData.toBytes( |
|
| 65 | [1], 16, {"base": 16, "float": true}), |
|
| 66 | ["3c", "00"]); |
|
| 67 | }); |
|
| 68 | it('should turn 2 bytes hex to 1 16-bit float (1/3)', function() { |
|
| 69 | assert.deepEqual(byteData.toBytes( |
|
| 70 | [0.33325], 16, {"base": 16, "float": true}), |
|
| 71 | ["35", "55"]); |
|
| 72 | }); |
|
| 73 | it('should turn 2 bytes hex to 1 16-bit float (-2)', function() { |
|
| 74 | assert.deepEqual(byteData.toBytes( |
|
| 75 | [-2], 16, {"base": 16, "float": true}), |
|
| 76 | ["c0", "00"]); |
|
| 77 | }); |
|
| 78 | it('should turn 2 bytes hex to 1 16-bit float (65504)', function() { |
|
| 79 | assert.deepEqual(byteData.toBytes( |
|
| 80 | [65504], 16, {"base": 16, "float": true}), |
|
| 81 | ["7b", "ff"]); |
|
| 82 | }); |
|
| 83 | it('should turn 4 bytes hex to 2 16-bit float (65504, 0.33325, extra byte)', function() { |
|
| 84 | assert.deepEqual(byteData.toBytes( |
|
| 85 | [65504, 0.33325], 16, {"base": 16, "float": true}), |
|
| 86 | ["7b", "ff", "35", "55"]); |
|
| 87 | }); |
|
| 88 | }); |
|
| 89 | ||
| @@ 2-79 (lines=78) @@ | ||
| 1 | ||
| 2 | var assert = require('assert'); |
|
| 3 | ||
| 4 | describe('crumbs from bytes', function() { |
|
| 5 | let byteData = require('../../index.js'); |
|
| 6 | ||
| 7 | // 2-bit |
|
| 8 | it('should turn 1 2-bit unsigned int to 2 crumb (0s)', function() { |
|
| 9 | assert.deepEqual(byteData.fromBytes([0], 2, {"base": 10}), |
|
| 10 | [0]); |
|
| 11 | }); |
|
| 12 | it('should turn 1 2-bit unsigned int to 2 crumb (0s)', function() { |
|
| 13 | assert.deepEqual(byteData.fromBytes([1], 2, {"base": 10}), |
|
| 14 | [1]); |
|
| 15 | }); |
|
| 16 | it('should turn 1 2-bit unsigned int to 2 crumb (0s)', function() { |
|
| 17 | assert.deepEqual(byteData.fromBytes([2], 2, {"base": 10}), |
|
| 18 | [2]); |
|
| 19 | }); |
|
| 20 | it('should turn 1 2-bit unsigned int to 2 crumb (0s)', function() { |
|
| 21 | assert.deepEqual(byteData.fromBytes([3], 2, {"base": 10}), |
|
| 22 | [3]); |
|
| 23 | }); |
|
| 24 | ||
| 25 | it('should turn 1 2-bit unsigned int to 2 crumb (0s)', function() { |
|
| 26 | assert.deepEqual(byteData.fromBytes(['00'], 2, {"base": 2}), |
|
| 27 | [0]); |
|
| 28 | }); |
|
| 29 | it('should turn 1 2-bit unsigned int to 2 crumb (0s)', function() { |
|
| 30 | assert.deepEqual(byteData.fromBytes(['01'], 2, {"base": 2}), |
|
| 31 | [1]); |
|
| 32 | }); |
|
| 33 | it('should turn 1 2-bit unsigned int to 2 crumb (0s)', function() { |
|
| 34 | assert.deepEqual(byteData.fromBytes(['10'], 2, {"base": 2}), |
|
| 35 | [2]); |
|
| 36 | }); |
|
| 37 | it('should turn 1 2-bit unsigned int to 2 crumb (0s)', function() { |
|
| 38 | assert.deepEqual(byteData.fromBytes(['11'], 2, {"base": 2}), |
|
| 39 | [3]); |
|
| 40 | }); |
|
| 41 | ||
| 42 | it('should turn 1 2-bit signed int to 2 crumb (0s)', function() { |
|
| 43 | assert.deepEqual(byteData.fromBytes(['10'], 2, {"base": 2, "signed": true}), |
|
| 44 | [-2]); |
|
| 45 | }); |
|
| 46 | it('should turn 1 2-bit signed int to 2 crumb (0s)', function() { |
|
| 47 | assert.deepEqual(byteData.fromBytes(['11'], 2, {"base": 2, "signed": true}), |
|
| 48 | [-1]); |
|
| 49 | }); |
|
| 50 | it('should turn 1 2-bit signed int to 2 crumb (0s)', function() { |
|
| 51 | assert.deepEqual(byteData.fromBytes(['00'], 2, {"base": 2, "signed": false}), |
|
| 52 | [0]); |
|
| 53 | }); |
|
| 54 | it('should turn 1 2-bit signed int to 2 crumb (0s)', function() { |
|
| 55 | assert.deepEqual(byteData.fromBytes(['01'], 2, {"base": 2, "signed": false}), |
|
| 56 | [1]); |
|
| 57 | }); |
|
| 58 | ||
| 59 | it('should turn 1 2-bit signed int to 2 bytes (-2)', function() { |
|
| 60 | assert.deepEqual(byteData.fromBytes([2], 2, {"base": 10, "signed": true}), |
|
| 61 | [-2]); |
|
| 62 | }); |
|
| 63 | it('should turn 1 2-bit signed int to 1 crumb (-1)', function() { |
|
| 64 | assert.deepEqual(byteData.fromBytes([3], 2, {"base": 10, "signed": true}), |
|
| 65 | [-1]); |
|
| 66 | }); |
|
| 67 | it('should turn 1 2-bit signed int to 1 crumb hex (-1)', function() { |
|
| 68 | assert.deepEqual(byteData.fromBytes(['03'], 2, {"base": 16, "signed": true}), |
|
| 69 | [-1]); |
|
| 70 | }); |
|
| 71 | it('should turn 1 2-bit unsigned int to 1 crumb hex (2)', function() { |
|
| 72 | assert.deepEqual(byteData.fromBytes(['02'], 2, {"base": 16, "signed": false}), |
|
| 73 | [2]); |
|
| 74 | }); |
|
| 75 | it('should turn 1 2-bit unsigned int to 1 crumb bin (1)', function() { |
|
| 76 | assert.deepEqual(byteData.fromBytes(['01'], 2, {"base": 2, "signed": false}), |
|
| 77 | [1]); |
|
| 78 | }); |
|
| 79 | }); |
|
| 80 | ||
| @@ 2-65 (lines=64) @@ | ||
| 1 | ||
| 2 | var assert = require('assert'); |
|
| 3 | ||
| 4 | describe('64-bit to bytes', function() { |
|
| 5 | ||
| 6 | let byteData = require('../../index.js'); |
|
| 7 | ||
| 8 | // 64-bit / 8 bytes |
|
| 9 | it('should turn 2 64-bit floats to 16 bytes (-1, 1)', function() { |
|
| 10 | assert.deepEqual(byteData.toBytes([1,-1], 64, {"base": 10}), |
|
| 11 | [0,0,0,0,0,0,240,63,0,0,0,0,0,0,240,191]); |
|
| 12 | }); |
|
| 13 | it('should turn 1 64-bit floats to 16 bytes hex (-1)', function() { |
|
| 14 | assert.deepEqual(byteData.toBytes([-1], 64, {"base": 16}), |
|
| 15 | ['00','00','00','00','00','00','f0','bf']); |
|
| 16 | }); |
|
| 17 | it('should turn 1 64-bit floats to 16 bytes hex (-0.5)', function() { |
|
| 18 | assert.deepEqual(byteData.toBytes([-0.5], 64, {"base": 16}), |
|
| 19 | ['00','00','00','00','00','00','e0','bf']); |
|
| 20 | }); |
|
| 21 | it('should turn 2 64-bit floats to 16 bytes (0s)', function() { |
|
| 22 | assert.deepEqual(byteData.toBytes([0, 0], 64, {"base": 10}), |
|
| 23 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]); |
|
| 24 | }); |
|
| 25 | it('should turn 3 64-bit floats to 16 bytes (0 0 1)', function() { |
|
| 26 | assert.deepEqual(byteData.toBytes([0, 0, 1], 64, {"base": 10}), |
|
| 27 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,240,63]); |
|
| 28 | }); |
|
| 29 | it('should turn 3 64-bit floats to 16 bytes (0 1 0)', function() { |
|
| 30 | assert.deepEqual(byteData.toBytes([0, 1, 0], 64, {"base": 10}), |
|
| 31 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,240,63,0,0,0,0,0,0,0,0]); |
|
| 32 | }); |
|
| 33 | it('should turn 1 64-bit floats to 8 bytes (0.5)', function() { |
|
| 34 | assert.deepEqual(byteData.toBytes([0.5], 64, {"base": 10}), |
|
| 35 | [0,0,0,0,0,0,224,63]); |
|
| 36 | }); |
|
| 37 | it('should turn 1 64-bit float to 8 bytes (-0.5)', function() { |
|
| 38 | assert.deepEqual(byteData.toBytes([-0.5], 64, {"base": 10}), |
|
| 39 | [0,0,0,0,0,0,224,191]); |
|
| 40 | }); |
|
| 41 | it('should turn 1 64-bit float to 8 bytes (pi)', function() { |
|
| 42 | assert.deepEqual(byteData.toBytes([3.141592653589793], 64, {"base": 10}), |
|
| 43 | [24,45,68,84,251,33,9,64]); |
|
| 44 | }); |
|
| 45 | it('should turn 1 64-bit float to 8 bytes (pi)', function() { |
|
| 46 | assert.deepEqual(byteData.toBytes([9], 64, {"base": 10}), |
|
| 47 | [0,0,0,0,0,0,34,64]); |
|
| 48 | }); |
|
| 49 | it('should turn 1 64-bit float to 8 bytes (14)', function() { |
|
| 50 | assert.deepEqual(byteData.toBytes([31.41592653589793], 64, {"base": 10}), |
|
| 51 | [94,56,85,41,122,106,63,64]); |
|
| 52 | }); |
|
| 53 | it('should turn 1 64-bit float to 8 bytes (1)', function() { |
|
| 54 | assert.deepEqual(byteData.toBytes([314159265358979.3], 64, {"base": 10}), |
|
| 55 | [53,72,162,118,158,219,241,66]); |
|
| 56 | }); |
|
| 57 | it('should turn 1 64-bit float to 8 bytes (hex) (0)', function() { |
|
| 58 | assert.deepEqual(byteData.toBytes([0], 64, {"base": 16}), |
|
| 59 | ["00","00","00","00","00","00","00","00"]); |
|
| 60 | }) |
|
| 61 | it('should turn 1 64-bit float to 8 bytes hex (2)', function() { |
|
| 62 | assert.deepEqual(byteData.toBytes([2], 64, {"base": 16}), |
|
| 63 | ["00","00","00","00","00","00","00","40"]); |
|
| 64 | }); |
|
| 65 | }); |
|
| 66 | ||