Math.test.js 1.8 KB

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