Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added boundary value generator functions for Sint8,
Browse files Browse the repository at this point in the history
Sint16, Sint32 and Sint64.
  • Loading branch information
mkauppila committed Aug 9, 2011
1 parent 67abf18 commit 35aa235
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 6 deletions.
187 changes: 184 additions & 3 deletions test/test-automation/src/libSDLtest/fuzzer/fuzzer.c
Expand Up @@ -20,6 +20,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#include "../../../include/SDL_test.h"

Expand Down Expand Up @@ -318,12 +319,192 @@ RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDoma
return retVal;
}

/*!
* Generates boundary values between the given boundaries.
* Boundary values are inclusive. See the examples below.
* If boundary2 < boundary1, the values are swapped.
* If boundary1 == boundary2, value of boundary1 will be returned
*
* Generating boundary values for Sint8:
* <Add examples>
*
* Generator works the same for other types of signed integers.
*
* \paran minValue The smallest value that is acceptable for this data type.
* For instance, for Uint8 -> -128, Uint16 -> -32,768 etc.
* \param maxValue The biggest value that is acceptable for this data type.
* For instance, for Uint8 -> 127, Uint16 -> 32767 etc.
* \param pBoundary1 defines lower boundary
* \param pBoundary2 defines upper boundary
* \param validDomain Generate only for valid domain (for the data type)
*
* \param outBuffer The generated boundary values are put here
* \param outBufferSize Size of outBuffer
*
* \returns NULL on error, outBuffer on success
*/
Uint64 *
GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue,
Sint64 pBoundary1, Sint64 pBoundary2, SDL_bool validDomain,
Sint64 *outBuffer, Uint32 *outBufferSize)
{
Sint64 boundary1 = pBoundary1, boundary2 = pBoundary2;

if(outBuffer != NULL) {
SDL_free(outBuffer);
}

if(boundary1 > boundary2) {
Sint64 temp = boundary1;
boundary1 = boundary2;
boundary2 = temp;
}

Sint64 tempBuf[8];
memset(tempBuf, 0, 8 * sizeof(Sint64));

Sint64 index = 0;

if(boundary1 == boundary2) {
tempBuf[index++] = boundary1;
}
else if(validDomain) {
tempBuf[index++] = boundary1;

if(boundary1 < LLONG_MAX)
tempBuf[index++] = boundary1 + 1;

if(boundary2 > LLONG_MIN)
tempBuf[index++] = boundary2 - 1;

tempBuf[index++] = boundary2;
}
else {
if(boundary1 > minValue && boundary1 > LLONG_MIN) {
tempBuf[index++] = boundary1 - 1;
}

if(boundary2 < maxValue && boundary2 < UINT64_MAX) {
tempBuf[index++] = boundary2 + 1;
}
}

if(index == 0) {
// There are no valid boundaries
return NULL;
}

// Create the return buffer
outBuffer = SDL_malloc(index * sizeof(Sint64));
if(outBuffer == NULL) {
return NULL;
}

SDL_memcpy(outBuffer, tempBuf, index * sizeof(Sint64));

*outBufferSize = index;

return outBuffer;
}

Sint8
RandomSint8BoundaryValue()
RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain)
{
Sint64 *buffer = NULL;
Uint32 size;

// min & max values for Sint8
const Sint64 maxValue = CHAR_MAX;
const Sint64 minValue = CHAR_MIN;

buffer = GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain, buffer, &size);
if(buffer == NULL) {
return CHAR_MIN;
}

Uint32 index = RandomInteger() % size;
Sint8 retVal = (Sint8) buffer[index];

SDL_free(buffer);

return retVal;
}

Sint16
RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain)
{
Sint64 *buffer = NULL;
Uint32 size;

// min & max values for Sint16
const Sint64 maxValue = SHRT_MAX;
const Sint64 minValue = SHRT_MIN;

buffer = GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain, buffer, &size);
if(buffer == NULL) {
return SHRT_MIN;
}

Uint32 index = RandomInteger() % size;
Sint16 retVal = (Sint16) buffer[index];

SDL_free(buffer);

return retVal;
}

Sint32
RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
{
int value = 0; //GenerateBoundaryValueForSize(8);
Sint64 *buffer = NULL;
Uint32 size;

// min & max values for Sint32
const Sint64 maxValue = INT_MAX;
const Sint64 minValue = INT_MIN;

buffer = GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain, buffer, &size);
if(buffer == NULL) {
return INT_MIN;
}

Uint32 index = RandomInteger() % size;
Sint32 retVal = (Sint32) buffer[index];

SDL_free(buffer);

return (RandomPositiveInteger() % 2 == 0 ? value : -value);
return retVal;
}

Sint64
RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
{
Sint64 *buffer = NULL;
Uint32 size;

// min & max values for Sint64
const Sint64 maxValue = LLONG_MAX;
const Sint64 minValue = LLONG_MIN;

buffer = GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain, buffer, &size);
if(buffer == NULL) {
return LLONG_MIN;
}

Uint32 index = RandomInteger() % size;
Sint64 retVal = (Sint64) buffer[index];

SDL_free(buffer);

return retVal;
}

char *
Expand Down
23 changes: 20 additions & 3 deletions test/test-automation/src/libSDLtest/fuzzer/fuzzer.h
Expand Up @@ -142,10 +142,27 @@ Uint64 RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool va

/*!
* Returns random Sint8 boundary value.
*
* Note: not implemented.
*/
Sint8 RandomSint8BoundaryValue();
Sint8
RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain);

/*!
* Returns random Sint8 boundary value.
*/
Sint16
RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain);

/*!
* Returns random Sint8 boundary value.
*/
Sint32
RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain);

/*!
* Returns random Sint8 boundary value.
*/
Sint64
RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain);


/*!
Expand Down

0 comments on commit 35aa235

Please sign in to comment.