Extract SMTChecker division tests

This commit is contained in:
Leonardo Alt 2019-12-04 13:11:13 +01:00
parent 225041738e
commit b870e4ea31
8 changed files with 64 additions and 80 deletions

View File

@ -59,86 +59,6 @@ protected:
BOOST_FIXTURE_TEST_SUITE(SMTChecker, SMTCheckerFramework)
BOOST_AUTO_TEST_CASE(division)
{
string text = R"(
contract C {
function f(uint x, uint y) public pure returns (uint) {
return x / y;
}
}
)";
CHECK_WARNING(text, "Division by zero");
text = R"(
contract C {
function f(uint x, uint y) public pure returns (uint) {
require(y != 0);
return x / y;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
text = R"(
contract C {
function f(int x, int y) public pure returns (int) {
require(y != 0);
return x / y;
}
}
)";
CHECK_WARNING(text, "Overflow");
text = R"(
contract C {
function f(int x, int y) public pure returns (int) {
require(y != 0);
require(y != -1);
return x / y;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
text = R"(
contract C {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c;
if (a != 0) {
c = a * b;
require(c / a == b);
}
return c;
}
}
)";
CHECK_SUCCESS_OR_WARNING(text, "might happen");
text = R"(
contract C {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// TODO remove when SMTChecker sees that this code is the `else` of the `return`.
require(a != 0);
uint256 c = a * b;
require(c / a == b);
return c;
}
}
)";
CHECK_SUCCESS_OR_WARNING(text, "might happen");
text = R"(
contract C {
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0);
uint256 c = a / b;
return c;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(division_truncates_correctly)
{
string text = R"(

View File

@ -0,0 +1,8 @@
pragma experimental SMTChecker;
contract C {
function f(uint x, uint y) public pure returns (uint) {
return x / y;
}
}
// ----
// Warning: (111-116): Division by zero happens here

View File

@ -0,0 +1,7 @@
pragma experimental SMTChecker;
contract C {
function f(uint x, uint y) public pure returns (uint) {
require(y != 0);
return x / y;
}
}

View File

@ -0,0 +1,9 @@
pragma experimental SMTChecker;
contract C {
function f(int x, int y) public pure returns (int) {
require(y != 0);
return x / y;
}
}
// ----
// Warning: (127-132): Overflow (resulting value larger than 0x80 * 2**248 - 1) happens here

View File

@ -0,0 +1,8 @@
pragma experimental SMTChecker;
contract C {
function f(int x, int y) public pure returns (int) {
require(y != 0);
require(y != -1);
return x / y;
}
}

View File

@ -0,0 +1,11 @@
pragma experimental SMTChecker;
contract C {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c;
if (a != 0) {
c = a * b;
require(c / a == b);
}
return c;
}
}

View File

@ -0,0 +1,13 @@
pragma experimental SMTChecker;
contract C {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// TODO remove when SMTChecker sees that this code is the `else` of the `return`.
require(a != 0);
uint256 c = a * b;
require(c / a == b);
return c;
}
}

View File

@ -0,0 +1,8 @@
pragma experimental SMTChecker;
contract C {
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0);
uint256 c = a / b;
return c;
}
}