From bde913f088c873cba75db30b2d463f50f9dfe862 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 1 Mar 2017 16:34:29 +0100 Subject: Some new tests for constant variables. --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 865fd0c5..aef93e92 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2173,6 +2173,56 @@ BOOST_AUTO_TEST_CASE(assigning_value_to_const_variable) CHECK_ERROR(text, TypeError, ""); } +BOOST_AUTO_TEST_CASE(assigning_state_to_const_variable) +{ + char const* text = R"( + contract C { + address constant x = msg.sender; + } + )"; + CHECK_ERROR(text, TypeError, "Expression is not 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 + 9x456; + } + )"; + CHECK_SUCCESS(text); +} + +BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_keccak) +{ + char const* text = R"( + contract C { + bytes32 constant x = keccak("abc"); + } + )"; + CHECK_SUCCESS(text); +} + +BOOST_AUTO_TEST_CASE(assignment_to_const_array_vars) +{ + char const* text = R"( + contract C { + uint[3] memory constant x = [1, 2, 3]; + } + )"; + CHECK_SUCCESS(text); +} + BOOST_AUTO_TEST_CASE(complex_const_variable) { //for now constant specifier is valid only for uint bytesXX and enums -- cgit From f39763e91c97b29bfe6d2c79cb1c1ebf80b2b9aa Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 1 Mar 2017 19:12:40 +0100 Subject: Type checking for pure expressions. --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 30 ++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index aef93e92..90831ccd 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2180,7 +2180,18 @@ BOOST_AUTO_TEST_CASE(assigning_state_to_const_variable) address constant x = msg.sender; } )"; - CHECK_ERROR(text, TypeError, "Expression is not compile-time constant."); + CHECK_ERROR(text, TypeError, "Initial value for constant variable has to be compile-time constant."); +} + +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(); + } + )"; + CHECK_ERROR(text, TypeError, "Initial value for constant variable has to be compile-time constant."); } BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_conversion) @@ -2197,7 +2208,7 @@ BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_expression) { char const* text = R"( contract C { - uint constant x = 0x123 + 9x456; + uint constant x = 0x123 + 0x456; } )"; CHECK_SUCCESS(text); @@ -2207,7 +2218,7 @@ BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_keccak) { char const* text = R"( contract C { - bytes32 constant x = keccak("abc"); + bytes32 constant x = keccak256("abc"); } )"; CHECK_SUCCESS(text); @@ -2217,22 +2228,21 @@ BOOST_AUTO_TEST_CASE(assignment_to_const_array_vars) { char const* text = R"( contract C { - uint[3] memory constant x = [1, 2, 3]; + uint[3] constant x = [uint(1), 2, 3]; } )"; CHECK_SUCCESS(text); } -BOOST_AUTO_TEST_CASE(complex_const_variable) +BOOST_AUTO_TEST_CASE(constant_struct) { - //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 { + struct S { uint x; uint[] y; } + S constant x = S(5, new uint[](4)); } )"; - CHECK_ERROR(text, TypeError, ""); + CHECK_SUCCESS(text); } BOOST_AUTO_TEST_CASE(uninitialized_const_variable) -- cgit From 49cfacced291ea73d99ec1353a0c77bfe476729a Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 1 Mar 2017 19:49:02 +0100 Subject: End to end tests for constants. --- test/libsolidity/SolidityEndToEndTest.cpp | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index baed3f1e..f14108ca 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -4553,6 +4553,56 @@ 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"))); +} + +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)); +} + +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"( -- cgit From 14948e514d7b15eb92d2c08f018bb23ead71fa5a Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 1 Mar 2017 19:49:15 +0100 Subject: Allow enum values for constants. --- test/libsolidity/SolidityEndToEndTest.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index f14108ca..552352e4 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; -- cgit From 592cec7e9074313e7ce5539769c065ecf5cbba12 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 3 Mar 2017 19:26:54 +0100 Subject: Disallow constants that are neither value types nor strings. --- test/libsolidity/SolidityEndToEndTest.cpp | 50 +++++++++++----------- test/libsolidity/SolidityNameAndTypeResolution.cpp | 20 ++++++++- 2 files changed, 44 insertions(+), 26 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 552352e4..831840e2 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -4576,31 +4576,33 @@ BOOST_AUTO_TEST_CASE(assignment_to_const_var_involving_keccak) BOOST_CHECK(callContractFunction("f()") == encodeArgs(dev::keccak256("abc"))); } -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(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)); +//} -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)); -} +// 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) { diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 90831ccd..71fef32d 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2183,6 +2183,22 @@ BOOST_AUTO_TEST_CASE(assigning_state_to_const_variable) CHECK_ERROR(text, TypeError, "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"( @@ -2231,7 +2247,7 @@ BOOST_AUTO_TEST_CASE(assignment_to_const_array_vars) uint[3] constant x = [uint(1), 2, 3]; } )"; - CHECK_SUCCESS(text); + CHECK_ERROR(text, TypeError, "implemented"); } BOOST_AUTO_TEST_CASE(constant_struct) @@ -2242,7 +2258,7 @@ BOOST_AUTO_TEST_CASE(constant_struct) S constant x = S(5, new uint[](4)); } )"; - CHECK_SUCCESS(text); + CHECK_ERROR(text, TypeError, "implemented"); } BOOST_AUTO_TEST_CASE(uninitialized_const_variable) -- cgit From 9f328ff749477106a569e679e5eeed5c7e78d29d Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 14 Mar 2017 19:25:16 +0100 Subject: Turn non-constant constants error into warning. --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 71fef32d..27791775 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2180,7 +2180,8 @@ BOOST_AUTO_TEST_CASE(assigning_state_to_const_variable) address constant x = msg.sender; } )"; - CHECK_ERROR(text, TypeError, "Initial value for constant variable has to be compile-time constant."); + // 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) @@ -2207,7 +2208,8 @@ BOOST_AUTO_TEST_CASE(assign_constant_function_value_to_constant) uint constant y = x(); } )"; - CHECK_ERROR(text, TypeError, "Initial value for constant variable has to be compile-time constant."); + // 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) -- cgit