mirror of
				https://github.com/ethereum/solidity
				synced 2023-10-03 13:03:40 +00:00 
			
		
		
		
	[yul-phaser] Tests for Chromosome class
This commit is contained in:
		
							parent
							
								
									f8e397b487
								
							
						
					
					
						commit
						bee62cdd9e
					
				| @ -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/") | ||||
| 
 | ||||
|  | ||||
| @ -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() | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user