diff options
author | Balajiganapathi S <balajiganapathi.s@gmail.com> | 2017-10-28 20:36:46 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2017-11-22 09:41:50 +0800 |
commit | c0b49694518d333a8ad62623321f839fc3b4bc6a (patch) | |
tree | 3d2e835b75385e9dd1d0d389ff3d81d32dfcdcaa /test | |
parent | 165857b1d40ecb8a1bcf40eec9e371370ebe2541 (diff) | |
download | dexon-solidity-c0b49694518d333a8ad62623321f839fc3b4bc6a.tar.gz dexon-solidity-c0b49694518d333a8ad62623321f839fc3b4bc6a.tar.zst dexon-solidity-c0b49694518d333a8ad62623321f839fc3b4bc6a.zip |
Add more tests for constant var as array lengths
Diffstat (limited to 'test')
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 97 |
1 files changed, 96 insertions, 1 deletions
diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index e00beefe..cedf7f3a 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2107,7 +2107,7 @@ BOOST_AUTO_TEST_CASE(array_with_nonconstant_length) function f(uint a) public { uint8[a] x; } } )"; - CHECK_ERROR(text, TypeError, "Identifier must be declared constant"); + CHECK_ERROR(text, TypeError, "Identifier must be declared constant."); } BOOST_AUTO_TEST_CASE(array_with_negative_length) @@ -7285,6 +7285,101 @@ BOOST_AUTO_TEST_CASE(array_length_non_integer_constant_var) CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal."); } +BOOST_AUTO_TEST_CASE(array_length_cannot_be_function) +{ + char const* text = R"( + contract C { + function f() {} + uint[f] ids; + } + )"; + CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal."); +} + +BOOST_AUTO_TEST_CASE(array_length_can_be_recursive_constant) +{ + char const* text = R"( + contract C { + uint constant L = 5; + uint constant LEN = L + 4 * L; + uint[LEN] ids; + } + )"; + CHECK_SUCCESS(text); +} + +BOOST_AUTO_TEST_CASE(array_length_cannot_be_function_call) +{ + char const* text = R"( + contract C { + function f(uint x) {} + uint constant LEN = f(); + uint[LEN] ids; + } + )"; + CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal."); +} + +BOOST_AUTO_TEST_CASE(array_length_const_cannot_be_fractional) +{ + char const* text = R"( + contract C { + fixed constant L = 10.5; + uint[L] ids; + } + )"; + CHECK_ERROR(text, TypeError, "Array with fractional length specified"); +} + +BOOST_AUTO_TEST_CASE(array_length_can_be_constant_in_struct) +{ + char const* text = R"( + contract C { + uint constant LEN = 10; + struct Test { + uint[LEN] ids; + } + } + )"; + CHECK_SUCCESS(text); +} + +BOOST_AUTO_TEST_CASE(array_length_can_be_constant_in_function) +{ + char const* text = R"( + contract C { + uint constant LEN = 10; + function f() { + uint[LEN] a; + } + } + )"; + CHECK_SUCCESS(text); +} + +BOOST_AUTO_TEST_CASE(array_length_cannot_be_constant_function_parameter) +{ + char const* text = R"( + contract C { + function f(uint constant LEN) { + uint[LEN] a; + } + } + )"; + CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal."); +} + +BOOST_AUTO_TEST_CASE(array_length_with_pure_functions) +{ + char const* text = R"( + contract C { + uint constant LEN = keccak256(ripemd160(33)); + uint[LEN] ids; + } + )"; + CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal."); +} + BOOST_AUTO_TEST_CASE(array_length_invalid_expression) { char const* text = R"( |