SDL: test: use SDL_test for parsing arguments in all non-interactive tests

From 2ae8b4c651681c8c907b8f4963112ffbe0190516 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 31 Aug 2024 19:06:24 +0200
Subject: [PATCH] test: use SDL_test for parsing arguments in all
 non-interactive tests

---
 VisualC/tests/testatomic/testatomic.vcxproj   |  6 ++
 .../tests/testplatform/testplatform.vcxproj   |  6 ++
 VisualC/tests/testpower/testpower.vcxproj     |  6 ++
 .../tests/testsurround/testsurround.vcxproj   |  6 ++
 test/CMakeLists.txt                           | 32 ++++-----
 test/testatomic.c                             | 19 +++++
 test/testaudioinfo.c                          | 17 ++++-
 test/testbounds.c                             | 19 ++++-
 test/testdisplayinfo.c                        | 17 ++++-
 test/testerror.c                              | 17 ++++-
 test/testfilesystem.c                         | 18 ++++-
 test/testfilesystem_pre.c                     | 29 ++++++--
 test/testkeys.c                               | 19 ++++-
 test/testlocale.c                             | 67 +++++++++++++-----
 test/testplatform.c                           | 35 +++++++++-
 test/testpower.c                              | 19 ++++-
 test/testqsort.c                              | 69 +++++++++++++++----
 test/testsurround.c                           | 16 ++++-
 test/testthread.c                             | 43 +++++++++---
 test/testtimer.c                              | 21 ++++--
 test/testver.c                                | 20 +++++-
 21 files changed, 408 insertions(+), 93 deletions(-)

diff --git a/VisualC/tests/testatomic/testatomic.vcxproj b/VisualC/tests/testatomic/testatomic.vcxproj
index 25e89048e755d..772f14f1c895f 100644
--- a/VisualC/tests/testatomic/testatomic.vcxproj
+++ b/VisualC/tests/testatomic/testatomic.vcxproj
@@ -188,6 +188,12 @@
       <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
       <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
     </ProjectReference>
+    <ProjectReference Include="..\..\SDLtest\SDLtest.vcxproj">
+      <Project>{da956fd3-e143-46f2-9fe5-c77bebc56b1a}</Project>
+      <Private>false</Private>
+      <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
+      <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
+    </ProjectReference>
     <ProjectReference Include="..\..\SDLmain\SDLmain.vcxproj">
       <Project>{da956fd3-e142-46f2-9dd5-c78bebb56b7a}</Project>
       <Private>false</Private>
diff --git a/VisualC/tests/testplatform/testplatform.vcxproj b/VisualC/tests/testplatform/testplatform.vcxproj
index c0a7008627054..68163f6b8c010 100644
--- a/VisualC/tests/testplatform/testplatform.vcxproj
+++ b/VisualC/tests/testplatform/testplatform.vcxproj
@@ -216,6 +216,12 @@
       <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
       <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
     </ProjectReference>
+    <ProjectReference Include="..\..\SDLtest\SDLtest.vcxproj">
+      <Project>{da956fd3-e143-46f2-9fe5-c77bebc56b1a}</Project>
+      <Private>false</Private>
+      <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
+      <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
+    </ProjectReference>
     <ProjectReference Include="..\..\SDLmain\SDLmain.vcxproj">
       <Project>{da956fd3-e142-46f2-9dd5-c78bebb56b7a}</Project>
       <Private>false</Private>
diff --git a/VisualC/tests/testpower/testpower.vcxproj b/VisualC/tests/testpower/testpower.vcxproj
index 906d4350d57c0..b0f87d30e7011 100644
--- a/VisualC/tests/testpower/testpower.vcxproj
+++ b/VisualC/tests/testpower/testpower.vcxproj
@@ -188,6 +188,12 @@
       <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
       <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
     </ProjectReference>
+    <ProjectReference Include="..\..\SDLtest\SDLtest.vcxproj">
+      <Project>{da956fd3-e143-46f2-9fe5-c77bebc56b1a}</Project>
+      <Private>false</Private>
+      <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
+      <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
+    </ProjectReference>
     <ProjectReference Include="..\..\SDLmain\SDLmain.vcxproj">
       <Project>{da956fd3-e142-46f2-9dd5-c78bebb56b7a}</Project>
       <Private>false</Private>
diff --git a/VisualC/tests/testsurround/testsurround.vcxproj b/VisualC/tests/testsurround/testsurround.vcxproj
index 32860525eb621..6de4442c5018b 100644
--- a/VisualC/tests/testsurround/testsurround.vcxproj
+++ b/VisualC/tests/testsurround/testsurround.vcxproj
@@ -194,6 +194,12 @@
       <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
       <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
     </ProjectReference>
+    <ProjectReference Include="..\..\SDLtest\SDLtest.vcxproj">
+      <Project>{da956fd3-e143-46f2-9fe5-c77bebc56b1a}</Project>
+      <Private>false</Private>
+      <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
+      <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
+    </ProjectReference>
     <ProjectReference Include="..\..\SDLmain\SDLmain.vcxproj">
       <Project>{da956fd3-e142-46f2-9dd5-c78bebb56b7a}</Project>
       <Private>false</Private>
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index a19f88a757701..8d413c85d571b 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -112,23 +112,23 @@ add_sdl_test_executable(checkkeys checkkeys.c)
 add_sdl_test_executable(checkkeysthreads checkkeysthreads.c)
 add_sdl_test_executable(loopwave NEEDS_RESOURCES loopwave.c testutils.c)
 add_sdl_test_executable(loopwavequeue NEEDS_RESOURCES loopwavequeue.c testutils.c)
-add_sdl_test_executable(testsurround NOTRACKMEM testsurround.c)
+add_sdl_test_executable(testsurround testsurround.c)
 add_sdl_test_executable(testresample NEEDS_RESOURCES testresample.c)
-add_sdl_test_executable(testaudioinfo NOTRACKMEM testaudioinfo.c)
+add_sdl_test_executable(testaudioinfo testaudioinfo.c)
 
 file(GLOB TESTAUTOMATION_SOURCE_FILES testautomation*.c)
 add_sdl_test_executable(testautomation NONINTERACTIVE NEEDS_RESOURCES ${TESTAUTOMATION_SOURCE_FILES})
 add_sdl_test_executable(testmultiaudio NEEDS_RESOURCES testmultiaudio.c testutils.c)
 add_sdl_test_executable(testaudiohotplug NEEDS_RESOURCES testaudiohotplug.c testutils.c)
 add_sdl_test_executable(testaudiocapture testaudiocapture.c)
-add_sdl_test_executable(testatomic NOTRACKMEM NONINTERACTIVE testatomic.c)
+add_sdl_test_executable(testatomic NONINTERACTIVE testatomic.c)
 add_sdl_test_executable(testintersections testintersections.c)
 add_sdl_test_executable(testrelative testrelative.c)
 add_sdl_test_executable(testhittesting testhittesting.c)
 add_sdl_test_executable(testdraw2 testdraw2.c)
 add_sdl_test_executable(testdrawchessboard testdrawchessboard.c)
 add_sdl_test_executable(testdropfile testdropfile.c)
-add_sdl_test_executable(testerror NOTRACKMEM NONINTERACTIVE testerror.c)
+add_sdl_test_executable(testerror NONINTERACTIVE testerror.c)
 
 if(LINUX)
     add_sdl_test_executable(testevdev NOTRACKMEM NONINTERACTIVE testevdev.c)
@@ -148,13 +148,13 @@ endif()
 add_sdl_test_executable(testhaptic testhaptic.c)
 add_sdl_test_executable(testhotplug testhotplug.c)
 add_sdl_test_executable(testrumble testrumble.c)
-add_sdl_test_executable(testthread NOTRACKMEM NONINTERACTIVE testthread.c)
+add_sdl_test_executable(testthread NONINTERACTIVE testthread.c)
 add_sdl_test_executable(testiconv NEEDS_RESOURCES testiconv.c testutils.c)
 add_sdl_test_executable(testime NEEDS_RESOURCES testime.c testutils.c)
 add_sdl_test_executable(testjoystick testjoystick.c)
-add_sdl_test_executable(testkeys NOTRACKMEM testkeys.c)
+add_sdl_test_executable(testkeys testkeys.c)
 add_sdl_test_executable(testloadso testloadso.c)
-add_sdl_test_executable(testlocale NOTRACKMEM NONINTERACTIVE testlocale.c)
+add_sdl_test_executable(testlocale NONINTERACTIVE testlocale.c)
 add_sdl_test_executable(testlock testlock.c)
 add_sdl_test_executable(testmouse testmouse.c)
 
@@ -182,11 +182,11 @@ elseif(OS2)
 endif()
 
 add_sdl_test_executable(testoverlay2 NEEDS_RESOURCES testoverlay2.c testyuv_cvt.c testutils.c)
-add_sdl_test_executable(testplatform NOTRACKMEM NONINTERACTIVE testplatform.c)
-add_sdl_test_executable(testpower NOTRACKMEM NONINTERACTIVE testpower.c)
-add_sdl_test_executable(testfilesystem NOTRACKMEM NONINTERACTIVE testfilesystem.c)
+add_sdl_test_executable(testplatform NONINTERACTIVE testplatform.c)
+add_sdl_test_executable(testpower NONINTERACTIVE testpower.c)
+add_sdl_test_executable(testfilesystem NONINTERACTIVE testfilesystem.c)
 if(WIN32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
-    add_sdl_test_executable(testfilesystem_pre NOTRACKMEM NONINTERACTIVE testfilesystem_pre.c)
+    add_sdl_test_executable(testfilesystem_pre NONINTERACTIVE testfilesystem_pre.c)
 endif()
 add_sdl_test_executable(testrendertarget NEEDS_RESOURCES testrendertarget.c testutils.c)
 add_sdl_test_executable(testscale NEEDS_RESOURCES testscale.c testutils.c)
@@ -197,18 +197,18 @@ add_sdl_test_executable(testshape NEEDS_RESOURCES testshape.c)
 add_sdl_test_executable(testsprite2 NEEDS_RESOURCES testsprite2.c testutils.c)
 add_sdl_test_executable(testspriteminimal NEEDS_RESOURCES testspriteminimal.c testutils.c)
 add_sdl_test_executable(teststreaming NEEDS_RESOURCES teststreaming.c testutils.c)
-add_sdl_test_executable(testtimer NOTRACKMEM NONINTERACTIVE testtimer.c)
+add_sdl_test_executable(testtimer NONINTERACTIVE testtimer.c)
 add_sdl_test_executable(testurl testurl.c)
-add_sdl_test_executable(testver NOTRACKMEM NONINTERACTIVE testver.c)
+add_sdl_test_executable(testver NONINTERACTIVE testver.c)
 add_sdl_test_executable(testviewport NEEDS_RESOURCES testviewport.c testutils.c)
 add_sdl_test_executable(testwm2 testwm2.c)
 add_sdl_test_executable(testyuv NEEDS_RESOURCES testyuv.c testyuv_cvt.c)
 add_sdl_test_executable(torturethread torturethread.c)
 add_sdl_test_executable(testrendercopyex NEEDS_RESOURCES testrendercopyex.c testutils.c)
 add_sdl_test_executable(testmessage testmessage.c)
-add_sdl_test_executable(testdisplayinfo NOTRACKMEM testdisplayinfo.c)
-add_sdl_test_executable(testqsort NOTRACKMEM NONINTERACTIVE testqsort.c)
-add_sdl_test_executable(testbounds NOTRACKMEM testbounds.c)
+add_sdl_test_executable(testdisplayinfo testdisplayinfo.c)
+add_sdl_test_executable(testqsort NONINTERACTIVE testqsort.c)
+add_sdl_test_executable(testbounds testbounds.c)
 add_sdl_test_executable(testcustomcursor testcustomcursor.c)
 add_sdl_test_executable(controllermap NEEDS_RESOURCES controllermap.c testutils.c)
 add_sdl_test_executable(testvulkan testvulkan.c)
diff --git a/test/testatomic.c b/test/testatomic.c
index c19415ac16a4d..1574295aa5ade 100644
--- a/test/testatomic.c
+++ b/test/testatomic.c
@@ -12,6 +12,7 @@
 #include <stdio.h>
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 /*
   Absolutely basic tests just to see if we get the expected value
@@ -699,9 +700,26 @@ static void RunFIFOTest(SDL_bool lock_free)
 
 int main(int argc, char *argv[])
 {
+    SDLTest_CommonState *state;
+
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
+
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
+        return 1;
+    }
+
     RunBasicTest();
 
     if (SDL_getenv("SDL_TESTS_QUICK") != NULL) {
@@ -715,6 +733,7 @@ int main(int argc, char *argv[])
     RunFIFOTest(SDL_FALSE);
 #endif
     RunFIFOTest(SDL_TRUE);
+    SDLTest_CommonQuit(state);
     return 0;
 }
 
diff --git a/test/testaudioinfo.c b/test/testaudioinfo.c
index ec4b86470d707..eb9afa70fb139 100644
--- a/test/testaudioinfo.c
+++ b/test/testaudioinfo.c
@@ -11,6 +11,7 @@
 */
 #include <stdio.h>
 #include "SDL.h"
+#include "SDL_test.h"
 
 static void
 print_devices(int iscapture)
@@ -50,12 +51,22 @@ int main(int argc, char **argv)
     char *deviceName = NULL;
     SDL_AudioSpec spec;
     int n;
+    SDLTest_CommonState *state;
+
+    state = SDLTest_CommonCreateState(argv, SDL_INIT_AUDIO);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
 
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
-    /* Load the SDL library */
-    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
         return 1;
     }
@@ -98,6 +109,6 @@ int main(int argc, char **argv)
         SDL_Log("SDL_AudioFormat: %X\n", spec.format);
     }
 
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
diff --git a/test/testbounds.c b/test/testbounds.c
index 9bd73ac962d18..8879d5fbdd84a 100644
--- a/test/testbounds.c
+++ b/test/testbounds.c
@@ -11,12 +11,27 @@
 */
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 int main(int argc, char **argv)
 {
     int total, i;
+    SDLTest_CommonState *state;
 
-    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
+
+    /* Enable standard application logging */
+    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
         SDL_Log("SDL_Init(SDL_INIT_VIDEO) failed: %s", SDL_GetError());
         return 1;
     }
@@ -32,7 +47,7 @@ int main(int argc, char **argv)
                 usable.x, usable.y, usable.w, usable.h);
     }
 
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
 
diff --git a/test/testdisplayinfo.c b/test/testdisplayinfo.c
index 9740ab1742878..94f5ccee6bb2e 100644
--- a/test/testdisplayinfo.c
+++ b/test/testdisplayinfo.c
@@ -13,6 +13,7 @@
 /* Program to test querying of display info */
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -33,12 +34,22 @@ int main(int argc, char *argv[])
 {
     SDL_DisplayMode mode;
     int num_displays, dpy;
+    SDLTest_CommonState *state;
+
+    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
 
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
-    /* Load the SDL library */
-    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
         return 1;
     }
@@ -88,7 +99,7 @@ int main(int argc, char *argv[])
         SDL_Log("\n");
     }
 
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
 
diff --git a/test/testerror.c b/test/testerror.c
index 25874d1ea78f7..3124da2f20c5e 100644
--- a/test/testerror.c
+++ b/test/testerror.c
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 static int alive = 0;
 
@@ -44,12 +45,22 @@ ThreadFunc(void *data)
 int main(int argc, char *argv[])
 {
     SDL_Thread *thread;
+    SDLTest_CommonState *state;
+
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
 
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
-    /* Load the SDL library */
-    if (SDL_Init(0) < 0) {
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
         return 1;
     }
@@ -76,6 +87,6 @@ int main(int argc, char *argv[])
 
     SDL_Log("Main thread error string: %s\n", SDL_GetError());
 
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
diff --git a/test/testfilesystem.c b/test/testfilesystem.c
index af0bd9f05a381..33426e2e1e081 100644
--- a/test/testfilesystem.c
+++ b/test/testfilesystem.c
@@ -13,17 +13,29 @@
 
 #include <stdio.h>
 #include "SDL.h"
+#include "SDL_test.h"
 
 int main(int argc, char *argv[])
 {
     char *base_path;
     char *pref_path;
+    SDLTest_CommonState *state;
+
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
 
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
-    if (SDL_Init(0) == -1) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
         return 1;
     }
 
@@ -54,6 +66,6 @@ int main(int argc, char *argv[])
         SDL_free(pref_path);
     }
 
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
diff --git a/test/testfilesystem_pre.c b/test/testfilesystem_pre.c
index 11615abd281f5..b9beaaf203ea3 100644
--- a/test/testfilesystem_pre.c
+++ b/test/testfilesystem_pre.c
@@ -17,16 +17,35 @@
  */
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 int main(int argc, char *argv[])
 {
+    SDLTest_CommonState *state;
     Uint64 start;
-    (void)argc;
-    (void)argv;
-    SDL_Init(0);
+    char *path;
+
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
+
+    /* Enable standard application logging */
+    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
     start = SDL_GetTicks();
-    SDL_GetPrefPath("libsdl", "test_filesystem");
+    if (!SDLTest_CommonInit(state)) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
+        return 1;
+    }
+    path = SDL_GetPrefPath("libsdl", "test_filesystem");
+    SDL_free(path);
     SDL_Log("SDL_GetPrefPath took %" SDL_PRIu64 "ms", SDL_GetTicks() - start);
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
diff --git a/test/testkeys.c b/test/testkeys.c
index c7b120b4f5297..d477047552d24 100644
--- a/test/testkeys.c
+++ b/test/testkeys.c
@@ -18,14 +18,31 @@
 #include <string.h>
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 int main(int argc, char *argv[])
 {
+    SDLTest_CommonState *state;
     SDL_Scancode scancode;
 
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
+
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
+        return 1;
+    }
+
     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
         exit(1);
@@ -34,6 +51,6 @@ int main(int argc, char *argv[])
         SDL_Log("Scancode #%d, \"%s\"\n", scancode,
                 SDL_GetScancodeName(scancode));
     }
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
diff --git a/test/testlocale.c b/test/testlocale.c
index 6e371ca10175a..4d17cec13d7a8 100644
--- a/test/testlocale.c
+++ b/test/testlocale.c
@@ -11,6 +11,7 @@
 */
 #include <stdio.h>
 #include "SDL.h"
+#include "SDL_test.h"
 
 /* !!! FIXME: move this to the test framework */
 
@@ -35,32 +36,64 @@ static void log_locales(void)
 
 int main(int argc, char **argv)
 {
+    SDLTest_CommonState *state;
+    SDL_bool listen = SDL_FALSE;
+    int i = 1;
+
+    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
+
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
+    /* Parse commandline */
+    for (i = 1; i < argc;) {
+        int consumed;
+
+        consumed = SDLTest_CommonArg(state, i);
+        if (!consumed) {
+            if (SDL_strcmp("--listen", argv[i]) == 0) {
+                listen = SDL_TRUE;
+                consumed = 1;
+            }
+        }
+        if (consumed <= 0) {
+            static const char *options[] = { "[--listen]", NULL };
+            SDLTest_CommonLogUsage(state, argv[0], options);
+            exit(1);
+        }
+
+        i += consumed;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
+        return 1;
+    }
+
     /* Print locales and languages */
-    if (SDL_Init(SDL_INIT_VIDEO) != -1) {
-        log_locales();
-
-        if ((argc == 2) && (SDL_strcmp(argv[1], "--listen") == 0)) {
-            SDL_bool keep_going = SDL_TRUE;
-            while (keep_going) {
-                SDL_Event e;
-                while (SDL_PollEvent(&e)) {
-                    if (e.type == SDL_QUIT) {
-                        keep_going = SDL_FALSE;
-                    } else if (e.type == SDL_LOCALECHANGED) {
-                        SDL_Log("Saw SDL_LOCALECHANGED event!");
-                        log_locales();
-                    }
+    log_locales();
+
+    if (listen) {
+        SDL_bool keep_going = SDL_TRUE;
+        while (keep_going) {
+            SDL_Event e;
+            while (SDL_PollEvent(&e)) {
+                if (e.type == SDL_QUIT) {
+                    keep_going = SDL_FALSE;
+                } else if (e.type == SDL_LOCALECHANGED) {
+                    SDL_Log("Saw SDL_LOCALECHANGED event!");
+                    log_locales();
                 }
-                SDL_Delay(10);
             }
+            SDL_Delay(10);
         }
-
-        SDL_Quit();
     }
 
+    SDLTest_CommonQuit(state);
+
     return 0;
 }
 
diff --git a/test/testplatform.c b/test/testplatform.c
index 353758a7dac2c..e017ef65da02f 100644
--- a/test/testplatform.c
+++ b/test/testplatform.c
@@ -13,6 +13,7 @@
 #include <stdio.h>
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 /*
  * Watcom C flags these as Warning 201: "Unreachable code" if you just
@@ -445,13 +446,42 @@ int main(int argc, char *argv[])
 {
     SDL_bool verbose = SDL_TRUE;
     int status = 0;
+    int i;
+    SDLTest_CommonState *state;
+
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
 
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
-    if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
-        verbose = SDL_FALSE;
+    /* Parse commandline */
+    for (i = 1; i < argc;) {
+        int consumed;
+
+        consumed = SDLTest_CommonArg(state, i);
+        if (!consumed) {
+            if (SDL_strcmp("-q", argv[i]) == 0) {
+                verbose = SDL_FALSE;
+                consumed = 1;
+            }
+        }
+        if (consumed <= 0) {
+            static const char *options[] = { "[-q]", NULL };
+            SDLTest_CommonLogUsage(state, argv[0], options);
+            exit(1);
+        }
+
+        i += consumed;
     }
+
+    if (!SDLTest_CommonInit(state)) {
+        return 1;
+    }
+
     if (verbose) {
         SDL_Log("This system is running %s\n", SDL_GetPlatform());
     }
@@ -462,5 +492,6 @@ int main(int argc, char *argv[])
     status += TestCPUInfo(verbose);
     status += TestAssertions(verbose);
 
+    SDLTest_CommonQuit(state);
     return status;
 }
diff --git a/test/testpower.c b/test/testpower.c
index 1c13b4dbaf589..426a61235c1bf 100644
--- a/test/testpower.c
+++ b/test/testpower.c
@@ -13,6 +13,7 @@
 
 #include <stdio.h>
 #include "SDL.h"
+#include "SDL_test.h"
 
 static void
 report_power(void)
@@ -60,17 +61,29 @@ report_power(void)
 
 int main(int argc, char *argv[])
 {
+    SDLTest_CommonState *state;
+
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
+
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
-    if (SDL_Init(0) == -1) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init() failed: %s\n", SDL_GetError());
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
         return 1;
     }
 
     report_power();
 
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
 
diff --git a/test/testqsort.c b/test/testqsort.c
index 9c1f7646d95c0..44dc337f2ddcf 100644
--- a/test/testqsort.c
+++ b/test/testqsort.c
@@ -43,36 +43,73 @@ test_sort(const char *desc, int *nums, const int arraylen)
 
 int main(int argc, char *argv[])
 {
+    SDLTest_CommonState *state;
     static int nums[1024 * 100];
     static const int itervals[] = { SDL_arraysize(nums), 12 };
     int iteration;
+    int i;
+    SDL_bool custom_seed = SDL_FALSE;
+    Uint64 seed;
     SDLTest_RandomContext rndctx;
 
-    if (argc > 1) {
-        int success;
-        Uint64 seed = 0;
-        if (argv[1][0] == '0' && argv[1][1] == 'x') {
-            success = SDL_sscanf(argv[1] + 2, "%" SDL_PRIx64, &seed);
-        } else {
-            success = SDL_sscanf(argv[1], "%" SDL_PRIu64, &seed);
-        }
-        if (!success) {
-            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid seed. Use a decimal or hexadecimal number.\n");
-            return 1;
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
+
+    /* Enable standard application logging */
+    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+
+    /* Parse commandline */
+    for (i = 1; i < argc;) {
+        int consumed;
+
+        consumed = SDLTest_CommonArg(state, i);
+        if (!consumed) {
+            if (!custom_seed) {
+                int success;
+
+                if (argv[i][0] == '0' && argv[i][1] == 'x') {
+                    success = SDL_sscanf(argv[i] + 2, "%" SDL_PRIx64, &seed);
+                } else {
+                    success = SDL_sscanf(argv[i], "%" SDL_PRIu64, &seed);
+                }
+                if (!success) {
+                    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;
+                }
+                custom_seed = SDL_TRUE;
+                consumed = 1;
+            }
         }
-        if (seed <= ((Uint64)0xffffffff)) {
-            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Seed must be equal or greater than 0x100000000.\n");
-            return 1;
+
+        if (consumed <= 0) {
+            static const char *options[] = { "[SEED]", NULL };
+            SDLTest_CommonLogUsage(state, argv[0], options);
+            exit(1);
         }
+
+        i += consumed;
+    }
+
+    if (custom_seed) {
         SDLTest_RandomInit(&rndctx, (unsigned int)(seed >> 32), (unsigned int)(seed & 0xffffffff));
     } else {
         SDLTest_RandomInitTime(&rndctx);
     }
     SDL_Log("Using random seed 0x%08x%08x\n", rndctx.x, rndctx.c);
 
+    if (!SDLTest_CommonInit(state)) {
+        return 1;
+    }
+
     for (iteration = 0; iteration < SDL_arraysize(itervals); iteration++) {
         const int arraylen = itervals[iteration];
-        int i;
 
         for (i = 0; i < arraylen; i++) {
             nums[i] = i;
@@ -96,6 +133,8 @@ int main(int argc, char *argv[])
         test_sort("random sorted", nums, arraylen);
     }
 
+    SDLTest_CommonQuit(state);
+
     return 0;
 }
 
diff --git a/test/testsurround.c b/test/testsurround.c
index b7dc7d7818cce..792b3951c3247 100644
--- a/test/testsurround.c
+++ b/test/testsurround.c
@@ -14,6 +14,7 @@
 #include "SDL_config.h"
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 static int total_channels;
 static int active_channel;
@@ -141,11 +142,22 @@ fill_buffer(void *unused, Uint8 *stream, int len)
 int main(int argc, char *argv[])
 {
     int i;
+    SDLTest_CommonState *state;
+
+    state = SDLTest_CommonCreateState(argv, SDL_INIT_AUDIO);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
 
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
-    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
+    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
+        return 1;
+    }
+
+    if (!SDLTest_CommonInit(state)) {
         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
         return 1;
     }
@@ -204,7 +216,7 @@ int main(int argc, char *argv[])
         SDL_CloseAudioDevice(dev);
     }
 
-    SDL_Quit();
+    SDLTest_CommonQuit(state);
     return 0;
 }
 
diff --git a/test/testthread.c b/test/testthread.c
index 5ceac05ffac38..892535055ec30 100644
--- a/test/testthread.c
+++ b/test/testthread.c
@@ -17,15 +17,18 @@
 #include <signal.h>
 
 #include "SDL.h"
+#include "SDL_test.h"
 
 static SDL_TLSID tls;
 static int alive = 0;
 static int testprio = 0;
+static SDLTest_CommonState *state;
 
 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
 static void
 quit(int rc)
 {
+    SDLTest_CommonQuit(state);
     SDL_Quit();
     exit(rc);
 }
@@ -82,14 +85,39 @@ killed(int sig)
 
 int main(int argc, char *argv[])
 {
-    int arg = 1;
+    int i = 1;
     SDL_Thread *thread;
 
+    state = SDLTest_CommonCreateState(argv, 0);
+    if (!state) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError());
+        return 1;
+    }
+

(Patch may be truncated, please check the link at the top of this post.)