[yul-phaser] Tests for Chromosome class

This commit is contained in:
cameel 2020-01-17 07:53:21 +01:00
parent f8e397b487
commit bee62cdd9e
2 changed files with 81 additions and 0 deletions

View File

@ -140,6 +140,12 @@ detect_stray_source_files("${libyul_sources}" "libyul/")
set(yul_phaser_sources
yulPhaser/Chromosome.cpp
# FIXME: yul-phaser is not a library so I can't just add it to target_link_libraries().
# My current workaround is just to include its source files here but this introduces
# unnecessary duplication. Create a library or find a way to reuse the list in both places.
../tools/yulPhaser/Chromosome.cpp
../tools/yulPhaser/Random.cpp
)
detect_stray_source_files("${yul_phaser_sources}" "yulPhaser/")

View File

@ -15,12 +15,87 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <tools/yulPhaser/Chromosome.h>
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/StructuralSimplifier.h>
#include <libyul/optimiser/Suite.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libsolutil/CommonIO.h>
#include <boost/test/unit_test.hpp>
using namespace std;
using namespace solidity::yul;
using namespace solidity::util;
namespace solidity::phaser::test
{
BOOST_AUTO_TEST_SUITE(Phaser)
BOOST_AUTO_TEST_SUITE(ChromosomeTest)
BOOST_AUTO_TEST_CASE(makeRandom_should_create_chromosome_with_random_optimisation_steps)
{
constexpr uint32_t numSteps = 1000;
auto chromosome1 = Chromosome::makeRandom(numSteps);
auto chromosome2 = Chromosome::makeRandom(numSteps);
BOOST_CHECK_EQUAL(chromosome1.length(), numSteps);
BOOST_CHECK_EQUAL(chromosome2.length(), numSteps);
multiset<string> steps1;
multiset<string> steps2;
for (auto const& step: chromosome1.optimisationSteps())
steps1.insert(step);
for (auto const& step: chromosome2.optimisationSteps())
steps2.insert(step);
// Check if steps are different and also if they're not just a permutation of the same set.
// Technically they could be the same and still random but the probability is infinitesimally low.
BOOST_TEST(steps1 != steps2);
}
BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps)
{
vector<string> steps = {
StructuralSimplifier::name,
BlockFlattener::name,
UnusedPruner::name,
};
Chromosome chromosome(steps);
BOOST_TEST(steps == chromosome.optimisationSteps());
}
BOOST_AUTO_TEST_CASE(constructor_should_allow_duplicate_steps)
{
vector<string> steps = {
StructuralSimplifier::name,
StructuralSimplifier::name,
BlockFlattener::name,
UnusedPruner::name,
BlockFlattener::name,
};
Chromosome chromosome(steps);
BOOST_TEST(steps == chromosome.optimisationSteps());
}
BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_string_representation)
{
vector<string> allSteps;
for (auto const& step: OptimiserSuite::allSteps())
allSteps.push_back(step.first);
Chromosome chromosome(allSteps);
BOOST_TEST(chromosome.length() == allSteps.size());
BOOST_TEST(chromosome.optimisationSteps() == allSteps);
BOOST_TEST(toString(chromosome) == "fcCUnDvejsxIOoighTLMrmVatud");
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
}