[yul-phaser] Chromosome: Make randomOptimisationStep() public

- And add tests now that it's public.
This commit is contained in:
Kamil Śliwak 2020-02-06 00:49:11 +01:00
parent 46d69d0941
commit 4ee9174454
2 changed files with 23 additions and 1 deletions

View File

@ -15,7 +15,10 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/yulPhaser/Common.h>
#include <tools/yulPhaser/Chromosome.h>
#include <tools/yulPhaser/Random.h>
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/StructuralSimplifier.h>
@ -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<string, size_t> stepIndices = enumerateOptmisationSteps();
vector<size_t> 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()

View File

@ -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<std::string> allStepNames();
static std::string const& randomOptimisationStep();
std::vector<std::string> m_optimisationSteps;
};