Browse Source

Tests and fixes for ether subdenominations

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
c11c26c094
  1. 4
      libsolidity/Parser.cpp
  2. 31
      libsolidity/Types.cpp
  3. 63
      test/SolidityExpressionCompiler.cpp

4
libsolidity/Parser.cpp

@ -694,10 +694,10 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
case Token::NUMBER: case Token::NUMBER:
nextToken = m_scanner->peekNextToken(); nextToken = m_scanner->peekNextToken();
case Token::STRING_LITERAL: case Token::STRING_LITERAL:
if (Token::isEtherSubdenomination(nextToken))
m_scanner->next();
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();
expression = nodeFactory.createNode<Literal>(token, getLiteralAndAdvance(), nextToken); expression = nodeFactory.createNode<Literal>(token, getLiteralAndAdvance(), nextToken);
if (Token::isEtherSubdenomination(nextToken))
m_scanner->next();
break; break;
case Token::IDENTIFIER: case Token::IDENTIFIER:
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();

31
libsolidity/Types.cpp

@ -328,31 +328,32 @@ string IntegerConstantType::toString() const
u256 IntegerConstantType::literalValue(Literal const* _literal) const u256 IntegerConstantType::literalValue(Literal const* _literal) const
{ {
Literal::ethSubDenomination sub =_literal->getSubDenomination();
u256 value; u256 value;
// we ignore the literal and hope that the type was correctly determined // we ignore the literal and hope that the type was correctly determined
solAssert(m_value <= u256(-1), "Integer constant too large."); solAssert(m_value <= u256(-1), "Integer constant too large.");
solAssert(m_value >= -(bigint(1) << 255), "Integer constant too small."); solAssert(m_value >= -(bigint(1) << 255), "Integer constant too small.");
if (m_value >= 0) if (m_value >= 0)
value = u256(m_value); value = u256(m_value);
else else
value = s2u(s256(m_value)); value = s2u(s256(m_value));
switch(sub) { if (_literal) {
case Literal::ethSubDenomination::WEI: Literal::ethSubDenomination sub =_literal->getSubDenomination();
case Literal::ethSubDenomination::NONE: switch(sub) {
break; case Literal::ethSubDenomination::WEI:
case Literal::ethSubDenomination::SZABO: case Literal::ethSubDenomination::NONE:
value *= u256(1000000000000); break;
break; case Literal::ethSubDenomination::SZABO:
case Literal::ethSubDenomination::FINNEY: value *= u256(1000000000000);
value *= u256(1000000000000000); break;
break; case Literal::ethSubDenomination::FINNEY:
case Literal::ethSubDenomination::ETHER: value *= u256(1000000000000000);
value *= u256(1000000000000000000); break;
break; case Literal::ethSubDenomination::ETHER:
value *= u256(1000000000000000000);
break;
}
} }
return value; return value;

63
test/SolidityExpressionCompiler.cpp

@ -100,7 +100,6 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
auto msg = std::string("Parsing source code failed with: \n") + boost::diagnostic_information(_e); auto msg = std::string("Parsing source code failed with: \n") + boost::diagnostic_information(_e);
BOOST_FAIL(msg); BOOST_FAIL(msg);
} }
// BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
vector<Declaration const*> declarations; vector<Declaration const*> declarations;
declarations.reserve(_globalDeclarations.size() + 1); declarations.reserve(_globalDeclarations.size() + 1);
@ -186,25 +185,63 @@ BOOST_AUTO_TEST_CASE(int_literal)
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }
BOOST_AUTO_TEST_CASE(int_literals_with_ether_subdenominations) BOOST_AUTO_TEST_CASE(int_with_wei_ether_subdenomination)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
contract c { contract test {
function c () function test ()
{ {
a = 1 wei; var x = 1 wei;
// b = 2 szabo;
// c = 3 finney;
// b = 4 ether;
} }
uint256 a;
uint256 b;
uint256 c;
uint256 d;
})"; })";
bytes code = compileFirstExpression(sourceCode); bytes code = compileFirstExpression(sourceCode);
bytes expectation({byte(eth::Instruction::PUSH5), 0x38, 0xd4, 0xa5, 0x10, 0x00}); bytes expectation({byte(eth::Instruction::PUSH1), 0x1});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
BOOST_AUTO_TEST_CASE(int_with_szabo_ether_subdenomination)
{
char const* sourceCode = R"(
contract test {
function test ()
{
var x = 1 szabo;
}
})";
bytes code = compileFirstExpression(sourceCode);
bytes expectation({byte(eth::Instruction::PUSH5), 0xe8, 0xd4, 0xa5, 0x10, 0x00});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
BOOST_AUTO_TEST_CASE(int_with_finney_ether_subdenomination)
{
char const* sourceCode = R"(
contract test {
function test ()
{
var x = 1 finney;
}
})";
bytes code = compileFirstExpression(sourceCode);
bytes expectation({byte(eth::Instruction::PUSH7), 0x3, 0x8d, 0x7e, 0xa4, 0xc6, 0x80, 0x00});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
BOOST_AUTO_TEST_CASE(int_with_ether_ether_subdenomination)
{
char const* sourceCode = R"(
contract test {
function test ()
{
var x = 1 ether;
}
})";
bytes code = compileFirstExpression(sourceCode);
bytes expectation({byte(eth::Instruction::PUSH8), 0xd, 0xe0, 0xb6, 0xb3, 0xa7, 0x64, 0x00, 0x00});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }

Loading…
Cancel
Save