testautomation_main.c 7.1 KB

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