[cond-expr] add a test for different types

This commit is contained in:
Lu Guanqun 2016-01-11 16:50:47 +08:00
parent 1cd3288311
commit ac3019298a
2 changed files with 16 additions and 1 deletions

View File

@ -759,7 +759,7 @@ bool TypeChecker::visit(Conditional const& _conditional)
commonType = trueType;
else
// we fake it as an equal operator, but any other comparison operator can work.
TypePointer commonType = trueType->binaryOperatorResult(Token::Equal, falseType);
commonType = trueType->binaryOperatorResult(Token::Equal, falseType);
if (!commonType)
{
typeError(

View File

@ -174,6 +174,21 @@ BOOST_AUTO_TEST_CASE(conditional_expression_storage_memory)
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(u256(2)));
}
BOOST_AUTO_TEST_CASE(conditional_expression_different_types)
{
char const* sourceCode = R"(
contract test {
function f(bool cond) returns (uint) {
uint8 x = 0xcd;
uint16 y = 0xabab;
return cond ? x : y;
}
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(u256(0xcd)));
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(u256(0xabab)));
}
BOOST_AUTO_TEST_CASE(recursive_calls)
{
char const* sourceCode = "contract test {\n"