Small changes in maths on error.

This commit is contained in:
Richard Moore 2022-12-10 16:07:58 -05:00
parent ddcf7d685b
commit 0859f84bac
2 changed files with 10 additions and 6 deletions

@ -146,7 +146,7 @@ describe("Tests Bad Math Values", function() {
{
name: "negative value",
value: -4,
error: "cannot toBeHex negative value"
error: "unsigned value cannot be negative"
},
{
name: "width too short",
@ -162,7 +162,7 @@ describe("Tests Bad Math Values", function() {
const result = toBeHex(value, width);
console.log(result);
}, (e: any) => {
return (isError(e, "INVALID_ARGUMENT") &&
return (isError(e, "NUMERIC_FAULT") && e.fault === "overflow" &&
e.message.startsWith(error));
});
});
@ -173,8 +173,8 @@ describe("Tests Bad Math Values", function() {
const result = toBeArray(-4);
console.log(result);
}, (e: any) => {
return (isError(e, "INVALID_ARGUMENT") &&
e.message.startsWith("cannot toBeArray negative value"));
return (isError(e, "NUMERIC_FAULT") && e.fault === "overflow" &&
e.message.startsWith("unsigned value cannot be negative"));
});
});
});

@ -115,7 +115,7 @@ export function getBigInt(value: BigNumberish, name?: string): bigint {
export function getUint(value: BigNumberish, name?: string): bigint {
const result = getBigInt(value, name);
assert(result >= BN_0, "overflow", "NUMERIC_FAULT", {
assert(result >= BN_0, "unsigned value cannot be negative", "NUMERIC_FAULT", {
fault: "overflow", operation: "getUint", value
});
return result;
@ -187,7 +187,11 @@ export function toBeHex(_value: BigNumberish, _width?: Numeric): string {
if (result.length % 2) { result = "0" + result; }
} else {
const width = getNumber(_width, "width");
assertArgument(width * 2 >= result.length, `value exceeds width`, "[ value, width ]", [ _value, _width ]);
assert(width * 2 >= result.length, `value exceeds width (${ width } bits)`, "NUMERIC_FAULT", {
operation: "toBeHex",
fault: "overflow",
value: _value
});
// Pad the value to the required width
while (result.length < (width * 2)) { result = "0" + result; }