summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testautomation_main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testautomation_main.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testautomation_main.c172
1 files changed, 172 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testautomation_main.c b/src/contrib/SDL-3.2.20/test/testautomation_main.c
new file mode 100644
index 0000000..89991c0
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testautomation_main.c
@@ -0,0 +1,172 @@
1/**
2 * Automated SDL subsystems management test.
3 *
4 * Written by J�rgen Tjern� "jorgenpt"
5 *
6 * Released under Public Domain.
7 */
8
9#include <SDL3/SDL.h>
10#include <SDL3/SDL_test.h>
11#include "testautomation_suites.h"
12#ifdef HAVE_BUILD_CONFIG
13#include "SDL_build_config.h"
14#endif
15
16/**
17 * Tests SDL_InitSubSystem() and SDL_QuitSubSystem()
18 * \sa SDL_Init
19 * \sa SDL_Quit
20 */
21static int SDLCALL main_testInitQuitSubSystem(void *arg)
22{
23 int i;
24 int subsystems[] = { SDL_INIT_JOYSTICK, SDL_INIT_HAPTIC, SDL_INIT_GAMEPAD };
25
26 for (i = 0; i < SDL_arraysize(subsystems); ++i) {
27 int initialized_system;
28 int subsystem = subsystems[i];
29
30 SDLTest_AssertCheck((SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) before init should be false", subsystem);
31 SDLTest_AssertCheck(SDL_InitSubSystem(subsystem), "SDL_InitSubSystem(%x)", subsystem);
32
33 initialized_system = SDL_WasInit(subsystem);
34 SDLTest_AssertCheck((initialized_system & subsystem) != 0, "SDL_WasInit(%x) should be true (%x)", subsystem, initialized_system);
35
36 SDL_QuitSubSystem(subsystem);
37
38 SDLTest_AssertCheck((SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) after shutdown should be false", subsystem);
39 }
40
41 return TEST_COMPLETED;
42}
43
44static const int joy_and_controller = SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD;
45static int SDLCALL main_testImpliedJoystickInit(void *arg)
46{
47 int initialized_system;
48
49 /* First initialize the controller */
50 SDLTest_AssertCheck((SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller");
51 SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD), "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
52
53 /* Then make sure this implicitly initialized the joystick subsystem */
54 initialized_system = SDL_WasInit(joy_and_controller);
55 SDLTest_AssertCheck((initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system);
56
57 /* Then quit the controller, and make sure that implicitly also quits the */
58 /* joystick subsystem */
59 SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
60 initialized_system = SDL_WasInit(joy_and_controller);
61 SDLTest_AssertCheck((initialized_system & joy_and_controller) == 0, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system);
62
63 return TEST_COMPLETED;
64}
65
66static int SDLCALL main_testImpliedJoystickQuit(void *arg)
67{
68 int initialized_system;
69
70 /* First initialize the controller and the joystick (explicitly) */
71 SDLTest_AssertCheck((SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller");
72 SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_JOYSTICK), "SDL_InitSubSystem(SDL_INIT_JOYSTICK)");
73 SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD), "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
74
75 /* Then make sure they're both initialized properly */
76 initialized_system = SDL_WasInit(joy_and_controller);
77 SDLTest_AssertCheck((initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system);
78
79 /* Then quit the controller, and make sure that it does NOT quit the */
80 /* explicitly initialized joystick subsystem. */
81 SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
82 initialized_system = SDL_WasInit(joy_and_controller);
83 SDLTest_AssertCheck((initialized_system & joy_and_controller) == SDL_INIT_JOYSTICK, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system);
84
85 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
86
87 return TEST_COMPLETED;
88}
89
90#if defined(__GNUC__) || defined(__clang__)
91#pragma GCC diagnostic push
92#pragma GCC diagnostic ignored "-Wformat-zero-length"
93#endif
94
95static int SDLCALL
96main_testSetError(void *arg)
97{
98 size_t i;
99 char error_input[1024];
100 int result;
101 const char *error;
102 size_t expected_len;
103
104 SDLTest_AssertPass("SDL_SetError(NULL)");
105 result = SDL_SetError(NULL);
106 SDLTest_AssertCheck(result == false, "SDL_SetError(NULL) -> %d (expected %d)", result, false);
107 error = SDL_GetError();
108 SDLTest_AssertCheck(SDL_strcmp(error, "") == 0, "SDL_GetError() -> \"%s\" (expected \"%s\")", error, "");
109
110 SDLTest_AssertPass("SDL_SetError(\"\")");
111 result = SDL_SetError("");
112 SDLTest_AssertCheck(result == false, "SDL_SetError(\"\") -> %d (expected %d)", result, false);
113 error = SDL_GetError();
114 SDLTest_AssertCheck(SDL_strcmp(error, "") == 0, "SDL_GetError() -> \"%s\" (expected \"%s\")", error, "");
115
116 error_input[0] = '\0';
117 for (i = 0; i < (sizeof(error_input) - 1); ++i) {
118 error_input[i] = 'a' + (i % 26);
119 }
120 error_input[i] = '\0';
121 SDLTest_AssertPass("SDL_SetError(\"abc...\")");
122 result = SDL_SetError("%s", error_input);
123 SDLTest_AssertCheck(result == false, "SDL_SetError(\"abc...\") -> %d (expected %d)", result, false);
124 error = SDL_GetError();
125
126#ifdef SDL_THREADS_DISABLED
127 expected_len = 128 - 1;
128#else
129 expected_len = sizeof(error_input) - 1;
130#endif
131 SDLTest_AssertPass("Verify SDL error is identical to the input error");
132 SDLTest_CompareMemory(error, SDL_strlen(error), error_input, expected_len);
133
134 return TEST_COMPLETED;
135}
136
137#if defined(__GNUC__) || defined(__clang__)
138#pragma GCC diagnostic pop
139#endif
140
141static const SDLTest_TestCaseReference mainTest1 = {
142 main_testInitQuitSubSystem, "main_testInitQuitSubSystem", "Tests SDL_InitSubSystem/QuitSubSystem", TEST_ENABLED
143};
144
145static const SDLTest_TestCaseReference mainTest2 = {
146 main_testImpliedJoystickInit, "main_testImpliedJoystickInit", "Tests that init for gamecontroller properly implies joystick", TEST_ENABLED
147};
148
149static const SDLTest_TestCaseReference mainTest3 = {
150 main_testImpliedJoystickQuit, "main_testImpliedJoystickQuit", "Tests that quit for gamecontroller doesn't quit joystick if you inited it explicitly", TEST_ENABLED
151};
152
153static const SDLTest_TestCaseReference mainTest4 = {
154 main_testSetError, "main_testSetError", "Tests that SDL_SetError() handles arbitrarily large strings", TEST_ENABLED
155};
156
157/* Sequence of Main test cases */
158static const SDLTest_TestCaseReference *mainTests[] = {
159 &mainTest1,
160 &mainTest2,
161 &mainTest3,
162 &mainTest4,
163 NULL
164};
165
166/* Main test suite (global) */
167SDLTest_TestSuiteReference mainTestSuite = {
168 "Main",
169 NULL,
170 mainTests,
171 NULL
172};