ethers.js/lib.commonjs/_tests/test-utils-maths.js

192 lines
5.9 KiB
JavaScript
Raw Normal View History

2022-11-30 23:44:23 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2023-04-25 14:04:48 +03:00
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
2022-11-30 23:44:23 +03:00
const index_js_1 = require("../index.js");
describe("Tests Quantity Functions", function () {
const quantities = [
{
name: "zero number",
value: 0,
expected: "0x0"
},
{
name: "zero single hex",
value: "0x0",
expected: "0x0"
},
{
name: "zero double hex",
value: "0x00",
expected: "0x0"
},
{
name: "zero array(0)",
value: new Uint8Array([]),
expected: "0x0"
},
{
name: "zero array(1)",
value: new Uint8Array([0]),
expected: "0x0"
},
{
name: "single hex digit",
value: 0x5,
expected: "0x5"
},
{
name: "double hex digit",
value: 0x42,
expected: "0x42"
},
{
name: "big array, odd output",
value: new Uint8Array([0x0f, 254, 253, 252]),
expected: "0xffefdfc"
},
{
name: "big array, even output",
value: new Uint8Array([255, 254, 253, 252]),
expected: "0xfffefdfc"
},
];
for (const { name, value, expected } of quantities) {
it(`computes quantity: ${name}`, function () {
assert_1.default.equal((0, index_js_1.toQuantity)(value), expected);
});
}
});
describe("Tests Bad Math Values", function () {
const badBigInts = [
{
name: "empty string",
value: "",
error: "invalid BigNumberish string"
},
{
name: "non-numeric string",
value: "foobar",
error: "invalid BigNumberish string"
},
{
name: "double negative sign",
value: "--42",
error: "invalid BigNumberish string"
},
{
name: "non-numeric thing",
value: true,
error: "invalid BigNumberish value"
},
];
for (const { name, value, error } of badBigInts) {
it(`correctly fails on bad bigint: ${name}`, function () {
assert_1.default.throws(() => {
const result = (0, index_js_1.getBigInt)(value);
console.log(result);
}, (e) => {
return ((0, index_js_1.isError)(e, "INVALID_ARGUMENT") &&
e.message.startsWith(error));
});
});
}
const badNumbers = [
{
name: "empty string",
value: "",
error: "invalid numeric string"
},
{
name: "non-numeric string",
value: "foobar",
error: "invalid numeric string"
},
{
name: "double negative sign",
value: "--42",
error: "invalid numeric string"
},
{
name: "non-numeric thing",
value: true,
error: "invalid numeric value"
},
{
name: "too big",
value: Number.MAX_SAFE_INTEGER + 10,
error: "overflow"
},
{
name: "too small",
value: -Number.MAX_SAFE_INTEGER - 10,
error: "overflow"
},
];
for (const { name, value, error } of badNumbers) {
it(`correctly fails on bad numeric: ${name}`, function () {
assert_1.default.throws(() => {
const result = (0, index_js_1.getNumber)(value);
console.log(result);
}, (e) => {
return ((0, index_js_1.isError)(e, "INVALID_ARGUMENT") &&
e.message.startsWith(error));
});
});
}
const badHex = [
{
name: "negative value",
value: -4,
2022-12-11 00:08:48 +03:00
error: "unsigned value cannot be negative"
2022-11-30 23:44:23 +03:00
},
{
name: "width too short",
value: 0x123456,
width: 2,
error: "value exceeds width"
},
];
for (const { name, value, error, width } of badHex) {
2022-12-10 02:24:58 +03:00
it(`correctly fails on bad toBeHex values: ${name}`, function () {
2022-11-30 23:44:23 +03:00
assert_1.default.throws(() => {
2022-12-10 02:24:58 +03:00
const result = (0, index_js_1.toBeHex)(value, width);
2022-11-30 23:44:23 +03:00
console.log(result);
}, (e) => {
2022-12-11 00:08:48 +03:00
return ((0, index_js_1.isError)(e, "NUMERIC_FAULT") && e.fault === "overflow" &&
2022-11-30 23:44:23 +03:00
e.message.startsWith(error));
});
});
}
2022-12-10 02:24:58 +03:00
it(`correctly fails on nad toBeArray values: negative value`, function () {
2022-11-30 23:44:23 +03:00
assert_1.default.throws(() => {
2022-12-10 02:24:58 +03:00
const result = (0, index_js_1.toBeArray)(-4);
2022-11-30 23:44:23 +03:00
console.log(result);
}, (e) => {
2022-12-11 00:08:48 +03:00
return ((0, index_js_1.isError)(e, "NUMERIC_FAULT") && e.fault === "overflow" &&
e.message.startsWith("unsigned value cannot be negative"));
2022-11-30 23:44:23 +03:00
});
});
});
2023-04-06 11:37:10 +03:00
describe("Tests Twos Compliemnts Functions", function () {
const tests = [
{ width: 8, value: 0, twos: 0 },
{ width: 8, value: 1, twos: 1 },
{ width: 8, value: -1, twos: 0xff },
{ width: 8, value: 127, twos: 127 },
{ width: 8, value: -128, twos: 0x80 },
];
for (const { twos, width, value } of tests) {
it(`computes twos compliment values: ${value}[${width} bits]`, function () {
const result = (0, index_js_1.toTwos)(value, width);
assert_1.default.equal(result, twos);
});
}
for (const { twos, width, value } of tests) {
it(`computes values from twos compliment: ${value}[${width} bits]`, function () {
const result = (0, index_js_1.fromTwos)(twos, width);
assert_1.default.equal(result, value);
});
}
});
2022-11-30 23:44:23 +03:00
//# sourceMappingURL=test-utils-maths.js.map