mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #8327 from imapp-pl/yul-phaser-random-algorithm
[yul-phaser] Random algorithm
This commit is contained in:
@@ -144,8 +144,10 @@ set(yul_phaser_sources
|
||||
yulPhaser/CommonTest.cpp
|
||||
yulPhaser/Chromosome.cpp
|
||||
yulPhaser/FitnessMetrics.cpp
|
||||
yulPhaser/GeneticAlgorithms.cpp
|
||||
yulPhaser/Population.cpp
|
||||
yulPhaser/Program.cpp
|
||||
yulPhaser/Selections.cpp
|
||||
yulPhaser/SimulationRNG.cpp
|
||||
|
||||
# FIXME: yul-phaser is not a library so I can't just add it to target_link_libraries().
|
||||
@@ -153,8 +155,10 @@ set(yul_phaser_sources
|
||||
# unnecessary duplication. Create a library or find a way to reuse the list in both places.
|
||||
../tools/yulPhaser/Chromosome.cpp
|
||||
../tools/yulPhaser/FitnessMetrics.cpp
|
||||
../tools/yulPhaser/GeneticAlgorithms.cpp
|
||||
../tools/yulPhaser/Population.cpp
|
||||
../tools/yulPhaser/Program.cpp
|
||||
../tools/yulPhaser/Selections.cpp
|
||||
../tools/yulPhaser/SimulationRNG.cpp
|
||||
)
|
||||
detect_stray_source_files("${yul_phaser_sources}" "yulPhaser/")
|
||||
|
||||
@@ -49,3 +49,18 @@ string phaser::test::stripWhitespace(string const& input)
|
||||
regex whitespaceRegex("\\s+");
|
||||
return regex_replace(input, whitespaceRegex, "");
|
||||
}
|
||||
|
||||
size_t phaser::test::countSubstringOccurrences(string const& _inputString, string const& _substring)
|
||||
{
|
||||
assert(_substring.size() > 0);
|
||||
|
||||
size_t count = 0;
|
||||
size_t lastOccurrence = 0;
|
||||
while ((lastOccurrence = _inputString.find(_substring, lastOccurrence)) != string::npos)
|
||||
{
|
||||
++count;
|
||||
lastOccurrence += _substring.size();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -67,6 +67,10 @@ std::map<std::string, size_t> enumerateOptmisationSteps();
|
||||
/// Returns the input string with all the whitespace characters (spaces, line endings, etc.) removed.
|
||||
std::string stripWhitespace(std::string const& input);
|
||||
|
||||
/// Counts the number of times one strinng can be found inside another. Only non-overlapping
|
||||
/// occurrences are counted.
|
||||
size_t countSubstringOccurrences(std::string const& _inputString, std::string const& _substring);
|
||||
|
||||
// STATISTICAL UTILITIES
|
||||
|
||||
/// Calculates the mean value of a series of samples given in a vector.
|
||||
|
||||
@@ -83,6 +83,25 @@ BOOST_AUTO_TEST_CASE(stripWhitespace_should_remove_all_whitespace_characters_fro
|
||||
BOOST_TEST(stripWhitespace(" a b \n\n c \n\t\v") == "abc");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(countSubstringOccurrences_should_count_non_overlapping_substring_occurrences_in_a_string)
|
||||
{
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "a") == 6);
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "aa") == 2);
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "aaa") == 2);
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "aaab") == 1);
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "b") == 2);
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "d") == 1);
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "cdc") == 1);
|
||||
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "x") == 0);
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "aaaa") == 0);
|
||||
BOOST_TEST(countSubstringOccurrences("aaabcdcbaaa", "dcd") == 0);
|
||||
|
||||
BOOST_TEST(countSubstringOccurrences("", "a") == 0);
|
||||
BOOST_TEST(countSubstringOccurrences("", "aa") == 0);
|
||||
BOOST_TEST(countSubstringOccurrences("a", "aa") == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(mean_should_calculate_statistical_mean)
|
||||
{
|
||||
BOOST_TEST(mean<int>({0}) == 0.0);
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/FitnessMetrics.h>
|
||||
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
|
||||
#include <liblangutil/CharStream.h>
|
||||
|
||||
#include <libsolutil/CommonIO.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/tools/output_test_stream.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost::unit_test::framework;
|
||||
using namespace boost::test_tools;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::util;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
class DummyAlgorithm: public GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
using GeneticAlgorithm::GeneticAlgorithm;
|
||||
void runNextRound() override { ++m_currentRound; }
|
||||
|
||||
size_t m_currentRound = 0;
|
||||
};
|
||||
|
||||
class GeneticAlgorithmFixture
|
||||
{
|
||||
protected:
|
||||
shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
||||
output_test_stream m_output;
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(GeneticAlgorithmsTest)
|
||||
BOOST_AUTO_TEST_SUITE(GeneticAlgorithmTest)
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_call_runNextRound_once_per_round, GeneticAlgorithmFixture)
|
||||
{
|
||||
DummyAlgorithm algorithm(Population(m_fitnessMetric), m_output);
|
||||
|
||||
BOOST_TEST(algorithm.m_currentRound == 0);
|
||||
algorithm.run(10);
|
||||
BOOST_TEST(algorithm.m_currentRound == 10);
|
||||
algorithm.run(3);
|
||||
BOOST_TEST(algorithm.m_currentRound == 13);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_print_the_top_chromosome, GeneticAlgorithmFixture)
|
||||
{
|
||||
// run() is allowed to print more but should at least print the first one
|
||||
|
||||
DummyAlgorithm algorithm(
|
||||
// NOTE: Chromosomes chosen so that they're not substrings of each other and are not
|
||||
// words likely to appear in the output in normal circumstances.
|
||||
Population(m_fitnessMetric, {Chromosome("fcCUnDve"), Chromosome("jsxIOo"), Chromosome("ighTLM")}),
|
||||
m_output
|
||||
);
|
||||
|
||||
BOOST_TEST(m_output.is_empty());
|
||||
algorithm.run(1);
|
||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(algorithm.population().individuals()[0].chromosome)) == 1);
|
||||
algorithm.run(3);
|
||||
BOOST_TEST(countSubstringOccurrences(m_output.str(), toString(algorithm.population().individuals()[0].chromosome)) == 4);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE(RandomAlgorithmTest)
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_preserve_elite_and_randomise_rest_of_population, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
RandomAlgorithm algorithm(population, m_output, {0.5, 1, 1});
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
|
||||
algorithm.runNextRound();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{1, 1, 1, 1, 3, 3, 3, 3}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_elite_with_worse_individuals, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
RandomAlgorithm algorithm(population, m_output, {0.5, 7, 7});
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
|
||||
algorithm.runNextRound();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 7, 7, 7, 7}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_replace_all_chromosomes_if_zero_size_elite, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
RandomAlgorithm algorithm(population, m_output, {0.0, 1, 1});
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
|
||||
algorithm.runNextRound();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{1, 1, 1, 1, 1, 1, 1, 1}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(runNextRound_should_not_replace_any_chromosomes_if_whole_population_is_the_elite, GeneticAlgorithmFixture)
|
||||
{
|
||||
auto population = Population::makeRandom(m_fitnessMetric, 4, 3, 3) + Population::makeRandom(m_fitnessMetric, 4, 5, 5);
|
||||
RandomAlgorithm algorithm(population, m_output, {1.0, 1, 1});
|
||||
assert((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
|
||||
algorithm.runNextRound();
|
||||
BOOST_TEST((chromosomeLengths(algorithm.population()) == vector<size_t>{3, 3, 3, 3, 5, 5, 5, 5}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <tools/yulPhaser/Chromosome.h>
|
||||
#include <tools/yulPhaser/Population.h>
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
#include <tools/yulPhaser/Selections.h>
|
||||
|
||||
#include <libyul/optimiser/BlockFlattener.h>
|
||||
#include <libyul/optimiser/SSAReverser.h>
|
||||
@@ -181,38 +182,6 @@ BOOST_FIXTURE_TEST_CASE(makeRandom_should_compute_fitness, PopulationFixture)
|
||||
BOOST_TEST(population.individuals()[2].fitness == m_fitnessMetric->evaluate(population.individuals()[2].chromosome));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(run_should_not_make_fitness_of_top_chromosomes_worse, PopulationFixture)
|
||||
{
|
||||
stringstream output;
|
||||
vector<Chromosome> chromosomes = {
|
||||
Chromosome(vector<string>{StructuralSimplifier::name}),
|
||||
Chromosome(vector<string>{BlockFlattener::name}),
|
||||
Chromosome(vector<string>{SSAReverser::name}),
|
||||
Chromosome(vector<string>{UnusedPruner::name}),
|
||||
Chromosome(vector<string>{StructuralSimplifier::name, BlockFlattener::name}),
|
||||
};
|
||||
Population population(m_fitnessMetric, chromosomes);
|
||||
|
||||
size_t initialTopFitness[2] = {
|
||||
m_fitnessMetric->evaluate(chromosomes[0]),
|
||||
m_fitnessMetric->evaluate(chromosomes[1]),
|
||||
};
|
||||
|
||||
for (int i = 0; i < 6; ++i)
|
||||
{
|
||||
population.run(1, output);
|
||||
BOOST_TEST(population.individuals().size() == 5);
|
||||
|
||||
size_t currentTopFitness[2] = {
|
||||
population.individuals()[0].fitness,
|
||||
population.individuals()[1].fitness,
|
||||
};
|
||||
BOOST_TEST(currentTopFitness[0] <= initialTopFitness[0]);
|
||||
BOOST_TEST(currentTopFitness[1] <= initialTopFitness[1]);
|
||||
BOOST_TEST(currentTopFitness[0] <= currentTopFitness[1]);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(plus_operator_should_add_two_populations, PopulationFixture)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(
|
||||
@@ -222,6 +191,41 @@ BOOST_FIXTURE_TEST_CASE(plus_operator_should_add_two_populations, PopulationFixt
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(select_should_return_population_containing_individuals_indicated_by_selection, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("a"), Chromosome("c"), Chromosome("g"), Chromosome("h")});
|
||||
RangeSelection selection(0.25, 0.75);
|
||||
assert(selection.materialise(population.individuals().size()) == (vector<size_t>{1, 2}));
|
||||
|
||||
BOOST_TEST(
|
||||
population.select(selection) ==
|
||||
Population(m_fitnessMetric, {population.individuals()[1].chromosome, population.individuals()[2].chromosome})
|
||||
);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(select_should_include_duplicates_if_selection_contains_duplicates, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("a"), Chromosome("c")});
|
||||
MosaicSelection selection({0, 1}, 2.0);
|
||||
assert(selection.materialise(population.individuals().size()) == (vector<size_t>{0, 1, 0, 1}));
|
||||
|
||||
BOOST_TEST(population.select(selection) == Population(m_fitnessMetric, {
|
||||
population.individuals()[0].chromosome,
|
||||
population.individuals()[1].chromosome,
|
||||
population.individuals()[0].chromosome,
|
||||
population.individuals()[1].chromosome,
|
||||
}));
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(select_should_return_empty_population_if_selection_is_empty, PopulationFixture)
|
||||
{
|
||||
Population population(m_fitnessMetric, {Chromosome("a"), Chromosome("c")});
|
||||
RangeSelection selection(0.0, 0.0);
|
||||
assert(selection.materialise(population.individuals().size()).empty());
|
||||
|
||||
BOOST_TEST(population.select(selection).individuals().empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <test/yulPhaser/Common.h>
|
||||
|
||||
#include <tools/yulPhaser/Selections.h>
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace solidity::phaser::test
|
||||
{
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(Phaser)
|
||||
BOOST_AUTO_TEST_SUITE(SelectionsTest)
|
||||
BOOST_AUTO_TEST_SUITE(RangeSelectionTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise)
|
||||
{
|
||||
BOOST_TEST(RangeSelection(0.0, 1.0).materialise(10) == vector<size_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
|
||||
BOOST_TEST(RangeSelection(0.0, 0.1).materialise(10) == vector<size_t>({0}));
|
||||
BOOST_TEST(RangeSelection(0.0, 0.2).materialise(10) == vector<size_t>({0, 1}));
|
||||
BOOST_TEST(RangeSelection(0.0, 0.7).materialise(10) == vector<size_t>({0, 1, 2, 3, 4, 5, 6}));
|
||||
|
||||
BOOST_TEST(RangeSelection(0.9, 1.0).materialise(10) == vector<size_t>({ 9}));
|
||||
BOOST_TEST(RangeSelection(0.8, 1.0).materialise(10) == vector<size_t>({ 8, 9}));
|
||||
BOOST_TEST(RangeSelection(0.5, 1.0).materialise(10) == vector<size_t>({ 5, 6, 7, 8, 9}));
|
||||
|
||||
BOOST_TEST(RangeSelection(0.3, 0.6).materialise(10) == vector<size_t>({ 3, 4, 5 }));
|
||||
BOOST_TEST(RangeSelection(0.2, 0.7).materialise(10) == vector<size_t>({ 2, 3, 4, 5, 6 }));
|
||||
BOOST_TEST(RangeSelection(0.4, 0.7).materialise(10) == vector<size_t>({ 4, 5, 6 }));
|
||||
|
||||
BOOST_TEST(RangeSelection(0.4, 0.7).materialise(5) == vector<size_t>({2, 3}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_round_indices)
|
||||
{
|
||||
BOOST_TEST(RangeSelection(0.01, 0.99).materialise(10) == vector<size_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
|
||||
BOOST_TEST(RangeSelection(0.04, 0.96).materialise(10) == vector<size_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
|
||||
BOOST_TEST(RangeSelection(0.05, 0.95).materialise(10) == vector<size_t>({ 1, 2, 3, 4, 5, 6, 7, 8, 9}));
|
||||
BOOST_TEST(RangeSelection(0.06, 0.94).materialise(10) == vector<size_t>({ 1, 2, 3, 4, 5, 6, 7, 8 }));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_handle_empty_collections)
|
||||
{
|
||||
BOOST_TEST(RangeSelection(0.0, 0.0).materialise(0).empty());
|
||||
BOOST_TEST(RangeSelection(0.0, 1.0).materialise(0).empty());
|
||||
BOOST_TEST(RangeSelection(0.5, 1.0).materialise(0).empty());
|
||||
BOOST_TEST(RangeSelection(0.0, 0.5).materialise(0).empty());
|
||||
BOOST_TEST(RangeSelection(0.2, 0.7).materialise(0).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_handle_empty_selection_ranges)
|
||||
{
|
||||
BOOST_TEST(RangeSelection(0.0, 0.0).materialise(1).empty());
|
||||
BOOST_TEST(RangeSelection(1.0, 1.0).materialise(1).empty());
|
||||
|
||||
BOOST_TEST(RangeSelection(0.0, 0.0).materialise(100).empty());
|
||||
BOOST_TEST(RangeSelection(1.0, 1.0).materialise(100).empty());
|
||||
BOOST_TEST(RangeSelection(0.5, 0.5).materialise(100).empty());
|
||||
|
||||
BOOST_TEST(RangeSelection(0.45, 0.54).materialise(10).empty());
|
||||
BOOST_TEST(!RangeSelection(0.45, 0.54).materialise(100).empty());
|
||||
BOOST_TEST(RangeSelection(0.045, 0.054).materialise(100).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE(MosaicSelectionTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise)
|
||||
{
|
||||
BOOST_TEST(MosaicSelection({1}, 0.5).materialise(4) == vector<size_t>({1, 1}));
|
||||
BOOST_TEST(MosaicSelection({1}, 1.0).materialise(4) == vector<size_t>({1, 1, 1, 1}));
|
||||
BOOST_TEST(MosaicSelection({1}, 2.0).materialise(4) == vector<size_t>({1, 1, 1, 1, 1, 1, 1, 1}));
|
||||
BOOST_TEST(MosaicSelection({1}, 1.0).materialise(2) == vector<size_t>({1, 1}));
|
||||
|
||||
BOOST_TEST(MosaicSelection({0, 1}, 0.5).materialise(4) == vector<size_t>({0, 1}));
|
||||
BOOST_TEST(MosaicSelection({0, 1}, 1.0).materialise(4) == vector<size_t>({0, 1, 0, 1}));
|
||||
BOOST_TEST(MosaicSelection({0, 1}, 2.0).materialise(4) == vector<size_t>({0, 1, 0, 1, 0, 1, 0, 1}));
|
||||
BOOST_TEST(MosaicSelection({0, 1}, 1.0).materialise(2) == vector<size_t>({0, 1}));
|
||||
|
||||
BOOST_TEST(MosaicSelection({3, 2, 1, 0}, 0.5).materialise(4) == vector<size_t>({3, 2}));
|
||||
BOOST_TEST(MosaicSelection({3, 2, 1, 0}, 1.0).materialise(4) == vector<size_t>({3, 2, 1, 0}));
|
||||
BOOST_TEST(MosaicSelection({3, 2, 1, 0}, 2.0).materialise(4) == vector<size_t>({3, 2, 1, 0, 3, 2, 1, 0}));
|
||||
BOOST_TEST(MosaicSelection({1, 0, 1, 0}, 1.0).materialise(2) == vector<size_t>({1, 0}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_round_indices)
|
||||
{
|
||||
BOOST_TEST(MosaicSelection({4, 3, 2, 1, 0}, 0.49).materialise(5) == vector<size_t>({4, 3}));
|
||||
BOOST_TEST(MosaicSelection({4, 3, 2, 1, 0}, 0.50).materialise(5) == vector<size_t>({4, 3, 2}));
|
||||
BOOST_TEST(MosaicSelection({4, 3, 2, 1, 0}, 0.51).materialise(5) == vector<size_t>({4, 3, 2}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_handle_empty_collections)
|
||||
{
|
||||
BOOST_TEST(MosaicSelection({1}, 1.0).materialise(0).empty());
|
||||
BOOST_TEST(MosaicSelection({1, 3}, 2.0).materialise(0).empty());
|
||||
BOOST_TEST(MosaicSelection({5, 4, 3, 2}, 0.5).materialise(0).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_handle_empty_selections)
|
||||
{
|
||||
BOOST_TEST(MosaicSelection({1}, 0.0).materialise(8).empty());
|
||||
BOOST_TEST(MosaicSelection({1, 3}, 0.0).materialise(8).empty());
|
||||
BOOST_TEST(MosaicSelection({5, 4, 3, 2}, 0.0).materialise(8).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_clamp_indices_at_collection_size)
|
||||
{
|
||||
BOOST_TEST(MosaicSelection({4, 3, 2, 1, 0}, 1.0).materialise(4) == vector<size_t>({3, 3, 2, 1}));
|
||||
BOOST_TEST(MosaicSelection({4, 3, 2, 1, 0}, 2.0).materialise(3) == vector<size_t>({2, 2, 2, 1, 0, 2}));
|
||||
BOOST_TEST(MosaicSelection({4, 3, 2, 1, 0}, 1.0).materialise(1) == vector<size_t>({0}));
|
||||
BOOST_TEST(MosaicSelection({4, 3, 2, 1, 0}, 7.0).materialise(1) == vector<size_t>({0, 0, 0, 0, 0, 0, 0}));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE(RandomSelectionTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_random_values_with_equal_probabilities)
|
||||
{
|
||||
constexpr int collectionSize = 10;
|
||||
constexpr int selectionSize = 100;
|
||||
constexpr double relativeTolerance = 0.1;
|
||||
constexpr double expectedValue = (collectionSize - 1) / 2.0;
|
||||
constexpr double variance = (collectionSize * collectionSize - 1) / 12.0;
|
||||
|
||||
SimulationRNG::reset(1);
|
||||
vector<size_t> samples = RandomSelection(selectionSize).materialise(collectionSize);
|
||||
|
||||
BOOST_TEST(abs(mean(samples) - expectedValue) < expectedValue * relativeTolerance);
|
||||
BOOST_TEST(abs(meanSquaredError(samples, expectedValue) - variance) < variance * relativeTolerance);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_only_values_that_can_be_used_as_collection_indices)
|
||||
{
|
||||
const size_t collectionSize = 200;
|
||||
|
||||
vector<size_t> indices = RandomSelection(0.5).materialise(collectionSize);
|
||||
|
||||
BOOST_TEST(indices.size() == 100);
|
||||
BOOST_TEST(all_of(indices.begin(), indices.end(), [&](auto const& index){ return index <= collectionSize; }));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_number_of_indices_thats_a_fraction_of_collection_size)
|
||||
{
|
||||
BOOST_TEST(RandomSelection(0.0).materialise(10).size() == 0);
|
||||
BOOST_TEST(RandomSelection(0.3).materialise(10).size() == 3);
|
||||
BOOST_TEST(RandomSelection(0.5).materialise(10).size() == 5);
|
||||
BOOST_TEST(RandomSelection(0.7).materialise(10).size() == 7);
|
||||
BOOST_TEST(RandomSelection(1.0).materialise(10).size() == 10);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_support_number_of_indices_bigger_than_collection_size)
|
||||
{
|
||||
BOOST_TEST(RandomSelection(2.0).materialise(5).size() == 10);
|
||||
BOOST_TEST(RandomSelection(1.5).materialise(10).size() == 15);
|
||||
BOOST_TEST(RandomSelection(10.0).materialise(10).size() == 100);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_round_the_number_of_indices_to_the_nearest_integer)
|
||||
{
|
||||
BOOST_TEST(RandomSelection(0.49).materialise(3).size() == 1);
|
||||
BOOST_TEST(RandomSelection(0.50).materialise(3).size() == 2);
|
||||
BOOST_TEST(RandomSelection(0.51).materialise(3).size() == 2);
|
||||
|
||||
BOOST_TEST(RandomSelection(1.51).materialise(3).size() == 5);
|
||||
|
||||
BOOST_TEST(RandomSelection(0.01).materialise(2).size() == 0);
|
||||
BOOST_TEST(RandomSelection(0.01).materialise(3).size() == 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(materialise_should_return_no_indices_if_collection_is_empty)
|
||||
{
|
||||
BOOST_TEST(RandomSelection(0.0).materialise(0).empty());
|
||||
BOOST_TEST(RandomSelection(0.5).materialise(0).empty());
|
||||
BOOST_TEST(RandomSelection(1.0).materialise(0).empty());
|
||||
BOOST_TEST(RandomSelection(2.0).materialise(0).empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user