testautomation_main.c 6.3 KB

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