You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.1 KiB
78 lines
2.1 KiB
3 years ago
|
const { BN, constants } = require('@openzeppelin/test-helpers');
|
||
|
const { expect } = require('chai');
|
||
|
const { MIN_INT256, MAX_INT256 } = constants;
|
||
|
|
||
|
const SignedMathMock = artifacts.require('SignedMathMock');
|
||
|
|
||
|
contract('SignedMath', function (accounts) {
|
||
|
const min = new BN('-1234');
|
||
|
const max = new BN('5678');
|
||
|
|
||
|
beforeEach(async function () {
|
||
|
this.math = await SignedMathMock.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})`);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
});
|