test/testautomation_main.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Tue, 12 Feb 2013 11:47:31 -0800
changeset 6866 e74a4b282450
child 6869 baac6ec78a95
permissions -rw-r--r--
Don't clobber refcounting in SDL_Init.

- Fixes bug 1712 by not overwriting SDL_SubsystemRefCount in SDL_Init.
- Removes the SDL_initialized variable, and makes SDL_SubsystemRefCount
the canonical source of truth for whether or not a subsystem has been
initialized.
- Refactors SDL_InitSubSystem and SDL_QuitSubSystem to use helper
functions to manage refcount.
- Adds automated tests for SDL_Init/Quit*.
- Adds SDL_bits.h which contains SDL_MostSignificantBitIndex.
     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 initialized_subsystems = SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC;
    22 
    23     SDLTest_AssertCheck( SDL_Init(initialized_subsystems) == 0, "SDL_Init multiple systems." );
    24 
    25     int enabled_subsystems = SDL_WasInit(initialized_subsystems);
    26     SDLTest_AssertCheck( enabled_subsystems == initialized_subsystems, "SDL_WasInit(SDL_INIT_EVERYTHING) contains all systems (%i)", enabled_subsystems );
    27 
    28     SDL_Quit();
    29 
    30     enabled_subsystems = SDL_WasInit(initialized_subsystems);
    31     SDLTest_AssertCheck( enabled_subsystems == 0, "SDL_Quit should shut down everything (%i)", enabled_subsystems );
    32 
    33     return TEST_COMPLETED;
    34 }
    35 
    36 /*!
    37  * \brief Tests SDL_InitSubSystem() and SDL_QuitSubSystem()
    38  * \sa
    39  * http://wiki.libsdl.org/moin.cgi/SDL_Init
    40  * http://wiki.libsdl.org/moin.cgi/SDL_Quit
    41  */
    42 static int main_testInitQuitSubSystem (void *arg)
    43 {
    44     int i;
    45     int subsystems[] = { SDL_INIT_JOYSTICK, SDL_INIT_HAPTIC, SDL_INIT_GAMECONTROLLER };
    46 
    47     for (i = 0; i < SDL_arraysize(subsystems); ++i) {
    48         int subsystem = subsystems[i];
    49 
    50         SDLTest_AssertCheck( (SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) before init should be false", subsystem );
    51         SDLTest_AssertCheck( SDL_InitSubSystem(subsystem) == 0, "SDL_InitSubSystem(%x)", subsystem );
    52 
    53         int initialized_system = SDL_WasInit(subsystem);
    54         SDLTest_AssertCheck( (initialized_system & subsystem) != 0, "SDL_WasInit(%x) should be true (%x)", subsystem, initialized_system );
    55 
    56         SDL_QuitSubSystem(subsystem);
    57 
    58         SDLTest_AssertCheck( (SDL_WasInit(subsystem) & subsystem) == 0, "SDL_WasInit(%x) after shutdown should be false", subsystem );
    59     }
    60 
    61     return TEST_COMPLETED;
    62 }
    63 
    64 const int joy_and_controller = SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER;
    65 static int main_testImpliedJoystickInit (void *arg)
    66 {
    67     // First initialize the controller
    68     SDLTest_AssertCheck( (SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller" );
    69     SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)" );
    70 
    71     // Then make sure this implicitly initialized the joystick subsystem
    72     int initialized_system = SDL_WasInit(joy_and_controller);
    73     SDLTest_AssertCheck( (initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system );
    74 
    75     // Then quit the controller, and make sure that imlicity also quits the
    76     // joystick subsystem
    77     SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
    78     initialized_system = SDL_WasInit(joy_and_controller);
    79     SDLTest_AssertCheck( (initialized_system & joy_and_controller) == 0, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system );
    80 }
    81 
    82 static int main_testImpliedJoystickQuit (void *arg)
    83 {
    84     // First initialize the controller and the joystick (explicitly)
    85     SDLTest_AssertCheck( (SDL_WasInit(joy_and_controller) & joy_and_controller) == 0, "SDL_WasInit() before init should be false for joystick & controller" );
    86     SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0, "SDL_InitSubSystem(SDL_INIT_JOYSTICK)" );
    87     SDLTest_AssertCheck( SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == 0, "SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)" );
    88 
    89     // Then make sure they're both initialized properly
    90     int initialized_system = SDL_WasInit(joy_and_controller);
    91     SDLTest_AssertCheck( (initialized_system & joy_and_controller) == joy_and_controller, "SDL_WasInit() should be true for joystick & controller (%x)", initialized_system );
    92 
    93     // Then quit the controller, and make sure that it does NOT quit the
    94     // explicitly initialized joystick subsystem.
    95     SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
    96     initialized_system = SDL_WasInit(joy_and_controller);
    97     SDLTest_AssertCheck( (initialized_system & joy_and_controller) == SDL_INIT_JOYSTICK, "SDL_WasInit() should be false for joystick & controller (%x)", initialized_system );
    98 
    99     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
   100 }
   101 
   102 static const SDLTest_TestCaseReference mainTest1 =
   103 		{ (SDLTest_TestCaseFp)main_testInitQuit, "main_testInitQuit", "Tests SDL_Init/Quit", TEST_ENABLED};
   104 
   105 static const SDLTest_TestCaseReference mainTest2 =
   106 		{ (SDLTest_TestCaseFp)main_testInitQuitSubSystem, "main_testInitQuitSubSystem", "Tests SDL_InitSubSystem/QuitSubSystem", TEST_ENABLED};
   107 
   108 static const SDLTest_TestCaseReference mainTest3 =
   109 		{ (SDLTest_TestCaseFp)main_testImpliedJoystickInit, "main_testImpliedJoystickInit", "Tests that init for gamecontroller properly implies joystick", TEST_ENABLED};
   110 
   111 static const SDLTest_TestCaseReference mainTest4 =
   112 		{ (SDLTest_TestCaseFp)main_testImpliedJoystickQuit, "main_testImpliedJoystickQuit", "Tests that quit for gamecontroller doesn't quit joystick if you inited it explicitly", TEST_ENABLED};
   113 
   114 /* Sequence of Platform test cases */
   115 static const SDLTest_TestCaseReference *mainTests[] =  {
   116 	&mainTest1,
   117 	&mainTest2,
   118 	&mainTest3,
   119 	&mainTest4,
   120 	NULL
   121 };
   122 
   123 /* Platform test suite (global) */
   124 SDLTest_TestSuiteReference mainTestSuite = {
   125 	"Main",
   126 	NULL,
   127 	mainTests,
   128 	NULL
   129 };
   130 
   131 /* vi: set ts=4 sw=4 expandtab: */