mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Tests
This commit is contained in:
parent
a2a06d0318
commit
3c4f558242
@ -27,6 +27,7 @@
|
||||
#include <test/libyul/YulOptimizerTest.h>
|
||||
#include <test/libyul/YulInterpreterTest.h>
|
||||
#include <test/libyul/ObjectCompilerTest.h>
|
||||
#include <test/libyul/FunctionSideEffects.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
@ -56,6 +57,7 @@ Testsuite const g_interactiveTestsuites[] = {
|
||||
{"Yul Optimizer", "libyul", "yulOptimizerTests", false, false, &yul::test::YulOptimizerTest::create},
|
||||
{"Yul Interpreter", "libyul", "yulInterpreterTests", false, false, &yul::test::YulInterpreterTest::create},
|
||||
{"Yul Object Compiler", "libyul", "objectCompiler", false, false, &yul::test::ObjectCompilerTest::create},
|
||||
{"Function Side Effects","libyul", "functionSideEffects", false, false, &yul::test::FunctionSideEffects::create},
|
||||
{"Syntax", "libsolidity", "syntaxTests", false, false, &SyntaxTest::create},
|
||||
{"Error Recovery", "libsolidity", "errorRecoveryTests", false, false, &SyntaxTest::createErrorRecovery},
|
||||
{"Semantic", "libsolidity", "semanticTests", false, true, &SemanticTest::create},
|
||||
|
125
test/libyul/FunctionSideEffects.cpp
Normal file
125
test/libyul/FunctionSideEffects.cpp
Normal file
@ -0,0 +1,125 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/libyul/FunctionSideEffects.h>
|
||||
#include <test/Options.h>
|
||||
#include <test/libyul/Common.h>
|
||||
|
||||
#include <libdevcore/AnsiColorized.h>
|
||||
|
||||
#include <libyul/SideEffects.h>
|
||||
#include <libyul/optimiser/CallGraphGenerator.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/Object.h>
|
||||
#include <libyul/backends/evm/EVMDialect.h>
|
||||
|
||||
#include <libdevcore/StringUtils.h>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
|
||||
using namespace dev;
|
||||
using namespace langutil;
|
||||
using namespace yul;
|
||||
using namespace yul::test;
|
||||
using namespace dev::solidity;
|
||||
using namespace dev::solidity::test;
|
||||
using namespace std;
|
||||
|
||||
namespace
|
||||
{
|
||||
string toString(SideEffects const& _sideEffects)
|
||||
{
|
||||
vector<string> ret;
|
||||
if (_sideEffects.movable)
|
||||
ret.push_back("movable");
|
||||
if (_sideEffects.sideEffectFree)
|
||||
ret.push_back("sideEffectFree");
|
||||
if (_sideEffects.sideEffectFreeIfNoMSize)
|
||||
ret.push_back("sideEffectFreeIfNoMSize");
|
||||
if (_sideEffects.invalidatesStorage)
|
||||
ret.push_back("invalidatesStorage");
|
||||
if (_sideEffects.invalidatesMemory)
|
||||
ret.push_back("invalidatesMemory");
|
||||
return joinHumanReadable(ret);
|
||||
}
|
||||
}
|
||||
|
||||
FunctionSideEffects::FunctionSideEffects(string const& _filename)
|
||||
{
|
||||
ifstream file(_filename);
|
||||
if (!file)
|
||||
BOOST_THROW_EXCEPTION(runtime_error("Cannot open test input: \"" + _filename + "\"."));
|
||||
file.exceptions(ios::badbit);
|
||||
|
||||
m_source = parseSourceAndSettings(file);
|
||||
m_expectation = parseSimpleExpectations(file);}
|
||||
|
||||
TestCase::TestResult FunctionSideEffects::run(ostream& _stream, string const& _linePrefix, bool _formatted)
|
||||
{
|
||||
Object obj;
|
||||
std::tie(obj.code, obj.analysisInfo) = yul::test::parse(m_source, false);
|
||||
if (!obj.code)
|
||||
BOOST_THROW_EXCEPTION(runtime_error("Parsing input failed."));
|
||||
|
||||
map<YulString, SideEffects> functionSideEffects = SideEffectsPropagator::sideEffects(
|
||||
EVMDialect::strictAssemblyForEVM(langutil::EVMVersion()),
|
||||
CallGraphGenerator::callGraph(*obj.code)
|
||||
);
|
||||
|
||||
std::map<std::string, std::string> functionSideEffectsStr;
|
||||
for (auto const& fun: functionSideEffects)
|
||||
functionSideEffectsStr[fun.first.str()] = toString(fun.second);
|
||||
|
||||
m_obtainedResult.clear();
|
||||
for (auto const& fun: functionSideEffectsStr)
|
||||
m_obtainedResult += fun.first + ": " + fun.second + "\n";
|
||||
|
||||
if (m_expectation != m_obtainedResult)
|
||||
{
|
||||
string nextIndentLevel = _linePrefix + " ";
|
||||
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
|
||||
printIndented(_stream, m_expectation, nextIndentLevel);
|
||||
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
|
||||
printIndented(_stream, m_obtainedResult, nextIndentLevel);
|
||||
return TestResult::Failure;
|
||||
}
|
||||
return TestResult::Success;
|
||||
}
|
||||
|
||||
|
||||
void FunctionSideEffects::printSource(ostream& _stream, string const& _linePrefix, bool const) const
|
||||
{
|
||||
printIndented(_stream, m_source, _linePrefix);
|
||||
}
|
||||
|
||||
void FunctionSideEffects::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
|
||||
{
|
||||
printIndented(_stream, m_obtainedResult, _linePrefix);
|
||||
}
|
||||
|
||||
void FunctionSideEffects::printIndented(ostream& _stream, string const& _output, string const& _linePrefix) const
|
||||
{
|
||||
stringstream output(_output);
|
||||
string line;
|
||||
while (getline(output, line))
|
||||
if (line.empty())
|
||||
// Avoid trailing spaces.
|
||||
_stream << boost::trim_right_copy(_linePrefix) << endl;
|
||||
else
|
||||
_stream << _linePrefix << line << endl;
|
||||
}
|
54
test/libyul/FunctionSideEffects.h
Normal file
54
test/libyul/FunctionSideEffects.h
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libdevcore/AnsiColorized.h>
|
||||
#include <test/TestCase.h>
|
||||
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
namespace test
|
||||
{
|
||||
|
||||
class FunctionSideEffects: public dev::solidity::test::TestCase
|
||||
{
|
||||
public:
|
||||
static std::unique_ptr<TestCase> create(Config const& _config)
|
||||
{ return std::unique_ptr<TestCase>(new FunctionSideEffects(_config.filename)); }
|
||||
explicit FunctionSideEffects(std::string const& _filename);
|
||||
|
||||
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
|
||||
|
||||
void printSource(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) const override;
|
||||
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
|
||||
|
||||
private:
|
||||
void printIndented(std::ostream& _stream, std::string const& _output, std::string const& _linePrefix = "") const;
|
||||
|
||||
std::string m_source;
|
||||
std::string m_expectation;
|
||||
std::string m_obtainedResult;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
10
test/libyul/functionSideEffects/cyclic_graph.yul
Normal file
10
test/libyul/functionSideEffects/cyclic_graph.yul
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
function a() { b() }
|
||||
function b() { c() }
|
||||
function c() { b() }
|
||||
}
|
||||
// ----
|
||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// a: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// b: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// c: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
function a() { b() }
|
||||
function b() { a() }
|
||||
}
|
||||
// ----
|
||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// a: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// b: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
4
test/libyul/functionSideEffects/empty.yul
Normal file
4
test/libyul/functionSideEffects/empty.yul
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
}
|
||||
// ----
|
||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
5
test/libyul/functionSideEffects/empty_with_sstore.yul
Normal file
5
test/libyul/functionSideEffects/empty_with_sstore.yul
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
sstore(0, 1)
|
||||
}
|
||||
// ----
|
||||
// : invalidatesStorage
|
22
test/libyul/functionSideEffects/multi_calls.yul
Normal file
22
test/libyul/functionSideEffects/multi_calls.yul
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
function a() {
|
||||
b()
|
||||
}
|
||||
function b() {
|
||||
sstore(0, 1)
|
||||
b()
|
||||
}
|
||||
function c() {
|
||||
mstore(0, 1)
|
||||
a()
|
||||
d()
|
||||
}
|
||||
function d() {
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// a: invalidatesStorage
|
||||
// b: invalidatesStorage
|
||||
// c: invalidatesStorage, invalidatesMemory
|
||||
// d: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
6
test/libyul/functionSideEffects/recursive_function.yul
Normal file
6
test/libyul/functionSideEffects/recursive_function.yul
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
function a() { a() }
|
||||
}
|
||||
// ----
|
||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// a: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
14
test/libyul/functionSideEffects/simple_functions.yul
Normal file
14
test/libyul/functionSideEffects/simple_functions.yul
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
function a() {}
|
||||
function f() { mstore(0, 1) }
|
||||
function g() { sstore(0, 1) }
|
||||
function h() { let x := msize() }
|
||||
function i() { let z := mload(0) }
|
||||
}
|
||||
// ----
|
||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// a: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// f: invalidatesMemory
|
||||
// g: invalidatesStorage
|
||||
// h: sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// i: sideEffectFreeIfNoMSize
|
40
test/libyul/functionSideEffects/structures.yul
Normal file
40
test/libyul/functionSideEffects/structures.yul
Normal file
@ -0,0 +1,40 @@
|
||||
{
|
||||
if calldataload(0)
|
||||
{
|
||||
f()
|
||||
}
|
||||
g()
|
||||
|
||||
function f() {
|
||||
pop(mload(0))
|
||||
}
|
||||
function g() {
|
||||
if sload(0)
|
||||
{
|
||||
h()
|
||||
}
|
||||
}
|
||||
function h() {
|
||||
switch t()
|
||||
case 1 {
|
||||
i()
|
||||
}
|
||||
}
|
||||
function t() -> x {
|
||||
mstore(0, 1)
|
||||
}
|
||||
function i() {
|
||||
sstore(0, 1)
|
||||
}
|
||||
function r(a) -> b {
|
||||
b := mul(a, 2)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// : invalidatesStorage, invalidatesMemory
|
||||
// f: sideEffectFreeIfNoMSize
|
||||
// g: invalidatesStorage, invalidatesMemory
|
||||
// h: invalidatesStorage, invalidatesMemory
|
||||
// i: invalidatesStorage
|
||||
// r: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
||||
// t: invalidatesMemory
|
@ -30,6 +30,8 @@ add_executable(isoltest
|
||||
../libsolidity/ABIJsonTest.cpp
|
||||
../libsolidity/ASTJSONTest.cpp
|
||||
../libsolidity/SMTCheckerJSONTest.cpp
|
||||
../libyul/Common.cpp
|
||||
../libyul/FunctionSideEffects.cpp
|
||||
../libyul/ObjectCompilerTest.cpp
|
||||
../libyul/YulOptimizerTest.cpp
|
||||
../libyul/YulInterpreterTest.cpp
|
||||
|
Loading…
Reference in New Issue
Block a user