SignedMath.test.js 2.44 KB
Newer Older
vicotor's avatar
vicotor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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());
      });
    }
  });
});