diff options
Diffstat (limited to 'test/libsolidity')
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 95 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 122 |
2 files changed, 191 insertions, 26 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 16b4cc9e..7ef34383 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -4542,7 +4542,6 @@ BOOST_AUTO_TEST_CASE(simple_constant_variables_test) BOOST_AUTO_TEST_CASE(constant_variables) { - //for now constant specifier is valid only for uint, bytesXX, string and enums char const* sourceCode = R"( contract Foo { uint constant x = 56; @@ -4553,6 +4552,58 @@ BOOST_AUTO_TEST_CASE(constant_variables) compileAndRun(sourceCode); } +BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_expression) +{ + char const* sourceCode = R"( + contract C { + uint constant x = 0x123 + 0x456; + function f() returns (uint) { return x + 1; } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(0x123 + 0x456 + 1)); +} + +BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_keccak) +{ + char const* sourceCode = R"( + contract C { + bytes32 constant x = keccak256("abc"); + function f() returns (bytes32) { return x; } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(dev::keccak256("abc"))); +} + +// Disabled until https://github.com/ethereum/solidity/issues/715 is implemented +//BOOST_AUTO_TEST_CASE(assignment_to_const_array_vars) +//{ +// char const* sourceCode = R"( +// contract C { +// uint[3] constant x = [uint(1), 2, 3]; +// uint constant y = x[0] + x[1] + x[2]; +// function f() returns (uint) { return y; } +// } +// )"; +// compileAndRun(sourceCode); +// BOOST_CHECK(callContractFunction("f()") == encodeArgs(1 + 2 + 3)); +//} + +// Disabled until https://github.com/ethereum/solidity/issues/715 is implemented +//BOOST_AUTO_TEST_CASE(constant_struct) +//{ +// char const* sourceCode = R"( +// contract C { +// struct S { uint x; uint[] y; } +// S constant x = S(5, new uint[](4)); +// function f() returns (uint) { return x.x; } +// } +// )"; +// compileAndRun(sourceCode); +// BOOST_CHECK(callContractFunction("f()") == encodeArgs(5)); +//} + BOOST_AUTO_TEST_CASE(packed_storage_structs_uint) { char const* sourceCode = R"( @@ -9133,24 +9184,30 @@ BOOST_AUTO_TEST_CASE(invalid_instruction) BOOST_CHECK(callContractFunction("f()") == encodeArgs()); } -//BOOST_AUTO_TEST_CASE(assert) -//{ -// char const* sourceCode = R"( -// contract C { -// function f() { -// assert(false); -// } -// function g(bool val) returns (bool) { -// assert(val == true); -// return true; -// } -// } -// )"; -// compileAndRun(sourceCode, 0, "C"); -// BOOST_CHECK(callContractFunction("f()") == encodeArgs()); -// BOOST_CHECK(callContractFunction("g(bool)", false) == encodeArgs()); -// BOOST_CHECK(callContractFunction("g(bool)", true) == encodeArgs(true)); -//} +BOOST_AUTO_TEST_CASE(assert_require) +{ + char const* sourceCode = R"( + contract C { + function f() { + assert(false); + } + function g(bool val) returns (bool) { + assert(val == true); + return true; + } + function h(bool val) returns (bool) { + require(val); + return true; + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs()); + BOOST_CHECK(callContractFunction("g(bool)", false) == encodeArgs()); + BOOST_CHECK(callContractFunction("g(bool)", true) == encodeArgs(true)); + BOOST_CHECK(callContractFunction("h(bool)", false) == encodeArgs()); + BOOST_CHECK(callContractFunction("h(bool)", true) == encodeArgs(true)); +} BOOST_AUTO_TEST_CASE(revert) { diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index fc5966af..fa310434 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -1663,6 +1663,36 @@ BOOST_AUTO_TEST_CASE(exp_operator_exponent_too_big) CHECK_ERROR(sourceCode, TypeError, ""); } +BOOST_AUTO_TEST_CASE(exp_warn_literal_base) +{ + char const* sourceCode = R"( + contract test { + function f() returns(uint d) { + uint8 x = 100; + return 10**x; + } + } + )"; + CHECK_WARNING(sourceCode, "might overflow"); + sourceCode = R"( + contract test { + function f() returns(uint d) { + uint8 x = 100; + return uint8(10)**x; + } + } + )"; + CHECK_SUCCESS(sourceCode); + sourceCode = R"( + contract test { + function f() returns(uint d) { + return 2**80; + } + } + )"; + CHECK_SUCCESS(sourceCode); +} + BOOST_AUTO_TEST_CASE(enum_member_access) { char const* text = R"( @@ -2143,16 +2173,94 @@ BOOST_AUTO_TEST_CASE(assigning_value_to_const_variable) CHECK_ERROR(text, TypeError, ""); } -BOOST_AUTO_TEST_CASE(complex_const_variable) +BOOST_AUTO_TEST_CASE(assigning_state_to_const_variable) { - //for now constant specifier is valid only for uint bytesXX and enums char const* text = R"( - contract Foo { - mapping(uint => bool) x; - mapping(uint => bool) constant mapVar = x; + contract C { + address constant x = msg.sender; } )"; - CHECK_ERROR(text, TypeError, ""); + // Change to TypeError for 0.5.0. + CHECK_WARNING(text, "Initial value for constant variable has to be compile-time constant."); +} + +BOOST_AUTO_TEST_CASE(constant_string_literal_disallows_assignment) +{ + char const* text = R"( + contract Test { + string constant x = "abefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabca"; + function f() { + x[0] = "f"; + } + } + )"; + + // Even if this is made possible in the future, we should not allow assignment + // to elements of constant arrays. + CHECK_ERROR(text, TypeError, "Index access for string is not possible."); +} + +BOOST_AUTO_TEST_CASE(assign_constant_function_value_to_constant) +{ + char const* text = R"( + contract C { + function () constant returns (uint) x; + uint constant y = x(); + } + )"; + // Change to TypeError for 0.5.0. + CHECK_WARNING(text, "Initial value for constant variable has to be compile-time constant."); +} + +BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_conversion) +{ + char const* text = R"( + contract C { + C constant x = C(0x123); + } + )"; + CHECK_SUCCESS(text); +} + +BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_expression) +{ + char const* text = R"( + contract C { + uint constant x = 0x123 + 0x456; + } + )"; + CHECK_SUCCESS(text); +} + +BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_keccak) +{ + char const* text = R"( + contract C { + bytes32 constant x = keccak256("abc"); + } + )"; + CHECK_SUCCESS(text); +} + +BOOST_AUTO_TEST_CASE(assignment_to_const_array_vars) +{ + char const* text = R"( + contract C { + uint[3] constant x = [uint(1), 2, 3]; + } + )"; + CHECK_ERROR(text, TypeError, "implemented"); +} + +BOOST_AUTO_TEST_CASE(constant_struct) +{ + char const* text = R"( + contract C { + struct S { uint x; uint[] y; } + S constant x = S(5, new uint[](4)); + } + )"; + CHECK_ERROR(text, TypeError, "implemented"); } BOOST_AUTO_TEST_CASE(uninitialized_const_variable) @@ -4300,7 +4408,7 @@ BOOST_AUTO_TEST_CASE(unused_return_value_send) } } )"; - CHECK_WARNING(text, "Return value of low-level calls not used"); + CHECK_WARNING(text, "Failure condition of 'send' ignored. Consider using 'transfer' instead."); } BOOST_AUTO_TEST_CASE(unused_return_value_call) |