Merge pull request #94 from adklempner/safemath

Create tests for SafeMath
This commit is contained in:
Manuel Aráoz
2016-11-23 20:20:08 -08:00
committed by GitHub
2 changed files with 100 additions and 0 deletions

View File

@ -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);
}
}

82
test/SafeMath.js Normal file
View File

@ -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);
});
});