mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-01 11:34:40 +00:00
Replaced test framework random code with SDL random functions
This commit is contained in:
@@ -37,7 +37,7 @@ static int fuzzerInvocationCounter = 0;
|
||||
/**
|
||||
* Context for shared random number generator
|
||||
*/
|
||||
static SDLTest_RandomContext rndContext;
|
||||
static Uint64 rndContext;
|
||||
|
||||
/*
|
||||
* Note: doxygen documentation markup for functions is in the header file.
|
||||
@@ -45,10 +45,7 @@ static SDLTest_RandomContext rndContext;
|
||||
|
||||
void SDLTest_FuzzerInit(Uint64 execKey)
|
||||
{
|
||||
Uint32 a = (execKey >> 32) & 0x00000000FFFFFFFF;
|
||||
Uint32 b = execKey & 0x00000000FFFFFFFF;
|
||||
SDL_memset((void *)&rndContext, 0, sizeof(SDLTest_RandomContext));
|
||||
SDLTest_RandomInit(&rndContext, a, b);
|
||||
rndContext = execKey;
|
||||
fuzzerInvocationCounter = 0;
|
||||
}
|
||||
|
||||
@@ -61,42 +58,42 @@ Uint8 SDLTest_RandomUint8(void)
|
||||
{
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
return (Uint8)SDLTest_RandomInt(&rndContext) & 0x000000FF;
|
||||
return (Uint8)SDL_rand_r(&rndContext, SDL_MAX_UINT8 + 1);
|
||||
}
|
||||
|
||||
Sint8 SDLTest_RandomSint8(void)
|
||||
{
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
return (Sint8)SDLTest_RandomInt(&rndContext) & 0x000000FF;
|
||||
return (Sint8)SDL_rand_r(&rndContext, SDL_MAX_UINT8 + 1) ;
|
||||
}
|
||||
|
||||
Uint16 SDLTest_RandomUint16(void)
|
||||
{
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
return (Uint16)SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
|
||||
return (Uint16)SDL_rand_r(&rndContext, SDL_MAX_UINT16 + 1);
|
||||
}
|
||||
|
||||
Sint16 SDLTest_RandomSint16(void)
|
||||
{
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
return (Sint16)SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
|
||||
return (Sint16)SDL_rand_r(&rndContext, SDL_MAX_UINT16 + 1);
|
||||
}
|
||||
|
||||
Sint32 SDLTest_RandomSint32(void)
|
||||
{
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
return (Sint32)SDLTest_RandomInt(&rndContext);
|
||||
return (Sint32)SDL_rand_bits_r(&rndContext);
|
||||
}
|
||||
|
||||
Uint32 SDLTest_RandomUint32(void)
|
||||
{
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
return (Uint32)SDLTest_RandomInt(&rndContext);
|
||||
return (Uint32)SDL_rand_bits_r(&rndContext);
|
||||
}
|
||||
|
||||
Uint64 SDLTest_RandomUint64(void)
|
||||
@@ -110,8 +107,8 @@ Uint64 SDLTest_RandomUint64(void)
|
||||
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
value.v32[0] = SDLTest_RandomSint32();
|
||||
value.v32[1] = SDLTest_RandomSint32();
|
||||
value.v32[0] = SDLTest_RandomUint32();
|
||||
value.v32[1] = SDLTest_RandomUint32();
|
||||
|
||||
return value.v64;
|
||||
}
|
||||
@@ -127,8 +124,8 @@ Sint64 SDLTest_RandomSint64(void)
|
||||
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
value.v32[0] = SDLTest_RandomSint32();
|
||||
value.v32[1] = SDLTest_RandomSint32();
|
||||
value.v32[0] = SDLTest_RandomUint32();
|
||||
value.v32[1] = SDLTest_RandomUint32();
|
||||
|
||||
return (Sint64)value.v64;
|
||||
}
|
||||
@@ -432,7 +429,7 @@ SDLTest_RandomDouble(void)
|
||||
double s = 1.0;
|
||||
do {
|
||||
s /= UINT_MAX + 1.0;
|
||||
r += (double)SDLTest_RandomInt(&rndContext) * s;
|
||||
r += (double)SDLTest_RandomSint32() * s;
|
||||
} while (s > DBL_EPSILON);
|
||||
|
||||
fuzzerInvocationCounter++;
|
||||
|
||||
@@ -63,7 +63,7 @@ static Uint32 SDLTest_TestCaseTimeout = 3600;
|
||||
char *SDLTest_GenerateRunSeed(const int length)
|
||||
{
|
||||
char *seed = NULL;
|
||||
SDLTest_RandomContext randomContext;
|
||||
Uint64 randomContext = SDL_GetPerformanceCounter();
|
||||
int counter;
|
||||
|
||||
/* Sanity check input */
|
||||
@@ -80,10 +80,8 @@ char *SDLTest_GenerateRunSeed(const int length)
|
||||
}
|
||||
|
||||
/* Generate a random string of alphanumeric characters */
|
||||
SDLTest_RandomInitTime(&randomContext);
|
||||
for (counter = 0; counter < length; counter++) {
|
||||
unsigned int number = SDLTest_Random(&randomContext);
|
||||
char ch = (char)(number % (91 - 48)) + 48;
|
||||
char ch = (char)(SDL_rand_r(&randomContext, (91 - 48) + 1) + 48);
|
||||
if (ch >= 58 && ch <= 64) {
|
||||
ch = 65;
|
||||
}
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
A portable "32-bit Multiply with carry" random number generator.
|
||||
|
||||
Used by the fuzzer component.
|
||||
Original source code contributed by A. Schiffler for GSOC project.
|
||||
|
||||
*/
|
||||
#include <SDL3/SDL_test.h>
|
||||
|
||||
#include <stdlib.h> /* Needed for srand() and rand() */
|
||||
#include <time.h> /* Needed for time() */
|
||||
|
||||
/* Initialize random number generator with two integer variables */
|
||||
|
||||
void SDLTest_RandomInit(SDLTest_RandomContext *rndContext, unsigned int xi, unsigned int ci)
|
||||
{
|
||||
if (!rndContext) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Choose a value for 'a' from this list
|
||||
* 1791398085 1929682203 1683268614 1965537969 1675393560
|
||||
* 1967773755 1517746329 1447497129 1655692410 1606218150
|
||||
* 2051013963 1075433238 1557985959 1781943330 1893513180
|
||||
* 1631296680 2131995753 2083801278 1873196400 1554115554
|
||||
*/
|
||||
rndContext->a = 1655692410;
|
||||
rndContext->x = 30903;
|
||||
rndContext->c = 0;
|
||||
if (xi != 0) {
|
||||
rndContext->x = xi;
|
||||
}
|
||||
rndContext->c = ci;
|
||||
rndContext->ah = rndContext->a >> 16;
|
||||
rndContext->al = rndContext->a & 65535;
|
||||
}
|
||||
|
||||
/* Initialize random number generator from system time */
|
||||
|
||||
void SDLTest_RandomInitTime(SDLTest_RandomContext *rndContext)
|
||||
{
|
||||
int a, b;
|
||||
|
||||
if (!rndContext) {
|
||||
return;
|
||||
}
|
||||
|
||||
srand((unsigned int)time(NULL));
|
||||
a = rand();
|
||||
srand((unsigned int)SDL_GetPerformanceCounter());
|
||||
b = rand();
|
||||
SDLTest_RandomInit(rndContext, a, b);
|
||||
}
|
||||
|
||||
/* Returns random numbers */
|
||||
|
||||
unsigned int SDLTest_Random(SDLTest_RandomContext *rndContext)
|
||||
{
|
||||
unsigned int xh, xl;
|
||||
|
||||
if (!rndContext) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
xh = rndContext->x >> 16;
|
||||
xl = rndContext->x & 65535;
|
||||
rndContext->x = rndContext->x * rndContext->a + rndContext->c;
|
||||
rndContext->c =
|
||||
xh * rndContext->ah + ((xh * rndContext->al) >> 16) +
|
||||
((xl * rndContext->ah) >> 16);
|
||||
if (xl * rndContext->al >= (~rndContext->c + 1)) {
|
||||
rndContext->c++;
|
||||
}
|
||||
return rndContext->x;
|
||||
}
|
||||
Reference in New Issue
Block a user