2024-10-12 17:12:41 +01:00
|
|
|
import * as chai from "chai";
|
2022-08-31 12:43:44 -07:00
|
|
|
import * as Scalar from "../src/scalar.js";
|
2020-04-20 13:34:41 +02:00
|
|
|
|
2024-10-12 17:12:41 +01:00
|
|
|
const assert = chai.assert;
|
|
|
|
|
2020-04-20 13:34:41 +02:00
|
|
|
describe("Basic scalar convertions", () => {
|
|
|
|
it("Should convertion Native", () => {
|
2022-08-31 12:43:44 -07:00
|
|
|
assert(Scalar.eq(Scalar.e("0x12"), 18));
|
|
|
|
assert(Scalar.eq(Scalar.e("0x12", 16), 18));
|
|
|
|
assert(Scalar.eq(Scalar.e("12", 16), 18));
|
|
|
|
assert(Scalar.eq(Scalar.e("18"), 18));
|
|
|
|
assert(Scalar.eq(Scalar.e("18", 10), 18));
|
|
|
|
assert(Scalar.eq(Scalar.e(18, 10), 18));
|
|
|
|
assert(Scalar.eq(Scalar.e(18n, 10), 18));
|
|
|
|
assert(Scalar.eq(Scalar.e(0x12, 10), 18));
|
|
|
|
assert(Scalar.eq(Scalar.e(0x12n, 10), 18));
|
2020-04-20 13:34:41 +02:00
|
|
|
|
|
|
|
});
|
2020-04-30 15:34:21 +02:00
|
|
|
it("Should convert to js Number Native", () => {
|
|
|
|
const maxJsNum = Number.MAX_SAFE_INTEGER;
|
2022-08-31 12:43:44 -07:00
|
|
|
const maxToScalar = Scalar.e(maxJsNum);
|
2020-04-30 15:34:21 +02:00
|
|
|
|
2022-08-31 12:43:44 -07:00
|
|
|
const backToNum = Scalar.toNumber(maxToScalar);
|
2020-04-30 15:34:21 +02:00
|
|
|
assert(backToNum, maxJsNum);
|
|
|
|
|
2022-08-31 12:43:44 -07:00
|
|
|
const overMaxJsNum = Scalar.add(maxToScalar, 1);
|
|
|
|
assert.throws(() => Scalar.toNumber(overMaxJsNum));
|
2020-04-30 15:34:21 +02:00
|
|
|
});
|
2020-04-20 13:34:41 +02:00
|
|
|
});
|