2020-02-13 20:33:37 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-02-13 20:33:37 +00:00
|
|
|
|
2020-03-02 08:40:58 +00:00
|
|
|
#include <test/yulPhaser/TestHelpers.h>
|
2020-02-14 22:42:01 +00:00
|
|
|
|
2020-02-14 18:37:43 +00:00
|
|
|
#include <libyul/optimiser/Suite.h>
|
|
|
|
|
2020-03-04 13:25:31 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2020-02-13 20:33:37 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2020-03-04 13:25:31 +00:00
|
|
|
#include <fstream>
|
2020-02-14 18:37:43 +00:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity::yul;
|
2020-02-14 22:42:01 +00:00
|
|
|
using namespace boost::test_tools;
|
|
|
|
|
2020-03-04 13:25:31 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2020-02-13 20:33:37 +00:00
|
|
|
namespace solidity::phaser::test
|
|
|
|
{
|
|
|
|
|
2020-07-08 15:56:14 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE(Phaser, *boost::unit_test::label("nooptions"))
|
2020-03-02 08:40:58 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE(TestHelpersTest)
|
2020-02-13 20:33:37 +00:00
|
|
|
|
2020-02-15 00:40:18 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(ChromosomeLengthMetric_evaluate_should_return_chromosome_length)
|
|
|
|
{
|
|
|
|
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome()) == 0);
|
|
|
|
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome("a")) == 1);
|
|
|
|
BOOST_TEST(ChromosomeLengthMetric{}.evaluate(Chromosome("aaaaa")) == 5);
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:23:17 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(wholeChromosomeReplacement_should_replace_whole_chromosome_with_another)
|
|
|
|
{
|
|
|
|
function<Mutation> mutation = wholeChromosomeReplacement(Chromosome("aaa"));
|
|
|
|
BOOST_TEST(mutation(Chromosome("ccc")) == Chromosome("aaa"));
|
2020-02-14 18:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(geneSubstitution_should_change_a_single_gene_at_a_given_index)
|
|
|
|
{
|
|
|
|
Chromosome chromosome("aaccff");
|
|
|
|
|
|
|
|
function<Mutation> mutation1 = geneSubstitution(0, chromosome.optimisationSteps()[5]);
|
|
|
|
BOOST_TEST(mutation1(chromosome) == Chromosome("faccff"));
|
|
|
|
|
|
|
|
function<Mutation> mutation2 = geneSubstitution(5, chromosome.optimisationSteps()[0]);
|
|
|
|
BOOST_TEST(mutation2(chromosome) == Chromosome("aaccfa"));
|
2020-02-14 19:23:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 18:41:43 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(chromosomeLengths_should_return_lengths_of_all_chromosomes_in_a_population)
|
|
|
|
{
|
2020-02-15 00:40:18 +00:00
|
|
|
shared_ptr<FitnessMetric> fitnessMetric = make_shared<ChromosomeLengthMetric>();
|
2020-02-14 18:41:43 +00:00
|
|
|
|
2020-02-15 00:40:18 +00:00
|
|
|
Population population1(fitnessMetric, {Chromosome(), Chromosome("a"), Chromosome("aa"), Chromosome("aaa")});
|
2020-02-14 18:41:43 +00:00
|
|
|
BOOST_TEST((chromosomeLengths(population1) == vector<size_t>{0, 1, 2, 3}));
|
|
|
|
|
2020-02-15 00:40:18 +00:00
|
|
|
Population population2(fitnessMetric);
|
2020-02-14 18:41:43 +00:00
|
|
|
BOOST_TEST((chromosomeLengths(population2) == vector<size_t>{}));
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:23:17 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(countDifferences_should_return_zero_for_identical_chromosomes)
|
|
|
|
{
|
|
|
|
BOOST_TEST(countDifferences(Chromosome(), Chromosome()) == 0);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("a"), Chromosome("a")) == 0);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("afxT"), Chromosome("afxT")) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(countDifferences_should_count_mismatched_positions_in_chromosomes_of_the_same_length)
|
|
|
|
{
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("a"), Chromosome("f")) == 1);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("ac")) == 1);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("ac"), Chromosome("cc")) == 1);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("cc")) == 2);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("afxT"), Chromosome("Txfa")) == 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(countDifferences_should_count_missing_characters_as_differences)
|
|
|
|
{
|
|
|
|
BOOST_TEST(countDifferences(Chromosome(""), Chromosome("a")) == 1);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("a"), Chromosome("")) == 1);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("")) == 2);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("aaa"), Chromosome("")) == 3);
|
|
|
|
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("aaaa")) == 2);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("aacc")) == 2);
|
|
|
|
BOOST_TEST(countDifferences(Chromosome("aa"), Chromosome("cccc")) == 4);
|
|
|
|
}
|
|
|
|
|
2020-02-14 18:37:43 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(enumerateOptimisationSteps_should_assing_indices_to_all_available_optimisation_steps)
|
|
|
|
{
|
|
|
|
map<string, char> stepsAndAbbreviations = OptimiserSuite::stepNameToAbbreviationMap();
|
|
|
|
map<string, size_t> stepsAndIndices = enumerateOptmisationSteps();
|
|
|
|
BOOST_TEST(stepsAndIndices.size() == stepsAndAbbreviations.size());
|
|
|
|
|
|
|
|
set<string> stepsSoFar;
|
|
|
|
for (auto& [name, index]: stepsAndIndices)
|
|
|
|
{
|
|
|
|
BOOST_TEST(index >= 0);
|
|
|
|
BOOST_TEST(index <= stepsAndAbbreviations.size());
|
|
|
|
BOOST_TEST(stepsAndAbbreviations.count(name) == 1);
|
|
|
|
BOOST_TEST(stepsSoFar.count(name) == 0);
|
|
|
|
|
|
|
|
stepsSoFar.insert(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 13:25:31 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_create_and_delete_a_unique_and_empty_directory)
|
|
|
|
{
|
|
|
|
fs::path dirPath;
|
|
|
|
{
|
|
|
|
TemporaryDirectory tempDir("temporary-directory-test-");
|
|
|
|
dirPath = tempDir.path();
|
|
|
|
|
|
|
|
BOOST_TEST(dirPath.stem().string().find("temporary-directory-test-") == 0);
|
|
|
|
BOOST_TEST(fs::equivalent(dirPath.parent_path(), fs::temp_directory_path()));
|
|
|
|
BOOST_TEST(fs::is_directory(dirPath));
|
|
|
|
BOOST_TEST(fs::is_empty(dirPath));
|
|
|
|
}
|
|
|
|
BOOST_TEST(!fs::exists(dirPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(TemporaryDirectory_should_delete_its_directory_even_if_not_empty)
|
|
|
|
{
|
|
|
|
fs::path dirPath;
|
|
|
|
{
|
|
|
|
TemporaryDirectory tempDir("temporary-directory-test-");
|
|
|
|
dirPath = tempDir.path();
|
|
|
|
|
|
|
|
BOOST_TEST(fs::is_directory(dirPath));
|
|
|
|
|
|
|
|
{
|
|
|
|
ofstream tmpFile((dirPath / "test-file.txt").string());
|
|
|
|
tmpFile << "Delete me!" << endl;
|
|
|
|
}
|
|
|
|
assert(fs::is_regular_file(dirPath / "test-file.txt"));
|
|
|
|
}
|
|
|
|
BOOST_TEST(!fs::exists(dirPath / "test-file.txt"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(TemporaryDirectory_memberPath_should_construct_paths_relative_to_the_temporary_directory)
|
|
|
|
{
|
|
|
|
TemporaryDirectory tempDir("temporary-directory-test-");
|
|
|
|
|
|
|
|
BOOST_TEST(fs::equivalent(tempDir.memberPath(""), tempDir.path()));
|
|
|
|
BOOST_TEST(fs::equivalent(tempDir.memberPath("."), tempDir.path() / fs::path(".")));
|
|
|
|
BOOST_TEST(fs::equivalent(tempDir.memberPath(".."), tempDir.path() / fs::path("..")));
|
|
|
|
|
|
|
|
// NOTE: fs::equivalent() only works with paths that actually exist
|
|
|
|
{
|
|
|
|
ofstream file;
|
|
|
|
file.open(tempDir.memberPath("file.txt"), ios::out);
|
|
|
|
}
|
|
|
|
BOOST_TEST(fs::equivalent(tempDir.memberPath("file.txt"), tempDir.path() / fs::path("file.txt")));
|
|
|
|
|
|
|
|
{
|
|
|
|
fs::create_directories(tempDir.memberPath("a/b/"));
|
|
|
|
|
|
|
|
ofstream file;
|
|
|
|
file.open(tempDir.memberPath("a/b/file.txt"), ios::out);
|
|
|
|
}
|
|
|
|
BOOST_TEST(fs::equivalent(tempDir.memberPath("a/b/file.txt"), tempDir.path() / fs::path("a") / fs::path("b") / fs::path("file.txt")));
|
|
|
|
}
|
|
|
|
|
2020-02-14 23:09:43 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(stripWhitespace_should_remove_all_whitespace_characters_from_a_string)
|
|
|
|
{
|
|
|
|
BOOST_TEST(stripWhitespace("") == "");
|
|
|
|
BOOST_TEST(stripWhitespace(" ") == "");
|
|
|
|
BOOST_TEST(stripWhitespace(" \n\t\v ") == "");
|
|
|
|
|
|
|
|
BOOST_TEST(stripWhitespace("abc") == "abc");
|
|
|
|
BOOST_TEST(stripWhitespace(" abc") == "abc");
|
|
|
|
BOOST_TEST(stripWhitespace("abc ") == "abc");
|
|
|
|
BOOST_TEST(stripWhitespace(" a b c ") == "abc");
|
|
|
|
BOOST_TEST(stripWhitespace(" a b\tc\n") == "abc");
|
|
|
|
BOOST_TEST(stripWhitespace(" a b \n\n c \n\t\v") == "abc");
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:37:49 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-14 22:42:01 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(mean_should_calculate_statistical_mean)
|
|
|
|
{
|
|
|
|
BOOST_TEST(mean<int>({0}) == 0.0);
|
|
|
|
BOOST_TEST(mean<int>({0, 0, 0, 0}) == 0.0);
|
|
|
|
BOOST_TEST(mean<int>({5, 5, 5, 5}) == 5.0);
|
|
|
|
BOOST_TEST(mean<int>({0, 1, 2, 3}) == 1.5);
|
|
|
|
BOOST_TEST(mean<int>({-4, -3, -2, -1, 0, 1, 2, 3}) == -0.5);
|
|
|
|
|
|
|
|
BOOST_TEST(mean<double>({1.3, 1.1, 0.0, 1.5, 1.1, 2.0, 1.5, 1.5}) == 1.25);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(meanSquaredError_should_calculate_average_squared_difference_between_samples_and_expected_value)
|
|
|
|
{
|
|
|
|
BOOST_TEST(meanSquaredError<int>({0}, 0.0) == 0.0);
|
|
|
|
BOOST_TEST(meanSquaredError<int>({0}, 1.0) == 1.0);
|
|
|
|
BOOST_TEST(meanSquaredError<int>({0, 0, 0, 0}, 0.0) == 0.0);
|
|
|
|
BOOST_TEST(meanSquaredError<int>({0, 0, 0, 0}, 1.0) == 1.0);
|
|
|
|
BOOST_TEST(meanSquaredError<int>({0, 0, 0, 0}, 2.0) == 4.0);
|
|
|
|
BOOST_TEST(meanSquaredError<int>({5, 5, 5, 5}, 1.0) == 16.0);
|
|
|
|
BOOST_TEST(meanSquaredError<int>({0, 1, 2, 3}, 2.0) == 1.5);
|
|
|
|
BOOST_TEST(meanSquaredError<int>({-4, -3, -2, -1, 0, 1, 2, 3}, -4.0) == 17.5);
|
|
|
|
|
|
|
|
BOOST_TEST(meanSquaredError<double>({1.3, 1.1, 0.0, 1.5, 1.1, 2.0, 1.5, 1.5}, 1.0) == 0.3575, tolerance(0.0001));
|
|
|
|
}
|
|
|
|
|
2020-02-13 20:33:37 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|