All files / ethers.js/src.ts/_tests test-utils-maths.ts

100% Statements 205/205
100% Branches 24/24
100% Functions 0/0
100% Lines 205/205

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 1x    
import assert from "assert";
 
import {
    isError,
    fromTwos, toTwos,
    getBigInt, getNumber, toBeArray, toBeHex, toQuantity,
} from "../index.js";
 
describe("Tests Quantity Functions", function() {
    const quantities: Array<{ name: string, value: any, expected: string }> = [
        {
            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.equal(toQuantity(value), expected);
        });
    }
});
 
describe("Tests Bad Math Values", function() {
    const badBigInts: Array<{ name: string, value: any, error: string }> = [
        {
            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.throws(() => {
                const result = getBigInt(value);
                console.log(result);
            }, (e: any) => {
                return (isError(e, "INVALID_ARGUMENT") &&
                    e.message.startsWith(error));
            });
        });
    }
 
    const badNumbers: Array<{ name: string, value: any, error: string }> = [
        {
            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.throws(() => {
                const result = getNumber(value);
                console.log(result);
            }, (e: any) => {
                return (isError(e, "INVALID_ARGUMENT") &&
                    e.message.startsWith(error));
            });
        });
    }
 
    const badHex: Array<{ name: string, value: any, error: string, width?: number }> = [
        {
            name: "negative value",
            value: -4,
            error: "unsigned value cannot be negative"
        },
        {
            name: "width too short",
            value: 0x123456,
            width: 2,
            error: "value exceeds width"
        },
    ];
 
    for (const { name, value, error, width } of badHex) {
        it(`correctly fails on bad toBeHex values: ${ name }`, function() {
            assert.throws(() => {
                const result = toBeHex(value, width);
                console.log(result);
            }, (e: any) => {
                return (isError(e, "NUMERIC_FAULT") && e.fault === "overflow" &&
                    e.message.startsWith(error));
            });
        });
    }
 
    it(`correctly fails on nad toBeArray values: negative value`, function() {
        assert.throws(() => {
            const result = toBeArray(-4);
            console.log(result);
        }, (e: any) => {
            return (isError(e, "NUMERIC_FAULT") && e.fault === "overflow" &&
                e.message.startsWith("unsigned value cannot be negative"));
        });
    });
});
 
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 = toTwos(value, width);
            assert.equal(result, twos);
        });
    }
 
    for (const { twos, width, value } of tests) {
        it(`computes values from twos compliment: ${ value }[${ width } bits]`, function() {
            const result = fromTwos(twos, width);
            assert.equal(result, value);
        });
    }
});