SignedSafeMath.test.js 4.8 KB

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