SignedSafeMath.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const { BN, constants, shouldFail } = require('openzeppelin-test-helpers');
  2. const { MAX_INT256, MIN_INT256 } = constants;
  3. const SignedSafeMathMock = artifacts.require('SignedSafeMathMock');
  4. contract('SignedSafeMath', function () {
  5. beforeEach(async function () {
  6. this.safeMath = await SignedSafeMathMock.new();
  7. });
  8. describe('add', function () {
  9. it('adds correctly if it does not overflow and the result is positve', async function () {
  10. const a = new BN('1234');
  11. const b = new BN('5678');
  12. (await this.safeMath.add(a, b)).should.be.bignumber.equal(a.add(b));
  13. });
  14. it('adds correctly if it does not overflow and the result is negative', async function () {
  15. const a = MAX_INT256;
  16. const b = MIN_INT256;
  17. const result = await this.safeMath.add(a, b);
  18. result.should.be.bignumber.equal(a.add(b));
  19. });
  20. it('reverts on positive addition overflow', async function () {
  21. const a = MAX_INT256;
  22. const b = new BN('1');
  23. await shouldFail.reverting(this.safeMath.add(a, b));
  24. });
  25. it('reverts on negative addition overflow', async function () {
  26. const a = MIN_INT256;
  27. const b = new BN('-1');
  28. await shouldFail.reverting(this.safeMath.add(a, b));
  29. });
  30. });
  31. describe('sub', function () {
  32. it('subtracts correctly if it does not overflow and the result is positive', async function () {
  33. const a = new BN('5678');
  34. const b = new BN('1234');
  35. const result = await this.safeMath.sub(a, b);
  36. result.should.be.bignumber.equal(a.sub(b));
  37. });
  38. it('subtracts correctly if it does not overflow and the result is negative', async function () {
  39. const a = new BN('1234');
  40. const b = new BN('5678');
  41. const result = await this.safeMath.sub(a, b);
  42. result.should.be.bignumber.equal(a.sub(b));
  43. });
  44. it('reverts on positive subtraction overflow', async function () {
  45. const a = MAX_INT256;
  46. const b = new BN('-1');
  47. await shouldFail.reverting(this.safeMath.sub(a, b));
  48. });
  49. it('reverts on negative subtraction overflow', async function () {
  50. const a = MIN_INT256;
  51. const b = new BN('1');
  52. await shouldFail.reverting(this.safeMath.sub(a, b));
  53. });
  54. });
  55. describe('mul', function () {
  56. it('multiplies correctly', async function () {
  57. const a = new BN('5678');
  58. const b = new BN('-1234');
  59. const result = await this.safeMath.mul(a, b);
  60. result.should.be.bignumber.equal(a.mul(b));
  61. });
  62. it('handles a zero product correctly', async function () {
  63. const a = new BN('0');
  64. const b = new BN('5678');
  65. const result = await this.safeMath.mul(a, b);
  66. result.should.be.bignumber.equal(a.mul(b));
  67. });
  68. it('reverts on multiplication overflow, positive operands', async function () {
  69. const a = MAX_INT256;
  70. const b = new BN('2');
  71. await shouldFail.reverting(this.safeMath.mul(a, b));
  72. });
  73. it('reverts when minimum integer is multiplied by -1', async function () {
  74. const a = MIN_INT256;
  75. const b = new BN('-1');
  76. await shouldFail.reverting(this.safeMath.mul(a, b));
  77. });
  78. it('reverts when -1 is multiplied by minimum integer', async function () {
  79. const a = new BN('-1');
  80. const b = MIN_INT256;
  81. await shouldFail.reverting(this.safeMath.mul(a, b));
  82. });
  83. });
  84. describe('div', function () {
  85. it('divides correctly', async function () {
  86. const a = new BN('-5678');
  87. const b = new BN('5678');
  88. const result = await this.safeMath.div(a, b);
  89. result.should.be.bignumber.equal(a.div(b));
  90. });
  91. it('reverts on zero division', async function () {
  92. const a = new BN('-5678');
  93. const b = new BN('0');
  94. await shouldFail.reverting(this.safeMath.div(a, b));
  95. });
  96. it('reverts on overflow, negative second', async function () {
  97. const a = new BN(MIN_INT256);
  98. const b = new BN('-1');
  99. await shouldFail.reverting(this.safeMath.div(a, b));
  100. });
  101. });
  102. });