SafeMath.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const { assertRevert } = require('../helpers/assertRevert');
  2. const BigNumber = web3.BigNumber;
  3. const SafeMathMock = artifacts.require('SafeMathMock');
  4. require('chai')
  5. .use(require('chai-bignumber')(BigNumber))
  6. .should();
  7. contract('SafeMath', function () {
  8. const MAX_UINT = new BigNumber(2).pow(256).minus(1);
  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_UINT;
  20. const b = new BigNumber(1);
  21. await assertRevert(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 assertRevert(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', 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('throws a revert error on multiplication overflow', async function () {
  48. const a = MAX_UINT;
  49. const b = new BigNumber(2);
  50. await assertRevert(this.safeMath.mul(a, b));
  51. });
  52. });
  53. describe('div', function () {
  54. it('divides correctly', async function () {
  55. const a = new BigNumber(5678);
  56. const b = new BigNumber(5678);
  57. (await this.safeMath.div(a, b)).should.be.bignumber.equal(a.div(b));
  58. });
  59. it('throws a revert error on zero division', async function () {
  60. const a = new BigNumber(5678);
  61. const b = new BigNumber(0);
  62. await assertRevert(this.safeMath.div(a, b));
  63. });
  64. });
  65. describe('mod', function () {
  66. describe('modulos correctly', async function () {
  67. it('when the dividend is smaller than the divisor', async function () {
  68. const a = new BigNumber(284);
  69. const b = new BigNumber(5678);
  70. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  71. });
  72. it('when the dividend is equal to the divisor', async function () {
  73. const a = new BigNumber(5678);
  74. const b = new BigNumber(5678);
  75. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  76. });
  77. it('when the dividend is larger than the divisor', async function () {
  78. const a = new BigNumber(7000);
  79. const b = new BigNumber(5678);
  80. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  81. });
  82. it('when the dividend is a multiple of the divisor', async function () {
  83. const a = new BigNumber(17034); // 17034 == 5678 * 3
  84. const b = new BigNumber(5678);
  85. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  86. });
  87. });
  88. it('reverts with a 0 divisor', async function () {
  89. const a = new BigNumber(5678);
  90. const b = new BigNumber(0);
  91. await assertRevert(this.safeMath.mod(a, b));
  92. });
  93. });
  94. });