From 4fbf5a3f122656d234d642a03f719c6267e69564 Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Wed, 24 Mar 2021 13:48:52 +0100 Subject: [PATCH] Added few more tests for low level inliner. --- test/libevmasm/Optimiser.cpp | 88 +++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/test/libevmasm/Optimiser.cpp b/test/libevmasm/Optimiser.cpp index 1899a9363..e71862fcc 100644 --- a/test/libevmasm/Optimiser.cpp +++ b/test/libevmasm/Optimiser.cpp @@ -1639,7 +1639,6 @@ BOOST_AUTO_TEST_CASE(inliner_cse_break) BOOST_AUTO_TEST_CASE(inliner_stop) { - AssemblyItem jumpTo{Instruction::JUMP}; AssemblyItems items{ AssemblyItem(PushTag, 1), Instruction::JUMP, @@ -1658,6 +1657,93 @@ BOOST_AUTO_TEST_CASE(inliner_stop) ); } +BOOST_AUTO_TEST_CASE(inliner_stop_jumpi) +{ + // Because of `jumpi`, will not be inlined. + AssemblyItems items{ + u256(1), + AssemblyItem(PushTag, 1), + Instruction::JUMPI, + AssemblyItem(Tag, 1), + Instruction::STOP + }; + AssemblyItems expectation = items; + Inliner{items, {}, 200, false, {}}.optimise(); + BOOST_CHECK_EQUAL_COLLECTIONS( + items.begin(), items.end(), + expectation.begin(), expectation.end() + ); +} + +BOOST_AUTO_TEST_CASE(inliner_revert) +{ + AssemblyItems items{ + AssemblyItem(PushTag, 1), + Instruction::JUMP, + AssemblyItem(Tag, 1), + u256(0), + Instruction::DUP1, + Instruction::REVERT + }; + AssemblyItems expectation{ + u256(0), + Instruction::DUP1, + Instruction::REVERT, + AssemblyItem(Tag, 1), + u256(0), + Instruction::DUP1, + Instruction::REVERT + }; + + Inliner{items, {}, 200, false, {}}.optimise(); + BOOST_CHECK_EQUAL_COLLECTIONS( + items.begin(), items.end(), + expectation.begin(), expectation.end() + ); +} + +BOOST_AUTO_TEST_CASE(inliner_revert_increased_datagas) +{ + // Inlining this would increase data gas (5 bytes v/s 4 bytes), therefore, skipped. + AssemblyItems items{ + AssemblyItem(PushTag, 1), + Instruction::JUMP, + AssemblyItem(Tag, 1), + u256(0), + u256(0), + Instruction::REVERT + }; + + AssemblyItems expectation = items; + Inliner{items, {}, 200, false, {}}.optimise(); + BOOST_CHECK_EQUAL_COLLECTIONS( + items.begin(), items.end(), + expectation.begin(), expectation.end() + ); +} + +BOOST_AUTO_TEST_CASE(inliner_invalid) +{ + AssemblyItems items{ + AssemblyItem(PushTag, 1), + Instruction::JUMP, + AssemblyItem(Tag, 1), + Instruction::INVALID + }; + + AssemblyItems expectation = { + Instruction::INVALID, + AssemblyItem(Tag, 1), + Instruction::INVALID + }; + Inliner{items, {}, 200, false, {}}.optimise(); + BOOST_CHECK_EQUAL_COLLECTIONS( + items.begin(), items.end(), + expectation.begin(), expectation.end() + ); +} + + BOOST_AUTO_TEST_SUITE_END() } // end namespaces