2020-01-24 03:56:32 +00:00
|
|
|
/*
|
|
|
|
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 <tools/yulPhaser/Chromosome.h>
|
|
|
|
#include <tools/yulPhaser/Population.h>
|
2020-01-31 12:26:54 +00:00
|
|
|
#include <tools/yulPhaser/Program.h>
|
2020-01-24 03:56:32 +00:00
|
|
|
|
|
|
|
#include <libyul/optimiser/BlockFlattener.h>
|
|
|
|
#include <libyul/optimiser/SSAReverser.h>
|
|
|
|
#include <libyul/optimiser/StructuralSimplifier.h>
|
|
|
|
#include <libyul/optimiser/UnusedPruner.h>
|
|
|
|
|
|
|
|
#include <liblangutil/CharStream.h>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace boost::unit_test::framework;
|
|
|
|
|
|
|
|
namespace solidity::phaser::test
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool fitnessNotSet(Individual const& individual)
|
|
|
|
{
|
|
|
|
return !individual.fitness.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fitnessSet(Individual const& individual)
|
|
|
|
{
|
|
|
|
return individual.fitness.has_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:25:25 +00:00
|
|
|
class PopulationFixture
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
PopulationFixture():
|
|
|
|
m_sourceStream(SampleSourceCode, ""),
|
|
|
|
m_program(Program::load(m_sourceStream)) {}
|
|
|
|
|
|
|
|
static constexpr char SampleSourceCode[] =
|
|
|
|
"{\n"
|
|
|
|
" let factor := 13\n"
|
|
|
|
" {\n"
|
|
|
|
" if factor\n"
|
|
|
|
" {\n"
|
|
|
|
" let variable := add(1, 2)\n"
|
|
|
|
" }\n"
|
|
|
|
" let result := factor\n"
|
|
|
|
" }\n"
|
|
|
|
" let something := 6\n"
|
|
|
|
" {\n"
|
|
|
|
" {\n"
|
|
|
|
" {\n"
|
|
|
|
" let value := 15\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" let something_else := mul(mul(something, 1), add(factor, 0))\n"
|
|
|
|
" if 1 { let x := 1 }\n"
|
|
|
|
" if 0 { let y := 2 }\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
CharStream m_sourceStream;
|
|
|
|
Program m_program;
|
|
|
|
};
|
|
|
|
|
2020-01-24 03:56:32 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
|
|
|
BOOST_AUTO_TEST_SUITE(PopulationTest)
|
|
|
|
|
2020-02-13 21:25:25 +00:00
|
|
|
BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness, PopulationFixture)
|
2020-01-24 03:56:32 +00:00
|
|
|
{
|
|
|
|
vector<Chromosome> chromosomes = {
|
|
|
|
Chromosome::makeRandom(5),
|
|
|
|
Chromosome::makeRandom(10),
|
|
|
|
};
|
2020-02-13 21:25:25 +00:00
|
|
|
Population population(m_program, chromosomes);
|
2020-01-24 03:56:32 +00:00
|
|
|
|
|
|
|
BOOST_TEST(population.individuals().size() == 2);
|
|
|
|
BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]);
|
|
|
|
BOOST_TEST(population.individuals()[1].chromosome == chromosomes[1]);
|
|
|
|
|
|
|
|
auto fitnessNotSet = [](auto const& individual){ return !individual.fitness.has_value(); };
|
|
|
|
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:25:25 +00:00
|
|
|
BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes, PopulationFixture)
|
2020-01-24 03:56:32 +00:00
|
|
|
{
|
2020-02-13 21:25:25 +00:00
|
|
|
auto population1 = Population::makeRandom(m_program, 100);
|
|
|
|
auto population2 = Population::makeRandom(m_program, 100);
|
2020-01-24 03:56:32 +00:00
|
|
|
|
|
|
|
BOOST_TEST(population1.individuals().size() == 100);
|
|
|
|
BOOST_TEST(population2.individuals().size() == 100);
|
|
|
|
|
|
|
|
int numMatchingPositions = 0;
|
|
|
|
for (size_t i = 0; i < 100; ++i)
|
|
|
|
if (population1.individuals()[i].chromosome == population2.individuals()[i].chromosome)
|
|
|
|
++numMatchingPositions;
|
|
|
|
|
|
|
|
// Assume that the results are random if there are no more than 10 identical chromosomes on the
|
|
|
|
// same positions. One duplicate is very unlikely but still possible after billions of runs
|
|
|
|
// (especially for short chromosomes). For ten the probability is so small that we can ignore it.
|
|
|
|
BOOST_TEST(numMatchingPositions < 10);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:25:25 +00:00
|
|
|
BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture)
|
2020-01-24 03:56:32 +00:00
|
|
|
{
|
2020-02-13 21:25:25 +00:00
|
|
|
auto population = Population::makeRandom(m_program, 5);
|
2020-01-24 03:56:32 +00:00
|
|
|
|
|
|
|
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:25:25 +00:00
|
|
|
BOOST_FIXTURE_TEST_CASE(run_should_evaluate_fitness, PopulationFixture)
|
2020-01-24 03:56:32 +00:00
|
|
|
{
|
|
|
|
stringstream output;
|
2020-02-13 21:25:25 +00:00
|
|
|
auto population = Population::makeRandom(m_program, 5);
|
2020-01-24 03:56:32 +00:00
|
|
|
assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
|
|
|
|
|
|
|
population.run(1, output);
|
|
|
|
|
|
|
|
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessSet));
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:25:25 +00:00
|
|
|
BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, PopulationFixture)
|
2020-01-24 03:56:32 +00:00
|
|
|
{
|
|
|
|
stringstream output;
|
|
|
|
vector<Chromosome> chromosomes = {
|
2020-02-08 00:51:04 +00:00
|
|
|
Chromosome(vector<string>{StructuralSimplifier::name}),
|
|
|
|
Chromosome(vector<string>{BlockFlattener::name}),
|
|
|
|
Chromosome(vector<string>{SSAReverser::name}),
|
|
|
|
Chromosome(vector<string>{UnusedPruner::name}),
|
|
|
|
Chromosome(vector<string>{StructuralSimplifier::name, BlockFlattener::name}),
|
2020-01-24 03:56:32 +00:00
|
|
|
};
|
2020-02-13 21:25:25 +00:00
|
|
|
Population population(m_program, chromosomes);
|
2020-01-24 03:56:32 +00:00
|
|
|
|
|
|
|
size_t initialTopFitness[2] = {
|
2020-02-13 21:25:25 +00:00
|
|
|
Population::measureFitness(chromosomes[0], m_program),
|
|
|
|
Population::measureFitness(chromosomes[1], m_program),
|
2020-01-24 03:56:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; ++i)
|
|
|
|
{
|
|
|
|
population.run(1, output);
|
|
|
|
BOOST_TEST(population.individuals().size() == 5);
|
|
|
|
BOOST_TEST(fitnessSet(population.individuals()[0]));
|
|
|
|
BOOST_TEST(fitnessSet(population.individuals()[1]));
|
|
|
|
|
|
|
|
size_t currentTopFitness[2] = {
|
|
|
|
population.individuals()[0].fitness.value(),
|
|
|
|
population.individuals()[1].fitness.value(),
|
|
|
|
};
|
|
|
|
BOOST_TEST(currentTopFitness[0] <= initialTopFitness[0]);
|
|
|
|
BOOST_TEST(currentTopFitness[1] <= initialTopFitness[1]);
|
|
|
|
BOOST_TEST(currentTopFitness[0] <= currentTopFitness[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|