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:
nextToken = m_scanner->peekNextToken();
case Token::STRING_LITERAL:
if (Token::isEtherSubdenomination(nextToken))
m_scanner->next();
nodeFactory.markEndPosition();
expression = nodeFactory.createNode<Literal>(token, getLiteralAndAdvance(), nextToken);
if (Token::isEtherSubdenomination(nextToken))
m_scanner->next();
break;
case Token::IDENTIFIER:
nodeFactory.markEndPosition();

31
libsolidity/Types.cpp

@ -328,31 +328,32 @@ string IntegerConstantType::toString() const
u256 IntegerConstantType::literalValue(Literal const* _literal) const
{
Literal::ethSubDenomination sub =_literal->getSubDenomination();
u256 value;
// we ignore the literal and hope that the type was correctly determined
solAssert(m_value <= u256(-1), "Integer constant too large.");
solAssert(m_value >= -(bigint(1) << 255), "Integer constant too small.");
if (m_value >= 0)
value = u256(m_value);
else
value = s2u(s256(m_value));
switch(sub) {
case Literal::ethSubDenomination::WEI:
case Literal::ethSubDenomination::NONE:
break;
case Literal::ethSubDenomination::SZABO:
value *= u256(1000000000000);
break;
case Literal::ethSubDenomination::FINNEY:
value *= u256(1000000000000000);
break;
case Literal::ethSubDenomination::ETHER:
value *= u256(1000000000000000000);
break;
if (_literal) {
Literal::ethSubDenomination sub =_literal->getSubDenomination();
switch(sub) {
case Literal::ethSubDenomination::WEI:
case Literal::ethSubDenomination::NONE:
break;
case Literal::ethSubDenomination::SZABO:
value *= u256(1000000000000);
break;
case Literal::ethSubDenomination::FINNEY:
value *= u256(1000000000000000);
break;
case Literal::ethSubDenomination::ETHER:
value *= u256(1000000000000000000);
break;
}
}
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);
BOOST_FAIL(msg);
}
// BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
vector<Declaration const*> declarations;
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_AUTO_TEST_CASE(int_literals_with_ether_subdenominations)
BOOST_AUTO_TEST_CASE(int_with_wei_ether_subdenomination)
{
char const* sourceCode = R"(
contract c {
function c ()
contract test {
function test ()
{
a = 1 wei;
// b = 2 szabo;
// c = 3 finney;
// b = 4 ether;
var x = 1 wei;
}
uint256 a;
uint256 b;
uint256 c;
uint256 d;
})";
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());
}

Loading…
Cancel
Save