Pseudo randomly add checks

This commit is contained in:
Bhargava Shastry 2019-07-29 16:38:24 +02:00
parent 00accd9daa
commit 3a6f28589e
2 changed files with 56 additions and 30 deletions

View File

@ -328,7 +328,7 @@ void ProtoConverter::visit(StructType const&)
std::string ProtoConverter::arrayDimInfoAsString(ArrayDimensionInfo const& _x)
{
unsigned arrLength = getArrayLengthFromFuzz(_x.length());
unsigned arrLength = getStaticArrayLengthFromFuzz(_x.length());
if (_x.is_static())
return Whiskers(R"([<length>])")
("length", std::to_string(arrLength))
@ -398,7 +398,7 @@ ProtoConverter::DataType ProtoConverter::getDataTypeByBaseType(ArrayType const&
// Adds a resize operation for a given dimension of type `_type` and expression referenced
// by `_var`. `_isStatic` is true for statically sized dimensions, false otherwise.
// `_arrayLen` is equal to length of statically sized array dimension. For dynamically
// sized dimension, we use `getArrayLengthFromFuzz()` and a monotonically increasing
// sized dimension, we use `getDynArrayLengthFromFuzz()` and a monotonically increasing
// counter to obtain actual length. Function returns dimension length.
unsigned ProtoConverter::resizeDimension(
bool _isStatic,
@ -413,7 +413,7 @@ unsigned ProtoConverter::resizeDimension(
length = _arrayLen;
else
{
length = getArrayLengthFromFuzz(_arrayLen, getNextCounter());
length = getDynArrayLengthFromFuzz(_arrayLen, getNextCounter());
// If local var, new T(l);
// Else, l;
@ -438,6 +438,8 @@ unsigned ProtoConverter::resizeDimension(
addVarDef(lhs, rhs);
}
// Add checks on array length pseudo randomly
if (addCheck(getNextCounter()))
// if (c.length != l)
checkResizeOp(_param, length);
return length;
@ -454,6 +456,12 @@ void ProtoConverter::resizeHelper(
// Initialize value expressions if we have arrived at leaf node,
// (depth-first) recurse otherwise.
if (_arrInfoVec.empty())
{
// We are at the leaf node now.
// To ensure we do not create a very large test case
// especially for multidimensional dynamic arrays,
// we create a checked assignment pseudo randomly.
if (addCheck(getNextCounter()))
{
// expression name is _var
// value is a value of base type
@ -462,6 +470,7 @@ void ProtoConverter::resizeHelper(
DataType dataType = getDataTypeByBaseType(_x);
addCheckedVarDef(dataType, _varName, _paramName, value);
}
}
else
{
auto& dim = _arrInfoVec.back();

View File

@ -334,14 +334,27 @@ private:
return toHex(maskUnsignedInt(_counter, _numMaskNibbles), HexPrefix::Add);
}
static unsigned getArrayLengthFromFuzz(unsigned _fuzz, unsigned _counter = 0)
static unsigned getDynArrayLengthFromFuzz(unsigned _fuzz, unsigned _counter)
{
return ((_fuzz + _counter) % s_maxArrayLength) + 1;
return ((_fuzz + _counter) % s_maxDynamicArrayLength) + 1;
}
static unsigned getStaticArrayLengthFromFuzz(unsigned _fuzz)
{
return (_fuzz % s_maxStaticArrayLength) + 1;
}
static std::pair<bool, unsigned> arrayDimInfoAsPair(ArrayDimensionInfo const& _x)
{
return std::make_pair(_x.is_static(), getArrayLengthFromFuzz(_x.length()));
return std::make_pair(
_x.is_static(),
getStaticArrayLengthFromFuzz(_x.length())
);
}
static bool addCheck(unsigned _counter)
{
return (_counter % s_arrayCheckFrequency == 0);
}
/// Contains the test program
@ -360,8 +373,12 @@ private:
unsigned m_varCounter;
/// Monotonically increasing return value for error reporting
unsigned m_returnValue;
static unsigned constexpr s_maxArrayLength = 2;
static unsigned constexpr s_maxArrayDimensions = 10;
static unsigned constexpr s_maxStaticArrayLength = 7;
static unsigned constexpr s_maxDynamicArrayLength = 5;
static unsigned constexpr s_maxArrayDimensions = 21;
/// Add check only if counter returned by getNextCounter()
/// is divisible by s_arrayCheckFrequency
static unsigned constexpr s_arrayCheckFrequency = 11;
/// Prefixes for declared and parameterized variable names
static auto constexpr s_varNamePrefix = "x_";
static auto constexpr s_paramNamePrefix = "c_";