SafeMath.test.js 4.3 KB

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