SignedSafeMath.test.js 4.4 KB

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