Still issues with BN decimals in tests

This commit is contained in:
Andy Tudhope 2019-04-10 22:39:38 +02:00
parent 20e0fd61c5
commit 95715458ea
No known key found for this signature in database
GPG Key ID: 02A3DFA93BF26AD2
1 changed files with 47 additions and 43 deletions

View File

@ -22,9 +22,9 @@ config({
});
contract('TestBancorFormula', function () {
let ILLEGAL_VAL = BigNumber(2).exponentiatedBy(256);
let MAX_BASE_N = BigNumber(2).exponentiatedBy(256 - constants.MAX_PRECISION).minus(1);
let MIN_BASE_D = BigNumber(1);
let ILLEGAL_VAL = new BigNumber(2).exponentiatedBy(256);
let MAX_BASE_N = new BigNumber(2).exponentiatedBy(256 - constants.MAX_PRECISION).minus(1);
let MIN_BASE_D = new BigNumber(1);
let MAX_EXPONENT = 1000000;
for (let percent = 1; percent <= 100; percent++) {
@ -91,7 +91,7 @@ contract('TestBancorFormula', function () {
try {
let temp = web3.utils.toHex(values[index]);
let retVal = await TestBancorFormula.methods.generalLogTest(temp).call();
let check = BigNumber(parseInt(retVal, 10) * MAX_EXPONENT);
let check = new BigNumber(parseInt(retVal, 10) * MAX_EXPONENT);
assert(check.isLessThan(ILLEGAL_VAL), `${test}: output is too large`);
}
catch (error) {
@ -101,13 +101,13 @@ contract('TestBancorFormula', function () {
}
for (let precision = constants.MIN_PRECISION; precision <= constants.MAX_PRECISION; precision++) {
let maxExp = BigNumber(constants.maxExpArray[precision]);
let shlVal = BigNumber(2).exponentiatedBy(constants.MAX_PRECISION - precision);
let maxExp = new BigNumber(constants.maxExpArray[precision]);
let shlVal = new BigNumber(2).exponentiatedBy(constants.MAX_PRECISION - precision);
let tuples = [
{'input' : maxExp.plus(0).times(shlVal).minus(1), 'output' : BigNumber(precision-0)},
{'input' : maxExp.plus(0).times(shlVal).minus(0), 'output' : BigNumber(precision-0)},
{'input' : maxExp.plus(1).times(shlVal).minus(1), 'output' : BigNumber(precision-0)},
{'input' : maxExp.plus(1).times(shlVal).minus(0), 'output' : BigNumber(precision-1)},
{'input' : maxExp.plus(0).times(shlVal).minus(1), 'output' : new BigNumber(precision-0)},
{'input' : maxExp.plus(0).times(shlVal).minus(0), 'output' : new BigNumber(precision-0)},
{'input' : maxExp.plus(1).times(shlVal).minus(1), 'output' : new BigNumber(precision-0)},
{'input' : maxExp.plus(1).times(shlVal).minus(0), 'output' : new BigNumber(precision-1)},
];
for (let index = 0; index < tuples.length; index++) {
@ -121,7 +121,7 @@ contract('TestBancorFormula', function () {
}
else {
let temp = await TestBancorFormula.methods.findPositionInMaxExpArrayTest(input).call();
let retVal = BigNumber(temp);
let retVal = new BigNumber(temp);
assert(retVal.isEqualTo(output), `${test}: output should be ${output.toString(10)} but it is ${retVal.toString(10)}`);
}
});
@ -129,8 +129,8 @@ contract('TestBancorFormula', function () {
}
for (let precision = constants.MIN_PRECISION; precision <= constants.MAX_PRECISION; precision++) {
let maxExp = BigNumber(constants.maxExpArray[precision]);
let maxVal = BigNumber(constants.maxValArray[precision]);
let maxExp = new BigNumber(constants.maxExpArray[precision]);
let maxVal = new BigNumber(constants.maxValArray[precision]);
let errExp = maxExp.plus(1);
let maxExpi = web3.utils.toHex(maxExp);
let errExpi = web3.utils.toHex(errExp);
@ -139,35 +139,35 @@ contract('TestBancorFormula', function () {
it(`${test1}:`, async () => {
let temp = await TestBancorFormula.methods.generalExpTest(maxExpi, precision).call();
let retVal = BigNumber(temp);
let retVal = new BigNumber(temp);
assert(retVal.isEqualTo(maxVal), `${test1}: output is wrong`);
});
it(`${test2}:`, async () => {
let temp = await TestBancorFormula.methods.generalExpTest(errExpi, precision).call();
let retVal = BigNumber(temp);
let retVal = new BigNumber(temp);
assert(retVal.isLessThan(maxVal), `${test2}: output indicates that maxExpArray[${precision}] is wrong`);
});
}
for (let precision = constants.MIN_PRECISION; precision <= constants.MAX_PRECISION; precision++) {
let minExp = BigNumber(constants.maxExpArray[precision-1]).plus(1);
let minVal = BigNumber(2).exponentiatedBy(precision);
let minExp = new BigNumber(constants.maxExpArray[precision-1]).plus(1);
let minVal = new BigNumber(2).exponentiatedBy(precision);
let minExpi = web3.utils.toHex(minExp);
let test = `Function generalExp(0x${minExp.toString(16)}, ${precision})`;
it(`${test}:`, async () => {
let temp = await TestBancorFormula.methods.generalExpTest(minExpi, precision).call();
let retVal = BigNumber(temp);
let retVal = new BigNumber(temp);
assert(retVal.isGreaterThanOrEqualTo(minVal), `${test}: output is too small`);
});
}
for (let n = 1; n <= 255; n++) {
let tuples = [
{'input' : BigNumber(2).exponentiatedBy(n) , 'output' : BigNumber(n)},
{'input' : BigNumber(2).exponentiatedBy(n).plus(1) , 'output' : BigNumber(n)},
{'input' : BigNumber(2).exponentiatedBy(n+1).minus(1), 'output' : BigNumber(n)},
{'input' : new BigNumber(2).exponentiatedBy(n) , 'output' : new BigNumber(n)},
{'input' : new BigNumber(2).exponentiatedBy(n).plus(1) , 'output' : new BigNumber(n)},
{'input' : new BigNumber(2).exponentiatedBy(n+1).minus(1), 'output' : new BigNumber(n)},
];
for (let index = 0; index < tuples.length; index++) {
@ -177,7 +177,7 @@ contract('TestBancorFormula', function () {
it(`${test}:`, async () => {
let temp = await TestBancorFormula.methods.floorLog2Test(input).call();
let retVal = BigNumber(temp);
let retVal = new BigNumber(temp);
assert(retVal.isEqualTo(output), `${test}: output should be ${output.toString(10)} but it is ${retVal.toString(10)}`);
});
}
@ -189,19 +189,20 @@ contract('TestBancorFormula', function () {
let LOG_MIN = 1;
let EXP_MIN = 0;
let LOG_MAX = BigNumber(Decimal.exp(1).toFixed());
let EXP_MAX = BigNumber(Decimal.pow(2,4).toFixed());
let FIXED_1 = BigNumber(2).exponentiatedBy(constants.MAX_PRECISION);
let LOG_MAX = new BigNumber(Decimal.exp(1).toFixed());
let EXP_MAX = new BigNumber(Decimal.pow(2,4).toFixed());
let FIXED_1 = new BigNumber(2).exponentiatedBy(constants.MAX_PRECISION);
// for (let percent = 0; percent < 100; percent++) {
// let x = BigNumber(percent).dividedBy(100).times(LOG_MAX.minus(LOG_MIN)).plus(LOG_MIN);
// let x = new BigNumber(percent).dividedBy(100).times(LOG_MAX.minus(LOG_MIN)).plus(LOG_MIN);
// it(`Function optimalLog(${x.toFixed()})`, async () => {
// try {
// let tmp = web3.utils.toHex(FIXED_1.times(x));
// let fixedPoint = await TestBancorFormula.methods.optimalLogTest(tmp).call();
// let floatPoint = BigNumber(Math.floor(Decimal(x.toFixed()).ln().times(FIXED_1.toFixed()).toFixed()));
// let ratio = fixedPoint.equals(floatPoint) ? BigNumber(1) : fixedPoint.dividedBy(floatPoint);
// let tmp = web3.utils.toHex(FIXED_1.times(x).toFormat());
// let temp = await TestBancorFormula.methods.optimalLogTest(tmp).call();
// let fixedPoint = new BigNumber(temp);
// let floatPoint = new BigNumber(Decimal(x.toFixed()).ln().times(FIXED_1.toFixed()).toFixed());
// let ratio = fixedPoint.isEqualTo(floatPoint) ? BigNumber(1) : fixedPoint.dividedBy(floatPoint);
// assert(ratio.isGreaterThanOrEqualTo("0.99999999999999999999999999999999999") && ratio.isLessThanOrEqualTo("1"), `ratio = ${ratio.toFixed()}`);
// }
// catch (error) {
@ -211,14 +212,16 @@ contract('TestBancorFormula', function () {
// }
// for (let percent = 0; percent < 100; percent++) {
// let x = web3.toBigNumber(percent).dividedBy(100).times(EXP_MAX.minus(EXP_MIN)).plus(EXP_MIN);
// let x = new BigNumber(percent).dividedBy(100).times(EXP_MAX.minus(EXP_MIN)).plus(EXP_MIN);
// it(`Function optimalExp(${x.toFixed()})`, async () => {
// try {
// let fixedPoint = await formula.optimalExpTest(FIXED_1.times(x).truncated());
// let floatPoint = web3.toBigNumber(Decimal(x.toFixed()).exp().times(FIXED_1.toFixed()).toFixed());
// let ratio = fixedPoint.equals(floatPoint) ? web3.toBigNumber(1) : fixedPoint.dividedBy(floatPoint);
// assert(ratio.greaterThanOrEqualTo("0.99999999999999999999999999999999999") && ratio.lessThanOrEqualTo("1"), `ratio = ${ratio.toFixed()}`);
// let tmp = web3.utils.toHex(FIXED_1.times(x).toFormat());
// let temp = await TestBancorFormula.methods.optimalExpTest(tmp).call();
// let fixedPoint = new BigNumber(temp);
// let floatPoint = new BigNumber(Decimal(x.toFixed()).exp().times(FIXED_1.toFixed()).toFixed());
// let ratio = fixedPoint.isEqualTo(floatPoint) ? new BigNumber(1) : fixedPoint.dividedBy(floatPoint);
// assert(ratio.isGreaterThanOrEqualTo("0.99999999999999999999999999999999999") && ratio.isLessThanOrEqualTo("1"), `ratio = ${ratio.toFixed()}`);
// }
// catch (error) {
// assert(false, error.message);
@ -228,10 +231,10 @@ contract('TestBancorFormula', function () {
// for (let n = 0; n < 256 - constants.MAX_PRECISION; n++) {
// let values = [
// web3.toBigNumber(2).toPower(n),
// web3.toBigNumber(2).toPower(n).plus(1),
// web3.toBigNumber(2).toPower(n).times(1.5),
// web3.toBigNumber(2).toPower(n+1).minus(1),
// new BigNumber(2).exponentiatedBy(n),
// new BigNumber(2).exponentiatedBy(n).plus(1),
// new BigNumber(2).exponentiatedBy(n).times(1.5),
// new BigNumber(2).exponentiatedBy(n+1).minus(1),
// ];
// for (let index = 0; index < values.length; index++) {
@ -239,10 +242,11 @@ contract('TestBancorFormula', function () {
// it(`Function generalLog(${x.toFixed()})`, async () => {
// try {
// let fixedPoint = await formula.generalLogTest(FIXED_1.times(x).truncated());
// let floatPoint = web3.toBigNumber(Decimal(x.toFixed()).ln().times(FIXED_1.toFixed()).toFixed());
// let ratio = fixedPoint.equals(floatPoint) ? web3.toBigNumber(1) : fixedPoint.dividedBy(floatPoint);
// assert(ratio.greaterThanOrEqualTo("0.99999999999999999999999999999999999") && ratio.lessThanOrEqualTo("1"), `ratio = ${ratio.toFixed()}`);
// let tmp = web3.utils.toHex(FIXED_1.times(x).toFormat());
// let fixedPoint = await TestBancorFormula.methods.generalLogTest(tmp).call();
// let floatPoint = new BigNumber(Decimal(x.toFixed()).ln().times(FIXED_1.toFixed()).toFixed());
// let ratio = fixedPoint.isEqualTo(floatPoint) ? new BigNumber(1) : fixedPoint.dividedBy(floatPoint);
// assert(ratio.isGreaterThanOrEqualTo("0.99999999999999999999999999999999999") && ratio.isLessThanOrEqualTo("1"), `ratio = ${ratio.toFixed()}`);
// }
// catch (error) {
// assert(false, error.message);