mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Population: Extract Program construction in tests into a fixture
This commit is contained in:
parent
d22c59aa0e
commit
e771f00971
@ -53,10 +53,14 @@ namespace
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
class PopulationFixture
|
||||||
BOOST_AUTO_TEST_SUITE(PopulationTest)
|
{
|
||||||
|
protected:
|
||||||
|
PopulationFixture():
|
||||||
|
m_sourceStream(SampleSourceCode, ""),
|
||||||
|
m_program(Program::load(m_sourceStream)) {}
|
||||||
|
|
||||||
string const& sampleSourceCode =
|
static constexpr char SampleSourceCode[] =
|
||||||
"{\n"
|
"{\n"
|
||||||
" let factor := 13\n"
|
" let factor := 13\n"
|
||||||
" {\n"
|
" {\n"
|
||||||
@ -79,14 +83,20 @@ string const& sampleSourceCode =
|
|||||||
" if 0 { let y := 2 }\n"
|
" if 0 { let y := 2 }\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness)
|
CharStream m_sourceStream;
|
||||||
|
Program m_program;
|
||||||
|
};
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||||
|
BOOST_AUTO_TEST_SUITE(PopulationTest)
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness, PopulationFixture)
|
||||||
{
|
{
|
||||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
|
||||||
vector<Chromosome> chromosomes = {
|
vector<Chromosome> chromosomes = {
|
||||||
Chromosome::makeRandom(5),
|
Chromosome::makeRandom(5),
|
||||||
Chromosome::makeRandom(10),
|
Chromosome::makeRandom(10),
|
||||||
};
|
};
|
||||||
Population population(Program::load(sourceStream), chromosomes);
|
Population population(m_program, chromosomes);
|
||||||
|
|
||||||
BOOST_TEST(population.individuals().size() == 2);
|
BOOST_TEST(population.individuals().size() == 2);
|
||||||
BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]);
|
BOOST_TEST(population.individuals()[0].chromosome == chromosomes[0]);
|
||||||
@ -96,12 +106,10 @@ BOOST_AUTO_TEST_CASE(constructor_should_copy_chromosomes_and_not_compute_fitness
|
|||||||
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes)
|
BOOST_FIXTURE_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes, PopulationFixture)
|
||||||
{
|
{
|
||||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
auto population1 = Population::makeRandom(m_program, 100);
|
||||||
auto program = Program::load(sourceStream);
|
auto population2 = Population::makeRandom(m_program, 100);
|
||||||
auto population1 = Population::makeRandom(program, 100);
|
|
||||||
auto population2 = Population::makeRandom(program, 100);
|
|
||||||
|
|
||||||
BOOST_TEST(population1.individuals().size() == 100);
|
BOOST_TEST(population1.individuals().size() == 100);
|
||||||
BOOST_TEST(population2.individuals().size() == 100);
|
BOOST_TEST(population2.individuals().size() == 100);
|
||||||
@ -117,19 +125,17 @@ BOOST_AUTO_TEST_CASE(makeRandom_should_return_population_with_random_chromosomes
|
|||||||
BOOST_TEST(numMatchingPositions < 10);
|
BOOST_TEST(numMatchingPositions < 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(makeRandom_should_not_compute_fitness)
|
BOOST_FIXTURE_TEST_CASE(makeRandom_should_not_compute_fitness, PopulationFixture)
|
||||||
{
|
{
|
||||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
auto population = Population::makeRandom(m_program, 5);
|
||||||
auto population = Population::makeRandom(Program::load(sourceStream), 5);
|
|
||||||
|
|
||||||
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(run_should_evaluate_fitness)
|
BOOST_FIXTURE_TEST_CASE(run_should_evaluate_fitness, PopulationFixture)
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
auto population = Population::makeRandom(m_program, 5);
|
||||||
auto population = Population::makeRandom(Program::load(sourceStream), 5);
|
|
||||||
assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
assert(all_of(population.individuals().begin(), population.individuals().end(), fitnessNotSet));
|
||||||
|
|
||||||
population.run(1, output);
|
population.run(1, output);
|
||||||
@ -137,10 +143,9 @@ BOOST_AUTO_TEST_CASE(run_should_evaluate_fitness)
|
|||||||
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessSet));
|
BOOST_TEST(all_of(population.individuals().begin(), population.individuals().end(), fitnessSet));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse)
|
BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, PopulationFixture)
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
CharStream sourceStream(sampleSourceCode, current_test_case().p_name);
|
|
||||||
vector<Chromosome> chromosomes = {
|
vector<Chromosome> chromosomes = {
|
||||||
Chromosome(vector<string>{StructuralSimplifier::name}),
|
Chromosome(vector<string>{StructuralSimplifier::name}),
|
||||||
Chromosome(vector<string>{BlockFlattener::name}),
|
Chromosome(vector<string>{BlockFlattener::name}),
|
||||||
@ -148,12 +153,11 @@ BOOST_AUTO_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse)
|
|||||||
Chromosome(vector<string>{UnusedPruner::name}),
|
Chromosome(vector<string>{UnusedPruner::name}),
|
||||||
Chromosome(vector<string>{StructuralSimplifier::name, BlockFlattener::name}),
|
Chromosome(vector<string>{StructuralSimplifier::name, BlockFlattener::name}),
|
||||||
};
|
};
|
||||||
auto program = Program::load(sourceStream);
|
Population population(m_program, chromosomes);
|
||||||
Population population(program, chromosomes);
|
|
||||||
|
|
||||||
size_t initialTopFitness[2] = {
|
size_t initialTopFitness[2] = {
|
||||||
Population::measureFitness(chromosomes[0], program),
|
Population::measureFitness(chromosomes[0], m_program),
|
||||||
Population::measureFitness(chromosomes[1], program),
|
Population::measureFitness(chromosomes[1], m_program),
|
||||||
};
|
};
|
||||||
|
|
||||||
for (int i = 0; i < 6; ++i)
|
for (int i = 0; i < 6; ++i)
|
||||||
|
Loading…
Reference in New Issue
Block a user