From 325355eda6696b44e8d91b1ce5814bcb7700f242 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Wed, 16 Aug 2017 15:36:30 +0200 Subject: [PATCH] Separate input and output in doTest functions --- test/tools/jsontests/BlockChainTests.cpp | 25 +++++++++++++--------- test/tools/jsontests/StateTests.cpp | 8 ++++--- test/tools/jsontests/TransactionTests.cpp | 8 ++++--- test/tools/jsontests/vm.cpp | 8 ++++--- test/tools/libtesteth/TestHelper.cpp | 10 ++++----- test/tools/libtesteth/TestHelper.h | 18 +++++++++------- test/tools/libtesteth/TestOutputHelper.cpp | 2 +- test/tools/libtesteth/TestOutputHelper.h | 2 +- 8 files changed, 47 insertions(+), 34 deletions(-) diff --git a/test/tools/jsontests/BlockChainTests.cpp b/test/tools/jsontests/BlockChainTests.cpp index 4828b34fad3..e9607f657e9 100644 --- a/test/tools/jsontests/BlockChainTests.cpp +++ b/test/tools/jsontests/BlockChainTests.cpp @@ -106,16 +106,18 @@ void fillBCTest(json_spirit::mObject& _o); void testBCTest(json_spirit::mObject& _o); //percent output for many tests in one file -void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) +json_spirit::mValue doBlockchainTests(json_spirit::mValue const& _v, bool _fillin) { TestOutputHelper::initTest(_v); //Count how many tests in the json object (read from .json file) - doBlockchainTestNoLog(_v, _fillin); //Do the test / test generation + json_spirit::mValue ret = doBlockchainTestNoLog(_v, _fillin); //Do the test / test generation TestOutputHelper::finishTest(); //Calculate the time of test execution and add it to the log + return ret; } -void doTransitionTest(json_spirit::mValue& _v, bool _fillin) +json_spirit::mValue doTransitionTest(json_spirit::mValue const& _input, bool _fillin) { - for (auto& i: _v.get_obj()) + json_spirit::mValue output = _input; + for (auto& i: output.get_obj()) { string testname = i.first; json_spirit::mObject& o = i.second.get_obj(); @@ -139,15 +141,17 @@ void doTransitionTest(json_spirit::mValue& _v, bool _fillin) else testBCTest(o); } + return output; } -void doBlockchainTestNoLog(json_spirit::mValue& _v, bool _fillin) +json_spirit::mValue doBlockchainTestNoLog(json_spirit::mValue const& _input, bool _fillin) { + json_spirit::mValue v = _input; // TODO: avoid copying and add only valid fields to the new object. map tests; - vector erase_list; + vector erase_list; // range-for is not used because iterators are necessary for removing elements later. - for (auto i = _v.get_obj().begin(); i != _v.get_obj().end(); i++) + for (auto i = v.get_obj().begin(); i != v.get_obj().end(); i++) { string testname = i->first; json_spirit::mObject& o = i->second.get_obj(); @@ -227,16 +231,17 @@ void doBlockchainTestNoLog(json_spirit::mValue& _v, bool _fillin) //Delete source test from the json for (auto i: erase_list) - _v.get_obj().erase(i); + v.get_obj().erase(i); //Add generated tests to the result file if (_fillin) { - BOOST_CHECK_MESSAGE(_v.get_obj().size() == 0, " Test Filler is incorrect. Still having the test source when generating from filler " + TestOutputHelper::testName()); - json_spirit::mObject& obj = _v.get_obj(); + BOOST_CHECK_MESSAGE(v.get_obj().size() == 0, " Test Filler is incorrect. Still having the test source when generating from filler " + TestOutputHelper::testName()); + json_spirit::mObject& obj = v.get_obj(); for (auto& test : tests) obj[test.first] = test.second; } + return v; } void fillBCTest(json_spirit::mObject& _o) diff --git a/test/tools/jsontests/StateTests.cpp b/test/tools/jsontests/StateTests.cpp index 02844b5bc16..38a000c4b20 100644 --- a/test/tools/jsontests/StateTests.cpp +++ b/test/tools/jsontests/StateTests.cpp @@ -39,12 +39,13 @@ using namespace dev::eth; namespace dev { namespace test { -void doStateTests(json_spirit::mValue& _v, bool _fillin) +json_spirit::mValue doStateTests(json_spirit::mValue const& _input, bool _fillin) { - BOOST_REQUIRE_MESSAGE(!_fillin || _v.get_obj().size() == 1, + BOOST_REQUIRE_MESSAGE(!_fillin || _input.get_obj().size() == 1, TestOutputHelper::testFileName() + " A GeneralStateTest filler should contain only one test."); + json_spirit::mValue v = _input; // TODO: avoid copying and only add valid fields into the new object. - for (auto& i: _v.get_obj()) + for (auto& i: v.get_obj()) { string testname = i.first; json_spirit::mObject& o = i.second.get_obj(); @@ -103,6 +104,7 @@ void doStateTests(json_spirit::mValue& _v, bool _fillin) importer.traceStateDiff(); } } + return v; } } }// Namespace Close diff --git a/test/tools/jsontests/TransactionTests.cpp b/test/tools/jsontests/TransactionTests.cpp index 8dafc4360b3..f16252cc827 100644 --- a/test/tools/jsontests/TransactionTests.cpp +++ b/test/tools/jsontests/TransactionTests.cpp @@ -34,11 +34,12 @@ using namespace dev::eth; namespace dev { namespace test { -void doTransactionTests(json_spirit::mValue& _v, bool _fillin) +json_spirit::mValue doTransactionTests(json_spirit::mValue const& _input, bool _fillin) { - TestOutputHelper::initTest(_v); + json_spirit::mValue v = _input; // TODO: avoid copying and only add valid fields into the new object. + TestOutputHelper::initTest(v); unique_ptr se(ChainParams(genesisInfo(eth::Network::MainNetworkTest)).createSealEngine()); - for (auto& i: _v.get_obj()) + for (auto& i: v.get_obj()) { string testname = i.first; json_spirit::mObject& o = i.second.get_obj(); @@ -159,6 +160,7 @@ void doTransactionTests(json_spirit::mValue& _v, bool _fillin) } }//for dev::test::TestOutputHelper::finishTest(); + return v; }//doTransactionTests } }// Namespace Close diff --git a/test/tools/jsontests/vm.cpp b/test/tools/jsontests/vm.cpp index 92674666eed..5071e34df59 100644 --- a/test/tools/jsontests/vm.cpp +++ b/test/tools/jsontests/vm.cpp @@ -292,12 +292,13 @@ eth::OnOpFunc FakeExtVM::simpleTrace() const namespace dev { namespace test { -void doVMTests(json_spirit::mValue& _v, bool _fillin) +json_spirit::mValue doVMTests(json_spirit::mValue const& _input, bool _fillin) { + json_spirit::mValue v = _input; // TODO: avoid copying and only add valid fields into the new object. if (string(boost::unit_test::framework::current_test_case().p_name) != "vmRandom") - TestOutputHelper::initTest(_v); + TestOutputHelper::initTest(v); - for (auto& i: _v.get_obj()) + for (auto& i: v.get_obj()) { string testname = i.first; json_spirit::mObject& o = i.second.get_obj(); @@ -470,6 +471,7 @@ void doVMTests(json_spirit::mValue& _v, bool _fillin) } TestOutputHelper::finishTest(); + return v; } } } // namespace close diff --git a/test/tools/libtesteth/TestHelper.cpp b/test/tools/libtesteth/TestHelper.cpp index 1ea639a3f47..81650cf4019 100644 --- a/test/tools/libtesteth/TestHelper.cpp +++ b/test/tools/libtesteth/TestHelper.cpp @@ -421,7 +421,7 @@ void checkCallCreates(eth::Transactions _resultCallCreates, eth::Transactions _e } } -void userDefinedTest(std::function doTests) +void userDefinedTest(std::function doTests) { if (!Options::get().singleTest) return; @@ -469,7 +469,7 @@ void userDefinedTest(std::function doTests) } } -void executeTests(const string& _name, const string& _testPathAppendix, const string& _fillerPathAppendix, std::function doTests, bool _addFillerSuffix) +void executeTests(const string& _name, const string& _testPathAppendix, const string& _fillerPathAppendix, std::function doTests, bool _addFillerSuffix) { string testPath = getTestPath() + _testPathAppendix; string testFillerPath = getTestPath() + "/src" + _fillerPathAppendix; @@ -500,9 +500,9 @@ void executeTests(const string& _name, const string& _testPathAppendix, const st json_spirit::read_string(s, v); removeComments(v); - doTests(v, true); - addClientInfo(v, testfilename); - writeFile(testPath + "/" + name + ".json", asBytes(json_spirit::write_string(v, true))); + json_spirit::mValue output = doTests(v, true); + addClientInfo(output, testfilename); + writeFile(testPath + "/" + name + ".json", asBytes(json_spirit::write_string(output, true))); } catch (Exception const& _e) { diff --git a/test/tools/libtesteth/TestHelper.h b/test/tools/libtesteth/TestHelper.h index bc0d7484349..3bf3137f76f 100644 --- a/test/tools/libtesteth/TestHelper.h +++ b/test/tools/libtesteth/TestHelper.h @@ -157,8 +157,8 @@ dev::eth::BlockHeader constructHeader( u256 const& _timestamp, bytes const& _extraData); void updateEthashSeal(dev::eth::BlockHeader& _header, h256 const& _mixHash, dev::eth::Nonce const& _nonce); -void executeTests(const std::string& _name, const std::string& _testPathAppendix, const std::string& _fillerPathAppendix, std::function doTests, bool _addFillerSuffix = true); -void userDefinedTest(std::function doTests); +void executeTests(const std::string& _name, const std::string& _testPathAppendix, const std::string& _fillerPathAppendix, std::function doTests, bool _addFillerSuffix = true); +void userDefinedTest(std::function doTests); RLPStream createRLPStreamFromTransactionFields(json_spirit::mObject const& _tObj); json_spirit::mObject fillJsonWithStateChange(eth::State const& _stateOrig, eth::State const& _statePost, eth::ChangeLog const& _changeLog); json_spirit::mObject fillJsonWithState(eth::State const& _state); @@ -167,12 +167,14 @@ json_spirit::mObject fillJsonWithTransaction(eth::Transaction const& _txn); //Fill Test Functions int createRandomTest(std::vector const& _parameters); -void doTransactionTests(json_spirit::mValue& _v, bool _fillin); -void doStateTests(json_spirit::mValue& v, bool _fillin); -void doVMTests(json_spirit::mValue& v, bool _fillin); -void doBlockchainTests(json_spirit::mValue& _v, bool _fillin); -void doBlockchainTestNoLog(json_spirit::mValue& _v, bool _fillin); -void doTransitionTest(json_spirit::mValue& _v, bool _fillin); +//do*Tests(_input, _fillin) always return a filled test. +//When _fillin is true, _input is supposed to contain a filler. Otherwise, _input is also a filled test. +json_spirit::mValue doTransactionTests(json_spirit::mValue const& _input, bool _fillin); +json_spirit::mValue doStateTests(json_spirit::mValue const& _input, bool _fillin); +json_spirit::mValue doVMTests(json_spirit::mValue const& _input, bool _fillin); +json_spirit::mValue doBlockchainTests(json_spirit::mValue const& _input, bool _fillin); +json_spirit::mValue doBlockchainTestNoLog(json_spirit::mValue const& _input, bool _fillin); +json_spirit::mValue doTransitionTest(json_spirit::mValue const& _input, bool _fillin); void doRlpTests(json_spirit::mValue& v, bool _fillin); void addClientInfo(json_spirit::mValue& v, std::string const& _testSource); void removeComments(json_spirit::mValue& _obj); diff --git a/test/tools/libtesteth/TestOutputHelper.cpp b/test/tools/libtesteth/TestOutputHelper.cpp index f171a8459e0..dd6d9fe2fb7 100644 --- a/test/tools/libtesteth/TestOutputHelper.cpp +++ b/test/tools/libtesteth/TestOutputHelper.cpp @@ -49,7 +49,7 @@ void TestOutputHelper::initTest(int _maxTests) m_currTest = 0; } -void TestOutputHelper::initTest(json_spirit::mValue& _v) +void TestOutputHelper::initTest(json_spirit::mValue const& _v) { Ethash::init(); BasicAuthority::init(); diff --git a/test/tools/libtesteth/TestOutputHelper.h b/test/tools/libtesteth/TestOutputHelper.h index 58578fac99f..603c9c6d5c9 100644 --- a/test/tools/libtesteth/TestOutputHelper.h +++ b/test/tools/libtesteth/TestOutputHelper.h @@ -32,7 +32,7 @@ class TestOutputHelper public: TestOutputHelper() { TestOutputHelper::initTest(); } static void initTest(int _maxTests = 1); - static void initTest(json_spirit::mValue& _v); + static void initTest(json_spirit::mValue const& _v); static bool passTest(std::string const& _testName); static void setMaxTests(int _count) { m_maxTests = _count; } static void setCurrentTestFileName(std::string const& _name) { m_currentTestFileName = _name; }