mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] ProgramSize metric: Refactor repeating parts of test code
- Also replace test fixture constructor with direct member initialisation
This commit is contained in:
parent
1c25104741
commit
bdc4d1ccd1
@ -34,10 +34,6 @@ namespace solidity::phaser::test
|
|||||||
class FitnessMetricFixture
|
class FitnessMetricFixture
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
FitnessMetricFixture():
|
|
||||||
m_sourceStream(SampleSourceCode, ""),
|
|
||||||
m_program(get<Program>(Program::load(m_sourceStream))) {}
|
|
||||||
|
|
||||||
static constexpr char SampleSourceCode[] =
|
static constexpr char SampleSourceCode[] =
|
||||||
"{\n"
|
"{\n"
|
||||||
" function foo() -> result\n"
|
" function foo() -> result\n"
|
||||||
@ -52,8 +48,22 @@ protected:
|
|||||||
" mstore(foo(), bar())\n"
|
" mstore(foo(), bar())\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
CharStream m_sourceStream;
|
Program optimisedProgram(Program _program) const
|
||||||
Program m_program;
|
{
|
||||||
|
[[maybe_unused]] size_t originalSize = _program.codeSize();
|
||||||
|
Program result = move(_program);
|
||||||
|
result.optimise(m_chromosome.optimisationSteps());
|
||||||
|
|
||||||
|
// Make sure that the program and the chromosome we have chosen are suitable for the test
|
||||||
|
assert(result.codeSize() != originalSize);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
CharStream m_sourceStream = CharStream(SampleSourceCode, "");
|
||||||
|
Chromosome m_chromosome{vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name}};
|
||||||
|
Program m_program = get<Program>(Program::load(m_sourceStream));
|
||||||
|
Program m_optimisedProgram = optimisedProgram(m_program);
|
||||||
};
|
};
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||||
@ -62,47 +72,32 @@ BOOST_AUTO_TEST_SUITE(ProgramSizeTest)
|
|||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(evaluate_should_compute_size_of_the_optimised_program, FitnessMetricFixture)
|
BOOST_FIXTURE_TEST_CASE(evaluate_should_compute_size_of_the_optimised_program, FitnessMetricFixture)
|
||||||
{
|
{
|
||||||
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
|
size_t fitness = ProgramSize(m_program).evaluate(m_chromosome);
|
||||||
|
|
||||||
Program optimisedProgram = m_program;
|
BOOST_TEST(fitness != m_program.codeSize());
|
||||||
optimisedProgram.optimise(chromosome.optimisationSteps());
|
BOOST_TEST(fitness == m_optimisedProgram.codeSize());
|
||||||
assert(m_program.codeSize() != optimisedProgram.codeSize());
|
|
||||||
|
|
||||||
BOOST_TEST(ProgramSize(m_program).evaluate(chromosome) != m_program.codeSize());
|
|
||||||
BOOST_TEST(ProgramSize(m_program).evaluate(chromosome) == optimisedProgram.codeSize());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(evaluate_should_repeat_the_optimisation_specified_number_of_times, FitnessMetricFixture)
|
BOOST_FIXTURE_TEST_CASE(evaluate_should_repeat_the_optimisation_specified_number_of_times, FitnessMetricFixture)
|
||||||
{
|
{
|
||||||
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
|
Program const& programOptimisedOnce = m_optimisedProgram;
|
||||||
|
Program programOptimisedTwice = optimisedProgram(programOptimisedOnce);
|
||||||
Program programOptimisedOnce = m_program;
|
|
||||||
programOptimisedOnce.optimise(chromosome.optimisationSteps());
|
|
||||||
Program programOptimisedTwice = programOptimisedOnce;
|
|
||||||
programOptimisedTwice.optimise(chromosome.optimisationSteps());
|
|
||||||
assert(m_program.codeSize() != programOptimisedOnce.codeSize());
|
|
||||||
assert(m_program.codeSize() != programOptimisedTwice.codeSize());
|
|
||||||
assert(programOptimisedOnce.codeSize() != programOptimisedTwice.codeSize());
|
|
||||||
|
|
||||||
ProgramSize metric(m_program, 2);
|
ProgramSize metric(m_program, 2);
|
||||||
|
size_t fitness = metric.evaluate(m_chromosome);
|
||||||
|
|
||||||
BOOST_TEST(metric.evaluate(chromosome) != m_program.codeSize());
|
BOOST_TEST(fitness != m_program.codeSize());
|
||||||
BOOST_TEST(metric.evaluate(chromosome) != programOptimisedOnce.codeSize());
|
BOOST_TEST(fitness != programOptimisedOnce.codeSize());
|
||||||
BOOST_TEST(metric.evaluate(chromosome) == programOptimisedTwice.codeSize());
|
BOOST_TEST(fitness == programOptimisedTwice.codeSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(evaluate_should_not_optimise_if_number_of_repetitions_is_zero, FitnessMetricFixture)
|
BOOST_FIXTURE_TEST_CASE(evaluate_should_not_optimise_if_number_of_repetitions_is_zero, FitnessMetricFixture)
|
||||||
{
|
{
|
||||||
Chromosome chromosome(vector<string>{UnusedPruner::name, EquivalentFunctionCombiner::name});
|
|
||||||
|
|
||||||
Program optimisedProgram = m_program;
|
|
||||||
optimisedProgram.optimise(chromosome.optimisationSteps());
|
|
||||||
assert(m_program.codeSize() != optimisedProgram.codeSize());
|
|
||||||
|
|
||||||
ProgramSize metric(m_program, 0);
|
ProgramSize metric(m_program, 0);
|
||||||
|
size_t fitness = metric.evaluate(m_chromosome);
|
||||||
|
|
||||||
BOOST_TEST(metric.evaluate(chromosome) == m_program.codeSize());
|
BOOST_TEST(fitness == m_program.codeSize());
|
||||||
BOOST_TEST(metric.evaluate(chromosome) != optimisedProgram.codeSize());
|
BOOST_TEST(fitness != m_optimisedProgram.codeSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
@ -110,4 +105,3 @@ BOOST_AUTO_TEST_SUITE_END()
|
|||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user