diff --git a/contracts/test-helpers/SafeMathMock.sol b/contracts/test-helpers/SafeMathMock.sol new file mode 100644 index 000000000..9cd618214 --- /dev/null +++ b/contracts/test-helpers/SafeMathMock.sol @@ -0,0 +1,18 @@ +pragma solidity ^0.4.4; +import '../SafeMath.sol'; + +contract SafeMathMock is SafeMath { + uint public result; + + function multiply(uint a, uint b) { + result = safeMul(a, b); + } + + function subtract(uint a, uint b) { + result = safeSub(a, b); + } + + function add(uint a, uint b) { + result = safeAdd(a, b); + } +} diff --git a/test/SafeMath.js b/test/SafeMath.js new file mode 100644 index 000000000..251c0d139 --- /dev/null +++ b/test/SafeMath.js @@ -0,0 +1,82 @@ + +contract('SafeMath', function(accounts) { + + var safeMath; + + before(function() { + return SafeMathMock.new() + .then(function(_safeMath) { + safeMath = _safeMath; + }); + }); + + it("multiplies correctly", function(done) { + var a = 5678; + var b = 1234; + return safeMath.multiply(a, b) + .then(function() { + return safeMath.result(); + }) + .then(function(result) { + assert.equal(result, a*b); + }) + .then(done); + }); + + it("adds correctly", function(done) { + var a = 5678; + var b = 1234; + return safeMath.add(a, b) + .then(function() { + return safeMath.result(); + }) + .then(function(result) { + assert.equal(result, a+b); + }) + .then(done); + }); + + it("subtracts correctly", function(done) { + var a = 5678; + var b = 1234; + return safeMath.subtract(a, b) + .then(function() { + return safeMath.result(); + }) + .then(function(result) { + assert.equal(result, a-b); + }) + .then(done); + }); + + it("should throw an error if subtraction result would be negative", function (done) { + var a = 1234; + var b = 5678; + return safeMath.subtract(a, b) + .catch(function(error) { + if (error.message.search('invalid JUMP') == -1) throw error + }) + .then(done); + }); + + it("should throw an error on addition overflow", function(done) { + var a = 115792089237316195423570985008687907853269984665640564039457584007913129639935; + var b = 1; + return safeMath.add(a, b) + .catch(function(error) { + if (error.message.search('invalid JUMP') == -1) throw error + }) + .then(done); + }); + + it("should throw an error on multiplication overflow", function(done) { + var a = 115792089237316195423570985008687907853269984665640564039457584007913129639933; + var b = 2; + return safeMath.multiply(a, b) + .catch(function(error) { + if (error.message.search('invalid JUMP') == -1) throw error + }) + .then(done); + }); + +});