test/testautomation_main.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 03 Apr 2013 16:48:23 -0700
changeset 7056 a5699778d80f
parent 6872 2f4c95464651
child 7144 c700de9c8ed4
permissions -rw-r--r--
Improve testgamecontroller output & robustness.
     1 /**
     2  * Automated SDL subsystems management test.
     3  *
     4  * Written by Jørgen Tjernø "jorgenpt"
     5  *
     6  * Released under Public Domain.
     7  */
     8 
     9 #include "SDL.h"
    10 #include "SDL_test.h"
    11 
    12 
    13 /*!
    14  * \brief Tests SDL_Init() and SDL_Quit()
    15  * \sa
    16  * http://wiki.libsdl.org/moin.cgi/SDL_Init
    17  * http://wiki.libsdl.org/moin.cgi/SDL_Quit
    18  */
    19 static int main_testInitQuit (void *arg)
    20 {
    21     int enabled_subsystems;
    22     int initialized_subsystems = SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC;
    23 
    24     SDLTest_AssertCheck( SDL_Init(initialized_subsystems) == 0, "SDL_Init multiple systems." );
    25 
    26     enabled_subsystems = SDL_WasInit(initialized_subsystems);
    27     SDLTest_AssertCheck( enabled_subsystems == initialized_subsystems, "SDL_WasInit(SDL_INIT_EVERYTHING) contains all systems (%i)", enabled_subsystems );
    28 
    29     SDL_Quit();
    30 
    31     enabled_subsystems = SDL_WasInit(initialized_subsystems);
    32     SDLTest_AssertCheck( enabled_subsystems == 0, "SDL_Quit should shut down everything (%i)", enabled_subsystems );
    33 
    34     return TEST_COMPLETED;
    35 }
    36 
    37 /*!
    38  * \brief Tests SDL_InitSubSystem() and SDL_QuitSubSystem()
    39  * \sa
    40  * http://wiki.libsdl.org/moin.cgi/SDL_Init
    41  * http://wiki.libsdl.org/moin.cgi/SDL_Quit
    42  */
    43 static int main_testInitQuitSubSystem (void *arg)
    44 {
    45     int i;
    46     int subsystems[] = { SDL_INIT_JOYSTICK, SDL_INIT_HAPTIC, SDL_INIT_GAMECONTROLLER };
    47 
    48     for (i = 0; i < SDL_arraysize(subsystems); ++i) {
    49         int initialized_system;
    50         int subsystem = subsystems[i];
    51 
    52         SDLTest_AssertCheck( (SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) before init should be false", subsystem );
    53         SDLTest_AssertCheck( SDL_InitSubSystem(subsystem) == 0, "SDL_InitSubSystem(%x)", subsystem );
    54 
    55         initialized_system = SDL_WasInit(subsystem);
    56         SDLTest_AssertCheck( (initialized_system & subsystem) != 0, "SDL_WasInit(%x) should be true (%x)", subsystem, initialized_system );
    57 
    58         SDL_QuitSubSystem(subsystem);
    59 
    60         SDLTest_AssertCheck( (SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) after shutdown should be false", subsystem );
    61     }
    62 
    63     return TEST_COMPLETED;
    64 }
    65 
    66 const int joy_and_controller = SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER;
    67 static int main_testImpliedJoystickInit (void *arg)
    68 {
    69     int initialized_system;
    70 
    71     // First initialize the controller
    72     SDLTest_AssertCheck( (SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller" );
    73     SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)" );
    74 
    75     // Then make sure this implicitly initialized the joystick subsystem
    76     initialized_system = SDL_WasInit(joy_and_controller);
    77     SDLTest_AssertCheck( (initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system );
    78 
    79     // Then quit the controller, and make sure that imlicity also quits the
    80     // joystick subsystem
    81     SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
    82     initialized_system = SDL_WasInit(joy_and_controller);
    83     SDLTest_AssertCheck( (initialized_system & joy_and_controller) == 0, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system );
    84 
    85 	return TEST_COMPLETED;
    86 }
    87 
    88 static int main_testImpliedJoystickQuit (void *arg)
    89 {
    90     int initialized_system;
    91 
    92     // First initialize the controller and the joystick (explicitly)
    93     SDLTest_AssertCheck( (SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller" );
    94     SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0, "SDL_InitSubSystem(SDL_INIT_JOYSTICK)" );
    95     SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)" );
    96 
    97     // Then make sure they're both initialized properly
    98     initialized_system = SDL_WasInit(joy_and_controller);
    99     SDLTest_AssertCheck( (initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system );
   100 
   101     // Then quit the controller, and make sure that it does NOT quit the
   102     // explicitly initialized joystick subsystem.
   103     SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
   104     initialized_system = SDL_WasInit(joy_and_controller);
   105     SDLTest_AssertCheck( (initialized_system & joy_and_controller) == SDL_INIT_JOYSTICK, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system );
   106 
   107     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
   108 
   109 	return TEST_COMPLETED;
   110 }
   111 
   112 static const SDLTest_TestCaseReference mainTest1 =
   113 		{ (SDLTest_TestCaseFp)main_testInitQuit, "main_testInitQuit", "Tests SDL_Init/Quit", TEST_ENABLED};
   114 
   115 static const SDLTest_TestCaseReference mainTest2 =
   116 		{ (SDLTest_TestCaseFp)main_testInitQuitSubSystem, "main_testInitQuitSubSystem", "Tests SDL_InitSubSystem/QuitSubSystem", TEST_ENABLED};
   117 
   118 static const SDLTest_TestCaseReference mainTest3 =
   119 		{ (SDLTest_TestCaseFp)main_testImpliedJoystickInit, "main_testImpliedJoystickInit", "Tests that init for gamecontroller properly implies joystick", TEST_ENABLED};
   120 
   121 static const SDLTest_TestCaseReference mainTest4 =
   122 		{ (SDLTest_TestCaseFp)main_testImpliedJoystickQuit, "main_testImpliedJoystickQuit", "Tests that quit for gamecontroller doesn't quit joystick if you inited it explicitly", TEST_ENABLED};
   123 
   124 /* Sequence of Platform test cases */
   125 static const SDLTest_TestCaseReference *mainTests[] =  {
   126 	&mainTest1,
   127 	&mainTest2,
   128 	&mainTest3,
   129 	&mainTest4,
   130 	NULL
   131 };
   132 
   133 /* Platform test suite (global) */
   134 SDLTest_TestSuiteReference mainTestSuite = {
   135 	"Main",
   136 	NULL,
   137 	mainTests,
   138 	NULL
   139 };
   140 
   141 /* vi: set ts=4 sw=4 expandtab: */