Merge pull request #8421 from imapp-pl/yul-phaser-refactor-main

[yul-phaser] Refactoring in main
This commit is contained in:
chriseth
2020-03-16 16:31:26 +01:00
committed by GitHub
23 changed files with 930 additions and 409 deletions
+5 -2
View File
@@ -139,11 +139,13 @@ set(libyul_sources
detect_stray_source_files("${libyul_sources}" "libyul/")
set(yul_phaser_sources
yulPhaser/Common.h
yulPhaser/TestHelpers.h
yulPhaser/TestHelpers.cpp
yulPhaser/TestHelpersTest.cpp
yulPhaser/Common.cpp
yulPhaser/CommonTest.cpp
yulPhaser/Chromosome.cpp
yulPhaser/FitnessMetrics.cpp
yulPhaser/AlgorithmRunner.cpp
yulPhaser/GeneticAlgorithms.cpp
yulPhaser/Mutations.cpp
yulPhaser/PairSelections.cpp
@@ -155,6 +157,7 @@ set(yul_phaser_sources
# FIXME: yul-phaser is not a library so I can't just add it to target_link_libraries().
# My current workaround is just to include its source files here but this introduces
# unnecessary duplication. Create a library or find a way to reuse the list in both places.
../tools/yulPhaser/AlgorithmRunner.cpp
../tools/yulPhaser/Chromosome.cpp
../tools/yulPhaser/FitnessMetrics.cpp
../tools/yulPhaser/GeneticAlgorithms.cpp
+99
View File
@@ -0,0 +1,99 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/AlgorithmRunner.h>
#include <libsolutil/CommonIO.h>
#include <boost/test/unit_test.hpp>
#include <boost/test/tools/output_test_stream.hpp>
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<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
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()
}
+1 -1
View File
@@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/Chromosome.h>
#include <tools/yulPhaser/SimulationRNG.h>
+85 -49
View File
@@ -15,79 +15,115 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <tools/yulPhaser/Common.h>
#include <libyul/optimiser/Suite.h>
#include <libsolutil/CommonData.h>
#include <regex>
#include <boost/test/unit_test.hpp>
#include <boost/test/tools/output_test_stream.hpp>
#include <sstream>
#include <string>
using namespace std;
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::phaser;
using namespace boost::test_tools;
using namespace solidity::util;
function<Mutation> phaser::test::wholeChromosomeReplacement(Chromosome _newChromosome)
namespace solidity::phaser::test
{
return [_newChromosome = move(_newChromosome)](Chromosome const&) { return _newChromosome; };
namespace
{
enum class TestEnum
{
A,
B,
AB,
CD,
EF,
GH,
};
map<TestEnum, string> const TestEnumToStringMap =
{
{TestEnum::A, "a"},
{TestEnum::B, "b"},
{TestEnum::AB, "a b"},
{TestEnum::CD, "c-d"},
{TestEnum::EF, "e f"},
};
map<string, TestEnum> const StringToTestEnumMap = invertMap(TestEnumToStringMap);
}
function<Mutation> phaser::test::geneSubstitution(size_t _geneIndex, string _geneValue)
{
return [=](Chromosome const& _chromosome)
{
vector<string> newGenes = _chromosome.optimisationSteps();
assert(_geneIndex < newGenes.size());
newGenes[_geneIndex] = _geneValue;
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(CommonTest)
return Chromosome(newGenes);
};
BOOST_AUTO_TEST_CASE(deserializeChoice_should_convert_string_to_enum)
{
istringstream aStream("a");
TestEnum aResult;
deserializeChoice(aStream, aResult, StringToTestEnumMap);
BOOST_CHECK(aResult == TestEnum::A);
BOOST_TEST(!aStream.fail());
istringstream bStream("b");
TestEnum bResult;
deserializeChoice(bStream, bResult, StringToTestEnumMap);
BOOST_CHECK(bResult == TestEnum::B);
BOOST_TEST(!bStream.fail());
istringstream cdStream("c-d");
TestEnum cdResult;
deserializeChoice(cdStream, cdResult, StringToTestEnumMap);
BOOST_CHECK(cdResult == TestEnum::CD);
BOOST_TEST(!cdStream.fail());
}
vector<size_t> phaser::test::chromosomeLengths(Population const& _population)
BOOST_AUTO_TEST_CASE(deserializeChoice_should_set_failbit_if_there_is_no_enum_corresponding_to_string)
{
vector<size_t> lengths;
for (auto const& individual: _population.individuals())
lengths.push_back(individual.chromosome.length());
return lengths;
istringstream xyzStream("xyz");
TestEnum xyzResult;
deserializeChoice(xyzStream, xyzResult, StringToTestEnumMap);
BOOST_TEST(xyzStream.fail());
}
map<string, size_t> phaser::test::enumerateOptmisationSteps()
BOOST_AUTO_TEST_CASE(deserializeChoice_does_not_have_to_support_strings_with_spaces)
{
map<string, size_t> stepIndices;
size_t i = 0;
for (auto const& nameAndAbbreviation: OptimiserSuite::stepNameToAbbreviationMap())
stepIndices.insert({nameAndAbbreviation.first, i++});
istringstream abStream("a b");
TestEnum abResult;
deserializeChoice(abStream, abResult, StringToTestEnumMap);
BOOST_CHECK(abResult == TestEnum::A);
BOOST_TEST(!abStream.fail());
return stepIndices;
istringstream efStream("e f");
TestEnum efResult;
deserializeChoice(efStream, efResult, StringToTestEnumMap);
BOOST_TEST(efStream.fail());
}
size_t phaser::test::countDifferences(Chromosome const& _chromosome1, Chromosome const& _chromosome2)
BOOST_AUTO_TEST_CASE(serializeChoice_should_convert_enum_to_string)
{
size_t count = 0;
for (size_t i = 0; i < min(_chromosome1.length(), _chromosome2.length()); ++i)
count += static_cast<int>(_chromosome1.optimisationSteps()[i] != _chromosome2.optimisationSteps()[i]);
output_test_stream output;
return count + abs(static_cast<int>(_chromosome1.length() - _chromosome2.length()));
serializeChoice(output, TestEnum::A, TestEnumToStringMap);
BOOST_CHECK(output.is_equal("a"));
BOOST_TEST(!output.fail());
serializeChoice(output, TestEnum::AB, TestEnumToStringMap);
BOOST_CHECK(output.is_equal("a b"));
BOOST_TEST(!output.fail());
}
string phaser::test::stripWhitespace(string const& input)
BOOST_AUTO_TEST_CASE(serializeChoice_should_set_failbit_if_there_is_no_string_corresponding_to_enum)
{
regex whitespaceRegex("\\s+");
return regex_replace(input, whitespaceRegex, "");
output_test_stream output;
serializeChoice(output, TestEnum::GH, TestEnumToStringMap);
BOOST_TEST(output.fail());
}
size_t phaser::test::countSubstringOccurrences(string const& _inputString, string const& _substring)
{
assert(_substring.size() > 0);
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
size_t count = 0;
size_t lastOccurrence = 0;
while ((lastOccurrence = _inputString.find(_substring, lastOccurrence)) != string::npos)
{
++count;
lastOccurrence += _substring.size();
}
return count;
}
+33 -81
View File
@@ -15,19 +15,15 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/FitnessMetrics.h>
#include <tools/yulPhaser/GeneticAlgorithms.h>
#include <tools/yulPhaser/Population.h>
#include <tools/yulPhaser/Program.h>
#include <liblangutil/CharStream.h>
#include <libsolutil/CommonIO.h>
#include <boost/test/unit_test.hpp>
#include <boost/test/tools/output_test_stream.hpp>
#include <algorithm>
#include <vector>
@@ -35,102 +31,58 @@
using namespace std;
using namespace boost::unit_test::framework;
using namespace boost::test_tools;
using namespace solidity::langutil;
using namespace solidity::util;
namespace solidity::phaser::test
{
class DummyAlgorithm: public GeneticAlgorithm
{
public:
using GeneticAlgorithm::GeneticAlgorithm;
void runNextRound() override { ++m_currentRound; }
size_t m_currentRound = 0;
};
class GeneticAlgorithmFixture
{
protected:
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
output_test_stream m_output;
};
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(GeneticAlgorithmsTest)
BOOST_AUTO_TEST_SUITE(GeneticAlgorithmTest)
BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, GeneticAlgorithmFixture)
{
DummyAlgorithm algorithm(Population(m_fitnessMetric), m_output);
BOOST_TEST(algorithm.m_currentRound == 0);
algorithm.run(10);
BOOST_TEST(algorithm.m_currentRound == 10);
algorithm.run(3);
BOOST_TEST(algorithm.m_currentRound == 13);
}
BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, GeneticAlgorithmFixture)
{
// run() is allowed to print more but should at least print the first one
DummyAlgorithm algorithm(
// 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_output
);
BOOST_TEST(m_output.is_empty());
algorithm.run(1);
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(algorithm.population().individuals()[0].chromosome)) == 1);
algorithm.run(3);
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(algorithm.population().individuals()[0].chromosome)) == 4);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(RandomAlgorithmTest)
BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_randomise_rest_of_population, GeneticAlgorithmFixture)
{
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
RandomAlgorithm algorithm(population, m_output, {0.5, 1, 1});
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
RandomAlgorithm algorithm({0.5, 1, 1});
algorithm.runNextRound();
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{1, 1, 1, 1, 3, 3, 3, 3}));
Population newPopulation = algorithm.runNextRound(population);
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{1, 1, 1, 1, 3, 3, 3, 3}));
}
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individuals, GeneticAlgorithmFixture)
{
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
RandomAlgorithm algorithm(population, m_output, {0.5, 7, 7});
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
RandomAlgorithm algorithm({0.5, 7, 7});
algorithm.runNextRound();
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 7, 7, 7, 7}));
Population newPopulation = algorithm.runNextRound(population);
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{3, 3, 3, 3, 7, 7, 7, 7}));
}
BOOST_FIXTURE_TEST_CASE(runNextRound_should_replace_all_chromosomes_if_zero_size_elite, GeneticAlgorithmFixture)
{
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
RandomAlgorithm algorithm(population, m_output, {0.0, 1, 1});
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
RandomAlgorithm algorithm({0.0, 1, 1});
algorithm.runNextRound();
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{1, 1, 1, 1, 1, 1, 1, 1}));
Population newPopulation = algorithm.runNextRound(population);
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{1, 1, 1, 1, 1, 1, 1, 1}));
}
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_any_chromosomes_if_whole_population_is_the_elite, GeneticAlgorithmFixture)
{
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
RandomAlgorithm algorithm(population, m_output, {1.0, 1, 1});
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
RandomAlgorithm algorithm({1.0, 1, 1});
algorithm.runNextRound();
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
Population newPopulation = algorithm.runNextRound(population);
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
}
BOOST_AUTO_TEST_SUITE_END()
@@ -139,6 +91,7 @@ BOOST_AUTO_TEST_SUITE(GenerationalElitistWithExclusivePoolsTest)
BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_regenerate_rest_of_population, GeneticAlgorithmFixture)
{
auto population = Population::makeRandom(m_fitnessMetric, 6, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
assert((chromosomeLengths(population) == vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
GenerationalElitistWithExclusivePools::Options options = {
/* mutationPoolSize = */ 0.2,
@@ -148,17 +101,17 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_regenerate_rest_o
/* percentGenesToRandomise = */ 0.0,
/* percentGenesToAddOrDelete = */ 1.0,
};
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
GenerationalElitistWithExclusivePools algorithm(options);
algorithm.runNextRound();
Population newPopulation = algorithm.runNextRound(population);
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{0, 0, 3, 3, 3, 3, 3, 3, 3, 3}));
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{0, 0, 3, 3, 3, 3, 3, 3, 3, 3}));
}
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individuals, GeneticAlgorithmFixture)
{
auto population = Population::makeRandom(m_fitnessMetric, 6, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
assert(chromosomeLengths(population) == (vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
GenerationalElitistWithExclusivePools::Options options = {
/* mutationPoolSize = */ 0.2,
@@ -168,12 +121,11 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individ
/* percentGenesToRandomise = */ 0.0,
/* percentGenesToAddOrDelete = */ 1.0,
};
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
assert(chromosomeLengths(algorithm.population()) == (vector<size_t>{3, 3, 3, 3, 3, 3, 5, 5, 5, 5}));
GenerationalElitistWithExclusivePools algorithm(options);
algorithm.runNextRound();
Population newPopulation = algorithm.runNextRound(population);
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 3, 3, 3, 3, 7, 7}));
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{3, 3, 3, 3, 3, 3, 3, 3, 7, 7}));
}
BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossover_pool_by_mutating_the_elite, GeneticAlgorithmFixture)
@@ -188,13 +140,13 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove
/* percentGenesToRandomise = */ 1.0,
/* percentGenesToAddOrDelete = */ 1.0,
};
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
GenerationalElitistWithExclusivePools algorithm(options);
SimulationRNG::reset(1);
algorithm.runNextRound();
Population newPopulation = algorithm.runNextRound(population);
BOOST_TEST((
chromosomeLengths(algorithm.population()) ==
chromosomeLengths(newPopulation) ==
vector<size_t>{0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 11, 11, 11}
));
}
@@ -205,6 +157,7 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove
Population(m_fitnessMetric, {Chromosome("aa"), Chromosome("ff")}) +
Population::makeRandom(m_fitnessMetric, 8, 6, 6)
);
assert((chromosomeLengths(population) == vector<size_t>{2, 2, 6, 6, 6, 6, 6, 6, 6, 6}));
GenerationalElitistWithExclusivePools::Options options = {
/* mutationPoolSize = */ 0.0,
@@ -214,14 +167,13 @@ BOOST_FIXTURE_TEST_CASE(runNextRound_should_generate_individuals_in_the_crossove
/* percentGenesToRandomise = */ 0.0,
/* percentGenesToAddOrDelete = */ 0.0,
};
GenerationalElitistWithExclusivePools algorithm(population, m_output, options);
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{2, 2, 6, 6, 6, 6, 6, 6, 6, 6}));
GenerationalElitistWithExclusivePools algorithm(options);
SimulationRNG::reset(1);
algorithm.runNextRound();
Population newPopulation = algorithm.runNextRound(population);
vector<Individual> const& newIndividuals = algorithm.population().individuals();
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}));
vector<Individual> const& newIndividuals = newPopulation.individuals();
BOOST_TEST((chromosomeLengths(newPopulation) == vector<size_t>{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}));
for (auto& individual: newIndividuals)
BOOST_TEST((
individual.chromosome == Chromosome("aa") ||
+1 -1
View File
@@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/Mutations.h>
+1 -1
View File
@@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/PairSelections.h>
#include <tools/yulPhaser/SimulationRNG.h>
+1 -1
View File
@@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/Chromosome.h>
#include <tools/yulPhaser/PairSelections.h>
+1 -1
View File
@@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/Exceptions.h>
#include <tools/yulPhaser/Program.h>
+1 -1
View File
@@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/Selections.h>
#include <tools/yulPhaser/SimulationRNG.h>
+1 -1
View File
@@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <tools/yulPhaser/SimulationRNG.h>
+93
View File
@@ -0,0 +1,93 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/TestHelpers.h>
#include <libyul/optimiser/Suite.h>
#include <regex>
using namespace std;
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::phaser;
function<Mutation> phaser::test::wholeChromosomeReplacement(Chromosome _newChromosome)
{
return [_newChromosome = move(_newChromosome)](Chromosome const&) { return _newChromosome; };
}
function<Mutation> phaser::test::geneSubstitution(size_t _geneIndex, string _geneValue)
{
return [=](Chromosome const& _chromosome)
{
vector<string> newGenes = _chromosome.optimisationSteps();
assert(_geneIndex < newGenes.size());
newGenes[_geneIndex] = _geneValue;
return Chromosome(newGenes);
};
}
vector<size_t> phaser::test::chromosomeLengths(Population const& _population)
{
vector<size_t> lengths;
for (auto const& individual: _population.individuals())
lengths.push_back(individual.chromosome.length());
return lengths;
}
map<string, size_t> phaser::test::enumerateOptmisationSteps()
{
map<string, size_t> stepIndices;
size_t i = 0;
for (auto const& nameAndAbbreviation: OptimiserSuite::stepNameToAbbreviationMap())
stepIndices.insert({nameAndAbbreviation.first, i++});
return stepIndices;
}
size_t phaser::test::countDifferences(Chromosome const& _chromosome1, Chromosome const& _chromosome2)
{
size_t count = 0;
for (size_t i = 0; i < min(_chromosome1.length(), _chromosome2.length()); ++i)
count += static_cast<int>(_chromosome1.optimisationSteps()[i] != _chromosome2.optimisationSteps()[i]);
return count + abs(static_cast<int>(_chromosome1.length() - _chromosome2.length()));
}
string phaser::test::stripWhitespace(string const& input)
{
regex whitespaceRegex("\\s+");
return regex_replace(input, whitespaceRegex, "");
}
size_t phaser::test::countSubstringOccurrences(string const& _inputString, string const& _substring)
{
assert(_substring.size() > 0);
size_t count = 0;
size_t lastOccurrence = 0;
while ((lastOccurrence = _inputString.find(_substring, lastOccurrence)) != string::npos)
{
++count;
lastOccurrence += _substring.size();
}
return count;
}
@@ -15,7 +15,7 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <test/yulPhaser/TestHelpers.h>
#include <libyul/optimiser/Suite.h>
@@ -31,7 +31,7 @@ namespace solidity::phaser::test
{
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(CommonTest)
BOOST_AUTO_TEST_SUITE(TestHelpersTest)
BOOST_AUTO_TEST_CASE(ChromosomeLengthMetric_evaluate_should_return_chromosome_length)
{