Math.test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const { contract } = require('@openzeppelin/test-environment');
  2. const { BN } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const MathMock = contract.fromArtifact('MathMock');
  5. describe('Math', function () {
  6. const min = new BN('1234');
  7. const max = new BN('5678');
  8. beforeEach(async function () {
  9. this.math = await MathMock.new();
  10. });
  11. describe('max', function () {
  12. it('is correctly detected in first argument position', async function () {
  13. expect(await this.math.max(max, min)).to.be.bignumber.equal(max);
  14. });
  15. it('is correctly detected in second argument position', async function () {
  16. expect(await this.math.max(min, max)).to.be.bignumber.equal(max);
  17. });
  18. });
  19. describe('min', function () {
  20. it('is correctly detected in first argument position', async function () {
  21. expect(await this.math.min(min, max)).to.be.bignumber.equal(min);
  22. });
  23. it('is correctly detected in second argument position', async function () {
  24. expect(await this.math.min(max, min)).to.be.bignumber.equal(min);
  25. });
  26. });
  27. describe('average', function () {
  28. function bnAverage (a, b) {
  29. return a.add(b).divn(2);
  30. }
  31. it('is correctly calculated with two odd numbers', async function () {
  32. const a = new BN('57417');
  33. const b = new BN('95431');
  34. expect(await this.math.average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  35. });
  36. it('is correctly calculated with two even numbers', async function () {
  37. const a = new BN('42304');
  38. const b = new BN('84346');
  39. expect(await this.math.average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  40. });
  41. it('is correctly calculated with one even and one odd number', async function () {
  42. const a = new BN('57417');
  43. const b = new BN('84346');
  44. expect(await this.math.average(a, b)).to.be.bignumber.equal(bnAverage(a, b));
  45. });
  46. });
  47. });