SafeMath.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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', () => {
  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. const result = await this.safeMath.add(a, b);
  17. result.should.be.bignumber.equal(a.plus(b));
  18. });
  19. it('throws a revert error on addition overflow', async function () {
  20. const a = MAX_UINT;
  21. const b = new BigNumber(1);
  22. await assertRevert(this.safeMath.add(a, b));
  23. });
  24. });
  25. describe('sub', function () {
  26. it('subtracts correctly', async function () {
  27. const a = new BigNumber(5678);
  28. const b = new BigNumber(1234);
  29. const result = await this.safeMath.sub(a, b);
  30. result.should.be.bignumber.equal(a.minus(b));
  31. });
  32. it('throws a revert error if subtraction result would be negative', async function () {
  33. const a = new BigNumber(1234);
  34. const b = new BigNumber(5678);
  35. await assertRevert(this.safeMath.sub(a, b));
  36. });
  37. });
  38. describe('mul', function () {
  39. it('multiplies correctly', async function () {
  40. const a = new BigNumber(1234);
  41. const b = new BigNumber(5678);
  42. const result = await this.safeMath.mul(a, b);
  43. result.should.be.bignumber.equal(a.times(b));
  44. });
  45. it('handles a zero product correctly', async function () {
  46. const a = new BigNumber(0);
  47. const b = new BigNumber(5678);
  48. const result = await this.safeMath.mul(a, b);
  49. result.should.be.bignumber.equal(a.times(b));
  50. });
  51. it('throws a revert error on multiplication overflow', async function () {
  52. const a = MAX_UINT;
  53. const b = new BigNumber(2);
  54. await assertRevert(this.safeMath.mul(a, b));
  55. });
  56. });
  57. describe('div', function () {
  58. it('divides correctly', async function () {
  59. const a = new BigNumber(5678);
  60. const b = new BigNumber(5678);
  61. const result = await this.safeMath.div(a, b);
  62. result.should.be.bignumber.equal(a.div(b));
  63. });
  64. it('throws a revert error on zero division', async function () {
  65. const a = new BigNumber(5678);
  66. const b = new BigNumber(0);
  67. await assertRevert(this.safeMath.div(a, b));
  68. });
  69. });
  70. describe('mod', function () {
  71. describe('modulos correctly', async function () {
  72. it('when the dividend is smaller than the divisor', async function () {
  73. const a = new BigNumber(284);
  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 equal to the divisor', async function () {
  78. const a = new BigNumber(5678);
  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 larger than the divisor', async function () {
  83. const a = new BigNumber(7000);
  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 a multiple of the divisor', async function () {
  88. const a = new BigNumber(17034); // 17034 == 5678 * 3
  89. const b = new BigNumber(5678);
  90. (await this.safeMath.mod(a, b)).should.be.bignumber.equal(a.mod(b));
  91. });
  92. });
  93. it('reverts with a 0 divisor', async function () {
  94. const a = new BigNumber(5678);
  95. const b = new BigNumber(0);
  96. await assertRevert(this.safeMath.mod(a, b));
  97. });
  98. });
  99. });