diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 7cf466e0b..37a78287d 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -139,10 +139,13 @@ set(libyul_sources
detect_stray_source_files("${libyul_sources}" "libyul/")
set(yul_phaser_sources
+ yulPhaser/Common.h
+ yulPhaser/Common.cpp
+ yulPhaser/CommonTest.cpp
yulPhaser/Chromosome.cpp
yulPhaser/Population.cpp
yulPhaser/Program.cpp
- yulPhaser/Random.cpp
+ yulPhaser/SimulationRNG.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
@@ -150,7 +153,7 @@ set(yul_phaser_sources
../tools/yulPhaser/Chromosome.cpp
../tools/yulPhaser/Population.cpp
../tools/yulPhaser/Program.cpp
- ../tools/yulPhaser/Random.cpp
+ ../tools/yulPhaser/SimulationRNG.cpp
)
detect_stray_source_files("${yul_phaser_sources}" "yulPhaser/")
diff --git a/test/yulPhaser/Chromosome.cpp b/test/yulPhaser/Chromosome.cpp
index bd07e1b2e..13f2f6811 100644
--- a/test/yulPhaser/Chromosome.cpp
+++ b/test/yulPhaser/Chromosome.cpp
@@ -15,7 +15,10 @@
along with solidity. If not, see .
*/
+#include
+
#include
+#include
#include
#include
@@ -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 stepIndices = enumerateOptmisationSteps();
+ vector 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()
diff --git a/tools/yulPhaser/Random.h b/test/yulPhaser/Common.cpp
similarity index 60%
rename from tools/yulPhaser/Random.h
rename to test/yulPhaser/Common.cpp
index 25091ddef..3cef4ff2b 100644
--- a/tools/yulPhaser/Random.h
+++ b/test/yulPhaser/Common.cpp
@@ -15,14 +15,20 @@
along with solidity. If not, see .
*/
-#pragma once
+#include
-#include
+#include
-namespace solidity::phaser
+using namespace std;
+using namespace solidity;
+using namespace solidity::yul;
+
+map phaser::test::enumerateOptmisationSteps()
{
+ map stepIndices;
+ size_t i = 0;
+ for (auto const& nameAndAbbreviation: OptimiserSuite::stepNameToAbbreviationMap())
+ stepIndices.insert({nameAndAbbreviation.first, i++});
-uint32_t uniformRandomInt(uint32_t _min, uint32_t _max);
-uint32_t binomialRandomInt(uint32_t _numTrials, double _successProbability);
-
+ return stepIndices;
}
diff --git a/test/yulPhaser/Common.h b/test/yulPhaser/Common.h
new file mode 100644
index 000000000..a3dd24bce
--- /dev/null
+++ b/test/yulPhaser/Common.h
@@ -0,0 +1,88 @@
+/*
+ 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 .
+*/
+/**
+ * Miscellaneous utilities for use in yul-phaser's test cases.
+ *
+ * - Generic code that's only used in these particular tests.
+ * - Convenience functions and wrappers to make tests more concise.
+ * - Mocks and dummy objects/functions used in multiple test suites.
+ *
+ * Note that the code included here may be not as generic, robust and/or complete as it could be
+ * since it's not meant for production use. If some utility seems useful enough to be moved to
+ * the normal code base, you should review its implementation before doing so.
+ */
+
+#pragma once
+
+#include
+#include