aboutsummaryrefslogtreecommitdiffstats
path: root/test/libyul/Metrics.cpp
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2019-01-10 18:10:34 +0800
committerGitHub <noreply@github.com>2019-01-10 18:10:34 +0800
commita379873acce9da89609d6c2daac2ef785747f1ff (patch)
tree4ec9e9caf45a3cb042336da7a0b0a05a672bcada /test/libyul/Metrics.cpp
parenta414e13b7a56315ece4af2fc58739f84df7f50c1 (diff)
parentb7ef8509554768de04d1cf7d11b122e98ab43ae8 (diff)
downloaddexon-solidity-a379873acce9da89609d6c2daac2ef785747f1ff.tar.gz
dexon-solidity-a379873acce9da89609d6c2daac2ef785747f1ff.tar.zst
dexon-solidity-a379873acce9da89609d6c2daac2ef785747f1ff.zip
Merge pull request #5774 from ethereum/variablesAreFree
[Yul] Variables are free with regards to code size.
Diffstat (limited to 'test/libyul/Metrics.cpp')
-rw-r--r--test/libyul/Metrics.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/test/libyul/Metrics.cpp b/test/libyul/Metrics.cpp
new file mode 100644
index 00000000..185a3755
--- /dev/null
+++ b/test/libyul/Metrics.cpp
@@ -0,0 +1,116 @@
+/*
+ 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/>.
+*/
+/**
+ * Unit tests for the code metrics.
+ */
+
+#include <test/Options.h>
+
+#include <test/libyul/Common.h>
+
+#include <libyul/optimiser/Metrics.h>
+#include <libyul/AsmData.h>
+
+
+using namespace std;
+using namespace langutil;
+
+namespace yul
+{
+namespace test
+{
+
+namespace
+{
+
+size_t codeSize(string const& _source)
+{
+ shared_ptr<Block> ast = parse(_source, false).first;
+ BOOST_REQUIRE(ast);
+ return CodeSize::codeSize(*ast);
+}
+
+}
+
+BOOST_AUTO_TEST_SUITE(YulCodeSize)
+
+BOOST_AUTO_TEST_CASE(empty_code)
+{
+ BOOST_CHECK_EQUAL(codeSize("{}"), 0);
+}
+
+BOOST_AUTO_TEST_CASE(nested_blocks)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ {} {} {{ }} }"), 0);
+}
+
+BOOST_AUTO_TEST_CASE(instruction)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ pop(calldatasize()) }"), 2);
+}
+
+BOOST_AUTO_TEST_CASE(variables_are_free)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ let x let y let a, b, c }"), 0);
+}
+
+BOOST_AUTO_TEST_CASE(constants_cost_one)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ let x := 3 }"), 1);
+}
+
+BOOST_AUTO_TEST_CASE(functions_are_skipped)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ function f(x) -> r { r := mload(x) } }"), 0);
+}
+
+BOOST_AUTO_TEST_CASE(function_with_arguments)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ function f(x) { sstore(x, 2) } f(2) }"), 2);
+}
+
+BOOST_AUTO_TEST_CASE(function_with_variables_as_arguments)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ function f(x) { sstore(x, 2) } let y f(y) }"), 1);
+}
+
+BOOST_AUTO_TEST_CASE(function_with_variables_and_constants_as_arguments)
+{
+ BOOST_CHECK_EQUAL(codeSize(
+ "{ function f(x, r) -> z { sstore(x, r) z := r } let y let t := f(y, 2) }"
+ ), 2);
+}
+
+BOOST_AUTO_TEST_CASE(assignment)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ let a a := 3 }"), 1);
+}
+
+BOOST_AUTO_TEST_CASE(assignments_between_vars_are_free)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ let a let b := a a := b }"), 0);
+}
+
+BOOST_AUTO_TEST_CASE(assignment_complex)
+{
+ BOOST_CHECK_EQUAL(codeSize("{ let a let x := mload(a) a := sload(x) }"), 2);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+}
+}