Debian Codegraph @

test/testautomation_main.c

raw
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 "SDL.h"
10 #include "SDL_test.h"
11
12 /* !
13 * \brief Tests SDL_InitSubSystem() and SDL_QuitSubSystem()
14 * \sa
15 * http://wiki.libsdl.org/SDL_Init
16 * http://wiki.libsdl.org/SDL_Quit
17 */
18 static int main_testInitQuitSubSystem(void *arg)
19 {
20 #if defined SDL_JOYSTICK_DISABLED || defined SDL_HAPTIC_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
21 return TEST_SKIPPED;
22 #else
23 int i;
24 int subsystems[] = { SDL_INIT_JOYSTICK, SDL_INIT_HAPTIC, SDL_INIT_GAMECONTROLLER };
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) == 0, "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 #endif
43 }
44
45 const int joy_and_controller = SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER;
46 static int main_testImpliedJoystickInit(void *arg)
47 {
48 #if defined SDL_JOYSTICK_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
49 return TEST_SKIPPED;
50 #else
51 int initialized_system;
52
53 /* First initialize the controller */
54 SDLTest_AssertCheck((SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller");
55 SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)");
56
57 /* Then make sure this implicitly initialized the joystick subsystem */
58 initialized_system = SDL_WasInit(joy_and_controller);
59 SDLTest_AssertCheck((initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system);
60
61 /* Then quit the controller, and make sure that implicitly also quits the */
62 /* joystick subsystem */
63 SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
64 initialized_system = SDL_WasInit(joy_and_controller);
65 SDLTest_AssertCheck((initialized_system & joy_and_controller) == 0, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system);
66
67 return TEST_COMPLETED;
68 #endif
69 }
70
71 static int main_testImpliedJoystickQuit(void *arg)
72 {
73 #if defined SDL_JOYSTICK_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
74 return TEST_SKIPPED;
75 #else
76 int initialized_system;
77
78 /* First initialize the controller and the joystick (explicitly) */
79 SDLTest_AssertCheck((SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller");
80 SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0, "SDL_InitSubSystem(SDL_INIT_JOYSTICK)");
81 SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)");
82
83 /* Then make sure they're both initialized properly */
84 initialized_system = SDL_WasInit(joy_and_controller);
85 SDLTest_AssertCheck((initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system);
86
87 /* Then quit the controller, and make sure that it does NOT quit the */
88 /* explicitly initialized joystick subsystem. */
89 SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
90 initialized_system = SDL_WasInit(joy_and_controller);
91 SDLTest_AssertCheck((initialized_system & joy_and_controller) == SDL_INIT_JOYSTICK, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system);
92
93 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
94
95 return TEST_COMPLETED;
96 #endif
97 }
98
99 #if defined(__GNUC__) || defined(__clang__)
100 #pragma GCC diagnostic push
101 #pragma GCC diagnostic ignored "-Wformat-zero-length"
102 #endif
103
104 static int
105 main_testSetError(void *arg)
106 {
107 size_t i;
108 char error[1024];
109
110 error[0] = '\0';
111 SDL_SetError("");
112 SDLTest_AssertCheck(SDL_strcmp(error, SDL_GetError()) == 0, "SDL_SetError(\"\")");
113
114 for (i = 0; i < (sizeof(error) - 1); ++i) {
115 error[i] = 'a' + (i % 26);
116 }
117 error[i] = '\0';
118 SDL_SetError("%s", error);
119 SDLTest_AssertCheck(SDL_strcmp(error, SDL_GetError()) == 0, "SDL_SetError(\"abc...1023\")");
120
121 return TEST_COMPLETED;
122 }
123
124 #if defined(__GNUC__) || defined(__clang__)
125 #pragma GCC diagnostic pop
126 #endif
127
128 static const SDLTest_TestCaseReference mainTest1 = {
129 (SDLTest_TestCaseFp)main_testInitQuitSubSystem, "main_testInitQuitSubSystem", "Tests SDL_InitSubSystem/QuitSubSystem", TEST_ENABLED
130 };
131
132 static const SDLTest_TestCaseReference mainTest2 = {
133 (SDLTest_TestCaseFp)main_testImpliedJoystickInit, "main_testImpliedJoystickInit", "Tests that init for gamecontroller properly implies joystick", TEST_ENABLED
134 };
135
136 static const SDLTest_TestCaseReference mainTest3 = {
137 (SDLTest_TestCaseFp)main_testImpliedJoystickQuit, "main_testImpliedJoystickQuit", "Tests that quit for gamecontroller doesn't quit joystick if you inited it explicitly", TEST_ENABLED
138 };
139
140 static const SDLTest_TestCaseReference mainTest4 = {
141 (SDLTest_TestCaseFp)main_testSetError, "main_testSetError", "Tests that SDL_SetError() handles arbitrarily large strings", TEST_ENABLED
142 };
143
144 /* Sequence of Main test cases */
145 static const SDLTest_TestCaseReference *mainTests[] = {
146 &mainTest1,
147 &mainTest2,
148 &mainTest3,
149 &mainTest4,
150 NULL
151 };
152
153 /* Main test suite (global) */
154 SDLTest_TestSuiteReference mainTestSuite = {
155 "Main",
156 NULL,
157 mainTests,
158 NULL
159 };