/* 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 . */ #include #include #include #include #include using namespace std; using namespace boost::unit_test::framework; using namespace boost::test_tools; using namespace solidity::util; namespace solidity::phaser::test { class DummyAlgorithm: public GeneticAlgorithm { public: using GeneticAlgorithm::GeneticAlgorithm; Population runNextRound(Population _population) override { ++m_currentRound; return _population; } size_t m_currentRound = 0; }; class AlgorithmRunnerFixture { protected: shared_ptr m_fitnessMetric = make_shared(); output_test_stream m_output; AlgorithmRunner::Options m_options; }; BOOST_AUTO_TEST_SUITE(Phaser) BOOST_AUTO_TEST_SUITE(AlgorithmRunnerTest) BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRunnerFixture) { m_options.maxRounds = 5; AlgorithmRunner runner(Population(m_fitnessMetric), m_options, m_output); DummyAlgorithm algorithm; BOOST_TEST(algorithm.m_currentRound == 0); runner.run(algorithm); BOOST_TEST(algorithm.m_currentRound == 5); runner.run(algorithm); BOOST_TEST(algorithm.m_currentRound == 10); } 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); BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 1); runner.run(algorithm); runner.run(algorithm); runner.run(algorithm); BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 4); } BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() }