From a938e2b979f66b9fa35d53ba4eb972dde9c43d10 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 23 Jun 2024 12:42:54 -0700 Subject: [PATCH] Replaced test framework random code with SDL random functions --- VisualC-GDK/SDL/SDL.vcxproj | 1 - VisualC-GDK/SDL/SDL.vcxproj.filters | 1 - VisualC-GDK/SDL_test/SDL_test.vcxproj | 1 - VisualC-WinRT/testdraw/testdraw.vcxproj | 1 - .../testdraw/testdraw.vcxproj.filters | 3 - VisualC/SDL/SDL.vcxproj | 1 - VisualC/SDL/SDL.vcxproj.filters | 3 - VisualC/SDL_test/SDL_test.vcxproj | 3 +- .../SDLTest/SDLTest.xcodeproj/project.pbxproj | 4 - include/SDL3/SDL_test.h | 1 - include/SDL3/SDL_test_random.h | 112 ------------------ src/test/SDL_test_fuzzer.c | 29 ++--- src/test/SDL_test_harness.c | 6 +- src/test/SDL_test_random.c | 98 --------------- test/testqsort.c | 16 +-- test/testsprite.c | 5 +- 16 files changed, 21 insertions(+), 264 deletions(-) delete mode 100644 include/SDL3/SDL_test_random.h delete mode 100644 src/test/SDL_test_random.c diff --git a/VisualC-GDK/SDL/SDL.vcxproj b/VisualC-GDK/SDL/SDL.vcxproj index 4c0c080532..bb7c85405b 100644 --- a/VisualC-GDK/SDL/SDL.vcxproj +++ b/VisualC-GDK/SDL/SDL.vcxproj @@ -375,7 +375,6 @@ - diff --git a/VisualC-GDK/SDL/SDL.vcxproj.filters b/VisualC-GDK/SDL/SDL.vcxproj.filters index 5be93eef16..2b4f69eb03 100644 --- a/VisualC-GDK/SDL/SDL.vcxproj.filters +++ b/VisualC-GDK/SDL/SDL.vcxproj.filters @@ -295,7 +295,6 @@ - diff --git a/VisualC-GDK/SDL_test/SDL_test.vcxproj b/VisualC-GDK/SDL_test/SDL_test.vcxproj index 2984a46099..2c7ada3ca0 100644 --- a/VisualC-GDK/SDL_test/SDL_test.vcxproj +++ b/VisualC-GDK/SDL_test/SDL_test.vcxproj @@ -195,7 +195,6 @@ - diff --git a/VisualC-WinRT/testdraw/testdraw.vcxproj b/VisualC-WinRT/testdraw/testdraw.vcxproj index f612acf98b..eea5a6ccd3 100644 --- a/VisualC-WinRT/testdraw/testdraw.vcxproj +++ b/VisualC-WinRT/testdraw/testdraw.vcxproj @@ -294,7 +294,6 @@ - true true diff --git a/VisualC-WinRT/testdraw/testdraw.vcxproj.filters b/VisualC-WinRT/testdraw/testdraw.vcxproj.filters index d41d939d6d..2640e2a472 100644 --- a/VisualC-WinRT/testdraw/testdraw.vcxproj.filters +++ b/VisualC-WinRT/testdraw/testdraw.vcxproj.filters @@ -75,9 +75,6 @@ Common - - Common - diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj index 07f1fa70c4..87dd7ee8ee 100644 --- a/VisualC/SDL/SDL.vcxproj +++ b/VisualC/SDL/SDL.vcxproj @@ -300,7 +300,6 @@ - diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters index a58186a588..4eb44ad41f 100644 --- a/VisualC/SDL/SDL.vcxproj.filters +++ b/VisualC/SDL/SDL.vcxproj.filters @@ -399,9 +399,6 @@ API Headers - - API Headers - API Headers diff --git a/VisualC/SDL_test/SDL_test.vcxproj b/VisualC/SDL_test/SDL_test.vcxproj index da264650fa..90b52243ca 100644 --- a/VisualC/SDL_test/SDL_test.vcxproj +++ b/VisualC/SDL_test/SDL_test.vcxproj @@ -163,9 +163,8 @@ - - \ No newline at end of file + diff --git a/Xcode/SDLTest/SDLTest.xcodeproj/project.pbxproj b/Xcode/SDLTest/SDLTest.xcodeproj/project.pbxproj index c0c1cb8a58..e40038e4c9 100644 --- a/Xcode/SDLTest/SDLTest.xcodeproj/project.pbxproj +++ b/Xcode/SDLTest/SDLTest.xcodeproj/project.pbxproj @@ -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 = ""; }; DB166D9016A1D1A500A1396C /* SDL_test_log.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_test_log.c; sourceTree = ""; }; DB166D9116A1D1A500A1396C /* SDL_test_md5.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_test_md5.c; sourceTree = ""; }; - DB166D9216A1D1A500A1396C /* SDL_test_random.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_test_random.c; sourceTree = ""; }; 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; }; diff --git a/include/SDL3/SDL_test.h b/include/SDL3/SDL_test.h index 634382cf41..e846873913 100644 --- a/include/SDL3/SDL_test.h +++ b/include/SDL3/SDL_test.h @@ -41,7 +41,6 @@ #include #include #include -#include #include /* Set up for C function definitions, even when using C++ */ diff --git a/include/SDL3/SDL_test_random.h b/include/SDL3/SDL_test_random.h deleted file mode 100644 index 73088e138f..0000000000 --- a/include/SDL3/SDL_test_random.h +++ /dev/null @@ -1,112 +0,0 @@ -/* - Simple DirectMedia Layer - Copyright (C) 1997-2024 Sam Lantinga - - 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 -/* 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 - -#endif /* SDL_test_random_h_ */ diff --git a/src/test/SDL_test_fuzzer.c b/src/test/SDL_test_fuzzer.c index 7e45f6c0e6..73fc07f1e3 100644 --- a/src/test/SDL_test_fuzzer.c +++ b/src/test/SDL_test_fuzzer.c @@ -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++; diff --git a/src/test/SDL_test_harness.c b/src/test/SDL_test_harness.c index fd46e8f140..674d125de0 100644 --- a/src/test/SDL_test_harness.c +++ b/src/test/SDL_test_harness.c @@ -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; } diff --git a/src/test/SDL_test_random.c b/src/test/SDL_test_random.c deleted file mode 100644 index 679ef7ee00..0000000000 --- a/src/test/SDL_test_random.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - Simple DirectMedia Layer - Copyright (C) 1997-2024 Sam Lantinga - - 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 - -#include /* Needed for srand() and rand() */ -#include /* 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; -} diff --git a/test/testqsort.c b/test/testqsort.c index 5fa5db7b8c..5b8eb5b2f4 100644 --- a/test/testqsort.c +++ b/test/testqsort.c @@ -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); } diff --git a/test/testsprite.c b/test/testsprite.c index a9e8888a7d..8fb3531d62 100644 --- a/test/testsprite.c +++ b/test/testsprite.c @@ -11,9 +11,6 @@ */ /* Simple program: Move N sprites around on the screen as fast as possible */ -#include -#include - #define SDL_MAIN_USE_CALLBACKS 1 #include #include @@ -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) {