From fd4d9f8257dbf0323a86660f3abcd1fea91fea77 Mon Sep 17 00:00:00 2001 From: barak manos <> Date: Wed, 17 Jan 2024 13:11:38 +0200 Subject: [PATCH] Add tests for function `MathEx.exp2` --- contracts/helpers/TestMathEx.sol | 6 +++++- test/utility/MathEx.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/contracts/helpers/TestMathEx.sol b/contracts/helpers/TestMathEx.sol index 8c14dc8b..8c71e790 100644 --- a/contracts/helpers/TestMathEx.sol +++ b/contracts/helpers/TestMathEx.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: SEE LICENSE IN LICENSE pragma solidity 0.8.19; -import { MathEx } from "../utility/MathEx.sol"; +import { MathEx, Fraction } from "../utility/MathEx.sol"; contract TestMathEx { function mulDivF(uint256 x, uint256 y, uint256 z) external pure returns (uint256) { @@ -15,4 +15,8 @@ contract TestMathEx { function minFactor(uint256 x, uint256 y) external pure returns (uint256) { return MathEx.minFactor(x, y); } + + function exp2(Fraction memory f) external pure returns (Fraction memory) { + return MathEx.exp2(f); + } } diff --git a/test/utility/MathEx.ts b/test/utility/MathEx.ts index 301be5b9..d933135a 100644 --- a/test/utility/MathEx.ts +++ b/test/utility/MathEx.ts @@ -1,6 +1,10 @@ import Contracts, { TestMathEx } from '../../components/Contracts'; import { expect } from 'chai'; import { BigNumber } from 'ethers'; +import { EXP2_INPUT_TOO_HIGH } from '../../utils/Constants'; +import { Fraction } from '../../utils/Types'; +import { Relation } from '../matchers'; +import Decimal from 'decimal.js'; const MAX_UINT256 = BigNumber.from(2).pow(256).sub(1); @@ -51,6 +55,25 @@ describe('MathEx', () => { } }; + const testExp2 = (f: Fraction, maxRelativeError: Decimal) => { + it(`exp2(${f.n} / ${f.d})`, async () => { + const fVal = new Decimal(f.n).div(f.d); + if (fVal.lt(EXP2_INPUT_TOO_HIGH)) { + const actual = await mathContract.exp2(f); + const expected = new Decimal(2).pow(fVal); + await expect(actual).to.almostEqual( + { n: expected, d: 1 }, + { + maxRelativeError, + relation: Relation.LesserOrEqual + } + ); + } else { + await expect(mathContract.exp2(f)).to.revertedWithError('Overflow'); + } + }); + }; + describe('quick tests', () => { for (const px of [128, 192, 256]) { for (const py of [128, 192, 256]) { @@ -76,6 +99,12 @@ describe('MathEx', () => { } } } + + for (let d = 1000; d < 1000000000; d *= 10) { + for (let n = EXP2_INPUT_TOO_HIGH.mul(d).sub(10); n.lte(EXP2_INPUT_TOO_HIGH.mul(d).sub(1)); n = n.add(1)) { + testExp2({ n: n.floor().toNumber(), d }, new Decimal('0.000000000000000000000000000000000002')); + } + } }); describe('@stress tests', () => {