1
0

testautomation_main.c 6.3 KB

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