diff --git a/test/yulPhaser/Chromosome.cpp b/test/yulPhaser/Chromosome.cpp index d47c556c5..824fe695e 100644 --- a/test/yulPhaser/Chromosome.cpp +++ b/test/yulPhaser/Chromosome.cpp @@ -47,25 +47,25 @@ using namespace solidity::util; namespace solidity::phaser::test { +vector const ChrOmOsoMeSteps{ + ConditionalSimplifier::name, + FunctionHoister::name, + RedundantAssignEliminator::name, + ForLoopConditionOutOfBody::name, + Rematerialiser::name, + ForLoopConditionOutOfBody::name, + ExpressionSimplifier::name, + ForLoopInitRewriter::name, + LoopInvariantCodeMotion::name, + ExpressionInliner::name +}; + BOOST_AUTO_TEST_SUITE(Phaser, *boost::unit_test::label("nooptions")) BOOST_AUTO_TEST_SUITE(ChromosomeTest) BOOST_AUTO_TEST_CASE(constructor_should_convert_from_string_to_optimisation_steps) { - vector expectedSteps{ - ConditionalSimplifier::name, - FunctionHoister::name, - RedundantAssignEliminator::name, - ForLoopConditionOutOfBody::name, - Rematerialiser::name, - ForLoopConditionOutOfBody::name, - ExpressionSimplifier::name, - ForLoopInitRewriter::name, - LoopInvariantCodeMotion::name, - ExpressionInliner::name - }; - - BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == expectedSteps); + BOOST_TEST(Chromosome("ChrOmOsoMe").optimisationSteps() == ChrOmOsoMeSteps); } BOOST_AUTO_TEST_CASE(makeRandom_should_return_different_chromosome_each_time) @@ -151,6 +151,18 @@ BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_pr BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance); } +BOOST_AUTO_TEST_CASE(stepsToGenes_should_translate_optimisation_step_names_to_abbreviations) +{ + BOOST_TEST(Chromosome::stepsToGenes({}) == ""); + BOOST_TEST(Chromosome::stepsToGenes(ChrOmOsoMeSteps) == "ChrOmOsoMe"); +} + +BOOST_AUTO_TEST_CASE(genesToSteps_should_translate_optimisation_step_abbreviations_to_names) +{ + BOOST_TEST(Chromosome::genesToSteps("") == vector{}); + BOOST_TEST(Chromosome::genesToSteps("ChrOmOsoMe") == ChrOmOsoMeSteps); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() diff --git a/test/yulPhaser/Mutations.cpp b/test/yulPhaser/Mutations.cpp index 1f2bfef44..1fe947f00 100644 --- a/test/yulPhaser/Mutations.cpp +++ b/test/yulPhaser/Mutations.cpp @@ -218,10 +218,11 @@ BOOST_AUTO_TEST_CASE(alternativeMutations_should_always_choose_second_mutation_i BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations) { Chromosome chromosome("aaaaa"); + vector steps = Chromosome::genesToSteps("gfc"); function mutation = mutationSequence({ - geneSubstitution(3, Chromosome("g").optimisationSteps()[0]), - geneSubstitution(2, Chromosome("f").optimisationSteps()[0]), - geneSubstitution(1, Chromosome("c").optimisationSteps()[0]), + geneSubstitution(3, steps[0]), + geneSubstitution(2, steps[1]), + geneSubstitution(1, steps[2]), }); BOOST_TEST(mutation(chromosome) == Chromosome("acfga")); @@ -230,11 +231,12 @@ BOOST_AUTO_TEST_CASE(mutationSequence_should_apply_all_mutations) BOOST_AUTO_TEST_CASE(mutationSequence_apply_mutations_in_the_order_they_are_given) { Chromosome chromosome("aa"); + vector steps = Chromosome::genesToSteps("gcfo"); function mutation = mutationSequence({ - geneSubstitution(0, Chromosome("g").optimisationSteps()[0]), - geneSubstitution(1, Chromosome("c").optimisationSteps()[0]), - geneSubstitution(0, Chromosome("f").optimisationSteps()[0]), - geneSubstitution(1, Chromosome("o").optimisationSteps()[0]), + geneSubstitution(0, steps[0]), + geneSubstitution(1, steps[1]), + geneSubstitution(0, steps[2]), + geneSubstitution(1, steps[3]), }); BOOST_TEST(mutation(chromosome) == Chromosome("fo")); diff --git a/test/yulPhaser/Phaser.cpp b/test/yulPhaser/Phaser.cpp index 673c43824..013f3a9e0 100644 --- a/test/yulPhaser/Phaser.cpp +++ b/test/yulPhaser/Phaser.cpp @@ -451,7 +451,7 @@ BOOST_AUTO_TEST_CASE(build_should_apply_prefix) CharStream nestedSource("{{{let x:= 1}}}", ""); Program nestedProgram = get(Program::load(nestedSource)); Program flatProgram = get(Program::load(nestedSource)); - flatProgram.optimise(Chromosome("f").optimisationSteps()); + flatProgram.optimise(Chromosome::genesToSteps("f")); assert(toString(nestedProgram) != toString(flatProgram)); { diff --git a/test/yulPhaser/ProgramCache.cpp b/test/yulPhaser/ProgramCache.cpp index 7fb74fca9..3a84aad67 100644 --- a/test/yulPhaser/ProgramCache.cpp +++ b/test/yulPhaser/ProgramCache.cpp @@ -53,7 +53,7 @@ protected: Program optimisedProgram(Program _program, string _abbreviatedOptimisationSteps) const { Program result = move(_program); - result.optimise(Chromosome(_abbreviatedOptimisationSteps).optimisationSteps()); + result.optimise(Chromosome::genesToSteps(_abbreviatedOptimisationSteps)); return result; } diff --git a/tools/yulPhaser/Chromosome.cpp b/tools/yulPhaser/Chromosome.cpp index 453c62d39..b7048ae61 100644 --- a/tools/yulPhaser/Chromosome.cpp +++ b/tools/yulPhaser/Chromosome.cpp @@ -37,12 +37,6 @@ ostream& operator<<(ostream& _stream, Chromosome const& _chromosome); } -Chromosome::Chromosome(string const& _optimisationSteps) -{ - for (char abbreviation: _optimisationSteps) - m_optimisationSteps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation)); -} - Chromosome Chromosome::makeRandom(size_t _length) { vector steps; @@ -54,10 +48,7 @@ Chromosome Chromosome::makeRandom(size_t _length) ostream& phaser::operator<<(ostream& _stream, Chromosome const& _chromosome) { - for (auto const& stepName: _chromosome.m_optimisationSteps) - _stream << OptimiserSuite::stepNameToAbbreviationMap().at(stepName); - - return _stream; + return _stream << Chromosome::stepsToGenes(_chromosome.m_optimisationSteps); } vector Chromosome::allStepNames() @@ -75,3 +66,21 @@ string const& Chromosome::randomOptimisationStep() return stepNames[SimulationRNG::uniformInt(0, stepNames.size() - 1)]; } + +string Chromosome::stepsToGenes(vector const& _optimisationSteps) +{ + string genes; + for (string const& stepName: _optimisationSteps) + genes.push_back(OptimiserSuite::stepNameToAbbreviationMap().at(stepName)); + + return genes; +} + +vector Chromosome::genesToSteps(string const& _genes) +{ + vector steps; + for (char abbreviation: _genes) + steps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation)); + + return steps; +} diff --git a/tools/yulPhaser/Chromosome.h b/tools/yulPhaser/Chromosome.h index 95f3ae58e..e97042041 100644 --- a/tools/yulPhaser/Chromosome.h +++ b/tools/yulPhaser/Chromosome.h @@ -43,7 +43,8 @@ public: Chromosome() = default; explicit Chromosome(std::vector _optimisationSteps): m_optimisationSteps(std::move(_optimisationSteps)) {} - explicit Chromosome(std::string const& _optimisationSteps); + explicit Chromosome(std::string const& _genes): + m_optimisationSteps(genesToSteps(_genes)) {} static Chromosome makeRandom(size_t _length); size_t length() const { return m_optimisationSteps.size(); } @@ -55,6 +56,8 @@ public: bool operator!=(Chromosome const& _other) const { return !(*this == _other); } static std::string const& randomOptimisationStep(); + static std::string stepsToGenes(std::vector const& _optimisationSteps); + static std::vector genesToSteps(std::string const& _genes); private: static std::vector allStepNames();