From 547173533c13a4c1f5163ede49e756c88ba78567 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Tue, 11 Jun 2019 14:05:45 +0200 Subject: [PATCH 1/2] Unify parsing of simple test expectations and require lines to start with ``//``. --- test/TestCase.cpp | 14 ++++++++++++++ test/TestCase.h | 2 ++ test/libyul/ObjectCompilerTest.cpp | 6 +----- test/libyul/YulInterpreterTest.cpp | 8 +------- test/libyul/YulOptimizerTest.cpp | 7 +------ 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/test/TestCase.cpp b/test/TestCase.cpp index c86635735..76c27d41a 100644 --- a/test/TestCase.cpp +++ b/test/TestCase.cpp @@ -95,6 +95,20 @@ string TestCase::parseSourceAndSettings(istream& _stream) return source; } +string TestCase::parseSimpleExpectations(std::istream& _file) +{ + string result; + string line; + while (getline(_file, line)) + if (boost::algorithm::starts_with(line, "// ")) + result += line.substr(3) + "\n"; + else if (line == "//") + result += "\n"; + else + BOOST_THROW_EXCEPTION(runtime_error("Test expectations must start with \"// \".")); + return result; +} + void TestCase::expect(string::iterator& _it, string::iterator _end, string::value_type _c) { if (_it == _end || *_it != _c) diff --git a/test/TestCase.h b/test/TestCase.h index ba5669506..c08f2d77e 100644 --- a/test/TestCase.h +++ b/test/TestCase.h @@ -92,6 +92,8 @@ protected: std::string parseSourceAndSettings(std::istream& _file); static void expect(std::string::iterator& _it, std::string::iterator _end, std::string::value_type _c); + static std::string parseSimpleExpectations(std::istream& _file); + template static void skipWhitespace(IteratorType& _it, IteratorType _end) { diff --git a/test/libyul/ObjectCompilerTest.cpp b/test/libyul/ObjectCompilerTest.cpp index f33416c09..de44cc67f 100644 --- a/test/libyul/ObjectCompilerTest.cpp +++ b/test/libyul/ObjectCompilerTest.cpp @@ -55,11 +55,7 @@ ObjectCompilerTest::ObjectCompilerTest(string const& _filename) m_optimize = true; m_source += line + "\n"; } - while (getline(file, line)) - if (boost::algorithm::starts_with(line, "//")) - m_expectation += line.substr((line.size() >= 3 && line[2] == ' ') ? 3 : 2) + "\n"; - else - m_expectation += line + "\n"; + m_expectation = parseSimpleExpectations(file); } TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted) diff --git a/test/libyul/YulInterpreterTest.cpp b/test/libyul/YulInterpreterTest.cpp index d473c3a5c..141dd1ae2 100644 --- a/test/libyul/YulInterpreterTest.cpp +++ b/test/libyul/YulInterpreterTest.cpp @@ -54,13 +54,7 @@ YulInterpreterTest::YulInterpreterTest(string const& _filename) file.exceptions(ios::badbit); m_source = parseSourceAndSettings(file); - - string line; - while (getline(file, line)) - if (boost::algorithm::starts_with(line, "// ")) - m_expectation += line.substr(3) + "\n"; - else - m_expectation += line + "\n"; + m_expectation = parseSimpleExpectations(file); } TestCase::TestResult YulInterpreterTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted) diff --git a/test/libyul/YulOptimizerTest.cpp b/test/libyul/YulOptimizerTest.cpp index e5552d675..3d2cd50eb 100644 --- a/test/libyul/YulOptimizerTest.cpp +++ b/test/libyul/YulOptimizerTest.cpp @@ -99,12 +99,7 @@ YulOptimizerTest::YulOptimizerTest(string const& _filename) m_settings.erase("step"); } - string line; - while (getline(file, line)) - if (boost::algorithm::starts_with(line, "// ")) - m_expectation += line.substr(3) + "\n"; - else - m_expectation += line + "\n"; + m_expectation = parseSimpleExpectations(file); } TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted) From ba8ad1a1d9a7c5f180c3d142251ba319085a6721 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Tue, 11 Jun 2019 14:11:27 +0200 Subject: [PATCH 2/2] Use the TestCase settings mechanism for object compiler tests. --- test/libyul/ObjectCompilerTest.cpp | 12 +++++------- test/libyul/objectCompiler/nested_optimizer.yul | 15 ++++++++------- test/libyul/objectCompiler/simple_optimizer.yul | 9 +++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/test/libyul/ObjectCompilerTest.cpp b/test/libyul/ObjectCompilerTest.cpp index de44cc67f..9773dedab 100644 --- a/test/libyul/ObjectCompilerTest.cpp +++ b/test/libyul/ObjectCompilerTest.cpp @@ -46,14 +46,12 @@ ObjectCompilerTest::ObjectCompilerTest(string const& _filename) BOOST_THROW_EXCEPTION(runtime_error("Cannot open test case: \"" + _filename + "\".")); file.exceptions(ios::badbit); - string line; - while (getline(file, line)) + m_source = parseSourceAndSettings(file); + if (m_settings.count("optimize")) { - if (boost::algorithm::starts_with(line, "// ----")) - break; - if (m_source.empty() && boost::algorithm::starts_with(line, "// optimize")) - m_optimize = true; - m_source += line + "\n"; + m_optimize = true; + m_validatedSettings["optimize"] = "true"; + m_settings.erase("optimize"); } m_expectation = parseSimpleExpectations(file); } diff --git a/test/libyul/objectCompiler/nested_optimizer.yul b/test/libyul/objectCompiler/nested_optimizer.yul index 2775c346d..1d5e568f1 100644 --- a/test/libyul/objectCompiler/nested_optimizer.yul +++ b/test/libyul/objectCompiler/nested_optimizer.yul @@ -1,4 +1,3 @@ -// optimize object "a" { code { let x := calldataload(0) @@ -15,24 +14,26 @@ object "a" { } } } +// ==== +// optimize: true // ---- // Assembly: -// /* "source":60:61 */ +// /* "source":48:49 */ // 0x00 // 0x00 -// /* "source":47:62 */ +// /* "source":35:50 */ // calldataload -// /* "source":119:139 */ +// /* "source":107:127 */ // sstore // stop // // sub_0: assembly { -// /* "source":200:201 */ +// /* "source":188:189 */ // 0x00 // 0x00 -// /* "source":187:202 */ +// /* "source":175:190 */ // calldataload -// /* "source":265:285 */ +// /* "source":253:273 */ // sstore // } // Bytecode: 600060003555fe diff --git a/test/libyul/objectCompiler/simple_optimizer.yul b/test/libyul/objectCompiler/simple_optimizer.yul index c757dee71..3d00e45d3 100644 --- a/test/libyul/objectCompiler/simple_optimizer.yul +++ b/test/libyul/objectCompiler/simple_optimizer.yul @@ -1,18 +1,19 @@ -// optimize { let x := calldataload(0) let y := calldataload(0) let z := sub(y, x) sstore(add(x, 0), z) } +// ==== +// optimize: true // ---- // Assembly: -// /* "source":38:39 */ +// /* "source":26:27 */ // 0x00 // 0x00 -// /* "source":25:40 */ +// /* "source":13:28 */ // calldataload -// /* "source":91:111 */ +// /* "source":79:99 */ // sstore // Bytecode: 600060003555 // Opcodes: PUSH1 0x0 PUSH1 0x0 CALLDATALOAD SSTORE