Replaced test framework random code with SDL random functions

This commit is contained in:
Sam Lantinga 2024-06-23 12:42:54 -07:00
parent 96f2f23240
commit a938e2b979
16 changed files with 21 additions and 264 deletions

View File

@ -375,7 +375,6 @@
<ClInclude Include="..\..\include\SDL3\SDL_test_log.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_md5.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_memory.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_random.h" />
<ClInclude Include="..\..\include\SDL3\SDL_thread.h" />
<ClInclude Include="..\..\include\SDL3\SDL_time.h" />
<ClInclude Include="..\..\include\SDL3\SDL_timer.h" />

View File

@ -295,7 +295,6 @@
<ClInclude Include="..\..\include\SDL3\SDL_test_log.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_md5.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_memory.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_random.h" />
<ClInclude Include="..\..\include\SDL3\SDL_thread.h" />
<ClInclude Include="..\..\include\SDL3\SDL_timer.h" />
<ClInclude Include="..\..\include\SDL3\SDL_touch.h" />

View File

@ -195,7 +195,6 @@
<ClCompile Include="..\..\src\test\SDL_test_log.c" />
<ClCompile Include="..\..\src\test\SDL_test_md5.c" />
<ClCompile Include="..\..\src\test\SDL_test_memory.c" />
<ClCompile Include="..\..\src\test\SDL_test_random.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -294,7 +294,6 @@
<ClCompile Include="..\..\src\test\SDL_test_log.c" />
<ClCompile Include="..\..\src\test\SDL_test_md5.c" />
<ClCompile Include="..\..\src\test\SDL_test_memory.c" />
<ClCompile Include="..\..\src\test\SDL_test_random.c" />
<ClCompile Include="..\..\test\main.cpp">
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>

View File

@ -75,9 +75,6 @@
<ClCompile Include="..\..\src\test\SDL_test_memory.c">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\src\test\SDL_test_random.c">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\main\winrt\SDL3-WinRTResource_BlankCursor.cur" />

View File

@ -300,7 +300,6 @@
<ClInclude Include="..\..\include\SDL3\SDL_test_log.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_md5.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_memory.h" />
<ClInclude Include="..\..\include\SDL3\SDL_test_random.h" />
<ClInclude Include="..\..\include\SDL3\SDL_thread.h" />
<ClInclude Include="..\..\include\SDL3\SDL_time.h" />
<ClInclude Include="..\..\include\SDL3\SDL_timer.h" />

View File

@ -399,9 +399,6 @@
<ClInclude Include="..\..\include\SDL3\SDL_test_md5.h">
<Filter>API Headers</Filter>
</ClInclude>
<ClInclude Include="..\..\include\SDL3\SDL_test_random.h">
<Filter>API Headers</Filter>
</ClInclude>
<ClInclude Include="..\..\include\SDL3\SDL_thread.h">
<Filter>API Headers</Filter>
</ClInclude>

View File

@ -163,9 +163,8 @@
<ClCompile Include="..\..\src\test\SDL_test_log.c" />
<ClCompile Include="..\..\src\test\SDL_test_md5.c" />
<ClCompile Include="..\..\src\test\SDL_test_memory.c" />
<ClCompile Include="..\..\src\test\SDL_test_random.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -116,7 +116,6 @@
DB166D9916A1D1A500A1396C /* SDL_test_harness.c in Sources */ = {isa = PBXBuildFile; fileRef = DB166D8A16A1D1A500A1396C /* SDL_test_harness.c */; };
DB166D9F16A1D1A500A1396C /* SDL_test_log.c in Sources */ = {isa = PBXBuildFile; fileRef = DB166D9016A1D1A500A1396C /* SDL_test_log.c */; };
DB166DA016A1D1A500A1396C /* SDL_test_md5.c in Sources */ = {isa = PBXBuildFile; fileRef = DB166D9116A1D1A500A1396C /* SDL_test_md5.c */; };
DB166DA116A1D1A500A1396C /* SDL_test_random.c in Sources */ = {isa = PBXBuildFile; fileRef = DB166D9216A1D1A500A1396C /* SDL_test_random.c */; };
DB166DD716A1D37800A1396C /* testmessage.c in Sources */ = {isa = PBXBuildFile; fileRef = DB166CBD16A1C74100A1396C /* testmessage.c */; };
DB166DDB16A1D42F00A1396C /* icon.bmp in CopyFiles */ = {isa = PBXBuildFile; fileRef = 00794E5D09D20839003FC8A1 /* icon.bmp */; };
DB166DF016A1D52500A1396C /* testrelative.c in Sources */ = {isa = PBXBuildFile; fileRef = DB166CBF16A1C74100A1396C /* testrelative.c */; };
@ -1315,7 +1314,6 @@
DB166D8A16A1D1A500A1396C /* SDL_test_harness.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_test_harness.c; sourceTree = "<group>"; };
DB166D9016A1D1A500A1396C /* SDL_test_log.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_test_log.c; sourceTree = "<group>"; };
DB166D9116A1D1A500A1396C /* SDL_test_md5.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_test_md5.c; sourceTree = "<group>"; };
DB166D9216A1D1A500A1396C /* SDL_test_random.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_test_random.c; sourceTree = "<group>"; };
DB166DD516A1D36A00A1396C /* testmessage.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = testmessage.app; sourceTree = BUILT_PRODUCTS_DIR; };
DB166DEE16A1D50C00A1396C /* testrelative.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = testrelative.app; sourceTree = BUILT_PRODUCTS_DIR; };
DB166E0516A1D57C00A1396C /* testrendercopyex.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = testrendercopyex.app; sourceTree = BUILT_PRODUCTS_DIR; };
@ -1935,7 +1933,6 @@
DB166D9016A1D1A500A1396C /* SDL_test_log.c */,
DB166D9116A1D1A500A1396C /* SDL_test_md5.c */,
AAF02FF41F90089800B9A9FB /* SDL_test_memory.c */,
DB166D9216A1D1A500A1396C /* SDL_test_random.c */,
);
name = SDL_Test;
path = ../../src/test;
@ -3308,7 +3305,6 @@
DB166D9F16A1D1A500A1396C /* SDL_test_log.c in Sources */,
DB166DA016A1D1A500A1396C /* SDL_test_md5.c in Sources */,
AAF02FFA1F90092700B9A9FB /* SDL_test_memory.c in Sources */,
DB166DA116A1D1A500A1396C /* SDL_test_random.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -41,7 +41,6 @@
#include <SDL3/SDL_test_log.h>
#include <SDL3/SDL_test_md5.h>
#include <SDL3/SDL_test_memory.h>
#include <SDL3/SDL_test_random.h>
#include <SDL3/SDL_begin_code.h>
/* Set up for C function definitions, even when using C++ */

View File

@ -1,112 +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.
*/
/**
* \file SDL_test_random.h
*
* Random number generator related function of SDL test framework.
*
* This code is a part of the SDL test library, not the main SDL library.
*/
/*
A "32-bit Multiply with carry random number generator. Very fast.
Includes a list of recommended multipliers.
multiply-with-carry generator: x(n) = a*x(n-1) + carry mod 2^32.
period: (a*2^31)-1
*/
#ifndef SDL_test_random_h_
#define SDL_test_random_h_
#include <SDL3/SDL_begin_code.h>
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif
/* --- Definitions */
/*
* Macros that return a random number in a specific format.
*/
#define SDLTest_RandomInt(c) ((int)SDLTest_Random(c))
/*
* Context structure for the random number generator state.
*/
typedef struct SDLTest_RandomContext {
unsigned int a;
unsigned int x;
unsigned int c;
unsigned int ah;
unsigned int al;
} SDLTest_RandomContext;
/* --- Function prototypes */
/**
* Initialize random number generator with two integers.
*
* Note: The random sequence of numbers returned by ...Random() is the
* same for the same two integers and has a period of 2^31.
*
* \param rndContext pointer to context structure
* \param xi integer that defines the random sequence
* \param ci integer that defines the random sequence
*
*/
void SDLTest_RandomInit(SDLTest_RandomContext *rndContext, unsigned int xi, unsigned int ci);
/**
* Initialize random number generator based on current system time.
*
* \param rndContext pointer to context structure
*
*/
void SDLTest_RandomInitTime(SDLTest_RandomContext *rndContext);
/**
* Initialize random number generator based on current system time.
*
* Note: ...RandomInit() or ...RandomInitTime() must have been called
* before using this function.
*
* \param rndContext pointer to context structure
*
* \returns a random number (32bit unsigned integer)
*
*/
unsigned int SDLTest_Random(SDLTest_RandomContext *rndContext);
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif
#include <SDL3/SDL_close_code.h>
#endif /* SDL_test_random_h_ */

View File

@ -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++;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -67,12 +67,10 @@ int main(int argc, char *argv[])
static const int itervals[] = { SDL_arraysize(nums), 12 };
int i;
int iteration;
SDLTest_RandomContext rndctx;
SDLTest_CommonState *state;
Uint64 seed = 0;
int seed_seen = 0;
SDL_zero(rndctx);
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, 0);
if (!state) {
@ -86,7 +84,6 @@ int main(int argc, char *argv[])
consumed = SDLTest_CommonArg(state, i);
if (!consumed) {
if (!seed_seen) {
Uint64 seed = 0;
char *endptr = NULL;
seed = SDL_strtoull(argv[i], &endptr, 0);
@ -97,11 +94,6 @@ int main(int argc, char *argv[])
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
return 1;
}
if (seed <= ((Uint64)0xffffffff)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n");
return 1;
}
SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff));
}
}
if (consumed <= 0) {
@ -114,9 +106,9 @@ int main(int argc, char *argv[])
}
if (!seed_seen) {
SDLTest_RandomInitTime(&rndctx);
seed = SDL_GetPerformanceCounter();
}
SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c);
SDL_Log("Using random seed 0x%" SDL_PRIx64 "\n", seed);
for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
const int arraylen = itervals[iteration];
@ -138,7 +130,7 @@ int main(int argc, char *argv[])
test_sort("reverse sorted", nums, arraylen);
for (i = 0; i < arraylen; i++) {
nums[i] = SDLTest_RandomInt(&rndctx);
nums[i] = SDL_rand_r(&seed, SDL_MAX_SINT32);
}
test_sort("random sorted", nums, arraylen);
}

View File

@ -11,9 +11,6 @@
*/
/* Simple program: Move N sprites around on the screen as fast as possible */
#include <stdlib.h>
#include <time.h>
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL_test.h>
#include <SDL3/SDL_test_common.h>
@ -558,7 +555,7 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
seed = (Uint64)iterations;
} else {
/* Pseudo-random seed generated from the time */
seed = (Uint64)time(NULL);
seed = SDL_GetPerformanceCounter();
}
SDLTest_FuzzerInit(seed);
for (i = 0; i < num_sprites; ++i) {