SafeMath.test.js 4.0 KB

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