[yul-phaser] Common: Add chromosomeLengths()

This commit is contained in:
Kamil Śliwak 2020-02-14 19:41:43 +01:00
parent 837ea96da7
commit 38f79a1761
3 changed files with 30 additions and 0 deletions

View File

@ -25,6 +25,15 @@ using namespace std;
using namespace solidity;
using namespace solidity::yul;
vector<size_t> phaser::test::chromosomeLengths(Population const& _population)
{
vector<size_t> lengths;
for (auto const& individual: _population.individuals())
lengths.push_back(individual.chromosome.length());
return lengths;
}
map<string, size_t> phaser::test::enumerateOptmisationSteps()
{
map<string, size_t> stepIndices;

View File

@ -28,6 +28,8 @@
#pragma once
#include <tools/yulPhaser/Population.h>
#include <cassert>
#include <map>
#include <string>
@ -37,6 +39,10 @@ namespace solidity::phaser::test
{
// CHROMOSOME AND POPULATION HELPERS
/// Returns a vector containing lengths of all chromosomes in the population (in the same order).
std::vector<size_t> chromosomeLengths(Population const& _population);
/// Assigns indices from 0 to N to all optimisation steps available in the OptimiserSuite.
/// This is a convenience helper to make it easier to test their distribution with tools made for
/// integers.

View File

@ -19,11 +19,14 @@
#include <libyul/optimiser/Suite.h>
#include <liblangutil/CharStream.h>
#include <boost/test/unit_test.hpp>
#include <set>
using namespace std;
using namespace solidity::langutil;
using namespace solidity::yul;
using namespace boost::test_tools;
@ -33,6 +36,18 @@ namespace solidity::phaser::test
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(CommonTest)
BOOST_AUTO_TEST_CASE(chromosomeLengths_should_return_lengths_of_all_chromosomes_in_a_population)
{
CharStream sourceStream("{}", "");
auto program = Program::load(sourceStream);
Population population1(program, {Chromosome(), Chromosome("a"), Chromosome("aa"), Chromosome("aaa")});
BOOST_TEST((chromosomeLengths(population1) == vector<size_t>{0, 1, 2, 3}));
Population population2(program);
BOOST_TEST((chromosomeLengths(population2) == vector<size_t>{}));
}
BOOST_AUTO_TEST_CASE(enumerateOptimisationSteps_should_assing_indices_to_all_available_optimisation_steps)
{
map<string, char> stepsAndAbbreviations = OptimiserSuite::stepNameToAbbreviationMap();