mirror of
https://github.com/th30d4y/OpenLearnX.git
synced 2026-05-26 19:26:33 +00:00
Fix .gitignore: stop tracking ignored files
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "forge-std/Test.sol";
|
||||
|
||||
import "../../../contracts/utils/math/Math.sol";
|
||||
import "../../../contracts/utils/math/SafeMath.sol";
|
||||
|
||||
contract MathTest is Test {
|
||||
// CEILDIV
|
||||
function testCeilDiv(uint256 a, uint256 b) public {
|
||||
vm.assume(b > 0);
|
||||
|
||||
uint256 result = Math.ceilDiv(a, b);
|
||||
|
||||
if (result == 0) {
|
||||
assertEq(a, 0);
|
||||
} else {
|
||||
uint256 maxdiv = UINT256_MAX / b;
|
||||
bool overflow = maxdiv * b < a;
|
||||
assertTrue(a > b * (result - 1));
|
||||
assertTrue(overflow ? result == maxdiv + 1 : a <= b * result);
|
||||
}
|
||||
}
|
||||
|
||||
// SQRT
|
||||
function testSqrt(uint256 input, uint8 r) public {
|
||||
Math.Rounding rounding = _asRounding(r);
|
||||
|
||||
uint256 result = Math.sqrt(input, rounding);
|
||||
|
||||
// square of result is bigger than input
|
||||
if (_squareBigger(result, input)) {
|
||||
assertTrue(rounding == Math.Rounding.Up);
|
||||
assertTrue(_squareSmaller(result - 1, input));
|
||||
}
|
||||
// square of result is smaller than input
|
||||
else if (_squareSmaller(result, input)) {
|
||||
assertFalse(rounding == Math.Rounding.Up);
|
||||
assertTrue(_squareBigger(result + 1, input));
|
||||
}
|
||||
// input is perfect square
|
||||
else {
|
||||
assertEq(result * result, input);
|
||||
}
|
||||
}
|
||||
|
||||
function _squareBigger(uint256 value, uint256 ref) private pure returns (bool) {
|
||||
(bool noOverflow, uint256 square) = SafeMath.tryMul(value, value);
|
||||
return !noOverflow || square > ref;
|
||||
}
|
||||
|
||||
function _squareSmaller(uint256 value, uint256 ref) private pure returns (bool) {
|
||||
return value * value < ref;
|
||||
}
|
||||
|
||||
// LOG2
|
||||
function testLog2(uint256 input, uint8 r) public {
|
||||
Math.Rounding rounding = _asRounding(r);
|
||||
|
||||
uint256 result = Math.log2(input, rounding);
|
||||
|
||||
if (input == 0) {
|
||||
assertEq(result, 0);
|
||||
} else if (_powerOf2Bigger(result, input)) {
|
||||
assertTrue(rounding == Math.Rounding.Up);
|
||||
assertTrue(_powerOf2Smaller(result - 1, input));
|
||||
} else if (_powerOf2Smaller(result, input)) {
|
||||
assertFalse(rounding == Math.Rounding.Up);
|
||||
assertTrue(_powerOf2Bigger(result + 1, input));
|
||||
} else {
|
||||
assertEq(2 ** result, input);
|
||||
}
|
||||
}
|
||||
|
||||
function _powerOf2Bigger(uint256 value, uint256 ref) private pure returns (bool) {
|
||||
return value >= 256 || 2 ** value > ref; // 2**256 overflows uint256
|
||||
}
|
||||
|
||||
function _powerOf2Smaller(uint256 value, uint256 ref) private pure returns (bool) {
|
||||
return 2 ** value < ref;
|
||||
}
|
||||
|
||||
// LOG10
|
||||
function testLog10(uint256 input, uint8 r) public {
|
||||
Math.Rounding rounding = _asRounding(r);
|
||||
|
||||
uint256 result = Math.log10(input, rounding);
|
||||
|
||||
if (input == 0) {
|
||||
assertEq(result, 0);
|
||||
} else if (_powerOf10Bigger(result, input)) {
|
||||
assertTrue(rounding == Math.Rounding.Up);
|
||||
assertTrue(_powerOf10Smaller(result - 1, input));
|
||||
} else if (_powerOf10Smaller(result, input)) {
|
||||
assertFalse(rounding == Math.Rounding.Up);
|
||||
assertTrue(_powerOf10Bigger(result + 1, input));
|
||||
} else {
|
||||
assertEq(10 ** result, input);
|
||||
}
|
||||
}
|
||||
|
||||
function _powerOf10Bigger(uint256 value, uint256 ref) private pure returns (bool) {
|
||||
return value >= 78 || 10 ** value > ref; // 10**78 overflows uint256
|
||||
}
|
||||
|
||||
function _powerOf10Smaller(uint256 value, uint256 ref) private pure returns (bool) {
|
||||
return 10 ** value < ref;
|
||||
}
|
||||
|
||||
// LOG256
|
||||
function testLog256(uint256 input, uint8 r) public {
|
||||
Math.Rounding rounding = _asRounding(r);
|
||||
|
||||
uint256 result = Math.log256(input, rounding);
|
||||
|
||||
if (input == 0) {
|
||||
assertEq(result, 0);
|
||||
} else if (_powerOf256Bigger(result, input)) {
|
||||
assertTrue(rounding == Math.Rounding.Up);
|
||||
assertTrue(_powerOf256Smaller(result - 1, input));
|
||||
} else if (_powerOf256Smaller(result, input)) {
|
||||
assertFalse(rounding == Math.Rounding.Up);
|
||||
assertTrue(_powerOf256Bigger(result + 1, input));
|
||||
} else {
|
||||
assertEq(256 ** result, input);
|
||||
}
|
||||
}
|
||||
|
||||
function _powerOf256Bigger(uint256 value, uint256 ref) private pure returns (bool) {
|
||||
return value >= 32 || 256 ** value > ref; // 256**32 overflows uint256
|
||||
}
|
||||
|
||||
function _powerOf256Smaller(uint256 value, uint256 ref) private pure returns (bool) {
|
||||
return 256 ** value < ref;
|
||||
}
|
||||
|
||||
// MULDIV
|
||||
function testMulDiv(uint256 x, uint256 y, uint256 d) public {
|
||||
// Full precision for x * y
|
||||
(uint256 xyHi, uint256 xyLo) = _mulHighLow(x, y);
|
||||
|
||||
// Assume result won't overflow (see {testMulDivDomain})
|
||||
// This also checks that `d` is positive
|
||||
vm.assume(xyHi < d);
|
||||
|
||||
// Perform muldiv
|
||||
uint256 q = Math.mulDiv(x, y, d);
|
||||
|
||||
// Full precision for q * d
|
||||
(uint256 qdHi, uint256 qdLo) = _mulHighLow(q, d);
|
||||
// Add remainder of x * y / d (computed as rem = (x * y % d))
|
||||
(uint256 qdRemLo, uint256 c) = _addCarry(qdLo, _mulmod(x, y, d));
|
||||
uint256 qdRemHi = qdHi + c;
|
||||
|
||||
// Full precision check that x * y = q * d + rem
|
||||
assertEq(xyHi, qdRemHi);
|
||||
assertEq(xyLo, qdRemLo);
|
||||
}
|
||||
|
||||
function testMulDivDomain(uint256 x, uint256 y, uint256 d) public {
|
||||
(uint256 xyHi, ) = _mulHighLow(x, y);
|
||||
|
||||
// Violate {testMulDiv} assumption (covers d is 0 and result overflow)
|
||||
vm.assume(xyHi >= d);
|
||||
|
||||
// we are outside the scope of {testMulDiv}, we expect muldiv to revert
|
||||
try this.muldiv(x, y, d) returns (uint256) {
|
||||
fail();
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// External call
|
||||
function muldiv(uint256 x, uint256 y, uint256 d) external pure returns (uint256) {
|
||||
return Math.mulDiv(x, y, d);
|
||||
}
|
||||
|
||||
// Helpers
|
||||
function _asRounding(uint8 r) private pure returns (Math.Rounding) {
|
||||
vm.assume(r < uint8(type(Math.Rounding).max));
|
||||
return Math.Rounding(r);
|
||||
}
|
||||
|
||||
function _mulmod(uint256 x, uint256 y, uint256 z) private pure returns (uint256 r) {
|
||||
assembly {
|
||||
r := mulmod(x, y, z)
|
||||
}
|
||||
}
|
||||
|
||||
function _mulHighLow(uint256 x, uint256 y) private pure returns (uint256 high, uint256 low) {
|
||||
(uint256 x0, uint256 x1) = (x & type(uint128).max, x >> 128);
|
||||
(uint256 y0, uint256 y1) = (y & type(uint128).max, y >> 128);
|
||||
|
||||
// Karatsuba algorithm
|
||||
// https://en.wikipedia.org/wiki/Karatsuba_algorithm
|
||||
uint256 z2 = x1 * y1;
|
||||
uint256 z1a = x1 * y0;
|
||||
uint256 z1b = x0 * y1;
|
||||
uint256 z0 = x0 * y0;
|
||||
|
||||
uint256 carry = ((z1a & type(uint128).max) + (z1b & type(uint128).max) + (z0 >> 128)) >> 128;
|
||||
|
||||
high = z2 + (z1a >> 128) + (z1b >> 128) + carry;
|
||||
|
||||
unchecked {
|
||||
low = x * y;
|
||||
}
|
||||
}
|
||||
|
||||
function _addCarry(uint256 x, uint256 y) private pure returns (uint256 res, uint256 carry) {
|
||||
unchecked {
|
||||
res = x + y;
|
||||
}
|
||||
carry = res < x ? 1 : 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const { MAX_UINT256 } = constants;
|
||||
const { Rounding } = require('../../helpers/enums.js');
|
||||
|
||||
const Math = artifacts.require('$Math');
|
||||
|
||||
contract('Math', function () {
|
||||
const min = new BN('1234');
|
||||
const max = new BN('5678');
|
||||
const MAX_UINT256_SUB1 = MAX_UINT256.sub(new BN('1'));
|
||||
const MAX_UINT256_SUB2 = MAX_UINT256.sub(new BN('2'));
|
||||
|
||||
beforeEach(async function () {
|
||||
this.math = await Math.new();
|
||||
});
|
||||
|
||||
describe('max', function () {
|
||||
it('is correctly detected in first argument position', async function () {
|
||||
expect(await this.math.$max(max, min)).to.be.bignumber.equal(max);
|
||||
});
|
||||
|
||||
it('is correctly detected in second argument position', async function () {
|
||||
expect(await this.math.$max(min, max)).to.be.bignumber.equal(max);
|
||||
});
|
||||
});
|
||||
|
||||
describe('min', function () {
|
||||
it('is correctly detected in first argument position', async function () {
|
||||
expect(await this.math.$min(min, max)).to.be.bignumber.equal(min);
|
||||
});
|
||||
|
||||
it('is correctly detected in second argument position', async function () {
|
||||
expect(await this.math.$min(max, min)).to.be.bignumber.equal(min);
|
||||
});
|
||||
});
|
||||
|
||||
describe('average', function () {
|
||||
function bnAverage(a, b) {
|
||||
return a.add(b).divn(2);
|
||||
}
|
||||
|
||||
it('is correctly calculated with two odd numbers', async function () {
|
||||
const a = new BN('57417');
|
||||
const b = new BN('95431');
|
||||
expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
|
||||
});
|
||||
|
||||
it('is correctly calculated with two even numbers', async function () {
|
||||
const a = new BN('42304');
|
||||
const b = new BN('84346');
|
||||
expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
|
||||
});
|
||||
|
||||
it('is correctly calculated with one even and one odd number', async function () {
|
||||
const a = new BN('57417');
|
||||
const b = new BN('84346');
|
||||
expect(await this.math.$average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
|
||||
});
|
||||
|
||||
it('is correctly calculated with two max uint256 numbers', async function () {
|
||||
const a = MAX_UINT256;
|
||||
expect(await this.math.$average(a, a)).to.be.bignumber.equal(bnAverage(a, a));
|
||||
});
|
||||
});
|
||||
|
||||
describe('ceilDiv', function () {
|
||||
it('does not round up on exact division', async function () {
|
||||
const a = new BN('10');
|
||||
const b = new BN('5');
|
||||
expect(await this.math.$ceilDiv(a, b)).to.be.bignumber.equal('2');
|
||||
});
|
||||
|
||||
it('rounds up on division with remainders', async function () {
|
||||
const a = new BN('42');
|
||||
const b = new BN('13');
|
||||
expect(await this.math.$ceilDiv(a, b)).to.be.bignumber.equal('4');
|
||||
});
|
||||
|
||||
it('does not overflow', async function () {
|
||||
const b = new BN('2');
|
||||
const result = new BN('1').shln(255);
|
||||
expect(await this.math.$ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(result);
|
||||
});
|
||||
|
||||
it('correctly computes max uint256 divided by 1', async function () {
|
||||
const b = new BN('1');
|
||||
expect(await this.math.$ceilDiv(MAX_UINT256, b)).to.be.bignumber.equal(MAX_UINT256);
|
||||
});
|
||||
});
|
||||
|
||||
describe('muldiv', function () {
|
||||
it('divide by 0', async function () {
|
||||
await expectRevert.unspecified(this.math.$mulDiv(1, 1, 0, Rounding.Down));
|
||||
});
|
||||
|
||||
describe('does round down', async function () {
|
||||
it('small values', async function () {
|
||||
expect(await this.math.$mulDiv('3', '4', '5', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$mulDiv('3', '5', '5', Rounding.Down)).to.be.bignumber.equal('3');
|
||||
});
|
||||
|
||||
it('large values', async function () {
|
||||
expect(
|
||||
await this.math.$mulDiv(new BN('42'), MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
|
||||
).to.be.bignumber.equal(new BN('41'));
|
||||
|
||||
expect(await this.math.$mulDiv(new BN('17'), MAX_UINT256, MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
|
||||
new BN('17'),
|
||||
);
|
||||
|
||||
expect(
|
||||
await this.math.$mulDiv(MAX_UINT256_SUB1, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
|
||||
).to.be.bignumber.equal(MAX_UINT256_SUB2);
|
||||
|
||||
expect(
|
||||
await this.math.$mulDiv(MAX_UINT256, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Down),
|
||||
).to.be.bignumber.equal(MAX_UINT256_SUB1);
|
||||
|
||||
expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256, MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
|
||||
MAX_UINT256,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('does round up', async function () {
|
||||
it('small values', async function () {
|
||||
expect(await this.math.$mulDiv('3', '4', '5', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.$mulDiv('3', '5', '5', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
});
|
||||
|
||||
it('large values', async function () {
|
||||
expect(await this.math.$mulDiv(new BN('42'), MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
|
||||
new BN('42'),
|
||||
);
|
||||
|
||||
expect(await this.math.$mulDiv(new BN('17'), MAX_UINT256, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
|
||||
new BN('17'),
|
||||
);
|
||||
|
||||
expect(
|
||||
await this.math.$mulDiv(MAX_UINT256_SUB1, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up),
|
||||
).to.be.bignumber.equal(MAX_UINT256_SUB1);
|
||||
|
||||
expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256_SUB1, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
|
||||
MAX_UINT256_SUB1,
|
||||
);
|
||||
|
||||
expect(await this.math.$mulDiv(MAX_UINT256, MAX_UINT256, MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
|
||||
MAX_UINT256,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('sqrt', function () {
|
||||
it('rounds down', async function () {
|
||||
expect(await this.math.$sqrt('0', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$sqrt('1', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$sqrt('2', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$sqrt('3', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$sqrt('4', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$sqrt('144', Rounding.Down)).to.be.bignumber.equal('12');
|
||||
expect(await this.math.$sqrt('999999', Rounding.Down)).to.be.bignumber.equal('999');
|
||||
expect(await this.math.$sqrt('1000000', Rounding.Down)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.$sqrt('1000001', Rounding.Down)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.$sqrt('1002000', Rounding.Down)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.$sqrt('1002001', Rounding.Down)).to.be.bignumber.equal('1001');
|
||||
expect(await this.math.$sqrt(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
|
||||
'340282366920938463463374607431768211455',
|
||||
);
|
||||
});
|
||||
|
||||
it('rounds up', async function () {
|
||||
expect(await this.math.$sqrt('0', Rounding.Up)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$sqrt('1', Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$sqrt('2', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$sqrt('3', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$sqrt('4', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$sqrt('144', Rounding.Up)).to.be.bignumber.equal('12');
|
||||
expect(await this.math.$sqrt('999999', Rounding.Up)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.$sqrt('1000000', Rounding.Up)).to.be.bignumber.equal('1000');
|
||||
expect(await this.math.$sqrt('1000001', Rounding.Up)).to.be.bignumber.equal('1001');
|
||||
expect(await this.math.$sqrt('1002000', Rounding.Up)).to.be.bignumber.equal('1001');
|
||||
expect(await this.math.$sqrt('1002001', Rounding.Up)).to.be.bignumber.equal('1001');
|
||||
expect(await this.math.$sqrt(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal(
|
||||
'340282366920938463463374607431768211456',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('log', function () {
|
||||
describe('log2', function () {
|
||||
it('rounds down', async function () {
|
||||
// For some reason calling .$log2() directly fails
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('0', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('1', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('2', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('3', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('4', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('5', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('6', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('7', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('8', Rounding.Down)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('9', Rounding.Down)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)'](MAX_UINT256, Rounding.Down)).to.be.bignumber.equal(
|
||||
'255',
|
||||
);
|
||||
});
|
||||
|
||||
it('rounds up', async function () {
|
||||
// For some reason calling .$log2() directly fails
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('0', Rounding.Up)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('1', Rounding.Up)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('2', Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('3', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('4', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('5', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('6', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('7', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('8', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)']('9', Rounding.Up)).to.be.bignumber.equal('4');
|
||||
expect(await this.math.methods['$log2(uint256,uint8)'](MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('256');
|
||||
});
|
||||
});
|
||||
|
||||
describe('log10', function () {
|
||||
it('rounds down', async function () {
|
||||
expect(await this.math.$log10('0', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log10('1', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log10('2', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log10('9', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log10('10', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log10('11', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log10('99', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log10('100', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log10('101', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log10('999', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log10('1000', Rounding.Down)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.$log10('1001', Rounding.Down)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.$log10(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal('77');
|
||||
});
|
||||
|
||||
it('rounds up', async function () {
|
||||
expect(await this.math.$log10('0', Rounding.Up)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log10('1', Rounding.Up)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log10('2', Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log10('9', Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log10('10', Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log10('11', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log10('99', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log10('100', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log10('101', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.$log10('999', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.$log10('1000', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.$log10('1001', Rounding.Up)).to.be.bignumber.equal('4');
|
||||
expect(await this.math.$log10(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('78');
|
||||
});
|
||||
});
|
||||
|
||||
describe('log256', function () {
|
||||
it('rounds down', async function () {
|
||||
expect(await this.math.$log256('0', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log256('1', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log256('2', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log256('255', Rounding.Down)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log256('256', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log256('257', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log256('65535', Rounding.Down)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log256('65536', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log256('65537', Rounding.Down)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log256(MAX_UINT256, Rounding.Down)).to.be.bignumber.equal('31');
|
||||
});
|
||||
|
||||
it('rounds up', async function () {
|
||||
expect(await this.math.$log256('0', Rounding.Up)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log256('1', Rounding.Up)).to.be.bignumber.equal('0');
|
||||
expect(await this.math.$log256('2', Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log256('255', Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log256('256', Rounding.Up)).to.be.bignumber.equal('1');
|
||||
expect(await this.math.$log256('257', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log256('65535', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log256('65536', Rounding.Up)).to.be.bignumber.equal('2');
|
||||
expect(await this.math.$log256('65537', Rounding.Up)).to.be.bignumber.equal('3');
|
||||
expect(await this.math.$log256(MAX_UINT256, Rounding.Up)).to.be.bignumber.equal('32');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,152 @@
|
||||
const { BN, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const { range } = require('../../../scripts/helpers');
|
||||
|
||||
const SafeCast = artifacts.require('$SafeCast');
|
||||
|
||||
contract('SafeCast', async function () {
|
||||
beforeEach(async function () {
|
||||
this.safeCast = await SafeCast.new();
|
||||
});
|
||||
|
||||
function testToUint(bits) {
|
||||
describe(`toUint${bits}`, () => {
|
||||
const maxValue = new BN('2').pow(new BN(bits)).subn(1);
|
||||
|
||||
it('downcasts 0', async function () {
|
||||
expect(await this.safeCast[`$toUint${bits}`](0)).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('downcasts 1', async function () {
|
||||
expect(await this.safeCast[`$toUint${bits}`](1)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it(`downcasts 2^${bits} - 1 (${maxValue})`, async function () {
|
||||
expect(await this.safeCast[`$toUint${bits}`](maxValue)).to.be.bignumber.equal(maxValue);
|
||||
});
|
||||
|
||||
it(`reverts when downcasting 2^${bits} (${maxValue.addn(1)})`, async function () {
|
||||
await expectRevert(
|
||||
this.safeCast[`$toUint${bits}`](maxValue.addn(1)),
|
||||
`SafeCast: value doesn't fit in ${bits} bits`,
|
||||
);
|
||||
});
|
||||
|
||||
it(`reverts when downcasting 2^${bits} + 1 (${maxValue.addn(2)})`, async function () {
|
||||
await expectRevert(
|
||||
this.safeCast[`$toUint${bits}`](maxValue.addn(2)),
|
||||
`SafeCast: value doesn't fit in ${bits} bits`,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
range(8, 256, 8).forEach(bits => testToUint(bits));
|
||||
|
||||
describe('toUint256', () => {
|
||||
const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
|
||||
const minInt256 = new BN('2').pow(new BN(255)).neg();
|
||||
|
||||
it('casts 0', async function () {
|
||||
expect(await this.safeCast.$toUint256(0)).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('casts 1', async function () {
|
||||
expect(await this.safeCast.$toUint256(1)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it(`casts INT256_MAX (${maxInt256})`, async function () {
|
||||
expect(await this.safeCast.$toUint256(maxInt256)).to.be.bignumber.equal(maxInt256);
|
||||
});
|
||||
|
||||
it('reverts when casting -1', async function () {
|
||||
await expectRevert(this.safeCast.$toUint256(-1), 'SafeCast: value must be positive');
|
||||
});
|
||||
|
||||
it(`reverts when casting INT256_MIN (${minInt256})`, async function () {
|
||||
await expectRevert(this.safeCast.$toUint256(minInt256), 'SafeCast: value must be positive');
|
||||
});
|
||||
});
|
||||
|
||||
function testToInt(bits) {
|
||||
describe(`toInt${bits}`, () => {
|
||||
const minValue = new BN('-2').pow(new BN(bits - 1));
|
||||
const maxValue = new BN('2').pow(new BN(bits - 1)).subn(1);
|
||||
|
||||
it('downcasts 0', async function () {
|
||||
expect(await this.safeCast[`$toInt${bits}`](0)).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('downcasts 1', async function () {
|
||||
expect(await this.safeCast[`$toInt${bits}`](1)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it('downcasts -1', async function () {
|
||||
expect(await this.safeCast[`$toInt${bits}`](-1)).to.be.bignumber.equal('-1');
|
||||
});
|
||||
|
||||
it(`downcasts -2^${bits - 1} (${minValue})`, async function () {
|
||||
expect(await this.safeCast[`$toInt${bits}`](minValue)).to.be.bignumber.equal(minValue);
|
||||
});
|
||||
|
||||
it(`downcasts 2^${bits - 1} - 1 (${maxValue})`, async function () {
|
||||
expect(await this.safeCast[`$toInt${bits}`](maxValue)).to.be.bignumber.equal(maxValue);
|
||||
});
|
||||
|
||||
it(`reverts when downcasting -2^${bits - 1} - 1 (${minValue.subn(1)})`, async function () {
|
||||
await expectRevert(
|
||||
this.safeCast[`$toInt${bits}`](minValue.subn(1)),
|
||||
`SafeCast: value doesn't fit in ${bits} bits`,
|
||||
);
|
||||
});
|
||||
|
||||
it(`reverts when downcasting -2^${bits - 1} - 2 (${minValue.subn(2)})`, async function () {
|
||||
await expectRevert(
|
||||
this.safeCast[`$toInt${bits}`](minValue.subn(2)),
|
||||
`SafeCast: value doesn't fit in ${bits} bits`,
|
||||
);
|
||||
});
|
||||
|
||||
it(`reverts when downcasting 2^${bits - 1} (${maxValue.addn(1)})`, async function () {
|
||||
await expectRevert(
|
||||
this.safeCast[`$toInt${bits}`](maxValue.addn(1)),
|
||||
`SafeCast: value doesn't fit in ${bits} bits`,
|
||||
);
|
||||
});
|
||||
|
||||
it(`reverts when downcasting 2^${bits - 1} + 1 (${maxValue.addn(2)})`, async function () {
|
||||
await expectRevert(
|
||||
this.safeCast[`$toInt${bits}`](maxValue.addn(2)),
|
||||
`SafeCast: value doesn't fit in ${bits} bits`,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
range(8, 256, 8).forEach(bits => testToInt(bits));
|
||||
|
||||
describe('toInt256', () => {
|
||||
const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
|
||||
const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
|
||||
|
||||
it('casts 0', async function () {
|
||||
expect(await this.safeCast.$toInt256(0)).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('casts 1', async function () {
|
||||
expect(await this.safeCast.$toInt256(1)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it(`casts INT256_MAX (${maxInt256})`, async function () {
|
||||
expect(await this.safeCast.$toInt256(maxInt256)).to.be.bignumber.equal(maxInt256);
|
||||
});
|
||||
|
||||
it(`reverts when casting INT256_MAX + 1 (${maxInt256.addn(1)})`, async function () {
|
||||
await expectRevert(this.safeCast.$toInt256(maxInt256.addn(1)), "SafeCast: value doesn't fit in an int256");
|
||||
});
|
||||
|
||||
it(`reverts when casting UINT256_MAX (${maxUint256})`, async function () {
|
||||
await expectRevert(this.safeCast.$toInt256(maxUint256), "SafeCast: value doesn't fit in an int256");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,433 @@
|
||||
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { MAX_UINT256 } = constants;
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const SafeMath = artifacts.require('$SafeMath');
|
||||
const SafeMathMemoryCheck = artifacts.require('$SafeMathMemoryCheck');
|
||||
|
||||
function expectStruct(value, expected) {
|
||||
for (const key in expected) {
|
||||
if (BN.isBN(value[key])) {
|
||||
expect(value[key]).to.be.bignumber.equal(expected[key]);
|
||||
} else {
|
||||
expect(value[key]).to.be.equal(expected[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
contract('SafeMath', function () {
|
||||
beforeEach(async function () {
|
||||
this.safeMath = await SafeMath.new();
|
||||
});
|
||||
|
||||
async function testCommutative(fn, lhs, rhs, expected, ...extra) {
|
||||
expect(await fn(lhs, rhs, ...extra)).to.be.bignumber.equal(expected);
|
||||
expect(await fn(rhs, lhs, ...extra)).to.be.bignumber.equal(expected);
|
||||
}
|
||||
|
||||
async function testFailsCommutative(fn, lhs, rhs, reason, ...extra) {
|
||||
if (reason === undefined) {
|
||||
await expectRevert.unspecified(fn(lhs, rhs, ...extra));
|
||||
await expectRevert.unspecified(fn(rhs, lhs, ...extra));
|
||||
} else {
|
||||
await expectRevert(fn(lhs, rhs, ...extra), reason);
|
||||
await expectRevert(fn(rhs, lhs, ...extra), reason);
|
||||
}
|
||||
}
|
||||
|
||||
async function testCommutativeIterable(fn, lhs, rhs, expected, ...extra) {
|
||||
expectStruct(await fn(lhs, rhs, ...extra), expected);
|
||||
expectStruct(await fn(rhs, lhs, ...extra), expected);
|
||||
}
|
||||
|
||||
describe('with flag', function () {
|
||||
describe('add', function () {
|
||||
it('adds correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
testCommutativeIterable(this.safeMath.$tryAdd, a, b, [true, a.add(b)]);
|
||||
});
|
||||
|
||||
it('reverts on addition overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('1');
|
||||
|
||||
testCommutativeIterable(this.safeMath.$tryAdd, a, b, [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sub', function () {
|
||||
it('subtracts correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
expectStruct(await this.safeMath.$trySub(a, b), [true, a.sub(b)]);
|
||||
});
|
||||
|
||||
it('reverts if subtraction result would be negative', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.$trySub(a, b), [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mul', function () {
|
||||
it('multiplies correctly', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
testCommutativeIterable(this.safeMath.$tryMul, a, b, [true, a.mul(b)]);
|
||||
});
|
||||
|
||||
it('multiplies by zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
testCommutativeIterable(this.safeMath.$tryMul, a, b, [true, a.mul(b)]);
|
||||
});
|
||||
|
||||
it('reverts on multiplication overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('2');
|
||||
|
||||
testCommutativeIterable(this.safeMath.$tryMul, a, b, [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('div', function () {
|
||||
it('divides correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.$tryDiv(a, b), [true, a.div(b)]);
|
||||
});
|
||||
|
||||
it('divides zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.$tryDiv(a, b), [true, a.div(b)]);
|
||||
});
|
||||
|
||||
it('returns complete number result on non-even division', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.$tryDiv(a, b), [true, a.div(b)]);
|
||||
});
|
||||
|
||||
it('reverts on division by zero', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
expectStruct(await this.safeMath.$tryDiv(a, b), [false, '0']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mod', function () {
|
||||
describe('modulos correctly', async function () {
|
||||
it('when the dividend is smaller than the divisor', async function () {
|
||||
const a = new BN('284');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [true, a.mod(b)]);
|
||||
});
|
||||
|
||||
it('when the dividend is equal to the divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [true, a.mod(b)]);
|
||||
});
|
||||
|
||||
it('when the dividend is larger than the divisor', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [true, a.mod(b)]);
|
||||
});
|
||||
|
||||
it('when the dividend is a multiple of the divisor', async function () {
|
||||
const a = new BN('17034'); // 17034 == 5678 * 3
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [true, a.mod(b)]);
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts with a 0 divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [false, '0']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with default revert message', function () {
|
||||
describe('add', function () {
|
||||
it('adds correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
await testCommutative(this.safeMath.$add, a, b, a.add(b));
|
||||
});
|
||||
|
||||
it('reverts on addition overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('1');
|
||||
|
||||
await testFailsCommutative(this.safeMath.$add, a, b, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sub', function () {
|
||||
it('subtracts correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
expect(await this.safeMath.$sub(a, b)).to.be.bignumber.equal(a.sub(b));
|
||||
});
|
||||
|
||||
it('reverts if subtraction result would be negative', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.$sub(a, b));
|
||||
});
|
||||
});
|
||||
|
||||
describe('mul', function () {
|
||||
it('multiplies correctly', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
await testCommutative(this.safeMath.$mul, a, b, a.mul(b));
|
||||
});
|
||||
|
||||
it('multiplies by zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
await testCommutative(this.safeMath.$mul, a, b, '0');
|
||||
});
|
||||
|
||||
it('reverts on multiplication overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('2');
|
||||
|
||||
await testFailsCommutative(this.safeMath.$mul, a, b, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('div', function () {
|
||||
it('divides correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal(a.div(b));
|
||||
});
|
||||
|
||||
it('divides zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('returns complete number result on non-even division', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it('reverts on division by zero', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.$div(a, b));
|
||||
});
|
||||
});
|
||||
|
||||
describe('mod', function () {
|
||||
describe('modulos correctly', async function () {
|
||||
it('when the dividend is smaller than the divisor', async function () {
|
||||
const a = new BN('284');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is equal to the divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is larger than the divisor', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is a multiple of the divisor', async function () {
|
||||
const a = new BN('17034'); // 17034 == 5678 * 3
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts with a 0 divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.$mod(a, b));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('with custom revert message', function () {
|
||||
describe('sub', function () {
|
||||
it('subtracts correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
expect(
|
||||
await this.safeMath.methods['$sub(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
).to.be.bignumber.equal(a.sub(b));
|
||||
});
|
||||
|
||||
it('reverts if subtraction result would be negative', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
await expectRevert(
|
||||
this.safeMath.methods['$sub(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
'MyErrorMessage',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('div', function () {
|
||||
it('divides correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(
|
||||
await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
).to.be.bignumber.equal(a.div(b));
|
||||
});
|
||||
|
||||
it('divides zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(
|
||||
await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('returns complete number result on non-even division', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(
|
||||
await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it('reverts on division by zero', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert(
|
||||
this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
'MyErrorMessage',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mod', function () {
|
||||
describe('modulos correctly', async function () {
|
||||
it('when the dividend is smaller than the divisor', async function () {
|
||||
const a = new BN('284');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(
|
||||
await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is equal to the divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(
|
||||
await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is larger than the divisor', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(
|
||||
await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is a multiple of the divisor', async function () {
|
||||
const a = new BN('17034'); // 17034 == 5678 * 3
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(
|
||||
await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts with a 0 divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert(
|
||||
this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
'MyErrorMessage',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('memory leakage', function () {
|
||||
beforeEach(async function () {
|
||||
this.safeMathMemoryCheck = await SafeMathMemoryCheck.new();
|
||||
});
|
||||
|
||||
it('add', async function () {
|
||||
expect(await this.safeMathMemoryCheck.$addMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('sub', async function () {
|
||||
expect(await this.safeMathMemoryCheck.$subMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('mul', async function () {
|
||||
expect(await this.safeMathMemoryCheck.$mulMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('div', async function () {
|
||||
expect(await this.safeMathMemoryCheck.$divMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('mod', async function () {
|
||||
expect(await this.safeMathMemoryCheck.$modMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
const { BN, constants } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const { MIN_INT256, MAX_INT256 } = constants;
|
||||
|
||||
const SignedMath = artifacts.require('$SignedMath');
|
||||
|
||||
contract('SignedMath', function () {
|
||||
const min = new BN('-1234');
|
||||
const max = new BN('5678');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.math = await SignedMath.new();
|
||||
});
|
||||
|
||||
describe('max', function () {
|
||||
it('is correctly detected in first argument position', async function () {
|
||||
expect(await this.math.$max(max, min)).to.be.bignumber.equal(max);
|
||||
});
|
||||
|
||||
it('is correctly detected in second argument position', async function () {
|
||||
expect(await this.math.$max(min, max)).to.be.bignumber.equal(max);
|
||||
});
|
||||
});
|
||||
|
||||
describe('min', function () {
|
||||
it('is correctly detected in first argument position', async function () {
|
||||
expect(await this.math.$min(min, max)).to.be.bignumber.equal(min);
|
||||
});
|
||||
|
||||
it('is correctly detected in second argument position', async function () {
|
||||
expect(await this.math.$min(max, min)).to.be.bignumber.equal(min);
|
||||
});
|
||||
});
|
||||
|
||||
describe('average', function () {
|
||||
function bnAverage(a, b) {
|
||||
return a.add(b).divn(2);
|
||||
}
|
||||
|
||||
it('is correctly calculated with various input', async function () {
|
||||
const valuesX = [
|
||||
new BN('0'),
|
||||
new BN('3'),
|
||||
new BN('-3'),
|
||||
new BN('4'),
|
||||
new BN('-4'),
|
||||
new BN('57417'),
|
||||
new BN('-57417'),
|
||||
new BN('42304'),
|
||||
new BN('-42304'),
|
||||
MIN_INT256,
|
||||
MAX_INT256,
|
||||
];
|
||||
|
||||
const valuesY = [
|
||||
new BN('0'),
|
||||
new BN('5'),
|
||||
new BN('-5'),
|
||||
new BN('2'),
|
||||
new BN('-2'),
|
||||
new BN('57417'),
|
||||
new BN('-57417'),
|
||||
new BN('42304'),
|
||||
new BN('-42304'),
|
||||
MIN_INT256,
|
||||
MAX_INT256,
|
||||
];
|
||||
|
||||
for (const x of valuesX) {
|
||||
for (const y of valuesY) {
|
||||
expect(await this.math.$average(x, y)).to.be.bignumber.equal(
|
||||
bnAverage(x, y),
|
||||
`Bad result for average(${x}, ${y})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('abs', function () {
|
||||
for (const n of [
|
||||
MIN_INT256,
|
||||
MIN_INT256.addn(1),
|
||||
new BN('-1'),
|
||||
new BN('0'),
|
||||
new BN('1'),
|
||||
MAX_INT256.subn(1),
|
||||
MAX_INT256,
|
||||
]) {
|
||||
it(`correctly computes the absolute value of ${n}`, async function () {
|
||||
expect(await this.math.$abs(n)).to.be.bignumber.equal(n.abs());
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,152 @@
|
||||
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { MAX_INT256, MIN_INT256 } = constants;
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const SignedSafeMath = artifacts.require('$SignedSafeMath');
|
||||
|
||||
contract('SignedSafeMath', function () {
|
||||
beforeEach(async function () {
|
||||
this.safeMath = await SignedSafeMath.new();
|
||||
});
|
||||
|
||||
async function testCommutative(fn, lhs, rhs, expected) {
|
||||
expect(await fn(lhs, rhs)).to.be.bignumber.equal(expected);
|
||||
expect(await fn(rhs, lhs)).to.be.bignumber.equal(expected);
|
||||
}
|
||||
|
||||
async function testFailsCommutative(fn, lhs, rhs) {
|
||||
await expectRevert.unspecified(fn(lhs, rhs));
|
||||
await expectRevert.unspecified(fn(rhs, lhs));
|
||||
}
|
||||
|
||||
describe('add', function () {
|
||||
it('adds correctly if it does not overflow and the result is positive', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
await testCommutative(this.safeMath.$add, a, b, a.add(b));
|
||||
});
|
||||
|
||||
it('adds correctly if it does not overflow and the result is negative', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = MIN_INT256;
|
||||
|
||||
await testCommutative(this.safeMath.$add, a, b, a.add(b));
|
||||
});
|
||||
|
||||
it('reverts on positive addition overflow', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BN('1');
|
||||
|
||||
await testFailsCommutative(this.safeMath.$add, a, b);
|
||||
});
|
||||
|
||||
it('reverts on negative addition overflow', async function () {
|
||||
const a = MIN_INT256;
|
||||
const b = new BN('-1');
|
||||
|
||||
await testFailsCommutative(this.safeMath.$add, a, b);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sub', function () {
|
||||
it('subtracts correctly if it does not overflow and the result is positive', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
const result = await this.safeMath.$sub(a, b);
|
||||
expect(result).to.be.bignumber.equal(a.sub(b));
|
||||
});
|
||||
|
||||
it('subtracts correctly if it does not overflow and the result is negative', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
const result = await this.safeMath.$sub(a, b);
|
||||
expect(result).to.be.bignumber.equal(a.sub(b));
|
||||
});
|
||||
|
||||
it('reverts on positive subtraction overflow', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BN('-1');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.$sub(a, b));
|
||||
});
|
||||
|
||||
it('reverts on negative subtraction overflow', async function () {
|
||||
const a = MIN_INT256;
|
||||
const b = new BN('1');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.$sub(a, b));
|
||||
});
|
||||
});
|
||||
|
||||
describe('mul', function () {
|
||||
it('multiplies correctly', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('-1234');
|
||||
|
||||
await testCommutative(this.safeMath.$mul, a, b, a.mul(b));
|
||||
});
|
||||
|
||||
it('multiplies by zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
await testCommutative(this.safeMath.$mul, a, b, '0');
|
||||
});
|
||||
|
||||
it('reverts on multiplication overflow, positive operands', async function () {
|
||||
const a = MAX_INT256;
|
||||
const b = new BN('2');
|
||||
|
||||
await testFailsCommutative(this.safeMath.$mul, a, b);
|
||||
});
|
||||
|
||||
it('reverts when minimum integer is multiplied by -1', async function () {
|
||||
const a = MIN_INT256;
|
||||
const b = new BN('-1');
|
||||
|
||||
await testFailsCommutative(this.safeMath.$mul, a, b);
|
||||
});
|
||||
});
|
||||
|
||||
describe('div', function () {
|
||||
it('divides correctly', async function () {
|
||||
const a = new BN('-5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
const result = await this.safeMath.$div(a, b);
|
||||
expect(result).to.be.bignumber.equal(a.div(b));
|
||||
});
|
||||
|
||||
it('divides zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('returns complete number result on non-even division', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it('reverts on division by zero', async function () {
|
||||
const a = new BN('-5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.$div(a, b));
|
||||
});
|
||||
|
||||
it('reverts on overflow, negative second', async function () {
|
||||
const a = new BN(MIN_INT256);
|
||||
const b = new BN('-1');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.$div(a, b));
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user