testautomation_main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_InitSubSystem() and SDL_QuitSubSystem()
  12. * \sa
  13. * http://wiki.libsdl.org/SDL_Init
  14. * http://wiki.libsdl.org/SDL_Quit
  15. */
  16. static int main_testInitQuitSubSystem(void *arg)
  17. {
  18. int i;
  19. int subsystems[] = { SDL_INIT_JOYSTICK, SDL_INIT_HAPTIC, SDL_INIT_GAMEPAD };
  20. for (i = 0; i < SDL_arraysize(subsystems); ++i) {
  21. int initialized_system;
  22. int subsystem = subsystems[i];
  23. SDLTest_AssertCheck((SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) before init should be false", subsystem);
  24. SDLTest_AssertCheck(SDL_InitSubSystem(subsystem) == 0, "SDL_InitSubSystem(%x)", subsystem);
  25. initialized_system = SDL_WasInit(subsystem);
  26. SDLTest_AssertCheck((initialized_system & subsystem) != 0, "SDL_WasInit(%x) should be true (%x)", subsystem, initialized_system);
  27. SDL_QuitSubSystem(subsystem);
  28. SDLTest_AssertCheck((SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) after shutdown should be false", subsystem);
  29. }
  30. return TEST_COMPLETED;
  31. }
  32. const int joy_and_controller = SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD;
  33. static int main_testImpliedJoystickInit(void *arg)
  34. {
  35. int initialized_system;
  36. /* First initialize the controller */
  37. SDLTest_AssertCheck((SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller");
  38. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD) == 0, "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
  39. /* Then make sure this implicitly initialized the joystick subsystem */
  40. initialized_system = SDL_WasInit(joy_and_controller);
  41. SDLTest_AssertCheck((initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system);
  42. /* Then quit the controller, and make sure that implicitly also quits the */
  43. /* joystick subsystem */
  44. SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
  45. initialized_system = SDL_WasInit(joy_and_controller);
  46. SDLTest_AssertCheck((initialized_system & joy_and_controller) == 0, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system);
  47. return TEST_COMPLETED;
  48. }
  49. static int main_testImpliedJoystickQuit(void *arg)
  50. {
  51. int initialized_system;
  52. /* First initialize the controller and the joystick (explicitly) */
  53. SDLTest_AssertCheck((SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller");
  54. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0, "SDL_InitSubSystem(SDL_INIT_JOYSTICK)");
  55. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD) == 0, "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
  56. /* Then make sure they're both initialized properly */
  57. initialized_system = SDL_WasInit(joy_and_controller);
  58. SDLTest_AssertCheck((initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system);
  59. /* Then quit the controller, and make sure that it does NOT quit the */
  60. /* explicitly initialized joystick subsystem. */
  61. SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
  62. initialized_system = SDL_WasInit(joy_and_controller);
  63. SDLTest_AssertCheck((initialized_system & joy_and_controller) == SDL_INIT_JOYSTICK, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system);
  64. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  65. return TEST_COMPLETED;
  66. }
  67. #if defined(__GNUC__) || defined(__clang__)
  68. #pragma GCC diagnostic push
  69. #pragma GCC diagnostic ignored "-Wformat-zero-length"
  70. #endif
  71. static int
  72. main_testSetError(void *arg)
  73. {
  74. size_t i;
  75. char error[1024];
  76. error[0] = '\0';
  77. SDL_SetError("");
  78. SDLTest_AssertCheck(SDL_strcmp(error, SDL_GetError()) == 0, "SDL_SetError(\"\")");
  79. for (i = 0; i < (sizeof(error) - 1); ++i) {
  80. error[i] = 'a' + (i % 26);
  81. }
  82. error[i] = '\0';
  83. SDL_SetError("%s", error);
  84. SDLTest_AssertCheck(SDL_strcmp(error, SDL_GetError()) == 0, "SDL_SetError(\"abc...1023\")");
  85. return TEST_COMPLETED;
  86. }
  87. #if defined(__GNUC__) || defined(__clang__)
  88. #pragma GCC diagnostic pop
  89. #endif
  90. static const SDLTest_TestCaseReference mainTest1 = {
  91. (SDLTest_TestCaseFp)main_testInitQuitSubSystem, "main_testInitQuitSubSystem", "Tests SDL_InitSubSystem/QuitSubSystem", TEST_ENABLED
  92. };
  93. static const SDLTest_TestCaseReference mainTest2 = {
  94. (SDLTest_TestCaseFp)main_testImpliedJoystickInit, "main_testImpliedJoystickInit", "Tests that init for gamecontroller properly implies joystick", TEST_ENABLED
  95. };
  96. static const SDLTest_TestCaseReference mainTest3 = {
  97. (SDLTest_TestCaseFp)main_testImpliedJoystickQuit, "main_testImpliedJoystickQuit", "Tests that quit for gamecontroller doesn't quit joystick if you inited it explicitly", TEST_ENABLED
  98. };
  99. static const SDLTest_TestCaseReference mainTest4 = {
  100. (SDLTest_TestCaseFp)main_testSetError, "main_testSetError", "Tests that SDL_SetError() handles arbitrarily large strings", TEST_ENABLED
  101. };
  102. /* Sequence of Main test cases */
  103. static const SDLTest_TestCaseReference *mainTests[] = {
  104. &mainTest1,
  105. &mainTest2,
  106. &mainTest3,
  107. &mainTest4,
  108. NULL
  109. };
  110. /* Main test suite (global) */
  111. SDLTest_TestSuiteReference mainTestSuite = {
  112. "Main",
  113. NULL,
  114. mainTests,
  115. NULL
  116. };