@ -6,6 +6,9 @@ const { expectRevertCustomError } = require('../../helpers/customError.js');
const Math = artifacts . require ( '$Math' ) ;
const RoundingDown = [ Rounding . Floor , Rounding . Trunc ] ;
const RoundingUp = [ Rounding . Ceil , Rounding . Expand ] ;
function expectStruct ( value , expected ) {
for ( const key in expected ) {
if ( BN . isBN ( value [ key ] ) ) {
@ -244,202 +247,222 @@ contract('Math', function () {
describe ( 'muldiv' , function ( ) {
it ( 'divide by 0' , async function ( ) {
await expectRevert . unspecified ( this . math . $mulDiv ( 1 , 1 , 0 , Rounding . Down ) ) ;
await expectRevert . unspecified ( this . math . $mulDiv ( 1 , 1 , 0 , Rounding . Floor ) ) ;
} ) ;
it ( 'reverts with result higher than 2 ^ 256' , async function ( ) {
await expectRevertCustomError ( this . math . $mulDiv ( 5 , MAX _UINT256 , 2 , Rounding . Down ) , 'MathOverflowedMulDiv' , [ ] ) ;
await expectRevertCustomError ( this . math . $mulDiv ( 5 , MAX _UINT256 , 2 , Rounding . Floor ) , 'MathOverflowedMulDiv' , [ ] ) ;
} ) ;
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' ) ;
for ( const rounding of RoundingDown ) {
expect ( await this . math . $mulDiv ( '3' , '4' , '5' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $mulDiv ( '3' , '5' , '5' , rounding ) ) . 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 ,
) ;
for ( const rounding of RoundingDown ) {
expect ( await this . math . $mulDiv ( new BN ( '42' ) , MAX _UINT256 _SUB1 , MAX _UINT256 , rounding ) ) . to . be . bignumber . equal (
new BN ( '41' ) ,
) ;
expect ( await this . math . $mulDiv ( new BN ( '17' ) , MAX _UINT256 , MAX _UINT256 , rounding ) ) . to . be . bignumber . equal (
new BN ( '17' ) ,
) ;
expect (
await this . math . $mulDiv ( MAX _UINT256 _SUB1 , MAX _UINT256 _SUB1 , MAX _UINT256 , rounding ) ,
) . to . be . bignumber . equal ( MAX _UINT256 _SUB2 ) ;
expect ( await this . math . $mulDiv ( MAX _UINT256 , MAX _UINT256 _SUB1 , MAX _UINT256 , rounding ) ) . to . be . bignumber . equal (
MAX _UINT256 _SUB1 ,
) ;
expect ( await this . math . $mulDiv ( MAX _UINT256 , MAX _UINT256 , MAX _UINT256 , rounding ) ) . 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' ) ;
for ( const rounding of RoundingUp ) {
expect ( await this . math . $mulDiv ( '3' , '4' , '5' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . $mulDiv ( '3' , '5' , '5' , rounding ) ) . 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 ,
) ;
for ( const rounding of RoundingUp ) {
expect ( await this . math . $mulDiv ( new BN ( '42' ) , MAX _UINT256 _SUB1 , MAX _UINT256 , rounding ) ) . to . be . bignumber . equal (
new BN ( '42' ) ,
) ;
expect ( await this . math . $mulDiv ( new BN ( '17' ) , MAX _UINT256 , MAX _UINT256 , rounding ) ) . to . be . bignumber . equal (
new BN ( '17' ) ,
) ;
expect (
await this . math . $mulDiv ( MAX _UINT256 _SUB1 , MAX _UINT256 _SUB1 , MAX _UINT256 , rounding ) ,
) . to . be . bignumber . equal ( MAX _UINT256 _SUB1 ) ;
expect ( await this . math . $mulDiv ( MAX _UINT256 , MAX _UINT256 _SUB1 , MAX _UINT256 , rounding ) ) . to . be . bignumber . equal (
MAX _UINT256 _SUB1 ,
) ;
expect ( await this . math . $mulDiv ( MAX _UINT256 , MAX _UINT256 , MAX _UINT256 , rounding ) ) . 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' ,
) ;
for ( const rounding of RoundingDown ) {
expect ( await this . math . $sqrt ( '0' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $sqrt ( '1' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $sqrt ( '2' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $sqrt ( '3' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $sqrt ( '4' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $sqrt ( '144' , rounding ) ) . to . be . bignumber . equal ( '12' ) ;
expect ( await this . math . $sqrt ( '999999' , rounding ) ) . to . be . bignumber . equal ( '999' ) ;
expect ( await this . math . $sqrt ( '1000000' , rounding ) ) . to . be . bignumber . equal ( '1000' ) ;
expect ( await this . math . $sqrt ( '1000001' , rounding ) ) . to . be . bignumber . equal ( '1000' ) ;
expect ( await this . math . $sqrt ( '1002000' , rounding ) ) . to . be . bignumber . equal ( '1000' ) ;
expect ( await this . math . $sqrt ( '1002001' , rounding ) ) . to . be . bignumber . equal ( '1001' ) ;
expect ( await this . math . $sqrt ( MAX _UINT256 , rounding ) ) . 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' ,
) ;
for ( const rounding of RoundingUp ) {
expect ( await this . math . $sqrt ( '0' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $sqrt ( '1' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $sqrt ( '2' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $sqrt ( '3' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $sqrt ( '4' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $sqrt ( '144' , rounding ) ) . to . be . bignumber . equal ( '12' ) ;
expect ( await this . math . $sqrt ( '999999' , rounding ) ) . to . be . bignumber . equal ( '1000' ) ;
expect ( await this . math . $sqrt ( '1000000' , rounding ) ) . to . be . bignumber . equal ( '1000' ) ;
expect ( await this . math . $sqrt ( '1000001' , rounding ) ) . to . be . bignumber . equal ( '1001' ) ;
expect ( await this . math . $sqrt ( '1002000' , rounding ) ) . to . be . bignumber . equal ( '1001' ) ;
expect ( await this . math . $sqrt ( '1002001' , rounding ) ) . to . be . bignumber . equal ( '1001' ) ;
expect ( await this . math . $sqrt ( MAX _UINT256 , rounding ) ) . 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' ,
) ;
for ( const rounding of RoundingDown ) {
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '0' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '1' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '2' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '3' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '4' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '5' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '6' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '7' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '8' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '9' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( MAX _UINT256 , rounding ) ) . 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' ) ;
for ( const rounding of RoundingUp ) {
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '0' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '1' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '2' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '3' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '4' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '5' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '6' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '7' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '8' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( '9' , rounding ) ) . to . be . bignumber . equal ( '4' ) ;
expect ( await this . math . methods [ '$log2(uint256,uint8)' ] ( MAX _UINT256 , rounding ) ) . 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' ) ;
for ( const rounding of RoundingDown ) {
expect ( await this . math . $log10 ( '0' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log10 ( '1' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log10 ( '2' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log10 ( '9' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log10 ( '10' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log10 ( '11' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log10 ( '99' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log10 ( '100' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log10 ( '101' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log10 ( '999' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log10 ( '1000' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . $log10 ( '1001' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . $log10 ( MAX _UINT256 , rounding ) ) . 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' ) ;
for ( const rounding of RoundingUp ) {
expect ( await this . math . $log10 ( '0' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log10 ( '1' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log10 ( '2' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log10 ( '9' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log10 ( '10' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log10 ( '11' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log10 ( '99' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log10 ( '100' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log10 ( '101' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . $log10 ( '999' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . $log10 ( '1000' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . $log10 ( '1001' , rounding ) ) . to . be . bignumber . equal ( '4' ) ;
expect ( await this . math . $log10 ( MAX _UINT256 , rounding ) ) . 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' ) ;
for ( const rounding of RoundingDown ) {
expect ( await this . math . $log256 ( '0' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log256 ( '1' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log256 ( '2' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log256 ( '255' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log256 ( '256' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log256 ( '257' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log256 ( '65535' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log256 ( '65536' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log256 ( '65537' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log256 ( MAX _UINT256 , rounding ) ) . 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' ) ;
for ( const rounding of RoundingUp ) {
expect ( await this . math . $log256 ( '0' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log256 ( '1' , rounding ) ) . to . be . bignumber . equal ( '0' ) ;
expect ( await this . math . $log256 ( '2' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log256 ( '255' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log256 ( '256' , rounding ) ) . to . be . bignumber . equal ( '1' ) ;
expect ( await this . math . $log256 ( '257' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log256 ( '65535' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log256 ( '65536' , rounding ) ) . to . be . bignumber . equal ( '2' ) ;
expect ( await this . math . $log256 ( '65537' , rounding ) ) . to . be . bignumber . equal ( '3' ) ;
expect ( await this . math . $log256 ( MAX _UINT256 , rounding ) ) . to . be . bignumber . equal ( '32' ) ;
}
} ) ;
} ) ;
} ) ;