testautomation_main.c 5.4 KB

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