quantumcoin 7.0.6 → 7.0.8
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-SDK.md +53 -1
- package/SPEC.md +1 -1
- package/examples/package-lock.json +103 -454
- package/examples/package.json +1 -1
- package/generate-sdk.js +1 -0
- package/package.json +2 -2
- package/src/utils/fixednumber.d.ts +57 -0
- package/src/utils/fixednumber.js +366 -0
- package/src/utils/hashing.js +1 -1
- package/src/utils/index.d.ts +1 -0
- package/src/utils/index.js +1 -0
- package/src/wallet/wallet.d.ts +15 -1
- package/src/wallet/wallet.js +45 -3
- package/test/unit/address-wallet.test.js +172 -0
- package/test/unit/address-wallet.test.ts +170 -0
- package/test/unit/fixednumber.test.js +656 -0
- package/test/unit/fixednumber.test.ts +660 -0
- package/test/unit/hashing.test.js +7 -0
- package/test/unit/hashing.test.ts +7 -0
|
@@ -0,0 +1,656 @@
|
|
|
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
|
+
});
|