Added boundary value generator functions for Sint8,
authorMarkus Kauppila <markus.kauppila@gmail.com>
Tue, 09 Aug 2011 23:37:10 +0300
changeset 5819 3a822667c601
parent 5818 598099cba0be
child 5820 293fed907724
Added boundary value generator functions for Sint8, Sint16, Sint32 and Sint64.
test/test-automation/src/libSDLtest/fuzzer/fuzzer.c
test/test-automation/src/libSDLtest/fuzzer/fuzzer.h
--- a/test/test-automation/src/libSDLtest/fuzzer/fuzzer.c	Tue Aug 09 18:45:29 2011 +0300
+++ b/test/test-automation/src/libSDLtest/fuzzer/fuzzer.c	Tue Aug 09 23:37:10 2011 +0300
@@ -20,6 +20,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <limits.h>
 
 #include "../../../include/SDL_test.h"
 
@@ -318,12 +319,192 @@
 	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)
 {
-	int value = 0; //GenerateBoundaryValueForSize(8);
+	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;
+}
 
-	return (RandomPositiveInteger() % 2 == 0 ? value : -value);
+Sint32
+RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
+{
+	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 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 *
--- a/test/test-automation/src/libSDLtest/fuzzer/fuzzer.h	Tue Aug 09 18:45:29 2011 +0300
+++ b/test/test-automation/src/libSDLtest/fuzzer/fuzzer.h	Tue Aug 09 23:37:10 2011 +0300
@@ -142,10 +142,27 @@
 
 /*!
  * Returns random Sint8 boundary value.
- *
- * Note: not implemented.
+ */
+Sint8
+RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain);
+
+/*!
+ * Returns random Sint8 boundary value.
  */
-Sint8 RandomSint8BoundaryValue();
+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);
 
 
 /*!