🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
7.0 KiB

  1. /**
  2. * Automated SDL subsystems management test.
  3. *
  4. * Written by Jrgen Tjern "jorgenpt"
  5. *
  6. * Released under Public Domain.
  7. */
  8. #include "SDL.h"
  9. #include "SDL_test.h"
  10. /* !
  11. * \brief Tests SDL_Init() and SDL_Quit() of Joystick and Haptic subsystems
  12. * \sa
  13. * http://wiki.libsdl.org/SDL_Init
  14. * http://wiki.libsdl.org/SDL_Quit
  15. */
  16. static int main_testInitQuitJoystickHaptic (void *arg)
  17. {
  18. #if defined SDL_JOYSTICK_DISABLED || defined SDL_HAPTIC_DISABLED
  19. return TEST_SKIPPED;
  20. #else
  21. int enabled_subsystems;
  22. int initialized_subsystems = SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC;
  23. SDLTest_AssertCheck( SDL_Init(initialized_subsystems) == 0, "SDL_Init multiple systems." );
  24. enabled_subsystems = SDL_WasInit(initialized_subsystems);
  25. SDLTest_AssertCheck( enabled_subsystems == initialized_subsystems, "SDL_WasInit(SDL_INIT_EVERYTHING) contains all systems (%i)", enabled_subsystems );
  26. SDL_Quit();
  27. enabled_subsystems = SDL_WasInit(initialized_subsystems);
  28. SDLTest_AssertCheck( enabled_subsystems == 0, "SDL_Quit should shut down everything (%i)", enabled_subsystems );
  29. return TEST_COMPLETED;
  30. #endif
  31. }
  32. /* !
  33. * \brief Tests SDL_InitSubSystem() and SDL_QuitSubSystem()
  34. * \sa
  35. * http://wiki.libsdl.org/SDL_Init
  36. * http://wiki.libsdl.org/SDL_Quit
  37. */
  38. static int main_testInitQuitSubSystem (void *arg)
  39. {
  40. #if defined SDL_JOYSTICK_DISABLED || defined SDL_HAPTIC_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
  41. return TEST_SKIPPED;
  42. #else
  43. int i;
  44. int subsystems[] = { SDL_INIT_JOYSTICK, SDL_INIT_HAPTIC, SDL_INIT_GAMECONTROLLER };
  45. for (i = 0; i < SDL_arraysize(subsystems); ++i) {
  46. int initialized_system;
  47. int subsystem = subsystems[i];
  48. SDLTest_AssertCheck( (SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) before init should be false", subsystem );
  49. SDLTest_AssertCheck( SDL_InitSubSystem(subsystem) == 0, "SDL_InitSubSystem(%x)", subsystem );
  50. initialized_system = SDL_WasInit(subsystem);
  51. SDLTest_AssertCheck( (initialized_system & subsystem) != 0, "SDL_WasInit(%x) should be true (%x)", subsystem, initialized_system );
  52. SDL_QuitSubSystem(subsystem);
  53. SDLTest_AssertCheck( (SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) after shutdown should be false", subsystem );
  54. }
  55. return TEST_COMPLETED;
  56. #endif
  57. }
  58. const int joy_and_controller = SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER;
  59. static int main_testImpliedJoystickInit (void *arg)
  60. {
  61. #if defined SDL_JOYSTICK_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
  62. return TEST_SKIPPED;
  63. #else
  64. int initialized_system;
  65. /* First initialize the controller */
  66. SDLTest_AssertCheck( (SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller" );
  67. SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)" );
  68. /* Then make sure this implicitly initialized the joystick subsystem */
  69. initialized_system = SDL_WasInit(joy_and_controller);
  70. SDLTest_AssertCheck( (initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system );
  71. /* Then quit the controller, and make sure that implicitly also quits the */
  72. /* joystick subsystem */
  73. SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
  74. initialized_system = SDL_WasInit(joy_and_controller);
  75. SDLTest_AssertCheck( (initialized_system & joy_and_controller) == 0, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system );
  76. return TEST_COMPLETED;
  77. #endif
  78. }
  79. static int main_testImpliedJoystickQuit (void *arg)
  80. {
  81. #if defined SDL_JOYSTICK_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
  82. return TEST_SKIPPED;
  83. #else
  84. int initialized_system;
  85. /* First initialize the controller and the joystick (explicitly) */
  86. SDLTest_AssertCheck( (SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller" );
  87. SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0, "SDL_InitSubSystem(SDL_INIT_JOYSTICK)" );
  88. SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)" );
  89. /* Then make sure they're both initialized properly */
  90. initialized_system = SDL_WasInit(joy_and_controller);
  91. SDLTest_AssertCheck( (initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system );
  92. /* Then quit the controller, and make sure that it does NOT quit the */
  93. /* explicitly initialized joystick subsystem. */
  94. SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
  95. initialized_system = SDL_WasInit(joy_and_controller);
  96. SDLTest_AssertCheck( (initialized_system & joy_and_controller) == SDL_INIT_JOYSTICK, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system );
  97. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  98. return TEST_COMPLETED;
  99. #endif
  100. }
  101. #if defined(__GNUC__) || defined(__clang__)
  102. #pragma GCC diagnostic push
  103. #pragma GCC diagnostic ignored "-Wformat-zero-length"
  104. #endif
  105. static int
  106. main_testSetError(void *arg)
  107. {
  108. size_t i;
  109. char error[1024];
  110. error[0] = '\0';
  111. SDL_SetError("");
  112. SDLTest_AssertCheck(SDL_strcmp(error, SDL_GetError()) == 0, "SDL_SetError(\"\")");
  113. for (i = 0; i < (sizeof(error)-1); ++i) {
  114. error[i] = 'a' + (i % 26);
  115. }
  116. error[i] = '\0';
  117. SDL_SetError("%s", error);
  118. SDLTest_AssertCheck(SDL_strcmp(error, SDL_GetError()) == 0, "SDL_SetError(\"abc...1023\")");
  119. return TEST_COMPLETED;
  120. }
  121. #if defined(__GNUC__) || defined(__clang__)
  122. #pragma GCC diagnostic pop
  123. #endif
  124. static const SDLTest_TestCaseReference mainTest1 =
  125. { (SDLTest_TestCaseFp)main_testInitQuitJoystickHaptic, "main_testInitQuitJoystickHaptic", "Tests SDL_Init/Quit of Joystick and Haptic subsystem", TEST_ENABLED};
  126. static const SDLTest_TestCaseReference mainTest2 =
  127. { (SDLTest_TestCaseFp)main_testInitQuitSubSystem, "main_testInitQuitSubSystem", "Tests SDL_InitSubSystem/QuitSubSystem", TEST_ENABLED};
  128. static const SDLTest_TestCaseReference mainTest3 =
  129. { (SDLTest_TestCaseFp)main_testImpliedJoystickInit, "main_testImpliedJoystickInit", "Tests that init for gamecontroller properly implies joystick", TEST_ENABLED};
  130. static const SDLTest_TestCaseReference mainTest4 =
  131. { (SDLTest_TestCaseFp)main_testImpliedJoystickQuit, "main_testImpliedJoystickQuit", "Tests that quit for gamecontroller doesn't quit joystick if you inited it explicitly", TEST_ENABLED};
  132. static const SDLTest_TestCaseReference mainTest5 =
  133. { (SDLTest_TestCaseFp)main_testSetError, "main_testSetError", "Tests that SDL_SetError() handles arbitrarily large strings", TEST_ENABLED};
  134. /* Sequence of Main test cases */
  135. static const SDLTest_TestCaseReference *mainTests[] = {
  136. &mainTest1,
  137. &mainTest2,
  138. &mainTest3,
  139. &mainTest4,
  140. &mainTest5,
  141. NULL
  142. };
  143. /* Main test suite (global) */
  144. SDLTest_TestSuiteReference mainTestSuite = {
  145. "Main",
  146. NULL,
  147. mainTests,
  148. NULL
  149. };
  150. /* vi: set ts=4 sw=4 expandtab: */