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