From c2cee62ac89e02d91231f17c07e5e63760892547 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 4 Feb 2025 15:31:46 +0100 Subject: [PATCH] coverage --- test/utils/math/Math.test.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/test/utils/math/Math.test.js b/test/utils/math/Math.test.js index 7dcbca656..b2d7cd7ea 100644 --- a/test/utils/math/Math.test.js +++ b/test/utils/math/Math.test.js @@ -16,10 +16,13 @@ const uint256 = value => ethers.Typed.uint256(value); bytes.zero = '0x'; uint256.zero = 0n; -async function testCommutative(fn, lhs, rhs, expected, ...extra) { - await expect(fn(lhs, rhs, ...extra)).to.eventually.deep.equal(expected); - await expect(fn(rhs, lhs, ...extra)).to.eventually.deep.equal(expected); -} +const testCommutative = (fn, lhs, rhs, expected, ...extra) => + Promise.all([ + expect(fn(lhs, rhs, ...extra)).to.eventually.deep.equal(expected), + expect(fn(rhs, lhs, ...extra)).to.eventually.deep.equal(expected), + ]); + +const splitHighLow = n => [n / (1n << 256n), n % (1n << 256n)]; async function fixture() { const mock = await ethers.deployContract('$Math'); @@ -39,6 +42,24 @@ describe('Math', function () { Object.assign(this, await loadFixture(fixture)); }); + describe('add512', function () { + it('adds correctly without reverting', async function () { + const values = [0n, 1n, 17n, 42n, ethers.MaxUint256 - 1n, ethers.MaxUint256]; + for (const [a, b] of product(values, values)) { + await expect(this.mock.$add512(a, b)).to.eventually.deep.equal(splitHighLow(a + b)); + } + }); + }); + + describe('mul512', function () { + it('multiplies correctly without reverting', async function () { + const values = [0n, 1n, 17n, 42n, ethers.MaxUint256 - 1n, ethers.MaxUint256]; + for (const [a, b] of product(values, values)) { + await expect(this.mock.$mul512(a, b)).to.eventually.deep.equal(splitHighLow(a * b)); + } + }); + }); + describe('tryAdd', function () { it('adds correctly', async function () { const a = 5678n;