testautomation_main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Automated SDL subsystems management test.
  3. *
  4. * Written by J�rgen Tjern� "jorgenpt"
  5. *
  6. * Released under Public Domain.
  7. */
  8. #include "SDL.h"
  9. #include "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. #if defined SDL_JOYSTICK_DISABLED || defined SDL_HAPTIC_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
  19. return TEST_SKIPPED;
  20. #else
  21. int i;
  22. int subsystems[] = { SDL_INIT_JOYSTICK, SDL_INIT_HAPTIC, SDL_INIT_GAMECONTROLLER };
  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) == 0, "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. #endif
  35. }
  36. const int joy_and_controller = SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER;
  37. static int main_testImpliedJoystickInit(void *arg)
  38. {
  39. #if defined SDL_JOYSTICK_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
  40. return TEST_SKIPPED;
  41. #else
  42. int initialized_system;
  43. /* First initialize the controller */
  44. SDLTest_AssertCheck((SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller");
  45. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)");
  46. /* Then make sure this implicitly initialized the joystick subsystem */
  47. initialized_system = SDL_WasInit(joy_and_controller);
  48. SDLTest_AssertCheck((initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system);
  49. /* Then quit the controller, and make sure that implicitly also quits the */
  50. /* joystick subsystem */
  51. SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
  52. initialized_system = SDL_WasInit(joy_and_controller);
  53. SDLTest_AssertCheck((initialized_system & joy_and_controller) == 0, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system);
  54. return TEST_COMPLETED;
  55. #endif
  56. }
  57. static int main_testImpliedJoystickQuit(void *arg)
  58. {
  59. #if defined SDL_JOYSTICK_DISABLED || defined SDL_GAMECONTROLLER_DISABLED
  60. return TEST_SKIPPED;
  61. #else
  62. int initialized_system;
  63. /* First initialize the controller and the joystick (explicitly) */
  64. SDLTest_AssertCheck((SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller");
  65. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0, "SDL_InitSubSystem(SDL_INIT_JOYSTICK)");
  66. SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)");
  67. /* Then make sure they're both initialized properly */
  68. initialized_system = SDL_WasInit(joy_and_controller);
  69. SDLTest_AssertCheck((initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system);
  70. /* Then quit the controller, and make sure that it does NOT quit the */
  71. /* explicitly initialized joystick subsystem. */
  72. SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
  73. initialized_system = SDL_WasInit(joy_and_controller);
  74. SDLTest_AssertCheck((initialized_system & joy_and_controller) == SDL_INIT_JOYSTICK, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system);
  75. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  76. return TEST_COMPLETED;
  77. #endif
  78. }
  79. #if defined(__GNUC__) || defined(__clang__)
  80. #pragma GCC diagnostic push
  81. #pragma GCC diagnostic ignored "-Wformat-zero-length"
  82. #endif
  83. static int
  84. main_testSetError(void *arg)
  85. {
  86. size_t i;
  87. char error[1024];
  88. error[0] = '\0';
  89. SDL_SetError("");
  90. SDLTest_AssertCheck(SDL_strcmp(error, SDL_GetError()) == 0, "SDL_SetError(\"\")");
  91. for (i = 0; i < (sizeof(error) - 1); ++i) {
  92. error[i] = 'a' + (i % 26);
  93. }
  94. error[i] = '\0';
  95. SDL_SetError("%s", error);
  96. SDLTest_AssertCheck(SDL_strcmp(error, SDL_GetError()) == 0, "SDL_SetError(\"abc...1023\")");
  97. return TEST_COMPLETED;
  98. }
  99. #if defined(__GNUC__) || defined(__clang__)
  100. #pragma GCC diagnostic pop
  101. #endif
  102. static const SDLTest_TestCaseReference mainTest1 = {
  103. (SDLTest_TestCaseFp)main_testInitQuitSubSystem, "main_testInitQuitSubSystem", "Tests SDL_InitSubSystem/QuitSubSystem", TEST_ENABLED
  104. };
  105. static const SDLTest_TestCaseReference mainTest2 = {
  106. (SDLTest_TestCaseFp)main_testImpliedJoystickInit, "main_testImpliedJoystickInit", "Tests that init for gamecontroller properly implies joystick", TEST_ENABLED
  107. };
  108. static const SDLTest_TestCaseReference mainTest3 = {
  109. (SDLTest_TestCaseFp)main_testImpliedJoystickQuit, "main_testImpliedJoystickQuit", "Tests that quit for gamecontroller doesn't quit joystick if you inited it explicitly", TEST_ENABLED
  110. };
  111. static const SDLTest_TestCaseReference mainTest4 = {
  112. (SDLTest_TestCaseFp)main_testSetError, "main_testSetError", "Tests that SDL_SetError() handles arbitrarily large strings", TEST_ENABLED
  113. };
  114. /* Sequence of Main test cases */
  115. static const SDLTest_TestCaseReference *mainTests[] = {
  116. &mainTest1,
  117. &mainTest2,
  118. &mainTest3,
  119. &mainTest4,
  120. NULL
  121. };
  122. /* Main test suite (global) */
  123. SDLTest_TestSuiteReference mainTestSuite = {
  124. "Main",
  125. NULL,
  126. mainTests,
  127. NULL
  128. };