Files
pezkuwi-common/packages/util/src/float/toU8a.spec.ts
T
Satoshi Qazi Muhammed c46ce1a9e6 Move to @pezkuwi/dev 0.86 (eslint 10 toolchain)
- @pezkuwi/dev, dev-test, dev-ts ^0.86.3
- rethrown errors carry the original as `cause` (hw-ledger, address decode)
- optional chain in isEthereumAddress, includes() in bip39 validation
- logger calls the lazy formatter through a typed function
- x-global: an assertion to the type the value already has removed,
  with the comment and directive that explained it
- type-only re-exports of modules that emit no JavaScript
- disable directives renamed for the new rule names, or removed where
  the rule no longer reports
2026-10-09 16:32:28 +04:00

53 lines
1.8 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
import { u8aToHex } from '../u8a/index.js';
import { floatToU8a } from './index.js';
class ExtNumber extends Number {
foo = 'bar';
}
class ExtString extends String {
foo = 'bar';
}
// NOTE Hex value outputs created via online conversion tool:
// https://www.h-schmidt.net/FloatConverter/IEEE754.html
// eslint-disable-next-line @typescript-eslint/no-wrapper-object-types
const TESTS: [isLe: boolean | undefined, bitLength: 32 | 64 | undefined, input: String | string | number | Number, output: string][] = [
[undefined, undefined, +0.0, '0x00000000'],
[undefined, undefined, -0.0, '0x00000080'],
[undefined, undefined, 123.456, '0x79e9f642'],
[undefined, undefined, '123.456', '0x79e9f642'],
[undefined, undefined, new ExtNumber(123.456), '0x79e9f642'],
[undefined, undefined, new ExtString(123.456), '0x79e9f642'],
[true, 32, new ExtString(123.456), '0x79e9f642'],
[true, undefined, Number.NaN, '0x0000c07f'],
[false, undefined, -0.0, '0x80000000'],
[undefined, 64, +0.0, '0x0000000000000000'],
[true, 64, -0.0, '0x0000000000000080'],
[false, 64, -0.0, '0x8000000000000000'],
[undefined, 64, Number.NaN, '0x000000000000f87f']
];
describe('floatToU8a', (): void => {
it('throws on invalid bitLength', (): void => {
expect(
() => floatToU8a(123, { bitLength: 48 as 32 })
).toThrow();
});
describe('conversion tests', (): void => {
TESTS.forEach(([isLe, bitLength, input, output], i): void => {
it(`#${i}: correctly encodes ${typeof input === 'number' ? input : input.toString()} (typeof=${typeof input})`, (): void => {
expect(
u8aToHex(floatToU8a(input, { bitLength, isLe }))
).toEqual(output);
});
});
});
});