Skip to content

Commit

Permalink
Add more tests for constant var as array lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
Balajiganapathi authored and axic committed Nov 22, 2017
1 parent 165857b commit c0b4969
Showing 1 changed file with 96 additions and 1 deletion.
97 changes: 96 additions & 1 deletion test/libsolidity/SolidityNameAndTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"(
Expand Down

0 comments on commit c0b4969

Please sign in to comment.