testautomation_main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include "testautomation_suites.h"
  11. /**
  12. * Tests SDL_InitSubSystem() and SDL_QuitSubSystem()
  13. * \sa SDL_Init
  14. * \sa 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. static 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_input[1024];
  76. int result;
  77. const char *error;
  78. SDLTest_AssertPass("SDL_SetError(NULL)");
  79. result = SDL_SetError(NULL);
  80. SDLTest_AssertCheck(result == -1, "SDL_SetError(NULL) -> %d (expected %d)", result, -1);
  81. error = SDL_GetError();
  82. SDLTest_AssertCheck(SDL_strcmp(error, "") == 0, "SDL_GetError() -> \"%s\" (expected \"%s\")", error, "");
  83. SDLTest_AssertPass("SDL_SetError(\"\")");
  84. result = SDL_SetError("");
  85. SDLTest_AssertCheck(result == -1, "SDL_SetError(\"\") -> %d (expected %d)", result, -1);
  86. error = SDL_GetError();
  87. SDLTest_AssertCheck(SDL_strcmp(error, "") == 0, "SDL_GetError() -> \"%s\" (expected \"%s\")", error, "");
  88. error_input[0] = '\0';
  89. for (i = 0; i < (sizeof(error_input) - 1); ++i) {
  90. error_input[i] = 'a' + (i % 26);
  91. }
  92. error_input[i] = '\0';
  93. SDLTest_AssertPass("SDL_SetError(\"abc...\")");
  94. result = SDL_SetError("%s", error_input);
  95. SDLTest_AssertCheck(result == -1, "SDL_SetError(\"abc...\") -> %d (expected %d)", result, -1);
  96. error = SDL_GetError();
  97. SDLTest_AssertPass("Verify SDL error is identical to the input error");
  98. SDLTest_CompareMemory(error, SDL_strlen(error), error_input, SDL_strlen(error_input));
  99. return TEST_COMPLETED;
  100. }
  101. #if defined(__GNUC__) || defined(__clang__)
  102. #pragma GCC diagnostic pop
  103. #endif
  104. static const SDLTest_TestCaseReference mainTest1 = {
  105. (SDLTest_TestCaseFp)main_testInitQuitSubSystem, "main_testInitQuitSubSystem", "Tests SDL_InitSubSystem/QuitSubSystem", TEST_ENABLED
  106. };
  107. static const SDLTest_TestCaseReference mainTest2 = {
  108. (SDLTest_TestCaseFp)main_testImpliedJoystickInit, "main_testImpliedJoystickInit", "Tests that init for gamecontroller properly implies joystick", TEST_ENABLED
  109. };
  110. static const SDLTest_TestCaseReference mainTest3 = {
  111. (SDLTest_TestCaseFp)main_testImpliedJoystickQuit, "main_testImpliedJoystickQuit", "Tests that quit for gamecontroller doesn't quit joystick if you inited it explicitly", TEST_ENABLED
  112. };
  113. static const SDLTest_TestCaseReference mainTest4 = {
  114. (SDLTest_TestCaseFp)main_testSetError, "main_testSetError", "Tests that SDL_SetError() handles arbitrarily large strings", TEST_ENABLED
  115. };
  116. /* Sequence of Main test cases */
  117. static const SDLTest_TestCaseReference *mainTests[] = {
  118. &mainTest1,
  119. &mainTest2,
  120. &mainTest3,
  121. &mainTest4,
  122. NULL
  123. };
  124. /* Main test suite (global) */
  125. SDLTest_TestSuiteReference mainTestSuite = {
  126. "Main",
  127. NULL,
  128. mainTests,
  129. NULL
  130. };