mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] AlgorithmRunner: Population autosave
This commit is contained in:
@@ -18,9 +18,11 @@
|
||||
#include <test/yulPhaser/TestHelpers.h>
|
||||
|
||||
#include <tools/yulPhaser/AlgorithmRunner.h>
|
||||
#include <tools/yulPhaser/Common.h>
|
||||
|
||||
#include <libsolutil/CommonIO.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/tools/output_test_stream.hpp>
|
||||
|
||||
@@ -29,10 +31,12 @@ using namespace boost::unit_test::framework;
|
||||
using namespace boost::test_tools;
|
||||
using namespace solidity::util;
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
class DummyAlgorithm: public GeneticAlgorithm
|
||||
class CountingAlgorithm: public GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
using GeneticAlgorithm::GeneticAlgorithm;
|
||||
@@ -45,6 +49,16 @@ public:
|
||||
size_t m_currentRound = 0;
|
||||
};
|
||||
|
||||
class RandomisingAlgorithm: public GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
using GeneticAlgorithm::GeneticAlgorithm;
|
||||
Population runNextRound(Population _population) override
|
||||
{
|
||||
return Population::makeRandom(_population.fitnessMetric(), _population.individuals().size(), 10, 20);
|
||||
}
|
||||
};
|
||||
|
||||
class AlgorithmRunnerFixture
|
||||
{
|
||||
protected:
|
||||
@@ -53,6 +67,25 @@ protected:
|
||||
AlgorithmRunner::Options m_options;
|
||||
};
|
||||
|
||||
class AlgorithmRunnerAutosaveFixture: public AlgorithmRunnerFixture
|
||||
{
|
||||
public:
|
||||
static vector<string> chromosomeStrings(Population const& _population)
|
||||
{
|
||||
vector<string> lines;
|
||||
for (auto const& individual: _population.individuals())
|
||||
lines.push_back(toString(individual.chromosome));
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
protected:
|
||||
TemporaryDirectory m_tempDir;
|
||||
string const m_autosavePath = m_tempDir.memberPath("population-autosave.txt");
|
||||
Population const m_population = Population::makeRandom(m_fitnessMetric, 5, 0, 20);
|
||||
RandomisingAlgorithm m_algorithm;
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(AlgorithmRunnerTest)
|
||||
|
||||
@@ -60,7 +93,8 @@ BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, AlgorithmRu
|
||||
{
|
||||
m_options.maxRounds = 5;
|
||||
AlgorithmRunner runner(Population(m_fitnessMetric), m_options, m_output);
|
||||
DummyAlgorithm algorithm;
|
||||
|
||||
CountingAlgorithm algorithm;
|
||||
|
||||
BOOST_TEST(algorithm.m_currentRound == 0);
|
||||
runner.run(algorithm);
|
||||
@@ -82,7 +116,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, AlgorithmRunnerFixt
|
||||
m_output
|
||||
);
|
||||
|
||||
DummyAlgorithm algorithm;
|
||||
CountingAlgorithm algorithm;
|
||||
|
||||
BOOST_TEST(m_output.is_empty());
|
||||
runner.run(algorithm);
|
||||
@@ -93,6 +127,67 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, AlgorithmRunnerFixt
|
||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(runner.population().individuals()[0].chromosome)) == 4);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_save_initial_population_to_file_if_autosave_file_specified, AlgorithmRunnerAutosaveFixture)
|
||||
{
|
||||
m_options.maxRounds = 0;
|
||||
m_options.populationAutosaveFile = m_autosavePath;
|
||||
AlgorithmRunner runner(m_population, m_options, m_output);
|
||||
assert(!fs::exists(m_autosavePath));
|
||||
|
||||
runner.run(m_algorithm);
|
||||
assert(runner.population() == m_population);
|
||||
|
||||
BOOST_TEST(fs::is_regular_file(m_autosavePath));
|
||||
BOOST_TEST(readLinesFromFile(m_autosavePath) == chromosomeStrings(runner.population()));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_save_population_to_file_if_autosave_file_specified, AlgorithmRunnerAutosaveFixture)
|
||||
{
|
||||
m_options.maxRounds = 1;
|
||||
m_options.populationAutosaveFile = m_autosavePath;
|
||||
AlgorithmRunner runner(m_population, m_options, m_output);
|
||||
assert(!fs::exists(m_autosavePath));
|
||||
|
||||
runner.run(m_algorithm);
|
||||
assert(runner.population() != m_population);
|
||||
|
||||
BOOST_TEST(fs::is_regular_file(m_autosavePath));
|
||||
BOOST_TEST(readLinesFromFile(m_autosavePath) == chromosomeStrings(runner.population()));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_overwrite_existing_file_if_autosave_file_specified, AlgorithmRunnerAutosaveFixture)
|
||||
{
|
||||
m_options.maxRounds = 5;
|
||||
m_options.populationAutosaveFile = m_autosavePath;
|
||||
AlgorithmRunner runner(m_population, m_options, m_output);
|
||||
assert(!fs::exists(m_autosavePath));
|
||||
|
||||
vector<string> originalContent = {"Original content"};
|
||||
{
|
||||
ofstream tmpFile(m_autosavePath);
|
||||
tmpFile << originalContent[0] << endl;
|
||||
}
|
||||
assert(fs::exists(m_autosavePath));
|
||||
assert(readLinesFromFile(m_autosavePath) == originalContent);
|
||||
|
||||
runner.run(m_algorithm);
|
||||
|
||||
BOOST_TEST(fs::is_regular_file(m_autosavePath));
|
||||
BOOST_TEST(readLinesFromFile(m_autosavePath) != originalContent);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_not_save_population_to_file_if_autosave_file_not_specified, AlgorithmRunnerAutosaveFixture)
|
||||
{
|
||||
m_options.maxRounds = 5;
|
||||
m_options.populationAutosaveFile = nullopt;
|
||||
AlgorithmRunner runner(m_population, m_options, m_output);
|
||||
assert(!fs::exists(m_autosavePath));
|
||||
|
||||
runner.run(m_algorithm);
|
||||
|
||||
BOOST_TEST(!fs::exists(m_autosavePath));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user