SafeMath.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const shouldFail = require('../helpers/shouldFail');
  2. const { MAX_UINT256 } = require('../helpers/constants');
  3. const SafeMathMock = artifacts.require('SafeMathMock');
  4. const { BigNumber } = require('../helpers/setup');
  5. contract('SafeMath', function () {
  6. beforeEach(async function () {
  7. this.safeMath = await SafeMathMock.new();
  8. });
  9. describe('add', function () {
  10. it('adds correctly', async function () {
  11. const a = new BigNumber(5678);
  12. const b = new BigNumber(1234);
  13. (await this.safeMath.add(a, b)).should.be.bignumber.equal(a.plus(b));
  14. });
  15. it('throws a revert error on addition overflow', async function () {
  16. const a = MAX_UINT256;
  17. const b = new BigNumber(1);
  18. await shouldFail.reverting(this.safeMath.add(a, b));
  19. });
  20. });
  21. describe('sub', function () {
  22. it('subtracts correctly', async function () {
  23. const a = new BigNumber(5678);
  24. const b = new BigNumber(1234);
  25. (await this.safeMath.sub(a, b)).should.be.bignumber.equal(a.minus(b));
  26. });
  27. it('throws a revert error if subtraction result would be negative', async function () {
  28. const a = new BigNumber(1234);
  29. const b = new BigNumber(5678);
  30. await shouldFail.reverting(this.safeMath.sub(a, b));
  31. });
  32. });
  33. describe('mul', function () {
  34. it('multiplies correctly', async function () {
  35. const a = new BigNumber(1234);
  36. const b = new BigNumber(5678);
  37. (await this.safeMath.mul(a, b)).should.be.bignumber.equal(a.times(b));
  38. });
  39. it('handles a zero product correctly (first number as zero)', async function () {
  40. const a = new BigNumber(0);
  41. const b = new BigNumber(5678);
  42. (await this.safeMath.mul(a, b)).should.be.bignumber.equal(a.times(b));
  43. });
  44. it('handles a zero product correctly (second number as zero)', async function () {
  45. const a = new BigNumber(5678);
  46. const b = new BigNumber(0);
  47. (await this.safeMath.mul(a, b)).should.be.bignumber.equal(a.times(b));
  48. });
  49. it('throws a revert error on multiplication overflow', async function () {
  50. const a = MAX_UINT256;
  51. const b = new BigNumber(2);
  52. await shouldFail.reverting(this.safeMath.mul(a, b));
  53. });
  54. });
  55. describe('div', function () {
  56. it('divides correctly', async function () {
  57. const a = new BigNumber(5678);
  58. const b = new BigNumber(5678);
  59. (await this.safeMath.div(a, b)).should.be.bignumber.equal(a.div(b));
  60. });
  61. it('divides zero correctly', async function () {
  62. const a = new BigNumber(0);
  63. const b = new BigNumber(5678);
  64. (await this.safeMath.div(a, b)).should.be.bignumber.equal(0);
  65. });
  66. it('returns complete number result on non-even division', async function () {
  67. const a = new BigNumber(7000);
  68. const b = new BigNumber(5678);
  69. (await this.safeMath.div(a, b)).should.be.bignumber.equal(1);
  70. });
  71. it('throws a revert error on zero division', async function () {
  72. const a = new BigNumber(5678);
  73. const b = new BigNumber(0);
  74. await shouldFail.reverting(this.safeMath.div(a, b));
  75. });
  76. });
  77. describe('mod', function () {
  78. describe('modulos correctly', async function () {
  79. it('when the dividend is smaller than the divisor', async function () {
  80. const a = new BigNumber(284);
  81. const b = new BigNumber(5678);
  82. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  83. });
  84. it('when the dividend is equal to the divisor', async function () {
  85. const a = new BigNumber(5678);
  86. const b = new BigNumber(5678);
  87. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  88. });
  89. it('when the dividend is larger than the divisor', async function () {
  90. const a = new BigNumber(7000);
  91. const b = new BigNumber(5678);
  92. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  93. });
  94. it('when the dividend is a multiple of the divisor', async function () {
  95. const a = new BigNumber(17034); // 17034 == 5678 * 3
  96. const b = new BigNumber(5678);
  97. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  98. });
  99. });
  100. it('reverts with a 0 divisor', async function () {
  101. const a = new BigNumber(5678);
  102. const b = new BigNumber(0);
  103. await shouldFail.reverting(this.safeMath.mod(a, b));
  104. });
  105. });
  106. });