Math.test.js 1.8 KB

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