SafeMath.js 1.8 KB

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