SafeMath.test.js 4.4 KB

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