diff --git a/include/SDL3/SDL_test_harness.h b/include/SDL3/SDL_test_harness.h index 54dc2dc829..c8ad19bc2d 100644 --- a/include/SDL3/SDL_test_harness.h +++ b/include/SDL3/SDL_test_harness.h @@ -101,15 +101,15 @@ typedef struct SDLTest_TestSuiteReference { /* - * Generates a random run seed string for the harness. The generated seed will contain alphanumeric characters (0-9A-Z). + * Generates a random run seed string for the harness. The generated seed + * will contain alphanumeric characters (0-9A-Z). * - * Note: The returned string needs to be deallocated by the caller. + * \param buffer Buffer in which to generate the random seed. Must have a capacity of at least length + 1 characters. + * \param length Number of alphanumeric characters to write to buffer, must be >0 * - * \param length The length of the seed string to generate - * - * \returns the generated seed string + * \returns A null-terminated seed string and equal to the in put buffer on success, NULL on failure */ -char *SDLTest_GenerateRunSeed(const int length); +char *SDLTest_GenerateRunSeed(char *buffer, int length); /* * Execute a test suite using the given run seed and execution key. diff --git a/src/test/SDL_test_harness.c b/src/test/SDL_test_harness.c index f1fc68f921..6f3161d28c 100644 --- a/src/test/SDL_test_harness.c +++ b/src/test/SDL_test_harness.c @@ -50,35 +50,22 @@ /* ! Timeout for single test case execution */ static Uint32 SDLTest_TestCaseTimeout = 3600; -/** - * Generates a random run seed string for the harness. The generated seed - * will contain alphanumeric characters (0-9A-Z). - * - * Note: The returned string needs to be deallocated by the caller. - * - * \param length The length of the seed string to generate - * - * \returns The generated seed string - */ -char *SDLTest_GenerateRunSeed(const int length) +char *SDLTest_GenerateRunSeed(char *buffer, int length) { - char *seed = NULL; Uint64 randomContext = SDL_GetPerformanceCounter(); int counter; + if (!buffer) { + SDLTest_LogError("Input buffer must not be NULL."); + return NULL; + } + /* Sanity check input */ if (length <= 0) { SDLTest_LogError("The length of the harness seed must be >0."); return NULL; } - /* Allocate output buffer */ - seed = (char *)SDL_malloc((length + 1) * sizeof(char)); - if (!seed) { - SDLTest_LogError("SDL_malloc for run seed output buffer failed."); - return NULL; - } - /* Generate a random string of alphanumeric characters */ for (counter = 0; counter < length; counter++) { char ch; @@ -88,11 +75,11 @@ char *SDLTest_GenerateRunSeed(const int length) } else { ch = (char)('A' + v - 10); } - seed[counter] = ch; + buffer[counter] = ch; } - seed[length] = '\0'; + buffer[length] = '\0'; - return seed; + return buffer; } /** @@ -417,14 +404,11 @@ int SDLTest_RunSuites(SDLTest_TestSuiteReference *testSuites[], const char *user /* Generate run see if we don't have one already */ if (!userRunSeed || userRunSeed[0] == '\0') { - char *tmp = SDLTest_GenerateRunSeed(16); - if (!tmp) { + runSeed = SDLTest_GenerateRunSeed(generatedSeed, 16); + if (!runSeed) { SDLTest_LogError("Generating a random seed failed"); return 2; } - SDL_memcpy(generatedSeed, tmp, 16 + 1); - SDL_free(tmp); - runSeed = generatedSeed; } else { runSeed = userRunSeed; } diff --git a/test/testautomation_sdltest.c b/test/testautomation_sdltest.c index fa7c7eb840..4b6d9eed1f 100644 --- a/test/testautomation_sdltest.c +++ b/test/testautomation_sdltest.c @@ -16,26 +16,30 @@ */ static int sdltest_generateRunSeed(void *arg) { + char buffer[32]; char *result; size_t i, l; int j; for (i = 1; i <= 10; i += 3) { - result = SDLTest_GenerateRunSeed((int)i); - SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed()"); + result = SDLTest_GenerateRunSeed(buffer, (int)i); + SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed(, %" SDL_PRIu64 ")", (Uint64)i); SDLTest_AssertCheck(result != NULL, "Verify returned value is not NULL"); if (result != NULL) { l = SDL_strlen(result); SDLTest_AssertCheck(l == i, "Verify length of returned value is %d, got: %d", (int)i, (int)l); - SDL_free(result); } } + result = SDLTest_GenerateRunSeed(NULL, 10); + SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed(NULL, 10)"); + SDLTest_AssertCheck(result == NULL, "Verify returned value is NULL"); + /* Negative cases */ for (j = -2; j <= 0; j++) { - result = SDLTest_GenerateRunSeed(j); - SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed()"); - SDLTest_AssertCheck(result == NULL, "Verify returned value is not NULL"); + result = SDLTest_GenerateRunSeed(buffer, j); + SDLTest_AssertPass("Call to SDLTest_GenerateRunSeed(, %d)", j); + SDLTest_AssertCheck(result == NULL, "Verify returned value is NULL"); } return TEST_COMPLETED;