diff --git a/test/yulPhaser/AlgorithmRunner.cpp b/test/yulPhaser/AlgorithmRunner.cpp index 16a4dd893..0270e4b83 100644 --- a/test/yulPhaser/AlgorithmRunner.cpp +++ b/test/yulPhaser/AlgorithmRunner.cpp @@ -50,6 +50,7 @@ class AlgorithmRunnerFixture protected: shared_ptr m_fitnessMetric = make_shared(); output_test_stream m_output; + AlgorithmRunner::Options m_options; }; BOOST_AUTO_TEST_SUITE(Phaser) @@ -57,33 +58,38 @@ BOOST_AUTO_TEST_SUITE(AlgorithmRunnerTest) BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRunnerFixture) { - AlgorithmRunner runner(Population(m_fitnessMetric), m_output); + m_options.maxRounds = 5; + AlgorithmRunner runner(Population(m_fitnessMetric), m_options, m_output); DummyAlgorithm algorithm; BOOST_TEST(algorithm.m_currentRound == 0); - runner.run(algorithm, 10); + runner.run(algorithm); + BOOST_TEST(algorithm.m_currentRound == 5); + runner.run(algorithm); BOOST_TEST(algorithm.m_currentRound == 10); - runner.run(algorithm, 3); - BOOST_TEST(algorithm.m_currentRound == 13); } BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, AlgorithmRunnerFixture) { // run() is allowed to print more but should at least print the first one + m_options.maxRounds = 1; AlgorithmRunner runner( // NOTE: Chromosomes chosen so that they're not substrings of each other and are not // words likely to appear in the output in normal circumstances. Population(m_fitnessMetric, {Chromosome("fcCUnDve"), Chromosome("jsxIOo"), Chromosome("ighTLM")}), + m_options, m_output ); DummyAlgorithm algorithm; BOOST_TEST(m_output.is_empty()); - runner.run(algorithm, 1); + runner.run(algorithm); BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 1); - runner.run(algorithm, 3); + runner.run(algorithm); + runner.run(algorithm); + runner.run(algorithm); BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 4); } diff --git a/tools/yulPhaser/AlgorithmRunner.cpp b/tools/yulPhaser/AlgorithmRunner.cpp index 881f5b46e..bd5ce5755 100644 --- a/tools/yulPhaser/AlgorithmRunner.cpp +++ b/tools/yulPhaser/AlgorithmRunner.cpp @@ -20,9 +20,9 @@ using namespace std; using namespace solidity::phaser; -void AlgorithmRunner::run(GeneticAlgorithm& _algorithm, optional _numRounds) +void AlgorithmRunner::run(GeneticAlgorithm& _algorithm) { - for (size_t round = 0; !_numRounds.has_value() || round < _numRounds.value(); ++round) + for (size_t round = 0; !m_options.maxRounds.has_value() || round < m_options.maxRounds.value(); ++round) { m_population = _algorithm.runNextRound(m_population); diff --git a/tools/yulPhaser/AlgorithmRunner.h b/tools/yulPhaser/AlgorithmRunner.h index ff436d73b..c0aaa5621 100644 --- a/tools/yulPhaser/AlgorithmRunner.h +++ b/tools/yulPhaser/AlgorithmRunner.h @@ -39,19 +39,28 @@ namespace solidity::phaser class AlgorithmRunner { public: + struct Options + { + std::optional maxRounds = std::nullopt; + }; + AlgorithmRunner( Population _initialPopulation, + Options _options, std::ostream& _outputStream ): m_population(std::move(_initialPopulation)), + m_options(std::move(_options)), m_outputStream(_outputStream) {} - void run(GeneticAlgorithm& _algorithm, std::optional _numRounds = std::nullopt); + void run(GeneticAlgorithm& _algorithm); + Options const& options() const { return m_options; } Population const& population() const { return m_population; } private: Population m_population; + Options m_options; std::ostream& m_outputStream; }; diff --git a/tools/yulPhaser/Phaser.cpp b/tools/yulPhaser/Phaser.cpp index 0a3e7f66e..7708e85ba 100644 --- a/tools/yulPhaser/Phaser.cpp +++ b/tools/yulPhaser/Phaser.cpp @@ -182,7 +182,7 @@ void Phaser::runAlgorithm(string const& _sourcePath, Algorithm _algorithm) maxChromosomeLength ); - AlgorithmRunner algorithmRunner(population, cout); + AlgorithmRunner algorithmRunner(population, AlgorithmRunner::Options{}, cout); switch (_algorithm) { case Algorithm::Random: