SafeMath.test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const assertRevert = require('./helpers/assertRevert');
  2. const assertJump = require('./helpers/assertJump');
  3. var SafeMathMock = artifacts.require('mocks/SafeMathMock.sol');
  4. contract('SafeMath', function (accounts) {
  5. let safeMath;
  6. before(async function () {
  7. safeMath = await SafeMathMock.new();
  8. });
  9. it('multiplies correctly', async function () {
  10. let a = 5678;
  11. let b = 1234;
  12. await safeMath.multiply(a, b);
  13. let result = await safeMath.result();
  14. assert.equal(result, a * b);
  15. });
  16. it('adds correctly', async function () {
  17. let a = 5678;
  18. let b = 1234;
  19. await safeMath.add(a, b);
  20. let result = await safeMath.result();
  21. assert.equal(result, a + b);
  22. });
  23. it('subtracts correctly', async function () {
  24. let a = 5678;
  25. let b = 1234;
  26. await safeMath.subtract(a, b);
  27. let result = await safeMath.result();
  28. assert.equal(result, a - b);
  29. });
  30. it('should throw an error if subtraction result would be negative', async function () {
  31. let a = 1234;
  32. let b = 5678;
  33. try {
  34. await safeMath.subtract(a, b);
  35. assert.fail('should have thrown before');
  36. } catch (error) {
  37. assertJump(error);
  38. }
  39. });
  40. it('should throw an error on addition overflow', async function () {
  41. let a = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
  42. let b = 1;
  43. try {
  44. await safeMath.add(a, b);
  45. assert.fail('should have thrown before');
  46. } catch (error) {
  47. assertRevert(error);
  48. }
  49. });
  50. it('should throw an error on multiplication overflow', async function () {
  51. let a = 115792089237316195423570985008687907853269984665640564039457584007913129639933;
  52. let b = 2;
  53. try {
  54. await safeMath.multiply(a, b);
  55. assert.fail('should have thrown before');
  56. } catch (error) {
  57. assertRevert(error);
  58. }
  59. });
  60. });