Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
bd5f7f57
Unverified
Commit
bd5f7f57
authored
Apr 03, 2018
by
Facundo Spagnuolo
Committed by
GitHub
Apr 03, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix SafeMath test file (#865)
parent
a6bef447
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
89 additions
and
49 deletions
+89
-49
SafeMathMock.sol
contracts/mocks/SafeMathMock.sol
+10
-7
assertJump.js
test/helpers/assertJump.js
+8
-2
SafeMath.test.js
test/math/SafeMath.test.js
+71
-40
No files found.
contracts/mocks/SafeMathMock.sol
View file @
bd5f7f57
...
...
@@ -5,17 +5,20 @@ import "../math/SafeMath.sol";
contract SafeMathMock {
uint256 public result;
function mul
tiply(uint256 a, uint256 b) public
{
re
sult =
SafeMath.mul(a, b);
function mul
(uint256 a, uint256 b) public pure returns (uint256)
{
re
turn
SafeMath.mul(a, b);
}
function
subtract(uint256 a, uint256 b) public
{
re
sult = SafeMath.sub
(a, b);
function
div(uint256 a, uint256 b) public pure returns (uint256)
{
re
turn SafeMath.div
(a, b);
}
function add(uint256 a, uint256 b) public {
result = SafeMath.add(a, b);
function sub(uint256 a, uint256 b) public pure returns (uint256) {
return SafeMath.sub(a, b);
}
function add(uint256 a, uint256 b) public pure returns (uint256) {
return SafeMath.add(a, b);
}
}
test/helpers/assertJump.js
View file @
bd5f7f57
module
.
exports
=
function
(
error
)
{
assert
.
isAbove
(
error
.
message
.
search
(
'invalid opcode'
),
-
1
,
'Invalid opcode error must be returned'
);
export
default
async
promise
=>
{
try
{
await
promise
;
assert
.
fail
(
'Expected invalid opcode not received'
);
}
catch
(
error
)
{
const
invalidOpcodeReceived
=
error
.
message
.
search
(
'invalid opcode'
)
>=
0
;
assert
(
invalidOpcodeReceived
,
`Expected "invalid opcode", got
${
error
}
instead`
);
}
};
test/math/SafeMath.test.js
View file @
bd5f7f57
import
assert
Revert
from
'../helpers/assertRevert
'
;
const
assertJump
=
require
(
'../helpers/assertJump'
)
;
var
SafeMathMock
=
artifacts
.
require
(
'SafeMathMock'
);
import
assert
Jump
from
'../helpers/assertJump
'
;
const
BigNumber
=
web3
.
BigNumber
;
const
SafeMathMock
=
artifacts
.
require
(
'SafeMathMock'
);
contract
(
'SafeMath'
,
function
(
accounts
)
{
let
safeMath
;
require
(
'chai'
)
.
use
(
require
(
'chai-bignumber'
)(
BigNumber
))
.
should
();
contract
(
'SafeMath'
,
()
=>
{
const
MAX_UINT
=
new
BigNumber
(
'115792089237316195423570985008687907853269984665640564039457584007913129639935'
);
before
(
async
function
()
{
safeMath
=
await
SafeMathMock
.
new
();
this
.
safeMath
=
await
SafeMathMock
.
new
();
});
it
(
'multiplies correctly'
,
async
function
()
{
let
a
=
5678
;
let
b
=
1234
;
await
safeMath
.
multiply
(
a
,
b
);
let
result
=
await
safeMath
.
result
();
assert
.
equal
(
result
,
a
*
b
);
describe
(
'add'
,
function
()
{
it
(
'adds correctly'
,
async
function
()
{
const
a
=
new
BigNumber
(
5678
);
const
b
=
new
BigNumber
(
1234
);
const
result
=
await
this
.
safeMath
.
add
(
a
,
b
);
result
.
should
.
be
.
bignumber
.
equal
(
a
.
plus
(
b
));
});
it
(
'adds correctly'
,
async
function
()
{
let
a
=
5678
;
let
b
=
1234
;
await
safeMath
.
add
(
a
,
b
);
let
result
=
await
safeMath
.
result
();
it
(
'throws an error on addition overflow'
,
async
function
()
{
const
a
=
MAX_UINT
;
const
b
=
new
BigNumber
(
1
);
assert
.
equal
(
result
,
a
+
b
);
await
assertJump
(
this
.
safeMath
.
add
(
a
,
b
));
});
});
describe
(
'sub'
,
function
()
{
it
(
'subtracts correctly'
,
async
function
()
{
let
a
=
5678
;
let
b
=
1234
;
await
safeMath
.
subtract
(
a
,
b
);
let
result
=
await
safeMath
.
result
();
const
a
=
new
BigNumber
(
5678
);
const
b
=
new
BigNumber
(
1234
);
const
result
=
await
this
.
safeMath
.
sub
(
a
,
b
);
result
.
should
.
be
.
bignumber
.
equal
(
a
.
minus
(
b
));
});
it
(
'throws an error if subtraction result would be negative'
,
async
function
()
{
const
a
=
new
BigNumber
(
1234
);
const
b
=
new
BigNumber
(
5678
);
await
assertJump
(
this
.
safeMath
.
sub
(
a
,
b
));
});
});
describe
(
'mul'
,
function
()
{
it
(
'multiplies correctly'
,
async
function
()
{
const
a
=
new
BigNumber
(
1234
);
const
b
=
new
BigNumber
(
5678
);
assert
.
equal
(
result
,
a
-
b
);
const
result
=
await
this
.
safeMath
.
mul
(
a
,
b
);
result
.
should
.
be
.
bignumber
.
equal
(
a
.
times
(
b
));
});
it
(
'should throw an error if subtraction result would be negative'
,
async
function
()
{
let
a
=
1234
;
let
b
=
5678
;
try
{
await
safeMath
.
subtract
(
a
,
b
);
assert
.
fail
(
'should have thrown before'
);
}
catch
(
error
)
{
assertJump
(
error
);
}
it
(
'handles a zero product correctly'
,
async
function
()
{
const
a
=
new
BigNumber
(
0
);
const
b
=
new
BigNumber
(
5678
);
const
result
=
await
this
.
safeMath
.
mul
(
a
,
b
);
result
.
should
.
be
.
bignumber
.
equal
(
a
.
times
(
b
));
});
it
(
'should throw an error on addition overflow'
,
async
function
()
{
let
a
=
115792089237316195423570985008687907853269984665640564039457584007913129639935
;
let
b
=
1
;
await
assertRevert
(
safeMath
.
add
(
a
,
b
));
it
(
'throws an error on multiplication overflow'
,
async
function
()
{
const
a
=
MAX_UINT
;
const
b
=
new
BigNumber
(
2
);
await
assertJump
(
this
.
safeMath
.
mul
(
a
,
b
));
});
});
it
(
'should throw an error on multiplication overflow'
,
async
function
()
{
let
a
=
115792089237316195423570985008687907853269984665640564039457584007913129639933
;
let
b
=
2
;
await
assertRevert
(
safeMath
.
multiply
(
a
,
b
));
describe
(
'div'
,
function
()
{
it
(
'divides correctly'
,
async
function
()
{
const
a
=
new
BigNumber
(
5678
);
const
b
=
new
BigNumber
(
5678
);
const
result
=
await
this
.
safeMath
.
div
(
a
,
b
);
result
.
should
.
be
.
bignumber
.
equal
(
a
.
div
(
b
));
});
it
(
'throws an error on zero division'
,
async
function
()
{
const
a
=
new
BigNumber
(
5678
);
const
b
=
new
BigNumber
(
0
);
await
assertJump
(
this
.
safeMath
.
div
(
a
,
b
));
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment