Math.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const result = await this.math.max(max, min);
  15. result.should.be.bignumber.equal(max);
  16. });
  17. it('is correctly detected in second argument position', async function () {
  18. const result = await this.math.max(min, max);
  19. result.should.be.bignumber.equal(max);
  20. });
  21. });
  22. describe('min', function () {
  23. it('is correctly detected in first argument position', async function () {
  24. const result = await this.math.min(min, max);
  25. result.should.be.bignumber.equal(min);
  26. });
  27. it('is correctly detected in second argument position', async function () {
  28. const result = await this.math.min(max, min);
  29. result.should.be.bignumber.equal(min);
  30. });
  31. });
  32. describe('average', function () {
  33. function bnAverage (a, b) {
  34. return a.plus(b).div(2).truncated();
  35. }
  36. it('is correctly calculated with two odd numbers', async function () {
  37. const a = new BigNumber(57417);
  38. const b = new BigNumber(95431);
  39. (await this.math.average(a, b)).should.be.bignumber.equal(bnAverage(a, b));
  40. });
  41. it('is correctly calculated with two even numbers', async function () {
  42. const a = new BigNumber(42304);
  43. const b = new BigNumber(84346);
  44. (await this.math.average(a, b)).should.be.bignumber.equal(bnAverage(a, b));
  45. });
  46. it('is correctly calculated with one even and one odd number', async function () {
  47. const a = new BigNumber(57417);
  48. const b = new BigNumber(84346);
  49. (await this.math.average(a, b)).should.be.bignumber.equal(bnAverage(a, b));
  50. });
  51. });
  52. });