SafeMath.test.js 4.3 KB

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