testautomation_main.c 6.5 KB

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