SignedSafeMath.test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. 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 if it does not overflow and the result is positive', async function () {
  18. const a = new BN('1234');
  19. const b = new BN('5678');
  20. await testCommutative(this.safeMath.add, a, b, a.add(b));
  21. });
  22. it('adds correctly if it does not overflow and the result is negative', async function () {
  23. const a = MAX_INT256;
  24. const b = MIN_INT256;
  25. await testCommutative(this.safeMath.add, a, b, a.add(b));
  26. });
  27. it('reverts on positive addition overflow', async function () {
  28. const a = MAX_INT256;
  29. const b = new BN('1');
  30. await testFailsCommutative(this.safeMath.add, a, b, 'SignedSafeMath: addition overflow');
  31. });
  32. it('reverts on negative addition overflow', async function () {
  33. const a = MIN_INT256;
  34. const b = new BN('-1');
  35. await testFailsCommutative(this.safeMath.add, a, b, 'SignedSafeMath: addition overflow');
  36. });
  37. });
  38. describe('sub', function () {
  39. it('subtracts correctly if it does not overflow and the result is positive', async function () {
  40. const a = new BN('5678');
  41. const b = new BN('1234');
  42. const result = await this.safeMath.sub(a, b);
  43. result.should.be.bignumber.equal(a.sub(b));
  44. });
  45. it('subtracts correctly if it does not overflow and the result is negative', async function () {
  46. const a = new BN('1234');
  47. const b = new BN('5678');
  48. const result = await this.safeMath.sub(a, b);
  49. result.should.be.bignumber.equal(a.sub(b));
  50. });
  51. it('reverts on positive subtraction overflow', async function () {
  52. const a = MAX_INT256;
  53. const b = new BN('-1');
  54. await shouldFail.reverting.withMessage(this.safeMath.sub(a, b), 'SignedSafeMath: subtraction overflow');
  55. });
  56. it('reverts on negative subtraction overflow', async function () {
  57. const a = MIN_INT256;
  58. const b = new BN('1');
  59. await shouldFail.reverting.withMessage(this.safeMath.sub(a, b), 'SignedSafeMath: subtraction overflow');
  60. });
  61. });
  62. describe('mul', function () {
  63. it('multiplies correctly', async function () {
  64. const a = new BN('5678');
  65. const b = new BN('-1234');
  66. await testCommutative(this.safeMath.mul, a, b, a.mul(b));
  67. });
  68. it('multiplies by zero correctly', async function () {
  69. const a = new BN('0');
  70. const b = new BN('5678');
  71. await testCommutative(this.safeMath.mul, a, b, '0');
  72. });
  73. it('reverts on multiplication overflow, positive operands', async function () {
  74. const a = MAX_INT256;
  75. const b = new BN('2');
  76. await testFailsCommutative(this.safeMath.mul, a, b, 'SignedSafeMath: multiplication overflow');
  77. });
  78. it('reverts when minimum integer is multiplied by -1', async function () {
  79. const a = MIN_INT256;
  80. const b = new BN('-1');
  81. await testFailsCommutative(this.safeMath.mul, a, b, 'SignedSafeMath: multiplication overflow');
  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('divides zero correctly', async function () {
  92. const a = new BN('0');
  93. const b = new BN('5678');
  94. (await this.safeMath.div(a, b)).should.be.bignumber.equal('0');
  95. });
  96. it('returns complete number result on non-even division', async function () {
  97. const a = new BN('7000');
  98. const b = new BN('5678');
  99. (await this.safeMath.div(a, b)).should.be.bignumber.equal('1');
  100. });
  101. it('reverts on division by zero', async function () {
  102. const a = new BN('-5678');
  103. const b = new BN('0');
  104. await shouldFail.reverting.withMessage(this.safeMath.div(a, b), 'SignedSafeMath: division by zero');
  105. });
  106. it('reverts on overflow, negative second', async function () {
  107. const a = new BN(MIN_INT256);
  108. const b = new BN('-1');
  109. await shouldFail.reverting.withMessage(this.safeMath.div(a, b), 'SignedSafeMath: division overflow');
  110. });
  111. });
  112. });