src/test/SDL_test_fuzzer.c
changeset 6711 e6355923901d
child 6719 c250f633c040
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/SDL_test_fuzzer.c	Tue Nov 27 21:40:46 2012 -0800
     1.3 @@ -0,0 +1,637 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/* 
    1.26 +
    1.27 +  Data generators for fuzzing test data in a reproducible way.
    1.28 + 
    1.29 +*/
    1.30 +
    1.31 +#include "SDL_config.h"
    1.32 +
    1.33 +#include <stdint.h>
    1.34 +#include <stdio.h>
    1.35 +#include <stdlib.h>
    1.36 +#include <limits.h>
    1.37 +#include <float.h>
    1.38 +
    1.39 +#include "SDL_test.h"
    1.40 +
    1.41 +/** 
    1.42 + *Counter for fuzzer invocations
    1.43 + */
    1.44 +static int fuzzerInvocationCounter;
    1.45 +
    1.46 +/**
    1.47 + * Context for shared random number generator
    1.48 + */
    1.49 +static SDLTest_RandomContext rndContext;
    1.50 +
    1.51 +/*
    1.52 + * Note: doxygen documentation markup for functions is in the header file.
    1.53 + */
    1.54 +
    1.55 +void
    1.56 +SDLTest_FuzzerInit(Uint64 execKey)
    1.57 +{
    1.58 +	Uint32 a = (execKey >> 32)  & 0x00000000FFFFFFFF;
    1.59 +	Uint32 b = execKey & 0x00000000FFFFFFFF;
    1.60 +	SDLTest_RandomInit(&rndContext, a, b);
    1.61 +}
    1.62 +
    1.63 +int
    1.64 +SDLTest_GetInvocationCount()
    1.65 +{
    1.66 +	return fuzzerInvocationCounter;
    1.67 +}
    1.68 +
    1.69 +Uint8
    1.70 +SDLTest_RandomUint8()
    1.71 +{
    1.72 +	fuzzerInvocationCounter++;
    1.73 +
    1.74 +	return (Uint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
    1.75 +}
    1.76 +
    1.77 +Sint8
    1.78 +SDLTest_RandomSint8()
    1.79 +{
    1.80 +	fuzzerInvocationCounter++;
    1.81 +
    1.82 +	return (Sint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
    1.83 +}
    1.84 +
    1.85 +Uint16
    1.86 +SDLTest_RandomUint16()
    1.87 +{
    1.88 +	fuzzerInvocationCounter++;
    1.89 +
    1.90 +	return (Uint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
    1.91 +}
    1.92 +
    1.93 +Sint16
    1.94 +SDLTest_RandomSint16()
    1.95 +{
    1.96 +	fuzzerInvocationCounter++;
    1.97 +
    1.98 +	return (Sint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
    1.99 +}
   1.100 +
   1.101 +Sint32
   1.102 +SDLTest_RandomSint32()
   1.103 +{
   1.104 +	fuzzerInvocationCounter++;
   1.105 +
   1.106 +	return (Sint32) SDLTest_RandomInt(&rndContext);
   1.107 +}
   1.108 +
   1.109 +Uint32
   1.110 +SDLTest_RandomUint32()
   1.111 +{
   1.112 +	fuzzerInvocationCounter++;
   1.113 +
   1.114 +	return (Uint32) SDLTest_RandomInt(&rndContext);
   1.115 +}
   1.116 +
   1.117 +Uint64
   1.118 +SDLTest_RandomUint64()
   1.119 +{
   1.120 +	Uint64 value;
   1.121 +	Uint32 *vp = (void*)&value;
   1.122 +
   1.123 +	fuzzerInvocationCounter++;
   1.124 +
   1.125 +	vp[0] = SDLTest_RandomSint32();
   1.126 +	vp[1] = SDLTest_RandomSint32();
   1.127 +
   1.128 +	return value;
   1.129 +}
   1.130 +
   1.131 +Sint64
   1.132 +SDLTest_RandomSint64()
   1.133 +{
   1.134 +	Uint64 value;
   1.135 +	Uint32 *vp = (void*)&value;
   1.136 +
   1.137 +	fuzzerInvocationCounter++;
   1.138 +
   1.139 +	vp[0] = SDLTest_RandomSint32();
   1.140 +	vp[1] = SDLTest_RandomSint32();
   1.141 +
   1.142 +	return value;
   1.143 +}
   1.144 +
   1.145 +
   1.146 +
   1.147 +Sint32
   1.148 +SDLTest_RandomIntegerInRange(Sint32 pMin, Sint32 pMax)
   1.149 +{
   1.150 +	Sint64 min = pMin;
   1.151 +	Sint64 max = pMax;
   1.152 +	Sint64 temp;
   1.153 +	Sint64 number;
   1.154 +
   1.155 +	if(pMin > pMax) {
   1.156 +		temp = min;
   1.157 +		min = max;
   1.158 +		max = temp;
   1.159 +	} else if(pMin == pMax) {
   1.160 +		return (Sint32)min;
   1.161 +	}
   1.162 +
   1.163 +	number = SDLTest_RandomUint32(); // invocation count increment in there
   1.164 +
   1.165 +	return (Sint32)((number % ((max + 1) - min)) + min);
   1.166 +}
   1.167 +
   1.168 +/*!
   1.169 + * Generates boundary values between the given boundaries.
   1.170 + * Boundary values are inclusive. See the examples below.
   1.171 + * If boundary2 < boundary1, the values are swapped.
   1.172 + * If boundary1 == boundary2, value of boundary1 will be returned
   1.173 + *
   1.174 + * Generating boundary values for Uint8:
   1.175 + * BoundaryValues(sizeof(Uint8), 10, 20, True) -> [10,11,19,20]
   1.176 + * BoundaryValues(sizeof(Uint8), 10, 20, False) -> [9,21]
   1.177 + * BoundaryValues(sizeof(Uint8), 0, 15, True) -> [0, 1, 14, 15]
   1.178 + * BoundaryValues(sizeof(Uint8), 0, 15, False) -> [16]
   1.179 + * BoundaryValues(sizeof(Uint8), 0, 255, False) -> NULL
   1.180 + *
   1.181 + * Generator works the same for other types of unsigned integers.
   1.182 + *
   1.183 + * Note: outBuffer will be allocated and needs to be freed later.
   1.184 + * If outbuffer != NULL, it'll be freed.
   1.185 + *
   1.186 + * \param maxValue The biggest value that is acceptable for this data type.
   1.187 + * 					For instance, for Uint8 -> 255, Uint16 -> 65536 etc.
   1.188 + * \param pBoundary1 defines lower boundary
   1.189 + * \param pBoundary2 defines upper boundary
   1.190 + * \param validDomain Generate only for valid domain (for the data type)
   1.191 + *
   1.192 + * \param outBuffer The generated boundary values are put here
   1.193 + *
   1.194 + * \returns Returns the number of elements in outBuffer or -1 in case of error
   1.195 + */
   1.196 +Uint32
   1.197 +SDLTest_GenerateUnsignedBoundaryValues(const Uint64 maxValue,
   1.198 +					Uint64 pBoundary1, Uint64 pBoundary2, SDL_bool validDomain,
   1.199 +					Uint64 *outBuffer)
   1.200 +{
   1.201 +	Uint64 boundary1 = pBoundary1, boundary2 = pBoundary2;
   1.202 +	Uint64 temp;
   1.203 +	Uint64 tempBuf[4];
   1.204 +	int index;
   1.205 +
   1.206 +	if(outBuffer != NULL) {
   1.207 +		SDL_free(outBuffer);
   1.208 +	}
   1.209 +
   1.210 +	if(boundary1 > boundary2) {
   1.211 +		temp = boundary1;
   1.212 +		boundary1 = boundary2;
   1.213 +		boundary2 = temp;
   1.214 +	}
   1.215 +
   1.216 +	index = 0;
   1.217 +	if(boundary1 == boundary2) {
   1.218 +		tempBuf[index++] = boundary1;
   1.219 +	}
   1.220 +	else if(validDomain) {
   1.221 +		tempBuf[index++] = boundary1;
   1.222 +
   1.223 +		if(boundary1 < UINT64_MAX)
   1.224 +			tempBuf[index++] = boundary1 + 1;
   1.225 +
   1.226 +		tempBuf[index++] = boundary2 - 1;
   1.227 +		tempBuf[index++] = boundary2;
   1.228 +	}
   1.229 +	else {
   1.230 +		if(boundary1 > 0) {
   1.231 +			tempBuf[index++] = boundary1 - 1;
   1.232 +		}
   1.233 +
   1.234 +		if(boundary2 < maxValue && boundary2 < UINT64_MAX) {
   1.235 +			tempBuf[index++] = boundary2 + 1;
   1.236 +		}
   1.237 +	}
   1.238 +
   1.239 +	if(index == 0) {
   1.240 +		// There are no valid boundaries
   1.241 +		return 0;
   1.242 +	}
   1.243 +
   1.244 +	// Create the return buffer
   1.245 +	outBuffer = (Uint64 *)SDL_malloc(index * sizeof(Uint64));
   1.246 +	if(outBuffer == NULL) {
   1.247 +		return 0;
   1.248 +	}
   1.249 +
   1.250 +	SDL_memcpy(outBuffer, tempBuf, index * sizeof(Uint64));
   1.251 +
   1.252 +	return index;
   1.253 +}
   1.254 +
   1.255 +Uint8
   1.256 +SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain)
   1.257 +{
   1.258 +	Uint64 *buffer = NULL;
   1.259 +	Uint32 size;
   1.260 +	Uint32 index;
   1.261 +	Uint8 retVal;
   1.262 +
   1.263 +	// max value for Uint8
   1.264 +	const Uint64 maxValue = UINT8_MAX;
   1.265 +
   1.266 +	size = SDLTest_GenerateUnsignedBoundaryValues(maxValue,
   1.267 +				(Uint64) boundary1, (Uint64) boundary2,
   1.268 +				validDomain, buffer);
   1.269 +	if (size == 0) {
   1.270 +		return 0;
   1.271 +	}
   1.272 +
   1.273 +	index = SDLTest_RandomSint32() % size;
   1.274 +	retVal = (Uint8)buffer[index];
   1.275 +
   1.276 +	SDL_free(buffer);
   1.277 +
   1.278 +	fuzzerInvocationCounter++;
   1.279 +
   1.280 +	return retVal;
   1.281 +}
   1.282 +
   1.283 +Uint16
   1.284 +SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain)
   1.285 +{
   1.286 +	Uint64 *buffer = NULL;
   1.287 +	Uint32 size;
   1.288 +	Uint32 index;
   1.289 +	Uint16 retVal;
   1.290 +
   1.291 +	// max value for Uint16
   1.292 +	const Uint64 maxValue = UINT16_MAX;
   1.293 +
   1.294 +	size = SDLTest_GenerateUnsignedBoundaryValues(maxValue,
   1.295 +				(Uint64) boundary1, (Uint64) boundary2,
   1.296 +				validDomain, buffer);
   1.297 +	if(size == 0) {
   1.298 +		return 0;
   1.299 +	}
   1.300 +
   1.301 +	index = SDLTest_RandomSint32() % size;
   1.302 +	retVal = (Uint16) buffer[index];
   1.303 +
   1.304 +	SDL_free(buffer);
   1.305 +
   1.306 +	fuzzerInvocationCounter++;
   1.307 +
   1.308 +	return retVal;
   1.309 +}
   1.310 +
   1.311 +Uint32
   1.312 +SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain)
   1.313 +{
   1.314 +	Uint64 *buffer = NULL;
   1.315 +	Uint32 size;
   1.316 +	Uint32 index;
   1.317 +	Uint32 retVal;
   1.318 +
   1.319 +	// max value for Uint32
   1.320 +	const Uint64 maxValue = UINT32_MAX;
   1.321 +
   1.322 +	size = SDLTest_GenerateUnsignedBoundaryValues(maxValue,
   1.323 +				(Uint64) boundary1, (Uint64) boundary2,
   1.324 +				validDomain, buffer);
   1.325 +	if(size == 0) {
   1.326 +		return 0;
   1.327 +	}
   1.328 +
   1.329 +	index = SDLTest_RandomSint32() % size;
   1.330 +	retVal = (Uint32) buffer[index];
   1.331 +
   1.332 +	SDL_free(buffer);
   1.333 +
   1.334 +	fuzzerInvocationCounter++;
   1.335 +
   1.336 +	return retVal;
   1.337 +}
   1.338 +
   1.339 +Uint64
   1.340 +SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
   1.341 +{
   1.342 +	Uint64 *buffer = NULL;
   1.343 +	Uint32 size;
   1.344 +	Uint32 index;
   1.345 +	Uint64 retVal;
   1.346 +
   1.347 +	// max value for Uint64
   1.348 +	const Uint64 maxValue = UINT64_MAX;
   1.349 +
   1.350 +	size = SDLTest_GenerateUnsignedBoundaryValues(maxValue,
   1.351 +				(Uint64) boundary1, (Uint64) boundary2,
   1.352 +				validDomain, buffer);
   1.353 +	if(size == 0) {
   1.354 +		return 0;
   1.355 +	}
   1.356 +
   1.357 +	index = SDLTest_RandomSint32() % size;
   1.358 +	retVal = (Uint64) buffer[index];
   1.359 +
   1.360 +	SDL_free(buffer);
   1.361 +
   1.362 +	fuzzerInvocationCounter++;
   1.363 +
   1.364 +	return retVal;
   1.365 +}
   1.366 +
   1.367 +/*!
   1.368 + * Generates boundary values between the given boundaries.
   1.369 + * Boundary values are inclusive. See the examples below.
   1.370 + * If boundary2 < boundary1, the values are swapped.
   1.371 + * If boundary1 == boundary2, value of boundary1 will be returned
   1.372 + *
   1.373 + * Generating boundary values for Sint8:
   1.374 + * SignedBoundaryValues(sizeof(Sint8), -10, 20, True) -> [-11,-10,19,20]
   1.375 + * SignedBoundaryValues(sizeof(Sint8), -10, 20, False) -> [-11,21]
   1.376 + * SignedBoundaryValues(sizeof(Sint8), -30, -15, True) -> [-30, -29, -16, -15]
   1.377 + * SignedBoundaryValues(sizeof(Sint8), -128, 15, False) -> [16]
   1.378 + * SignedBoundaryValues(sizeof(Sint8), -128, 127, False) -> NULL
   1.379 + *
   1.380 + * Generator works the same for other types of signed integers.
   1.381 + *
   1.382 + * Note: outBuffer will be allocated and needs to be freed later.
   1.383 + * If outbuffer != NULL, it'll be freed.
   1.384 + *
   1.385 + *
   1.386 + * \param minValue The smallest value  that is acceptable for this data type.
   1.387 + *					For instance, for Uint8 -> -128, Uint16 -> -32,768 etc.
   1.388 + * \param maxValue The biggest value that is acceptable for this data type.
   1.389 + * 					For instance, for Uint8 -> 127, Uint16 -> 32767 etc.
   1.390 + * \param pBoundary1 defines lower boundary
   1.391 + * \param pBoundary2 defines upper boundary
   1.392 + * \param validDomain Generate only for valid domain (for the data type)
   1.393 + *
   1.394 + * \param outBuffer The generated boundary values are put here
   1.395 + *
   1.396 + * \returns Returns the number of elements in outBuffer or -1 in case of error
   1.397 + */
   1.398 +Uint32
   1.399 +SDLTest_GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue,
   1.400 +					Sint64 pBoundary1, Sint64 pBoundary2, SDL_bool validDomain,
   1.401 +					Sint64 *outBuffer)
   1.402 +{
   1.403 +	int index;
   1.404 +	Sint64 tempBuf[4];
   1.405 +	Sint64 boundary1 = pBoundary1, boundary2 = pBoundary2;
   1.406 +
   1.407 +	if(outBuffer != NULL) {
   1.408 +		SDL_free(outBuffer);
   1.409 +	}
   1.410 +
   1.411 +	if(boundary1 > boundary2) {
   1.412 +		Sint64 temp = boundary1;
   1.413 +		boundary1 = boundary2;
   1.414 +		boundary2 = temp;
   1.415 +	}
   1.416 +
   1.417 +	index = 0;
   1.418 +	if(boundary1 == boundary2) {
   1.419 +		tempBuf[index++] = boundary1;
   1.420 +	}
   1.421 +	else if(validDomain) {
   1.422 +		tempBuf[index++] = boundary1;
   1.423 +
   1.424 +		if(boundary1 < LLONG_MAX)
   1.425 +			tempBuf[index++] = boundary1 + 1;
   1.426 +
   1.427 +		if(boundary2 > LLONG_MIN)
   1.428 +			tempBuf[index++] = boundary2 - 1;
   1.429 +
   1.430 +		tempBuf[index++] = boundary2;
   1.431 +	}
   1.432 +	else {
   1.433 +		if(boundary1 > minValue &&  boundary1 > LLONG_MIN) {
   1.434 +			tempBuf[index++] = boundary1 - 1;
   1.435 +		}
   1.436 +
   1.437 +		if(boundary2 < maxValue && boundary2 < UINT64_MAX) {
   1.438 +			tempBuf[index++] = boundary2 + 1;
   1.439 +		}
   1.440 +	}
   1.441 +
   1.442 +	if(index == 0) {
   1.443 +		// There are no valid boundaries
   1.444 +		return 0;
   1.445 +	}
   1.446 +
   1.447 +	// Create the return buffer
   1.448 +	outBuffer = (Sint64 *)SDL_malloc(index * sizeof(Sint64));
   1.449 +	if(outBuffer == NULL) {
   1.450 +		return 0;
   1.451 +	}
   1.452 +
   1.453 +	SDL_memcpy((void *)outBuffer, (void *)tempBuf, index * sizeof(Sint64));
   1.454 +
   1.455 +	return (Uint32)index;
   1.456 +}
   1.457 +
   1.458 +Sint8
   1.459 +SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain)
   1.460 +{
   1.461 +	// min & max values for Sint8
   1.462 +	const Sint64 maxValue = CHAR_MAX;
   1.463 +	const Sint64 minValue = CHAR_MIN;
   1.464 +
   1.465 +	Sint64 *buffer = NULL;
   1.466 +	Uint32 size;
   1.467 +	Uint32 index;
   1.468 +	Sint8 retVal;
   1.469 +
   1.470 +	size = SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
   1.471 +				(Sint64) boundary1, (Sint64) boundary2,
   1.472 +				validDomain, buffer);
   1.473 +	if(size == 0) {
   1.474 +		return CHAR_MIN;
   1.475 +	}
   1.476 +
   1.477 +	index = SDLTest_RandomSint32() % size;
   1.478 +	retVal = (Sint8) buffer[index];
   1.479 +
   1.480 +	SDL_free(buffer);
   1.481 +
   1.482 +	fuzzerInvocationCounter++;
   1.483 +
   1.484 +	return retVal;
   1.485 +}
   1.486 +
   1.487 +Sint16
   1.488 +SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain)
   1.489 +{
   1.490 +	// min & max values for Sint16
   1.491 +	const Sint64 maxValue = SHRT_MAX;
   1.492 +	const Sint64 minValue = SHRT_MIN;
   1.493 +	Sint64 *buffer = NULL;
   1.494 +	Uint32 size;
   1.495 +	Uint32 index;
   1.496 +	Sint16 retVal;
   1.497 +
   1.498 +	size = SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
   1.499 +					(Sint64) boundary1, (Sint64) boundary2,
   1.500 +					validDomain, buffer);
   1.501 +	if(size == 0) {
   1.502 +		return SHRT_MIN;
   1.503 +	}
   1.504 +
   1.505 +	index = SDLTest_RandomSint32() % size;
   1.506 +	retVal = (Sint16) buffer[index];
   1.507 +
   1.508 +	SDL_free(buffer);
   1.509 +
   1.510 +	fuzzerInvocationCounter++;
   1.511 +
   1.512 +	return retVal;
   1.513 +}
   1.514 +
   1.515 +Sint32
   1.516 +SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
   1.517 +{
   1.518 +	// min & max values for Sint32
   1.519 +	const Sint64 maxValue = INT_MAX;
   1.520 +	const Sint64 minValue = INT_MIN;
   1.521 +
   1.522 +	Sint64 *buffer = NULL;
   1.523 +	Uint32 size;
   1.524 +	Uint32 index;
   1.525 +	Sint32 retVal;
   1.526 +
   1.527 +	size = SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
   1.528 +				(Sint64) boundary1, (Sint64) boundary2,
   1.529 +				validDomain, buffer);
   1.530 +	if(size == 0) {
   1.531 +		return INT_MIN;
   1.532 +	}
   1.533 +
   1.534 +	index = SDLTest_RandomSint32() % size;
   1.535 +	retVal = (Sint32) buffer[index];
   1.536 +
   1.537 +	SDL_free(buffer);
   1.538 +
   1.539 +	fuzzerInvocationCounter++;
   1.540 +
   1.541 +	return retVal;
   1.542 +}
   1.543 +
   1.544 +Sint64
   1.545 +SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
   1.546 +{
   1.547 +	Sint64 *buffer = NULL;
   1.548 +	Uint32 size;
   1.549 +	Uint32 index;
   1.550 +	Sint64 retVal;
   1.551 +
   1.552 +	// min & max values for Sint64
   1.553 +	const Sint64 maxValue = LLONG_MAX;
   1.554 +	const Sint64 minValue = LLONG_MIN;
   1.555 +
   1.556 +	size = SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
   1.557 +				(Sint64) boundary1, (Sint64) boundary2,
   1.558 +				validDomain, buffer);
   1.559 +	if(size == 0) {
   1.560 +		return LLONG_MIN;
   1.561 +	}
   1.562 +
   1.563 +	index = SDLTest_RandomSint32() % size;
   1.564 +	retVal = (Sint64) buffer[index];
   1.565 +
   1.566 +	SDL_free(buffer);
   1.567 +
   1.568 +	fuzzerInvocationCounter++;
   1.569 +
   1.570 +	return retVal;
   1.571 +}
   1.572 +
   1.573 +float
   1.574 +SDLTest_RandomUnitFloat()
   1.575 +{
   1.576 +	return (float) SDLTest_RandomUint32() / UINT_MAX;
   1.577 +}
   1.578 +
   1.579 +float
   1.580 +SDLTest_RandomFloat()
   1.581 +{
   1.582 +        return (float) (FLT_MIN + SDLTest_RandomUnitDouble() * (FLT_MAX - FLT_MIN));
   1.583 +}
   1.584 +
   1.585 +double
   1.586 +SDLTest_RandomUnitDouble()
   1.587 +{
   1.588 +	return (double) (SDLTest_RandomUint64() >> 11) * (1.0/9007199254740992.0);
   1.589 +}
   1.590 +
   1.591 +double
   1.592 +SDLTest_RandomDouble()
   1.593 +{
   1.594 +	double r = 0.0;
   1.595 +	double s = 1.0;
   1.596 +	do {
   1.597 +	  s /= UINT_MAX + 1.0;
   1.598 +	  r += (double)SDLTest_RandomInt(&rndContext) * s;
   1.599 +	} while (s > DBL_EPSILON);
   1.600 +	  
   1.601 +	fuzzerInvocationCounter++;
   1.602 +	
   1.603 +	return r;
   1.604 +}
   1.605 +
   1.606 +
   1.607 +char *
   1.608 +SDLTest_RandomAsciiString()
   1.609 +{
   1.610 +	// note: fuzzerInvocationCounter is increment in the RandomAsciiStringWithMaximumLenght
   1.611 +	return SDLTest_RandomAsciiStringWithMaximumLength(255);
   1.612 +}
   1.613 +
   1.614 +char *
   1.615 +SDLTest_RandomAsciiStringWithMaximumLength(int maxSize)
   1.616 +{
   1.617 +	int size;
   1.618 +	char *string;
   1.619 +	int counter;
   1.620 +
   1.621 +	fuzzerInvocationCounter++;
   1.622 +
   1.623 +	if(maxSize < 1) {
   1.624 +		return NULL;
   1.625 +	}
   1.626 +
   1.627 +	size = (SDLTest_RandomUint32() % (maxSize + 1)) + 1;
   1.628 +	string = (char *)SDL_malloc(size * sizeof(char));
   1.629 +	if (string==NULL) {
   1.630 +	  return NULL;
   1.631 +        }
   1.632 +
   1.633 +	for(counter = 0; counter < size; ++counter) {
   1.634 +		string[counter] = (char)SDLTest_RandomIntegerInRange(1, 127);
   1.635 +	}
   1.636 +
   1.637 +	string[counter] = '\0';
   1.638 +
   1.639 +	return string;
   1.640 +}