diff --git a/test/yulPhaser/Chromosome.cpp b/test/yulPhaser/Chromosome.cpp index bd07e1b2e..e8dbbe6c3 100644 --- a/test/yulPhaser/Chromosome.cpp +++ b/test/yulPhaser/Chromosome.cpp @@ -15,7 +15,10 @@ along with solidity. If not, see . */ +#include + #include +#include #include #include @@ -95,6 +98,24 @@ BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_strin BOOST_TEST(toString(chromosome) == "flcCUnDvejsxIOoighTLMrmVatud"); } +BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_probability) +{ + SimulationRNG::reset(1); + constexpr int samplesPerStep = 100; + constexpr double relativeTolerance = 0.01; + + map stepIndices = enumerateOptmisationSteps(); + vector samples; + for (size_t i = 0; i <= stepIndices.size() * samplesPerStep; ++i) + samples.push_back(stepIndices.at(Chromosome::randomOptimisationStep())); + + const double expectedValue = (stepIndices.size() - 1) / 2.0; + const double variance = (stepIndices.size() * stepIndices.size() - 1) / 12.0; + + BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance); + BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance); +} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() diff --git a/tools/yulPhaser/Chromosome.h b/tools/yulPhaser/Chromosome.h index 6db99dc4e..bf91dbd30 100644 --- a/tools/yulPhaser/Chromosome.h +++ b/tools/yulPhaser/Chromosome.h @@ -52,9 +52,10 @@ public: bool operator==(Chromosome const& _other) const { return m_optimisationSteps == _other.m_optimisationSteps; } bool operator!=(Chromosome const& _other) const { return !(*this == _other); } + static std::string const& randomOptimisationStep(); + private: static std::vector allStepNames(); - static std::string const& randomOptimisationStep(); std::vector m_optimisationSteps; };