mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Introduce a piecewise constant dist over [-1, 1]
This commit is contained in:
parent
cb1e6b3179
commit
30550429eb
@ -63,7 +63,7 @@ size_t LPSolverCustomMutatorInterface::generate()
|
||||
string testCase = generator->generate();
|
||||
solAssert(
|
||||
!testCase.empty() && data,
|
||||
"Solc custom mutator: Invalid mutant or memory pointer"
|
||||
"LP constraints custom mutator: Invalid mutant or memory pointer"
|
||||
);
|
||||
size_t mutantSize = min(testCase.size(), maxMutantSize - 1);
|
||||
mempcpy(data, testCase.data(), mutantSize);
|
||||
|
@ -30,9 +30,12 @@ string ConstraintGenerator::generate()
|
||||
{
|
||||
// First entry is always constraint type. If it is equal to "1", it is an equality constraint
|
||||
// otherwise an less-than-equal constraint.
|
||||
constraint += to_string(randomBool());
|
||||
constraint += to_string(zeroOrOne());
|
||||
for (int j = 0; j < numFactors(); j++)
|
||||
constraint += "," + to_string(randomInteger());
|
||||
if (bernoulliDist(s_piecewiseConstantProb))
|
||||
constraint += "," + to_string(randomMinusOneToOne());
|
||||
else
|
||||
constraint += "," + to_string(randomInteger());
|
||||
constraint += "\n";
|
||||
}
|
||||
return constraint;
|
||||
|
@ -28,6 +28,7 @@ namespace solidity::test::fuzzer::lpsolver
|
||||
|
||||
using RandomEngine = std::mt19937;
|
||||
using Distribution = std::uniform_int_distribution<int>;
|
||||
using Bernoulli = std::bernoulli_distribution;
|
||||
|
||||
struct ConstraintGenerator
|
||||
{
|
||||
@ -54,18 +55,32 @@ struct ConstraintGenerator
|
||||
return Distribution(std::numeric_limits<int>::min(), std::numeric_limits<int>::max())(*prng);
|
||||
}
|
||||
|
||||
/// @returns a boolean integer [0, 1]
|
||||
int randomBool()
|
||||
/// @returns an integer in the range [-1, 1] chosen uniformly at random.
|
||||
int randomMinusOneToOne()
|
||||
{
|
||||
return Distribution(-1, 1)(*prng);
|
||||
}
|
||||
|
||||
/// @returns zero or one with a probability of occurance of 0.5 each.
|
||||
int zeroOrOne()
|
||||
{
|
||||
return Distribution(0, 1)(*prng);
|
||||
}
|
||||
|
||||
/// @returns true with a probability @param _p, false otherwise.
|
||||
bool bernoulliDist(double _truthProbability)
|
||||
{
|
||||
return Bernoulli(_truthProbability)(*prng);
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<RandomEngine> prng;
|
||||
|
||||
static constexpr int s_minFactors = 2;
|
||||
static constexpr int s_maxFactors = 10;
|
||||
static constexpr int s_minConstraints = 1;
|
||||
static constexpr int s_maxConstraints = 10;
|
||||
static constexpr double s_piecewiseConstantProb = 0.25;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user