|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import { NumberUtils } from '../../../src/utils/number_utils';
|
3 | 3 |
|
| 4 | +const FLOAT = new Float64Array(1); |
| 5 | +const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8); |
| 6 | + |
| 7 | +FLOAT[0] = -1; |
| 8 | +// Little endian [0, 0, 0, 0, 0, 0, 240, 191] |
| 9 | +// Big endian [240, 191, 0, 0, 0, 0, 0, 0] |
| 10 | +const isBigEndian = FLOAT_BYTES[7] === 0; |
| 11 | + |
4 | 12 | describe('NumberUtils', () => {
|
| 13 | + context(`handles ${isBigEndian ? 'big' : 'little'} endianness correctly`, () => { |
| 14 | + context('getFloat64LE()', () => { |
| 15 | + it('should return -1', () => { |
| 16 | + const res = NumberUtils.getFloat64LE(new Uint8Array([0, 0, 0, 0, 0, 0, 240, 191]), 0); |
| 17 | + expect(res).to.equal(-1); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + context('setFloat64LE()', () => { |
| 22 | + it('should return -1 as little endian bytes', () => { |
| 23 | + const buf = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]); |
| 24 | + NumberUtils.setFloat64LE(buf, 0, -1); |
| 25 | + expect(buf).to.deep.equal(new Uint8Array([0, 0, 0, 0, 0, 0, 240, 191])); |
| 26 | + }); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
5 | 30 | /** Make a Uint8Array in a less verbose way */
|
6 | 31 | const b = (...values) => new Uint8Array(values);
|
7 | 32 |
|
|
0 commit comments