SafeMath.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const { contract } = require('@openzeppelin/test-environment');
  2. const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { MAX_UINT256 } = constants;
  4. const { expect } = require('chai');
  5. const SafeMathMock = contract.fromArtifact('SafeMathMock');
  6. describe('SafeMath', function () {
  7. beforeEach(async function () {
  8. this.safeMath = await SafeMathMock.new();
  9. });
  10. async function testCommutative (fn, lhs, rhs, expected) {
  11. expect(await fn(lhs, rhs)).to.be.bignumber.equal(expected);
  12. expect(await fn(rhs, lhs)).to.be.bignumber.equal(expected);
  13. }
  14. async function testFailsCommutative (fn, lhs, rhs, reason) {
  15. await expectRevert(fn(lhs, rhs), reason);
  16. await expectRevert(fn(rhs, lhs), reason);
  17. }
  18. describe('add', function () {
  19. it('adds correctly', async function () {
  20. const a = new BN('5678');
  21. const b = new BN('1234');
  22. await testCommutative(this.safeMath.add, a, b, a.add(b));
  23. });
  24. it('reverts on addition overflow', async function () {
  25. const a = MAX_UINT256;
  26. const b = new BN('1');
  27. await testFailsCommutative(this.safeMath.add, a, b, 'SafeMath: addition overflow');
  28. });
  29. });
  30. describe('sub', function () {
  31. it('subtracts correctly', async function () {
  32. const a = new BN('5678');
  33. const b = new BN('1234');
  34. expect(await this.safeMath.sub(a, b)).to.be.bignumber.equal(a.sub(b));
  35. });
  36. it('reverts if subtraction result would be negative', async function () {
  37. const a = new BN('1234');
  38. const b = new BN('5678');
  39. await expectRevert(this.safeMath.sub(a, b), 'SafeMath: subtraction overflow');
  40. });
  41. });
  42. describe('mul', function () {
  43. it('multiplies correctly', async function () {
  44. const a = new BN('1234');
  45. const b = new BN('5678');
  46. await testCommutative(this.safeMath.mul, a, b, a.mul(b));
  47. });
  48. it('multiplies by zero correctly', async function () {
  49. const a = new BN('0');
  50. const b = new BN('5678');
  51. await testCommutative(this.safeMath.mul, a, b, '0');
  52. });
  53. it('reverts on multiplication overflow', async function () {
  54. const a = MAX_UINT256;
  55. const b = new BN('2');
  56. await testFailsCommutative(this.safeMath.mul, a, b, 'SafeMath: multiplication overflow');
  57. });
  58. });
  59. describe('div', function () {
  60. it('divides correctly', async function () {
  61. const a = new BN('5678');
  62. const b = new BN('5678');
  63. expect(await this.safeMath.div(a, b)).to.be.bignumber.equal(a.div(b));
  64. });
  65. it('divides zero correctly', async function () {
  66. const a = new BN('0');
  67. const b = new BN('5678');
  68. expect(await this.safeMath.div(a, b)).to.be.bignumber.equal('0');
  69. });
  70. it('returns complete number result on non-even division', async function () {
  71. const a = new BN('7000');
  72. const b = new BN('5678');
  73. expect(await this.safeMath.div(a, b)).to.be.bignumber.equal('1');
  74. });
  75. it('reverts on division by zero', async function () {
  76. const a = new BN('5678');
  77. const b = new BN('0');
  78. await expectRevert(this.safeMath.div(a, b), 'SafeMath: division by zero');
  79. });
  80. });
  81. describe('mod', function () {
  82. describe('modulos correctly', async function () {
  83. it('when the dividend is smaller than the divisor', async function () {
  84. const a = new BN('284');
  85. const b = new BN('5678');
  86. expect(await this.safeMath.mod(a, b)).to.be.bignumber.equal(a.mod(b));
  87. });
  88. it('when the dividend is equal to the divisor', async function () {
  89. const a = new BN('5678');
  90. const b = new BN('5678');
  91. expect(await this.safeMath.mod(a, b)).to.be.bignumber.equal(a.mod(b));
  92. });
  93. it('when the dividend is larger than the divisor', async function () {
  94. const a = new BN('7000');
  95. const b = new BN('5678');
  96. expect(await this.safeMath.mod(a, b)).to.be.bignumber.equal(a.mod(b));
  97. });
  98. it('when the dividend is a multiple of the divisor', async function () {
  99. const a = new BN('17034'); // 17034 == 5678 * 3
  100. const b = new BN('5678');
  101. expect(await this.safeMath.mod(a, b)).to.be.bignumber.equal(a.mod(b));
  102. });
  103. });
  104. it('reverts with a 0 divisor', async function () {
  105. const a = new BN('5678');
  106. const b = new BN('0');
  107. await expectRevert(this.safeMath.mod(a, b), 'SafeMath: modulo by zero');
  108. });
  109. });
  110. });