Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purge using namespace std from test/yulPhaser #14777

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/check_style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ NAMESPACE_STD_FREE_FILES=(
test/libyul/*
test/solc/*
test/tools/yulInterpreter/*
test/yulPhaser/*
)

(
Expand Down
125 changes: 62 additions & 63 deletions test/yulPhaser/AlgorithmRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <regex>
#include <sstream>

using namespace std;
using namespace boost::unit_test::framework;
using namespace boost::test_tools;
using namespace solidity::langutil;
Expand Down Expand Up @@ -74,44 +73,44 @@ class AlgorithmRunnerFixture
{
protected:
// NOTE: Regexes here should not contain spaces because we strip them before matching
regex RoundSummaryRegex{R"(-+ROUND\d+\[round:[0-9.]+s,total:[0-9.]+s\]-+)"};
regex InitialPopulationHeaderRegex{"-+INITIALPOPULATION-+"};
std::regex RoundSummaryRegex{R"(-+ROUND\d+\[round:[0-9.]+s,total:[0-9.]+s\]-+)"};
std::regex InitialPopulationHeaderRegex{"-+INITIALPOPULATION-+"};

string individualPattern(Individual const& individual) const
std::string individualPattern(Individual const& individual) const
{
ostringstream output;
std::ostringstream output;
output << individual.fitness << individual.chromosome;
return output.str();
}

string topChromosomePattern(size_t roundNumber, Individual const& individual) const
std::string topChromosomePattern(size_t roundNumber, Individual const& individual) const
{
ostringstream output;
std::ostringstream output;
output << roundNumber << R"(\|[0-9.]+\|)" << individualPattern(individual);
return output.str();
}

bool nextLineMatches(stringstream& stream, regex const& pattern) const
bool nextLineMatches(std::stringstream& stream, std::regex const& pattern) const
{
string line;
if (getline(stream, line).fail())
std::string line;
if (std::getline(stream, line).fail())
return false;

return regex_match(stripWhitespace(line), pattern);
return std::regex_match(stripWhitespace(line), pattern);
}

shared_ptr<FitnessMetric> m_fitnessMetric = make_shared<ChromosomeLengthMetric>();
std::shared_ptr<FitnessMetric> m_fitnessMetric = std::make_shared<ChromosomeLengthMetric>();
Population const m_population = Population::makeRandom(m_fitnessMetric, 5, 0, 20);
stringstream m_output;
std::stringstream m_output;
AlgorithmRunner::Options m_options;
};

class AlgorithmRunnerAutosaveFixture: public AlgorithmRunnerFixture
{
public:
static vector<string> chromosomeStrings(Population const& _population)
static std::vector<std::string> chromosomeStrings(Population const& _population)
{
vector<string> lines;
std::vector<std::string> lines;
for (auto const& individual: _population.individuals())
lines.push_back(toString(individual.chromosome));

Expand All @@ -120,7 +119,7 @@ class AlgorithmRunnerAutosaveFixture: public AlgorithmRunnerFixture

protected:
TemporaryDirectory m_tempDir;
string const m_autosavePath = (m_tempDir.path() / "population-autosave.txt").string();
std::string const m_autosavePath = (m_tempDir.path() / "population-autosave.txt").string();
RandomisingAlgorithm m_algorithm;
};

Expand Down Expand Up @@ -153,12 +152,12 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_round_summary_after_each_round, Algorit
runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, RoundSummaryRegex));
for (auto const& individual: runner.population().individuals())
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(individual))));
BOOST_TEST(nextLineMatches(m_output, std::regex(individualPattern(individual))));

runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, RoundSummaryRegex));
for (auto const& individual: runner.population().individuals())
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(individual))));
BOOST_TEST(nextLineMatches(m_output, std::regex(individualPattern(individual))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand All @@ -172,9 +171,9 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_print_round_summary_if_not_requested, Alg
RandomisingAlgorithm algorithm;

runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, regex("")));
BOOST_TEST(nextLineMatches(m_output, std::regex("")));
for (auto const& individual: runner.population().individuals())
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(individual))));
BOOST_TEST(nextLineMatches(m_output, std::regex(individualPattern(individual))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand Down Expand Up @@ -202,7 +201,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_only_top_chromosome_if_requested, Algor
RandomisingAlgorithm algorithm;

runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, regex(topChromosomePattern(1, runner.population().individuals()[0]))));
BOOST_TEST(nextLineMatches(m_output, std::regex(topChromosomePattern(1, runner.population().individuals()[0]))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand All @@ -216,7 +215,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_print_round_number_for_top_chromosome_if_
RandomisingAlgorithm algorithm;

runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(runner.population().individuals()[0]))));
BOOST_TEST(nextLineMatches(m_output, std::regex(individualPattern(runner.population().individuals()[0]))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand Down Expand Up @@ -246,7 +245,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_initial_population_if_requested, Algori

BOOST_TEST(nextLineMatches(m_output, InitialPopulationHeaderRegex));
for (auto const& individual: m_population.individuals())
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(individual))));
BOOST_TEST(nextLineMatches(m_output, std::regex(individualPattern(individual))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand Down Expand Up @@ -277,7 +276,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_whole_initial_population_even_if_only_t

BOOST_TEST(nextLineMatches(m_output, InitialPopulationHeaderRegex));
for (auto const& individual: m_population.individuals())
BOOST_TEST(nextLineMatches(m_output, regex(individualPattern(individual))));
BOOST_TEST(nextLineMatches(m_output, std::regex(individualPattern(individual))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand All @@ -290,21 +289,21 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_cache_stats_if_requested, AlgorithmRunn
m_options.showCacheStats = true;
RandomisingAlgorithm algorithm;

vector<CharStream> sourceStreams = {
std::vector<CharStream> sourceStreams = {
CharStream("{mstore(10, 20)}", ""),
CharStream("{mstore(10, 20)\nsstore(10, 20)}", ""),
};
vector<Program> programs = {
std::vector<Program> programs = {
get<Program>(Program::load(sourceStreams[0])),
get<Program>(Program::load(sourceStreams[1])),
};
vector<shared_ptr<ProgramCache>> caches = {
make_shared<ProgramCache>(programs[0]),
make_shared<ProgramCache>(programs[1]),
std::vector<std::shared_ptr<ProgramCache>> caches = {
std::make_shared<ProgramCache>(programs[0]),
std::make_shared<ProgramCache>(programs[1]),
};
shared_ptr<FitnessMetric> fitnessMetric = make_shared<FitnessMetricAverage>(vector<shared_ptr<FitnessMetric>>{
make_shared<ProgramSize>(nullopt, caches[0], CodeWeights{}),
make_shared<ProgramSize>(nullopt, caches[1], CodeWeights{}),
std::shared_ptr<FitnessMetric> fitnessMetric = std::make_shared<FitnessMetricAverage>(std::vector<std::shared_ptr<FitnessMetric>>{
std::make_shared<ProgramSize>(std::nullopt, caches[0], CodeWeights{}),
std::make_shared<ProgramSize>(std::nullopt, caches[1], CodeWeights{}),
});
Population population = Population::makeRandom(fitnessMetric, 2, 0, 5);

Expand All @@ -318,28 +317,28 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_cache_stats_if_requested, AlgorithmRunn

for (size_t i = 0; i < m_options.maxRounds.value() - 1; ++i)
{
BOOST_TEST(nextLineMatches(m_output, regex(".*")));
BOOST_TEST(nextLineMatches(m_output, regex("-+CACHESTATS-+")));
BOOST_TEST(nextLineMatches(m_output, std::regex(".*")));
BOOST_TEST(nextLineMatches(m_output, std::regex("-+CACHESTATS-+")));
if (i > 0)
BOOST_TEST(nextLineMatches(m_output, regex(R"(Round\d+:\d+entries)")));
BOOST_TEST(nextLineMatches(m_output, regex(R"(Round\d+:\d+entries)")));
BOOST_TEST(nextLineMatches(m_output, regex(R"(Totalhits:\d+)")));
BOOST_TEST(nextLineMatches(m_output, regex(R"(Totalmisses:\d+)")));
BOOST_TEST(nextLineMatches(m_output, regex(R"(Sizeofcachedcode:\d+)")));
BOOST_TEST(nextLineMatches(m_output, std::regex(R"(Round\d+:\d+entries)")));
BOOST_TEST(nextLineMatches(m_output, std::regex(R"(Round\d+:\d+entries)")));
BOOST_TEST(nextLineMatches(m_output, std::regex(R"(Totalhits:\d+)")));
BOOST_TEST(nextLineMatches(m_output, std::regex(R"(Totalmisses:\d+)")));
BOOST_TEST(nextLineMatches(m_output, std::regex(R"(Sizeofcachedcode:\d+)")));
}

BOOST_REQUIRE(stats.roundEntryCounts.size() == 2);
BOOST_REQUIRE(stats.roundEntryCounts.count(m_options.maxRounds.value() - 1) == 1);
BOOST_REQUIRE(stats.roundEntryCounts.count(m_options.maxRounds.value()) == 1);

size_t round = m_options.maxRounds.value();
BOOST_TEST(nextLineMatches(m_output, regex(".*")));
BOOST_TEST(nextLineMatches(m_output, regex("-+CACHESTATS-+")));
BOOST_TEST(nextLineMatches(m_output, regex("Round" + toString(round - 1) + ":" + toString(stats.roundEntryCounts[round - 1]) + "entries")));
BOOST_TEST(nextLineMatches(m_output, regex("Round" + toString(round) + ":" + toString(stats.roundEntryCounts[round]) + "entries")));
BOOST_TEST(nextLineMatches(m_output, regex("Totalhits:" + toString(stats.hits))));
BOOST_TEST(nextLineMatches(m_output, regex("Totalmisses:" + toString(stats.misses))));
BOOST_TEST(nextLineMatches(m_output, regex("Sizeofcachedcode:" + toString(stats.totalCodeSize))));
BOOST_TEST(nextLineMatches(m_output, std::regex(".*")));
BOOST_TEST(nextLineMatches(m_output, std::regex("-+CACHESTATS-+")));
BOOST_TEST(nextLineMatches(m_output, std::regex("Round" + toString(round - 1) + ":" + toString(stats.roundEntryCounts[round - 1]) + "entries")));
BOOST_TEST(nextLineMatches(m_output, std::regex("Round" + toString(round) + ":" + toString(stats.roundEntryCounts[round]) + "entries")));
BOOST_TEST(nextLineMatches(m_output, std::regex("Totalhits:" + toString(stats.hits))));
BOOST_TEST(nextLineMatches(m_output, std::regex("Totalmisses:" + toString(stats.misses))));
BOOST_TEST(nextLineMatches(m_output, std::regex("Sizeofcachedcode:" + toString(stats.totalCodeSize))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand All @@ -355,9 +354,9 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_message_if_cache_stats_requested_but_ca
AlgorithmRunner runner(m_population, {nullptr}, m_options, m_output);
runner.run(algorithm);

BOOST_TEST(nextLineMatches(m_output, regex(".*")));
BOOST_TEST(nextLineMatches(m_output, regex("-+CACHESTATS-+")));
BOOST_TEST(nextLineMatches(m_output, regex(stripWhitespace("Program cache disabled"))));
BOOST_TEST(nextLineMatches(m_output, std::regex(".*")));
BOOST_TEST(nextLineMatches(m_output, std::regex("-+CACHESTATS-+")));
BOOST_TEST(nextLineMatches(m_output, std::regex(stripWhitespace("Program cache disabled"))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand All @@ -371,18 +370,18 @@ BOOST_FIXTURE_TEST_CASE(run_should_print_partial_stats_and_message_if_some_cache
RandomisingAlgorithm algorithm;

CharStream sourceStream = CharStream("{}", "");
shared_ptr<ProgramCache> cache = make_shared<ProgramCache>(get<Program>(Program::load(sourceStream)));
std::shared_ptr<ProgramCache> cache = std::make_shared<ProgramCache>(std::get<Program>(Program::load(sourceStream)));

AlgorithmRunner runner(m_population, {cache, nullptr}, m_options, m_output);
BOOST_REQUIRE(cache->gatherStats().roundEntryCounts.size() == 0);

runner.run(algorithm);
BOOST_TEST(nextLineMatches(m_output, regex(".*")));
BOOST_TEST(nextLineMatches(m_output, regex("-+CACHESTATS-+")));
BOOST_TEST(nextLineMatches(m_output, regex(R"(Totalhits:\d+)")));
BOOST_TEST(nextLineMatches(m_output, regex(R"(Totalmisses:\d+)")));
BOOST_TEST(nextLineMatches(m_output, regex(R"(Sizeofcachedcode:\d+)")));
BOOST_TEST(nextLineMatches(m_output, regex(stripWhitespace("Program cache disabled for 1 out of 2 programs"))));
BOOST_TEST(nextLineMatches(m_output, std::regex(".*")));
BOOST_TEST(nextLineMatches(m_output, std::regex("-+CACHESTATS-+")));
BOOST_TEST(nextLineMatches(m_output, std::regex(R"(Totalhits:\d+)")));
BOOST_TEST(nextLineMatches(m_output, std::regex(R"(Totalmisses:\d+)")));
BOOST_TEST(nextLineMatches(m_output, std::regex(R"(Sizeofcachedcode:\d+)")));
BOOST_TEST(nextLineMatches(m_output, std::regex(stripWhitespace("Program cache disabled for 1 out of 2 programs"))));
BOOST_TEST(m_output.peek() == EOF);
}

Expand Down Expand Up @@ -421,10 +420,10 @@ BOOST_FIXTURE_TEST_CASE(run_should_overwrite_existing_file_if_autosave_file_spec
AlgorithmRunner runner(m_population, {}, m_options, m_output);
assert(!fs::exists(m_autosavePath));

vector<string> originalContent = {"Original content"};
std::vector<std::string> originalContent = {"Original content"};
{
ofstream tmpFile(m_autosavePath);
tmpFile << originalContent[0] << endl;
std::ofstream tmpFile(m_autosavePath);
tmpFile << originalContent[0] << std::endl;
}
assert(fs::exists(m_autosavePath));
assert(readLinesFromFile(m_autosavePath) == originalContent);
Expand All @@ -438,7 +437,7 @@ BOOST_FIXTURE_TEST_CASE(run_should_overwrite_existing_file_if_autosave_file_spec
BOOST_FIXTURE_TEST_CASE(run_should_not_save_population_to_file_if_autosave_file_not_specified, AlgorithmRunnerAutosaveFixture)
{
m_options.maxRounds = 5;
m_options.populationAutosaveFile = nullopt;
m_options.populationAutosaveFile = std::nullopt;
AlgorithmRunner runner(m_population, {}, m_options, m_output);
assert(!fs::exists(m_autosavePath));

Expand Down Expand Up @@ -499,9 +498,9 @@ BOOST_FIXTURE_TEST_CASE(run_should_not_randomise_duplicate_chromosomes_if_not_re
BOOST_FIXTURE_TEST_CASE(run_should_clear_cache_at_the_beginning_and_update_it_before_each_round, AlgorithmRunnerFixture)
{
CharStream sourceStream = CharStream("{}", current_test_case().p_name);
vector<shared_ptr<ProgramCache>> caches = {
make_shared<ProgramCache>(get<Program>(Program::load(sourceStream))),
make_shared<ProgramCache>(get<Program>(Program::load(sourceStream))),
std::vector<std::shared_ptr<ProgramCache>> caches = {
std::make_shared<ProgramCache>(std::get<Program>(Program::load(sourceStream))),
std::make_shared<ProgramCache>(std::get<Program>(Program::load(sourceStream))),
};

m_options.maxRounds = 10;
Expand Down
19 changes: 9 additions & 10 deletions test/yulPhaser/Chromosome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@

#include <boost/test/unit_test.hpp>

using namespace std;
using namespace solidity::yul;
using namespace solidity::util;

namespace solidity::phaser::test
{

vector<string> const ChrOmOsoMeSteps{
std::vector<std::string> const ChrOmOsoMeSteps{
ConditionalSimplifier::name,
FunctionHoister::name,
UnusedAssignEliminator::name,
Expand Down Expand Up @@ -76,10 +75,10 @@ BOOST_AUTO_TEST_CASE(makeRandom_should_use_every_possible_step_with_the_same_pro
constexpr int samplesPerStep = 500;
constexpr double relativeTolerance = 0.02;

map<string, size_t> stepIndices = enumerateOptmisationSteps();
std::map<std::string, size_t> stepIndices = enumerateOptmisationSteps();
auto chromosome = Chromosome::makeRandom(stepIndices.size() * samplesPerStep);

vector<size_t> samples;
std::vector<size_t> samples;
for (auto& step: chromosome.optimisationSteps())
samples.push_back(stepIndices.at(step));

Expand All @@ -97,7 +96,7 @@ BOOST_AUTO_TEST_CASE(constructor_should_store_genes)

BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps)
{
vector<string> steps = {
std::vector<std::string> steps = {
StructuralSimplifier::name,
BlockFlattener::name,
UnusedPruner::name,
Expand All @@ -108,7 +107,7 @@ BOOST_AUTO_TEST_CASE(constructor_should_store_optimisation_steps)

BOOST_AUTO_TEST_CASE(constructor_should_allow_duplicate_steps)
{
vector<string> steps = {
std::vector<std::string> steps = {
StructuralSimplifier::name,
StructuralSimplifier::name,
BlockFlattener::name,
Expand All @@ -131,7 +130,7 @@ BOOST_AUTO_TEST_CASE(constructor_should_allow_genes_that_do_not_correspond_to_an

BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_string_representation)
{
vector<string> allSteps;
std::vector<std::string> allSteps;
for (auto const& step: OptimiserSuite::allSteps())
allSteps.push_back(step.first);
Chromosome chromosome(allSteps);
Expand All @@ -152,8 +151,8 @@ BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_pr
constexpr int samplesPerStep = 500;
constexpr double relativeTolerance = 0.02;

map<string, size_t> stepIndices = enumerateOptmisationSteps();
vector<size_t> samples;
std::map<std::string, size_t> stepIndices = enumerateOptmisationSteps();
std::vector<size_t> samples;
for (size_t i = 0; i <= stepIndices.size() * samplesPerStep; ++i)
samples.push_back(stepIndices.at(Chromosome::randomOptimisationStep()));

Expand All @@ -172,7 +171,7 @@ BOOST_AUTO_TEST_CASE(stepsToGenes_should_translate_optimisation_step_names_to_ab

BOOST_AUTO_TEST_CASE(genesToSteps_should_translate_optimisation_step_abbreviations_to_names)
{
BOOST_TEST(Chromosome::genesToSteps("") == vector<string>{});
BOOST_TEST(Chromosome::genesToSteps("") == std::vector<std::string>{});
BOOST_TEST(Chromosome::genesToSteps("ChrOmOsoMe") == ChrOmOsoMeSteps);
}

Expand Down
Loading