SignedSafeMath.test.js 4.1 KB

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