Math.test.js 1.8 KB

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