quantum-coin-js-sdk 2.0.1 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/index.d.ts +9 -0
- package/index.js +37 -4
- package/package.json +12 -4
- package/wasmBase64.js +3 -2
- package/.gitignore +0 -4
- package/.idea/.name +0 -1
- package/.idea/modules.xml +0 -8
- package/.idea/quantum-coin-js-sdk.iml +0 -12
- package/.idea/vcs.xml +0 -6
- package/.vs/ProjectSettings.json +0 -3
- package/.vs/VSWorkspaceState.json +0 -8
- package/.vs/quantum-coin-js-sdk/FileContentIndex/a9d6de82-cfc3-4c22-ac38-06d706ff3673.vsidx +0 -0
- package/.vs/quantum-coin-js-sdk/FileContentIndex/fdc816df-1898-4153-99f2-c69ef4c2283b.vsidx +0 -0
- package/.vs/quantum-coin-js-sdk/config/applicationhost.config +0 -1011
- package/.vs/quantum-coin-js-sdk/v17/.wsuo +0 -0
- package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.backup.json +0 -39
- package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.json +0 -57
- package/.vs/slnx.sqlite +0 -0
- package/example/README.md +0 -14
- package/example/conversion-example.js +0 -19
- package/example/example-create-contract.js +0 -396
- package/example/example-encode-decode-rlp.js +0 -225
- package/example/example-event-pack-unpack.js +0 -391
- package/example/example-misc.js +0 -96
- package/example/example-token-pack-unpack.js +0 -960
- package/example/example-wallet-version4.js +0 -34
- package/example/example-wallet.js +0 -39
- package/example/example.js +0 -115
- package/example/node_modules/.bin/sdkgen +0 -16
- package/example/node_modules/.bin/sdkgen.cmd +0 -17
- package/example/node_modules/.bin/sdkgen.ps1 +0 -28
- package/example/node_modules/.package-lock.json +0 -51
- package/example/node_modules/quantumcoin/.github/workflows/publish-npmjs.yaml +0 -22
- package/example/node_modules/quantumcoin/LICENSE +0 -21
- package/example/node_modules/quantumcoin/README-SDK.md +0 -828
- package/example/node_modules/quantumcoin/README.md +0 -197
- package/example/node_modules/quantumcoin/config.d.ts +0 -50
- package/example/node_modules/quantumcoin/config.js +0 -123
- package/example/node_modules/quantumcoin/examples/AllSolidityTypes.sol +0 -184
- package/example/node_modules/quantumcoin/examples/SimpleIERC20.sol +0 -74
- package/example/node_modules/quantumcoin/examples/events.js +0 -41
- package/example/node_modules/quantumcoin/examples/events.ts +0 -35
- package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.js +0 -100
- package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.ts +0 -77
- package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.js +0 -100
- package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.ts +0 -77
- package/example/node_modules/quantumcoin/examples/example.js +0 -67
- package/example/node_modules/quantumcoin/examples/example.ts +0 -56
- package/example/node_modules/quantumcoin/examples/offline-signing.js +0 -77
- package/example/node_modules/quantumcoin/examples/offline-signing.ts +0 -65
- package/example/node_modules/quantumcoin/examples/package-lock.json +0 -596
- package/example/node_modules/quantumcoin/examples/package.json +0 -32
- package/example/node_modules/quantumcoin/examples/read-operations.js +0 -32
- package/example/node_modules/quantumcoin/examples/read-operations.ts +0 -31
- package/example/node_modules/quantumcoin/examples/sdk-generator-erc20.inline.json +0 -251
- package/example/node_modules/quantumcoin/examples/solidity-types.ts +0 -43
- package/example/node_modules/quantumcoin/examples/wallet-offline.js +0 -36
- package/example/node_modules/quantumcoin/examples/wallet-offline.ts +0 -26
- package/example/node_modules/quantumcoin/generate-sdk.js +0 -1846
- package/example/node_modules/quantumcoin/index.js +0 -12
- package/example/node_modules/quantumcoin/package.json +0 -95
- package/example/node_modules/quantumcoin/scripts/copy-declarations.js +0 -31
- package/example/node_modules/quantumcoin/scripts/run-all-one-by-one.js +0 -151
- package/example/node_modules/quantumcoin/src/abi/fragments.d.ts +0 -42
- package/example/node_modules/quantumcoin/src/abi/fragments.js +0 -63
- package/example/node_modules/quantumcoin/src/abi/index.d.ts +0 -13
- package/example/node_modules/quantumcoin/src/abi/index.js +0 -9
- package/example/node_modules/quantumcoin/src/abi/interface.d.ts +0 -128
- package/example/node_modules/quantumcoin/src/abi/interface.js +0 -605
- package/example/node_modules/quantumcoin/src/abi/js-abi-coder.d.ts +0 -8
- package/example/node_modules/quantumcoin/src/abi/js-abi-coder.js +0 -538
- package/example/node_modules/quantumcoin/src/constants.d.ts +0 -61
- package/example/node_modules/quantumcoin/src/constants.js +0 -94
- package/example/node_modules/quantumcoin/src/contract/contract-factory.d.ts +0 -28
- package/example/node_modules/quantumcoin/src/contract/contract-factory.js +0 -111
- package/example/node_modules/quantumcoin/src/contract/contract.d.ts +0 -113
- package/example/node_modules/quantumcoin/src/contract/contract.js +0 -408
- package/example/node_modules/quantumcoin/src/contract/index.d.ts +0 -9
- package/example/node_modules/quantumcoin/src/contract/index.js +0 -9
- package/example/node_modules/quantumcoin/src/errors/index.d.ts +0 -92
- package/example/node_modules/quantumcoin/src/errors/index.js +0 -200
- package/example/node_modules/quantumcoin/src/generator/index.d.ts +0 -81
- package/example/node_modules/quantumcoin/src/generator/index.js +0 -1571
- package/example/node_modules/quantumcoin/src/index.d.ts +0 -122
- package/example/node_modules/quantumcoin/src/index.js +0 -41
- package/example/node_modules/quantumcoin/src/internal/hex.d.ts +0 -68
- package/example/node_modules/quantumcoin/src/internal/hex.js +0 -180
- package/example/node_modules/quantumcoin/src/providers/extra-providers.d.ts +0 -139
- package/example/node_modules/quantumcoin/src/providers/extra-providers.js +0 -614
- package/example/node_modules/quantumcoin/src/providers/index.d.ts +0 -17
- package/example/node_modules/quantumcoin/src/providers/index.js +0 -10
- package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.d.ts +0 -12
- package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.js +0 -89
- package/example/node_modules/quantumcoin/src/providers/provider.d.ts +0 -206
- package/example/node_modules/quantumcoin/src/providers/provider.js +0 -524
- package/example/node_modules/quantumcoin/src/types/index.d.ts +0 -214
- package/example/node_modules/quantumcoin/src/types/index.js +0 -9
- package/example/node_modules/quantumcoin/src/utils/address.d.ts +0 -58
- package/example/node_modules/quantumcoin/src/utils/address.js +0 -144
- package/example/node_modules/quantumcoin/src/utils/encoding.d.ts +0 -120
- package/example/node_modules/quantumcoin/src/utils/encoding.js +0 -306
- package/example/node_modules/quantumcoin/src/utils/fixednumber.d.ts +0 -57
- package/example/node_modules/quantumcoin/src/utils/fixednumber.js +0 -366
- package/example/node_modules/quantumcoin/src/utils/hashing.d.ts +0 -76
- package/example/node_modules/quantumcoin/src/utils/hashing.js +0 -298
- package/example/node_modules/quantumcoin/src/utils/index.d.ts +0 -63
- package/example/node_modules/quantumcoin/src/utils/index.js +0 -14
- package/example/node_modules/quantumcoin/src/utils/result.d.ts +0 -57
- package/example/node_modules/quantumcoin/src/utils/result.js +0 -128
- package/example/node_modules/quantumcoin/src/utils/rlp.d.ts +0 -12
- package/example/node_modules/quantumcoin/src/utils/rlp.js +0 -215
- package/example/node_modules/quantumcoin/src/utils/units.d.ts +0 -29
- package/example/node_modules/quantumcoin/src/utils/units.js +0 -107
- package/example/node_modules/quantumcoin/src/wallet/index.d.ts +0 -10
- package/example/node_modules/quantumcoin/src/wallet/index.js +0 -8
- package/example/node_modules/quantumcoin/src/wallet/wallet.d.ts +0 -192
- package/example/node_modules/quantumcoin/src/wallet/wallet.js +0 -713
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.js +0 -207
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.ts +0 -191
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.fixtures.js +0 -231
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +0 -387
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +0 -350
- package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.js +0 -165
- package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.ts +0 -160
- package/example/node_modules/quantumcoin/test/e2e/helpers.js +0 -59
- package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.js +0 -141
- package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.ts +0 -128
- package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.js +0 -168
- package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +0 -141
- package/example/node_modules/quantumcoin/test/e2e/transactional.test.js +0 -245
- package/example/node_modules/quantumcoin/test/e2e/transactional.test.ts +0 -208
- package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.js +0 -407
- package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.ts +0 -337
- package/example/node_modules/quantumcoin/test/fixtures/ConstructorParam.sol +0 -23
- package/example/node_modules/quantumcoin/test/fixtures/MultiContracts.sol +0 -37
- package/example/node_modules/quantumcoin/test/fixtures/SimpleStorage.sol +0 -18
- package/example/node_modules/quantumcoin/test/fixtures/StakingContract.abi.json +0 -1
- package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.js +0 -49
- package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.ts +0 -44
- package/example/node_modules/quantumcoin/test/integration/provider.test.js +0 -88
- package/example/node_modules/quantumcoin/test/integration/provider.test.ts +0 -85
- package/example/node_modules/quantumcoin/test/integration/ws-provider.test.js +0 -41
- package/example/node_modules/quantumcoin/test/integration/ws-provider.test.ts +0 -38
- package/example/node_modules/quantumcoin/test/security/abi-decoder-bounds.test.js +0 -122
- package/example/node_modules/quantumcoin/test/security/contract-overrides.test.js +0 -112
- package/example/node_modules/quantumcoin/test/security/generator-injection.test.js +0 -195
- package/example/node_modules/quantumcoin/test/security/malformed-input.test.js +0 -330
- package/example/node_modules/quantumcoin/test/security/malformed-input.test.ts +0 -35
- package/example/node_modules/quantumcoin/test/security/rpc-numeric-bounds.test.js +0 -81
- package/example/node_modules/quantumcoin/test/security/rpc-trust.test.js +0 -202
- package/example/node_modules/quantumcoin/test/unit/_encrypted-output.txt +0 -6
- package/example/node_modules/quantumcoin/test/unit/_log-encrypted-jsons.js +0 -45
- package/example/node_modules/quantumcoin/test/unit/_write-keystore-fixture.js +0 -16
- package/example/node_modules/quantumcoin/test/unit/abi-interface.test.js +0 -110
- package/example/node_modules/quantumcoin/test/unit/abi-interface.test.ts +0 -109
- package/example/node_modules/quantumcoin/test/unit/address-wallet.test.js +0 -923
- package/example/node_modules/quantumcoin/test/unit/address-wallet.test.ts +0 -877
- package/example/node_modules/quantumcoin/test/unit/browser-provider.test.js +0 -85
- package/example/node_modules/quantumcoin/test/unit/browser-provider.test.ts +0 -79
- package/example/node_modules/quantumcoin/test/unit/contract.test.js +0 -85
- package/example/node_modules/quantumcoin/test/unit/contract.test.ts +0 -83
- package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.js +0 -127
- package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.ts +0 -91
- package/example/node_modules/quantumcoin/test/unit/errors.test.js +0 -77
- package/example/node_modules/quantumcoin/test/unit/errors.test.ts +0 -76
- package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.js +0 -55
- package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.ts +0 -54
- package/example/node_modules/quantumcoin/test/unit/fixednumber.test.js +0 -656
- package/example/node_modules/quantumcoin/test/unit/fixednumber.test.ts +0 -660
- package/example/node_modules/quantumcoin/test/unit/fixtures/encrypted-keystores-48-32-36.js +0 -9
- package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.js +0 -42
- package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.ts +0 -41
- package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.js +0 -113
- package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.ts +0 -110
- package/example/node_modules/quantumcoin/test/unit/generator.test.js +0 -149
- package/example/node_modules/quantumcoin/test/unit/generator.test.ts +0 -148
- package/example/node_modules/quantumcoin/test/unit/hashing.test.js +0 -64
- package/example/node_modules/quantumcoin/test/unit/hashing.test.ts +0 -63
- package/example/node_modules/quantumcoin/test/unit/init.test.js +0 -39
- package/example/node_modules/quantumcoin/test/unit/init.test.ts +0 -38
- package/example/node_modules/quantumcoin/test/unit/interface.test.js +0 -56
- package/example/node_modules/quantumcoin/test/unit/interface.test.ts +0 -54
- package/example/node_modules/quantumcoin/test/unit/internal-hex.test.js +0 -81
- package/example/node_modules/quantumcoin/test/unit/internal-hex.test.ts +0 -80
- package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.js +0 -98
- package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.ts +0 -64
- package/example/node_modules/quantumcoin/test/unit/providers.test.js +0 -252
- package/example/node_modules/quantumcoin/test/unit/providers.test.ts +0 -251
- package/example/node_modules/quantumcoin/test/unit/result.test.js +0 -80
- package/example/node_modules/quantumcoin/test/unit/result.test.ts +0 -79
- package/example/node_modules/quantumcoin/test/unit/solidity-types.test.js +0 -49
- package/example/node_modules/quantumcoin/test/unit/solidity-types.test.ts +0 -39
- package/example/node_modules/quantumcoin/test/unit/utils.test.js +0 -57
- package/example/node_modules/quantumcoin/test/unit/utils.test.ts +0 -56
- package/example/node_modules/quantumcoin/test/verbose-logger.js +0 -74
- package/example/node_modules/quantumcoin/tsconfig.build.json +0 -14
- package/example/node_modules/seed-words/.github/workflows/publish-npmjs.yaml +0 -22
- package/example/node_modules/seed-words/BUILD.md +0 -7
- package/example/node_modules/seed-words/LICENSE +0 -121
- package/example/node_modules/seed-words/README.md +0 -67
- package/example/node_modules/seed-words/dist/seedwords.d.ts +0 -39
- package/example/node_modules/seed-words/package.json +0 -27
- package/example/node_modules/seed-words/seedwords.js +0 -315
- package/example/node_modules/seed-words/seedwords.txt +0 -65536
- package/example/node_modules/seed-words/tsconfig.json +0 -21
- package/example/package-lock.json +0 -60
- package/example/package.json +0 -15
- package/scripts/build-browser-tests.js +0 -29
- package/scripts/build-wasm.js +0 -85
- package/tests/browser/bundle.js +0 -97643
- package/tests/browser/entry.js +0 -57
- package/tests/browser/harness.html +0 -20
- package/tests/browser/run-browser.js +0 -50
- package/tests/encrypted-32.json +0 -1
- package/tests/encrypted-36.json +0 -1
- package/tests/encrypted-48.json +0 -1
- package/tests/generate-verify-vectors.js +0 -88
- package/tests/get-gas-price.test.js +0 -59
- package/tests/hashing.test.js +0 -34
- package/tests/non-transactional.preinit.test.js +0 -33
- package/tests/non-transactional.test.js +0 -1318
- package/tests/sign-raw-keytype5-context-null.test.js +0 -105
- package/tests/sign-raw-transaction.test.js +0 -194
- package/tests/sign-verify.test.js +0 -308
- package/tests/specs/assert.js +0 -74
- package/tests/specs/crypto.spec.js +0 -104
- package/tests/specs/hex.js +0 -31
- package/tests/specs/preinit.spec.js +0 -19
- package/tests/specs/sign.spec.js +0 -90
- package/tests/specs/wallet.spec.js +0 -98
- package/tests/transactional.rpc.test.js +0 -102
- package/tests/verify-vectors.json +0 -95035
|
@@ -1,656 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @testCategory unit
|
|
3
|
-
* @blockchainRequired false
|
|
4
|
-
* @transactional false
|
|
5
|
-
* @description FixedNumber: creation, arithmetic, comparison, rounding, conversion, and error handling
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const { describe, it } = require("node:test");
|
|
9
|
-
const assert = require("node:assert/strict");
|
|
10
|
-
const qc = require("../../index");
|
|
11
|
-
|
|
12
|
-
const { FixedNumber } = qc;
|
|
13
|
-
|
|
14
|
-
// ---------------------------------------------------------------------------
|
|
15
|
-
// Ported from ethers.js v5 test-utils.ts (FixedNumber section)
|
|
16
|
-
// ---------------------------------------------------------------------------
|
|
17
|
-
|
|
18
|
-
describe("FixedNumber (ethers v5 ported)", () => {
|
|
19
|
-
describe("Creation from value", () => {
|
|
20
|
-
const tests = [
|
|
21
|
-
{ value: "0.0", expected: "0.0" },
|
|
22
|
-
{ value: "-0.0", expected: "0.0" },
|
|
23
|
-
{ value: "1.0", expected: "1.0" },
|
|
24
|
-
{ value: "1.00", expected: "1.0" },
|
|
25
|
-
{ value: "01.00", expected: "1.0" },
|
|
26
|
-
{ value: 1, expected: "1.0" },
|
|
27
|
-
{ value: "-1.0", expected: "-1.0" },
|
|
28
|
-
{ value: "-1.00", expected: "-1.0" },
|
|
29
|
-
{ value: "-01.00", expected: "-1.0" },
|
|
30
|
-
{ value: -1, expected: "-1.0" },
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
for (const test of tests) {
|
|
34
|
-
it(`from(${test.value}) => "${test.expected}"`, () => {
|
|
35
|
-
const value = FixedNumber.from(test.value);
|
|
36
|
-
assert.equal(value.toString(), test.expected);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
describe("Rounding", () => {
|
|
42
|
-
const tests = [
|
|
43
|
-
{ value: "1.0", round: 1, expected: "1.0" },
|
|
44
|
-
{ value: "1.4", round: 1, expected: "1.4" },
|
|
45
|
-
{ value: "1.4", round: 2, expected: "1.4" },
|
|
46
|
-
{ value: "1.4", round: 0, expected: "1.0" },
|
|
47
|
-
{ value: "1.5", round: 0, expected: "2.0" },
|
|
48
|
-
{ value: "1.6", round: 0, expected: "2.0" },
|
|
49
|
-
{ value: "-1.0", round: 1, expected: "-1.0" },
|
|
50
|
-
{ value: "-1.4", round: 1, expected: "-1.4" },
|
|
51
|
-
{ value: "-1.4", round: 2, expected: "-1.4" },
|
|
52
|
-
{ value: "-1.4", round: 0, expected: "-1.0" },
|
|
53
|
-
{ value: "-1.5", round: 0, expected: "-2.0" },
|
|
54
|
-
{ value: "-1.6", round: 0, expected: "-2.0" },
|
|
55
|
-
{ value: "1.51", round: 1, expected: "1.5" },
|
|
56
|
-
{ value: "1.55", round: 1, expected: "1.6" },
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
for (const test of tests) {
|
|
60
|
-
it(`round(${test.value}, ${test.round}) => "${test.expected}"`, () => {
|
|
61
|
-
const value = FixedNumber.from(test.value).round(test.round);
|
|
62
|
-
assert.equal(value.toString(), test.expected);
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
describe("Floor / Ceiling", () => {
|
|
68
|
-
const tests = [
|
|
69
|
-
{ value: "1.0", ceiling: "1.0", floor: "1.0" },
|
|
70
|
-
{ value: "1.1", ceiling: "2.0", floor: "1.0" },
|
|
71
|
-
{ value: "1.9", ceiling: "2.0", floor: "1.0" },
|
|
72
|
-
{ value: "-1.0", ceiling: "-1.0", floor: "-1.0" },
|
|
73
|
-
{ value: "-1.1", ceiling: "-1.0", floor: "-2.0" },
|
|
74
|
-
{ value: "-1.9", ceiling: "-1.0", floor: "-2.0" },
|
|
75
|
-
];
|
|
76
|
-
|
|
77
|
-
for (const test of tests) {
|
|
78
|
-
it(`floor/ceiling(${test.value})`, () => {
|
|
79
|
-
const value = FixedNumber.from(test.value);
|
|
80
|
-
assert.equal(value.floor().toString(), test.floor);
|
|
81
|
-
assert.equal(value.ceiling().toString(), test.ceiling);
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// ---------------------------------------------------------------------------
|
|
88
|
-
// New v6 API tests
|
|
89
|
-
// ---------------------------------------------------------------------------
|
|
90
|
-
|
|
91
|
-
describe("FixedNumber (v6 API)", () => {
|
|
92
|
-
describe("Properties", () => {
|
|
93
|
-
it("exposes format, signed, width, decimals, value", () => {
|
|
94
|
-
const f = FixedNumber.fromString("1.5");
|
|
95
|
-
assert.equal(f.format, "fixed128x18");
|
|
96
|
-
assert.equal(f.signed, true);
|
|
97
|
-
assert.equal(f.width, 128);
|
|
98
|
-
assert.equal(f.decimals, 18);
|
|
99
|
-
assert.equal(typeof f.value, "bigint");
|
|
100
|
-
assert.equal(f.value, 1500000000000000000n);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it("unsigned format properties", () => {
|
|
104
|
-
const f = FixedNumber.fromString("10", "ufixed32x0");
|
|
105
|
-
assert.equal(f.format, "ufixed32x0");
|
|
106
|
-
assert.equal(f.signed, false);
|
|
107
|
-
assert.equal(f.width, 32);
|
|
108
|
-
assert.equal(f.decimals, 0);
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
describe("fromString", () => {
|
|
113
|
-
it("parses with custom format", () => {
|
|
114
|
-
const f = FixedNumber.fromString("3.14", "fixed128x2");
|
|
115
|
-
assert.equal(f.toString(), "3.14");
|
|
116
|
-
assert.equal(f.value, 314n);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("parses zero-decimal format", () => {
|
|
120
|
-
const f = FixedNumber.fromString("42", "fixed128x0");
|
|
121
|
-
assert.equal(f.toString(), "42");
|
|
122
|
-
assert.equal(f.value, 42n);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it("parses unsigned format", () => {
|
|
126
|
-
const f = FixedNumber.fromString("255", "ufixed8x0");
|
|
127
|
-
assert.equal(f.toString(), "255");
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
describe("fromValue", () => {
|
|
132
|
-
it("creates from bigint with decimals", () => {
|
|
133
|
-
const f = FixedNumber.fromValue(1500000000000000000n, 18);
|
|
134
|
-
assert.equal(f.toString(), "1.5");
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it("creates from number", () => {
|
|
138
|
-
const f = FixedNumber.fromValue(100, 2, "fixed128x2");
|
|
139
|
-
assert.equal(f.toString(), "1.0");
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe("fromBytes round-trip", () => {
|
|
144
|
-
it("roundtrips through hex bytes", () => {
|
|
145
|
-
const f1 = FixedNumber.fromString("42", "ufixed32x0");
|
|
146
|
-
const hex = f1.toHexString();
|
|
147
|
-
const bytes = Buffer.from(hex.replace(/^0x/, ""), "hex");
|
|
148
|
-
const f2 = FixedNumber.fromBytes(bytes, "ufixed32x0");
|
|
149
|
-
assert.equal(f2.toString(), "42");
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
describe("from() dispatch", () => {
|
|
154
|
-
it("from string", () => {
|
|
155
|
-
assert.equal(FixedNumber.from("2.5").toString(), "2.5");
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
it("from number", () => {
|
|
159
|
-
assert.equal(FixedNumber.from(3).toString(), "3.0");
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it("from bigint", () => {
|
|
163
|
-
assert.equal(FixedNumber.from(5n).toString(), "5.0");
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
describe("Safe arithmetic", () => {
|
|
168
|
-
it("add", () => {
|
|
169
|
-
const a = FixedNumber.fromString("1.5");
|
|
170
|
-
const b = FixedNumber.fromString("2.3");
|
|
171
|
-
assert.equal(a.add(b).toString(), "3.8");
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it("sub", () => {
|
|
175
|
-
const a = FixedNumber.fromString("5.0");
|
|
176
|
-
const b = FixedNumber.fromString("2.3");
|
|
177
|
-
assert.equal(a.sub(b).toString(), "2.7");
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it("mul", () => {
|
|
181
|
-
const a = FixedNumber.fromString("2.0");
|
|
182
|
-
const b = FixedNumber.fromString("3.5");
|
|
183
|
-
assert.equal(a.mul(b).toString(), "7.0");
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
it("div", () => {
|
|
187
|
-
const a = FixedNumber.fromString("7.0");
|
|
188
|
-
const b = FixedNumber.fromString("2.0");
|
|
189
|
-
assert.equal(a.div(b).toString(), "3.5");
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe("Unsafe arithmetic (silent overflow)", () => {
|
|
194
|
-
it("addUnsafe wraps on overflow in fixed8x0", () => {
|
|
195
|
-
const a = FixedNumber.fromString("127", "fixed8x0");
|
|
196
|
-
const b = FixedNumber.fromString("1", "fixed8x0");
|
|
197
|
-
const result = a.addUnsafe(b);
|
|
198
|
-
assert.equal(typeof result.toString(), "string");
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it("subUnsafe wraps on underflow in fixed8x0", () => {
|
|
202
|
-
const a = FixedNumber.fromString("-128", "fixed8x0");
|
|
203
|
-
const b = FixedNumber.fromString("1", "fixed8x0");
|
|
204
|
-
const result = a.subUnsafe(b);
|
|
205
|
-
assert.equal(typeof result.toString(), "string");
|
|
206
|
-
});
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
describe("Comparison", () => {
|
|
210
|
-
it("cmp returns -1, 0, 1", () => {
|
|
211
|
-
const a = FixedNumber.fromString("1.0");
|
|
212
|
-
const b = FixedNumber.fromString("2.0");
|
|
213
|
-
const c = FixedNumber.fromString("1.0");
|
|
214
|
-
assert.equal(a.cmp(b), -1);
|
|
215
|
-
assert.equal(b.cmp(a), 1);
|
|
216
|
-
assert.equal(a.cmp(c), 0);
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it("eq, lt, lte, gt, gte", () => {
|
|
220
|
-
const a = FixedNumber.fromString("1.0");
|
|
221
|
-
const b = FixedNumber.fromString("2.0");
|
|
222
|
-
assert.equal(a.eq(a), true);
|
|
223
|
-
assert.equal(a.eq(b), false);
|
|
224
|
-
assert.equal(a.lt(b), true);
|
|
225
|
-
assert.equal(a.lte(b), true);
|
|
226
|
-
assert.equal(a.lte(a), true);
|
|
227
|
-
assert.equal(b.gt(a), true);
|
|
228
|
-
assert.equal(b.gte(a), true);
|
|
229
|
-
assert.equal(b.gte(b), true);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it("cmp across different decimal formats", () => {
|
|
233
|
-
const a = FixedNumber.fromString("1.5", "fixed128x2");
|
|
234
|
-
const b = FixedNumber.fromString("1.5", "fixed128x18");
|
|
235
|
-
assert.equal(a.cmp(b), 0);
|
|
236
|
-
});
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
describe("isZero / isNegative", () => {
|
|
240
|
-
it("isZero", () => {
|
|
241
|
-
assert.equal(FixedNumber.fromString("0.0").isZero(), true);
|
|
242
|
-
assert.equal(FixedNumber.fromString("1.0").isZero(), false);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it("isNegative", () => {
|
|
246
|
-
assert.equal(FixedNumber.fromString("-1.0").isNegative(), true);
|
|
247
|
-
assert.equal(FixedNumber.fromString("0.0").isNegative(), false);
|
|
248
|
-
assert.equal(FixedNumber.fromString("1.0").isNegative(), false);
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
describe("toUnsafeFloat", () => {
|
|
253
|
-
it("returns approximate float", () => {
|
|
254
|
-
const f = FixedNumber.fromString("3.14");
|
|
255
|
-
assert.ok(Math.abs(f.toUnsafeFloat() - 3.14) < 0.001);
|
|
256
|
-
});
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
describe("toFormat", () => {
|
|
260
|
-
it("converts between formats", () => {
|
|
261
|
-
const f = FixedNumber.fromString("1.5", "fixed128x18");
|
|
262
|
-
const g = f.toFormat("fixed128x2");
|
|
263
|
-
assert.equal(g.toString(), "1.5");
|
|
264
|
-
assert.equal(g.format, "fixed128x2");
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
describe("isFixedNumber", () => {
|
|
269
|
-
it("returns true for FixedNumber", () => {
|
|
270
|
-
assert.equal(FixedNumber.isFixedNumber(FixedNumber.from("1.0")), true);
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it("returns false for non-FixedNumber", () => {
|
|
274
|
-
assert.equal(FixedNumber.isFixedNumber("1.0"), false);
|
|
275
|
-
assert.equal(FixedNumber.isFixedNumber(1), false);
|
|
276
|
-
assert.equal(FixedNumber.isFixedNumber({}), false);
|
|
277
|
-
assert.equal(FixedNumber.isFixedNumber(null), false);
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
describe("toHexString", () => {
|
|
282
|
-
it("returns hex for unsigned value", () => {
|
|
283
|
-
const f = FixedNumber.fromString("255", "ufixed8x0");
|
|
284
|
-
assert.equal(f.toHexString(), "0xff");
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
it("returns hex for positive signed value", () => {
|
|
288
|
-
const f = FixedNumber.fromString("1", "fixed8x0");
|
|
289
|
-
assert.equal(f.toHexString(), "0x01");
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
// ---------------------------------------------------------------------------
|
|
295
|
-
// Negative tests -- invalid inputs
|
|
296
|
-
// ---------------------------------------------------------------------------
|
|
297
|
-
|
|
298
|
-
describe("FixedNumber negative tests -- invalid inputs", () => {
|
|
299
|
-
it("fromString rejects empty string", () => {
|
|
300
|
-
assert.throws(() => FixedNumber.fromString(""), /invalid/);
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
it("fromString rejects non-numeric string", () => {
|
|
304
|
-
assert.throws(() => FixedNumber.fromString("abc"), /invalid/);
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
it("fromString rejects multiple decimal points", () => {
|
|
308
|
-
assert.throws(() => FixedNumber.fromString("1.2.3"), /invalid/);
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
it("fromString rejects whitespace-only string", () => {
|
|
312
|
-
assert.throws(() => FixedNumber.fromString(" "), /invalid/);
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
it("fromString rejects too many decimals for format", () => {
|
|
316
|
-
assert.throws(() => FixedNumber.fromString("1.123", "fixed128x2"), /too many decimals/i);
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
it("fromValue rejects NaN", () => {
|
|
320
|
-
assert.throws(() => FixedNumber.fromValue(NaN, 0), /invalid/);
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
it("fromValue rejects Infinity", () => {
|
|
324
|
-
assert.throws(() => FixedNumber.fromValue(Infinity, 0), /invalid/);
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
it("fromValue rejects object", () => {
|
|
328
|
-
assert.throws(() => FixedNumber.fromValue({}, 0), /invalid/);
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
it("from() rejects boolean", () => {
|
|
332
|
-
assert.throws(() => FixedNumber.from(true), /invalid/);
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
it("from() rejects function", () => {
|
|
336
|
-
assert.throws(() => FixedNumber.from(() => {}), /invalid/);
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
it("constructor is guarded", () => {
|
|
340
|
-
assert.throws(() => new FixedNumber({}, 0n, {}), /cannot use FixedNumber constructor/);
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
// ---------------------------------------------------------------------------
|
|
345
|
-
// Negative tests -- overflow
|
|
346
|
-
// ---------------------------------------------------------------------------
|
|
347
|
-
|
|
348
|
-
describe("FixedNumber negative tests -- overflow", () => {
|
|
349
|
-
it("add throws on signed overflow (127 + 1 in fixed8x0)", () => {
|
|
350
|
-
const a = FixedNumber.fromString("127", "fixed8x0");
|
|
351
|
-
const b = FixedNumber.fromString("1", "fixed8x0");
|
|
352
|
-
assert.throws(() => a.add(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow");
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
it("sub throws on signed underflow (-128 - 1 in fixed8x0)", () => {
|
|
356
|
-
const a = FixedNumber.fromString("-128", "fixed8x0");
|
|
357
|
-
const b = FixedNumber.fromString("1", "fixed8x0");
|
|
358
|
-
assert.throws(() => a.sub(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow");
|
|
359
|
-
});
|
|
360
|
-
|
|
361
|
-
it("mul throws on overflow", () => {
|
|
362
|
-
const a = FixedNumber.fromString("127", "fixed8x0");
|
|
363
|
-
const b = FixedNumber.fromString("2", "fixed8x0");
|
|
364
|
-
assert.throws(() => a.mul(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow");
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
it("unsigned sub throws on 0 - 1 (ufixed8x0)", () => {
|
|
368
|
-
const a = FixedNumber.fromString("0", "ufixed8x0");
|
|
369
|
-
const b = FixedNumber.fromString("1", "ufixed8x0");
|
|
370
|
-
assert.throws(() => a.sub(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow");
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
it("fromString throws overflow for 256 in ufixed8x0", () => {
|
|
374
|
-
assert.throws(
|
|
375
|
-
() => FixedNumber.fromString("256", "ufixed8x0"),
|
|
376
|
-
(e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
|
|
377
|
-
);
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
it("fromString throws overflow for 128 in fixed8x0", () => {
|
|
381
|
-
assert.throws(
|
|
382
|
-
() => FixedNumber.fromString("128", "fixed8x0"),
|
|
383
|
-
(e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
|
|
384
|
-
);
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
it("fromValue throws overflow when scaled value exceeds range", () => {
|
|
388
|
-
assert.throws(
|
|
389
|
-
() => FixedNumber.fromValue(256n, 0, "ufixed8x0"),
|
|
390
|
-
(e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
|
|
391
|
-
);
|
|
392
|
-
});
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
// ---------------------------------------------------------------------------
|
|
396
|
-
// Negative tests -- underflow (precision loss)
|
|
397
|
-
// ---------------------------------------------------------------------------
|
|
398
|
-
|
|
399
|
-
describe("FixedNumber negative tests -- underflow", () => {
|
|
400
|
-
it("mulSignal throws on precision loss", () => {
|
|
401
|
-
const a = FixedNumber.fromString("0.1", "fixed128x1");
|
|
402
|
-
const b = FixedNumber.fromString("0.1", "fixed128x1");
|
|
403
|
-
assert.throws(() => a.mulSignal(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "underflow");
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
it("divSignal throws when division is not exact", () => {
|
|
407
|
-
const a = FixedNumber.fromString("1.0");
|
|
408
|
-
const b = FixedNumber.fromString("3.0");
|
|
409
|
-
assert.throws(() => a.divSignal(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "underflow");
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
it("mul (non-signal) silently truncates", () => {
|
|
413
|
-
const a = FixedNumber.fromString("0.1", "fixed128x1");
|
|
414
|
-
const b = FixedNumber.fromString("0.1", "fixed128x1");
|
|
415
|
-
const result = a.mul(b);
|
|
416
|
-
assert.equal(result.toString(), "0.0");
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
it("div (non-signal) silently truncates", () => {
|
|
420
|
-
const a = FixedNumber.fromString("1.0");
|
|
421
|
-
const b = FixedNumber.fromString("3.0");
|
|
422
|
-
const result = a.div(b);
|
|
423
|
-
assert.equal(typeof result.toString(), "string");
|
|
424
|
-
});
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
// ---------------------------------------------------------------------------
|
|
428
|
-
// Negative tests -- division by zero
|
|
429
|
-
// ---------------------------------------------------------------------------
|
|
430
|
-
|
|
431
|
-
describe("FixedNumber negative tests -- division by zero", () => {
|
|
432
|
-
it("div throws on zero divisor", () => {
|
|
433
|
-
const a = FixedNumber.fromString("1.0");
|
|
434
|
-
const b = FixedNumber.fromString("0.0");
|
|
435
|
-
assert.throws(() => a.div(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "divide-by-zero");
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
it("divUnsafe throws on zero divisor", () => {
|
|
439
|
-
const a = FixedNumber.fromString("1.0");
|
|
440
|
-
const b = FixedNumber.fromString("0.0");
|
|
441
|
-
assert.throws(() => a.divUnsafe(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "divide-by-zero");
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
it("divSignal throws on zero divisor", () => {
|
|
445
|
-
const a = FixedNumber.fromString("1.0");
|
|
446
|
-
const b = FixedNumber.fromString("0.0");
|
|
447
|
-
assert.throws(() => a.divSignal(b), (e) => e.code === "NUMERIC_FAULT" && e.fault === "divide-by-zero");
|
|
448
|
-
});
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
// ---------------------------------------------------------------------------
|
|
452
|
-
// Negative tests -- unsigned format rejects negatives
|
|
453
|
-
// ---------------------------------------------------------------------------
|
|
454
|
-
|
|
455
|
-
describe("FixedNumber negative tests -- unsigned rejects negatives", () => {
|
|
456
|
-
it("fromString rejects negative in unsigned format", () => {
|
|
457
|
-
assert.throws(
|
|
458
|
-
() => FixedNumber.fromString("-1", "ufixed128x18"),
|
|
459
|
-
(e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
|
|
460
|
-
);
|
|
461
|
-
});
|
|
462
|
-
|
|
463
|
-
it("fromValue rejects negative in unsigned format", () => {
|
|
464
|
-
assert.throws(
|
|
465
|
-
() => FixedNumber.fromValue(-1n, 0, "ufixed128x18"),
|
|
466
|
-
(e) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
|
|
467
|
-
);
|
|
468
|
-
});
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
// ---------------------------------------------------------------------------
|
|
472
|
-
// Negative tests -- format parsing
|
|
473
|
-
// ---------------------------------------------------------------------------
|
|
474
|
-
|
|
475
|
-
describe("FixedNumber negative tests -- format parsing", () => {
|
|
476
|
-
it("rejects invalid format string", () => {
|
|
477
|
-
assert.throws(() => FixedNumber.fromString("1", "fixd128x18"), /invalid fixed format/);
|
|
478
|
-
});
|
|
479
|
-
|
|
480
|
-
it("rejects non-byte-aligned width", () => {
|
|
481
|
-
assert.throws(() => FixedNumber.fromString("1", "fixed7x0"), /not byte aligned/);
|
|
482
|
-
});
|
|
483
|
-
|
|
484
|
-
it("rejects decimals > 80", () => {
|
|
485
|
-
assert.throws(() => FixedNumber.fromString("1", "fixed128x81"), /too large/);
|
|
486
|
-
});
|
|
487
|
-
|
|
488
|
-
it("rejects invalid format object (wrong type for signed)", () => {
|
|
489
|
-
assert.throws(() => FixedNumber.fromString("1", { signed: "yes" }), /not boolean/);
|
|
490
|
-
});
|
|
491
|
-
|
|
492
|
-
it("rejects invalid format object (non-byte-aligned width)", () => {
|
|
493
|
-
assert.throws(() => FixedNumber.fromString("1", { width: 7 }), /not byte aligned/);
|
|
494
|
-
});
|
|
495
|
-
|
|
496
|
-
it("rejects invalid format object (decimals too large)", () => {
|
|
497
|
-
assert.throws(() => FixedNumber.fromString("1", { decimals: 81 }), /too large/);
|
|
498
|
-
});
|
|
499
|
-
});
|
|
500
|
-
|
|
501
|
-
// ---------------------------------------------------------------------------
|
|
502
|
-
// Negative tests -- incompatible format arithmetic
|
|
503
|
-
// ---------------------------------------------------------------------------
|
|
504
|
-
|
|
505
|
-
describe("FixedNumber negative tests -- incompatible formats", () => {
|
|
506
|
-
it("add throws with different formats", () => {
|
|
507
|
-
const a = FixedNumber.fromString("1.0", "fixed128x18");
|
|
508
|
-
const b = FixedNumber.fromString("1.0", "fixed128x2");
|
|
509
|
-
assert.throws(() => a.add(b), /incompatible format/);
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
it("sub throws with different formats", () => {
|
|
513
|
-
const a = FixedNumber.fromString("1.0", "fixed128x18");
|
|
514
|
-
const b = FixedNumber.fromString("1.0", "fixed128x2");
|
|
515
|
-
assert.throws(() => a.sub(b), /incompatible format/);
|
|
516
|
-
});
|
|
517
|
-
|
|
518
|
-
it("mul throws with different formats", () => {
|
|
519
|
-
const a = FixedNumber.fromString("1.0", "fixed128x18");
|
|
520
|
-
const b = FixedNumber.fromString("1.0", "fixed128x2");
|
|
521
|
-
assert.throws(() => a.mul(b), /incompatible format/);
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
it("div throws with different formats", () => {
|
|
525
|
-
const a = FixedNumber.fromString("1.0", "fixed128x18");
|
|
526
|
-
const b = FixedNumber.fromString("1.0", "fixed128x2");
|
|
527
|
-
assert.throws(() => a.div(b), /incompatible format/);
|
|
528
|
-
});
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
// ---------------------------------------------------------------------------
|
|
532
|
-
// Boundary condition tests
|
|
533
|
-
// ---------------------------------------------------------------------------
|
|
534
|
-
|
|
535
|
-
describe("FixedNumber boundary conditions", () => {
|
|
536
|
-
describe("fixed8x0 range [-128, 127]", () => {
|
|
537
|
-
it("accepts min value -128", () => {
|
|
538
|
-
const f = FixedNumber.fromString("-128", "fixed8x0");
|
|
539
|
-
assert.equal(f.toString(), "-128");
|
|
540
|
-
});
|
|
541
|
-
|
|
542
|
-
it("accepts max value 127", () => {
|
|
543
|
-
const f = FixedNumber.fromString("127", "fixed8x0");
|
|
544
|
-
assert.equal(f.toString(), "127");
|
|
545
|
-
});
|
|
546
|
-
|
|
547
|
-
it("rejects 128", () => {
|
|
548
|
-
assert.throws(() => FixedNumber.fromString("128", "fixed8x0"),
|
|
549
|
-
(e) => e.code === "NUMERIC_FAULT");
|
|
550
|
-
});
|
|
551
|
-
|
|
552
|
-
it("rejects -129", () => {
|
|
553
|
-
assert.throws(() => FixedNumber.fromString("-129", "fixed8x0"),
|
|
554
|
-
(e) => e.code === "NUMERIC_FAULT");
|
|
555
|
-
});
|
|
556
|
-
});
|
|
557
|
-
|
|
558
|
-
describe("ufixed8x0 range [0, 255]", () => {
|
|
559
|
-
it("accepts 0", () => {
|
|
560
|
-
assert.equal(FixedNumber.fromString("0", "ufixed8x0").toString(), "0");
|
|
561
|
-
});
|
|
562
|
-
|
|
563
|
-
it("accepts 255", () => {
|
|
564
|
-
assert.equal(FixedNumber.fromString("255", "ufixed8x0").toString(), "255");
|
|
565
|
-
});
|
|
566
|
-
|
|
567
|
-
it("rejects 256", () => {
|
|
568
|
-
assert.throws(() => FixedNumber.fromString("256", "ufixed8x0"),
|
|
569
|
-
(e) => e.code === "NUMERIC_FAULT");
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
it("rejects -1", () => {
|
|
573
|
-
assert.throws(() => FixedNumber.fromString("-1", "ufixed8x0"),
|
|
574
|
-
(e) => e.code === "NUMERIC_FAULT");
|
|
575
|
-
});
|
|
576
|
-
});
|
|
577
|
-
|
|
578
|
-
describe("fixed16x2 range [-327.68, 327.67]", () => {
|
|
579
|
-
it("accepts 327.67", () => {
|
|
580
|
-
assert.equal(FixedNumber.fromString("327.67", "fixed16x2").toString(), "327.67");
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
it("accepts -327.68", () => {
|
|
584
|
-
assert.equal(FixedNumber.fromString("-327.68", "fixed16x2").toString(), "-327.68");
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
it("rejects 327.68", () => {
|
|
588
|
-
assert.throws(() => FixedNumber.fromString("327.68", "fixed16x2"),
|
|
589
|
-
(e) => e.code === "NUMERIC_FAULT");
|
|
590
|
-
});
|
|
591
|
-
|
|
592
|
-
it("arithmetic at edges: 327.67 + 0.01 overflows", () => {
|
|
593
|
-
const a = FixedNumber.fromString("327.67", "fixed16x2");
|
|
594
|
-
const b = FixedNumber.fromString("0.01", "fixed16x2");
|
|
595
|
-
assert.throws(() => a.add(b), (e) => e.code === "NUMERIC_FAULT");
|
|
596
|
-
});
|
|
597
|
-
});
|
|
598
|
-
|
|
599
|
-
describe("zero-decimal format (fixed128x0)", () => {
|
|
600
|
-
it("toString does not append .0", () => {
|
|
601
|
-
const f = FixedNumber.fromString("42", "fixed128x0");
|
|
602
|
-
assert.equal(f.toString(), "42");
|
|
603
|
-
});
|
|
604
|
-
});
|
|
605
|
-
|
|
606
|
-
describe("high decimals (fixed256x40)", () => {
|
|
607
|
-
it("construction works", () => {
|
|
608
|
-
const f = FixedNumber.fromString("1.0", "fixed256x40");
|
|
609
|
-
assert.equal(f.decimals, 40);
|
|
610
|
-
assert.ok(f.toString().startsWith("1.0"));
|
|
611
|
-
});
|
|
612
|
-
});
|
|
613
|
-
|
|
614
|
-
describe("rounding edge cases", () => {
|
|
615
|
-
it("round(0) on 0.5 rounds up", () => {
|
|
616
|
-
assert.equal(FixedNumber.from("0.5").round(0).toString(), "1.0");
|
|
617
|
-
});
|
|
618
|
-
|
|
619
|
-
it("round(0) on -0.5 rounds away from zero", () => {
|
|
620
|
-
assert.equal(FixedNumber.from("-0.5").round(0).toString(), "-1.0");
|
|
621
|
-
});
|
|
622
|
-
});
|
|
623
|
-
|
|
624
|
-
describe("floor/ceiling at exact integers", () => {
|
|
625
|
-
it("floor of integer returns same value", () => {
|
|
626
|
-
const f = FixedNumber.from("5.0");
|
|
627
|
-
assert.equal(f.floor().toString(), "5.0");
|
|
628
|
-
});
|
|
629
|
-
|
|
630
|
-
it("ceiling of integer returns same value", () => {
|
|
631
|
-
const f = FixedNumber.from("5.0");
|
|
632
|
-
assert.equal(f.ceiling().toString(), "5.0");
|
|
633
|
-
});
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
describe("toFormat precision loss", () => {
|
|
637
|
-
it("throws when converting to lower precision with data loss", () => {
|
|
638
|
-
const f = FixedNumber.fromString("1.23", "fixed128x18");
|
|
639
|
-
assert.throws(() => f.toFormat("fixed128x1"), /too many decimals/i);
|
|
640
|
-
});
|
|
641
|
-
});
|
|
642
|
-
|
|
643
|
-
describe("large values near 2^127 - 1 for fixed128x0", () => {
|
|
644
|
-
it("accepts 2^127 - 1", () => {
|
|
645
|
-
const maxVal = (2n ** 127n - 1n).toString();
|
|
646
|
-
const f = FixedNumber.fromString(maxVal, "fixed128x0");
|
|
647
|
-
assert.equal(f.toString(), maxVal);
|
|
648
|
-
});
|
|
649
|
-
|
|
650
|
-
it("rejects 2^127", () => {
|
|
651
|
-
const overVal = (2n ** 127n).toString();
|
|
652
|
-
assert.throws(() => FixedNumber.fromString(overVal, "fixed128x0"),
|
|
653
|
-
(e) => e.code === "NUMERIC_FAULT");
|
|
654
|
-
});
|
|
655
|
-
});
|
|
656
|
-
});
|