test/testautomation_platform.c
author Gabriel Jacobo <gabomdq@gmail.com>
Wed, 21 Aug 2013 09:47:10 -0300
changeset 7678 286c42d7c5ed
parent 7663 53fe1b64eb2d
child 8148 56ba41ac64fe
permissions -rw-r--r--
OCD fixes: Adds a space after /* (glory to regular expressions!)
     1 /**
     2  * Original code: automated SDL platform test written by Edgar Simo "bobbens"
     3  * Extended and updated by aschiffler at ferzkopp dot net
     4  */
     5 
     6 #include <stdio.h>
     7 
     8 #include "SDL.h"
     9 #include "SDL_test.h"
    10 
    11 /* ================= Test Case Implementation ================== */
    12 
    13 /* Helper functions */
    14 
    15 /**
    16  * @brief Compare sizes of types.
    17  *
    18  * @note Watcom C flags these as Warning 201: "Unreachable code" if you just
    19  *  compare them directly, so we push it through a function to keep the
    20  *  compiler quiet.  --ryan.
    21  */
    22 static int _compareSizeOfType( size_t sizeoftype, size_t hardcodetype )
    23 {
    24     return sizeoftype != hardcodetype;
    25 }
    26 
    27 /* Test case functions */
    28 
    29 /**
    30  * @brief Tests type sizes.
    31  */
    32 int platform_testTypes(void *arg)
    33 {
    34    int ret;
    35 
    36    ret = _compareSizeOfType( sizeof(Uint8), 1 );
    37    SDLTest_AssertCheck( ret == 0, "sizeof(Uint8) = %lu, expected  1", sizeof(Uint8) );
    38 
    39    ret = _compareSizeOfType( sizeof(Uint16), 2 );
    40    SDLTest_AssertCheck( ret == 0, "sizeof(Uint16) = %lu, expected 2", sizeof(Uint16) );
    41 
    42    ret = _compareSizeOfType( sizeof(Uint32), 4 );
    43    SDLTest_AssertCheck( ret == 0, "sizeof(Uint32) = %lu, expected 4", sizeof(Uint32) );
    44 
    45    ret = _compareSizeOfType( sizeof(Uint64), 8 );
    46    SDLTest_AssertCheck( ret == 0, "sizeof(Uint64) = %lu, expected 8", sizeof(Uint64) );
    47 
    48    return TEST_COMPLETED;
    49 }
    50 
    51 /**
    52  * @brief Tests platform endianness and SDL_SwapXY functions.
    53  */
    54 int platform_testEndianessAndSwap(void *arg)
    55 {
    56     int real_byteorder;
    57     Uint16 value = 0x1234;
    58     Uint16 value16 = 0xCDAB;
    59     Uint16 swapped16 = 0xABCD;
    60     Uint32 value32 = 0xEFBEADDE;
    61     Uint32 swapped32 = 0xDEADBEEF;
    62 
    63     Uint64 value64, swapped64;
    64     value64 = 0xEFBEADDE;
    65     value64 <<= 32;
    66     value64 |= 0xCDAB3412;
    67     swapped64 = 0x1234ABCD;
    68     swapped64 <<= 32;
    69     swapped64 |= 0xDEADBEEF;
    70 
    71     if ((*((char *) &value) >> 4) == 0x1) {
    72         real_byteorder = SDL_BIG_ENDIAN;
    73     } else {
    74         real_byteorder = SDL_LIL_ENDIAN;
    75     }
    76 
    77     /* Test endianness. */
    78     SDLTest_AssertCheck( real_byteorder == SDL_BYTEORDER,
    79              "Machine detected as %s endian, appears to be %s endian.",
    80              (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big",
    81              (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big" );
    82 
    83     /* Test 16 swap. */
    84     SDLTest_AssertCheck( SDL_Swap16(value16) == swapped16,
    85              "SDL_Swap16(): 16 bit swapped: 0x%X => 0x%X",
    86              value16, SDL_Swap16(value16) );
    87 
    88     /* Test 32 swap. */
    89     SDLTest_AssertCheck( SDL_Swap32(value32) == swapped32,
    90              "SDL_Swap32(): 32 bit swapped: 0x%X => 0x%X",
    91              value32, SDL_Swap32(value32) );
    92 
    93     /* Test 64 swap. */
    94     SDLTest_AssertCheck( SDL_Swap64(value64) == swapped64,
    95 #ifdef _MSC_VER
    96              "SDL_Swap64(): 64 bit swapped: 0x%I64X => 0x%I64X",
    97 #else
    98              "SDL_Swap64(): 64 bit swapped: 0x%llX => 0x%llX",
    99 #endif
   100              value64, SDL_Swap64(value64) );
   101 
   102    return TEST_COMPLETED;
   103 }
   104 
   105 /* !
   106  * \brief Tests SDL_GetXYZ() functions
   107  * \sa
   108  * http://wiki.libsdl.org/moin.cgi/SDL_GetPlatform
   109  * http://wiki.libsdl.org/moin.cgi/SDL_GetCPUCount
   110  * http://wiki.libsdl.org/moin.cgi/SDL_GetCPUCacheLineSize
   111  * http://wiki.libsdl.org/moin.cgi/SDL_GetRevision
   112  * http://wiki.libsdl.org/moin.cgi/SDL_GetRevisionNumber
   113  */
   114 int platform_testGetFunctions (void *arg)
   115 {
   116    char *platform;
   117    char *revision;
   118    int ret;
   119    int len;
   120 
   121    platform = (char *)SDL_GetPlatform();
   122    SDLTest_AssertPass("SDL_GetPlatform()");
   123    SDLTest_AssertCheck(platform != NULL, "SDL_GetPlatform() != NULL");
   124    if (platform != NULL) {
   125      len = SDL_strlen(platform);
   126      SDLTest_AssertCheck(len > 0,
   127              "SDL_GetPlatform(): expected non-empty platform, was platform: '%s', len: %i",
   128              platform,
   129              len);
   130    }
   131 
   132    ret = SDL_GetCPUCount();
   133    SDLTest_AssertPass("SDL_GetCPUCount()");
   134    SDLTest_AssertCheck(ret > 0,
   135              "SDL_GetCPUCount(): expected count > 0, was: %i",
   136              ret);
   137 
   138    ret = SDL_GetCPUCacheLineSize();
   139    SDLTest_AssertPass("SDL_GetCPUCacheLineSize()");
   140    SDLTest_AssertCheck(ret >= 0,
   141              "SDL_GetCPUCacheLineSize(): expected size >= 0, was: %i",
   142              ret);
   143 
   144    revision = (char *)SDL_GetRevision();
   145    SDLTest_AssertPass("SDL_GetRevision()");
   146    SDLTest_AssertCheck(revision != NULL, "SDL_GetRevision() != NULL");
   147 
   148    ret = SDL_GetRevisionNumber();
   149    SDLTest_AssertPass("SDL_GetRevisionNumber()");
   150 
   151    return TEST_COMPLETED;
   152 }
   153 
   154 /* !
   155  * \brief Tests SDL_HasXYZ() functions
   156  * \sa
   157  * http://wiki.libsdl.org/moin.cgi/SDL_Has3DNow
   158  * http://wiki.libsdl.org/moin.cgi/SDL_HasAltiVec
   159  * http://wiki.libsdl.org/moin.cgi/SDL_HasMMX
   160  * http://wiki.libsdl.org/moin.cgi/SDL_HasRDTSC
   161  * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE
   162  * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE2
   163  * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE3
   164  * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE41
   165  * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE42
   166  */
   167 int platform_testHasFunctions (void *arg)
   168 {
   169    int ret;
   170 
   171    /* TODO: independently determine and compare values as well */
   172 
   173    ret = SDL_HasRDTSC();
   174    SDLTest_AssertPass("SDL_HasRDTSC()");
   175 
   176    ret = SDL_HasAltiVec();
   177    SDLTest_AssertPass("SDL_HasAltiVec()");
   178 
   179    ret = SDL_HasMMX();
   180    SDLTest_AssertPass("SDL_HasMMX()");
   181 
   182    ret = SDL_Has3DNow();
   183    SDLTest_AssertPass("SDL_Has3DNow()");
   184 
   185    ret = SDL_HasSSE();
   186    SDLTest_AssertPass("SDL_HasSSE()");
   187 
   188    ret = SDL_HasSSE2();
   189    SDLTest_AssertPass("SDL_HasSSE2()");
   190 
   191    ret = SDL_HasSSE3();
   192    SDLTest_AssertPass("SDL_HasSSE3()");
   193 
   194    ret = SDL_HasSSE41();
   195    SDLTest_AssertPass("SDL_HasSSE41()");
   196 
   197    ret = SDL_HasSSE42();
   198    SDLTest_AssertPass("SDL_HasSSE42()");
   199 
   200    return TEST_COMPLETED;
   201 }
   202 
   203 /* !
   204  * \brief Tests SDL_GetVersion
   205  * \sa
   206  * http://wiki.libsdl.org/moin.cgi/SDL_GetVersion
   207  */
   208 int platform_testGetVersion(void *arg)
   209 {
   210    SDL_version linked;
   211    int major = SDL_MAJOR_VERSION;
   212    int minor = SDL_MINOR_VERSION;
   213 
   214    SDL_GetVersion(&linked);
   215    SDLTest_AssertCheck( linked.major >= major,
   216              "SDL_GetVersion(): returned major %i (>= %i)",
   217              linked.major,
   218              major);
   219    SDLTest_AssertCheck( linked.minor >= minor,
   220              "SDL_GetVersion(): returned minor %i (>= %i)",
   221              linked.minor,
   222              minor);
   223 
   224    return TEST_COMPLETED;
   225 }
   226 
   227 
   228 /* !
   229  * \brief Tests SDL_VERSION macro
   230  */
   231 int platform_testSDLVersion(void *arg)
   232 {
   233    SDL_version compiled;
   234    int major = SDL_MAJOR_VERSION;
   235    int minor = SDL_MINOR_VERSION;
   236 
   237    SDL_VERSION(&compiled);
   238    SDLTest_AssertCheck( compiled.major >= major,
   239              "SDL_VERSION() returned major %i (>= %i)",
   240              compiled.major,
   241              major);
   242    SDLTest_AssertCheck( compiled.minor >= minor,
   243              "SDL_VERSION() returned minor %i (>= %i)",
   244              compiled.minor,
   245              minor);
   246 
   247    return TEST_COMPLETED;
   248 }
   249 
   250 
   251 /* !
   252  * \brief Tests default SDL_Init
   253  */
   254 int platform_testDefaultInit(void *arg)
   255 {
   256    int ret;
   257    int subsystem;
   258 
   259    subsystem = SDL_WasInit(SDL_INIT_EVERYTHING);
   260    SDLTest_AssertCheck( subsystem != 0,
   261              "SDL_WasInit(0): returned %i, expected != 0",
   262              subsystem);
   263 
   264    ret = SDL_Init(SDL_WasInit(SDL_INIT_EVERYTHING));
   265    SDLTest_AssertCheck( ret == 0,
   266              "SDL_Init(0): returned %i, expected 0, error: %s",
   267              ret,
   268              SDL_GetError());
   269 
   270    return TEST_COMPLETED;
   271 }
   272 
   273 /* !
   274  * \brief Tests SDL_Get/Set/ClearError
   275  * \sa
   276  * http://wiki.libsdl.org/moin.cgi/SDL_GetError
   277  * http://wiki.libsdl.org/moin.cgi/SDL_SetError
   278  * http://wiki.libsdl.org/moin.cgi/SDL_ClearError
   279  */
   280 int platform_testGetSetClearError(void *arg)
   281 {
   282    const char *testError = "Testing";
   283    char *lastError;
   284    int len;
   285 
   286    SDL_ClearError();
   287    SDLTest_AssertPass("SDL_ClearError()");
   288 
   289    lastError = (char *)SDL_GetError();
   290    SDLTest_AssertPass("SDL_GetError()");
   291    SDLTest_AssertCheck(lastError != NULL,
   292              "SDL_GetError() != NULL");
   293    if (lastError != NULL)
   294    {
   295      len = SDL_strlen(lastError);
   296      SDLTest_AssertCheck(len == 0,
   297              "SDL_GetError(): no message expected, len: %i", len);
   298    }
   299 
   300    SDL_SetError("%s", testError);
   301    SDLTest_AssertPass("SDL_SetError()");
   302    lastError = (char *)SDL_GetError();
   303    SDLTest_AssertCheck(lastError != NULL,
   304              "SDL_GetError() != NULL");
   305    if (lastError != NULL)
   306    {
   307      len = SDL_strlen(lastError);
   308      SDLTest_AssertCheck(len == SDL_strlen(testError),
   309              "SDL_GetError(): expected message len %i, was len: %i",
   310              SDL_strlen(testError),
   311              len);
   312      SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
   313              "SDL_GetError(): expected message %s, was message: %s",
   314              testError,
   315              lastError);
   316    }
   317 
   318    /* Clean up */
   319    SDL_ClearError();
   320    SDLTest_AssertPass("SDL_ClearError()");
   321 
   322    return TEST_COMPLETED;
   323 }
   324 
   325 /* !
   326  * \brief Tests SDL_SetError with empty input
   327  * \sa
   328  * http://wiki.libsdl.org/moin.cgi/SDL_SetError
   329  */
   330 int platform_testSetErrorEmptyInput(void *arg)
   331 {
   332    const char *testError = "";
   333    char *lastError;
   334    int len;
   335 
   336    SDL_SetError("%s", testError);
   337    SDLTest_AssertPass("SDL_SetError()");
   338    lastError = (char *)SDL_GetError();
   339    SDLTest_AssertCheck(lastError != NULL,
   340              "SDL_GetError() != NULL");
   341    if (lastError != NULL)
   342    {
   343      len = SDL_strlen(lastError);
   344      SDLTest_AssertCheck(len == SDL_strlen(testError),
   345              "SDL_GetError(): expected message len %i, was len: %i",
   346              SDL_strlen(testError),
   347              len);
   348      SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
   349              "SDL_GetError(): expected message '%s', was message: '%s'",
   350              testError,
   351              lastError);
   352    }
   353 
   354    /* Clean up */
   355    SDL_ClearError();
   356    SDLTest_AssertPass("SDL_ClearError()");
   357 
   358    return TEST_COMPLETED;
   359 }
   360 
   361 /* !
   362  * \brief Tests SDL_SetError with invalid input
   363  * \sa
   364  * http://wiki.libsdl.org/moin.cgi/SDL_SetError
   365  */
   366 int platform_testSetErrorInvalidInput(void *arg)
   367 {
   368    const char *testError = NULL;
   369    const char *probeError = "Testing";
   370    char *lastError;
   371    int len;
   372 
   373    /* Reset */
   374    SDL_ClearError();
   375    SDLTest_AssertPass("SDL_ClearError()");
   376 
   377    /* Check for no-op */
   378    SDL_SetError(testError);
   379    SDLTest_AssertPass("SDL_SetError()");
   380    lastError = (char *)SDL_GetError();
   381    SDLTest_AssertCheck(lastError != NULL,
   382              "SDL_GetError() != NULL");
   383    if (lastError != NULL)
   384    {
   385      len = SDL_strlen(lastError);
   386      SDLTest_AssertCheck(len == 0,
   387              "SDL_GetError(): expected message len 0, was len: %i",
   388              0,
   389              len);
   390      SDLTest_AssertCheck(SDL_strcmp(lastError, "") == 0,
   391              "SDL_GetError(): expected message '', was message: '%s'",
   392              lastError);
   393    }
   394 
   395    /* Set */
   396    SDL_SetError(probeError);
   397    SDLTest_AssertPass("SDL_SetError()");
   398 
   399    /* Check for no-op */
   400    SDL_SetError(testError);
   401    SDLTest_AssertPass("SDL_SetError()");
   402    lastError = (char *)SDL_GetError();
   403    SDLTest_AssertCheck(lastError != NULL,
   404              "SDL_GetError() != NULL");
   405    if (lastError != NULL)
   406    {
   407      len = SDL_strlen(lastError);
   408      SDLTest_AssertCheck(len == SDL_strlen(probeError),
   409              "SDL_GetError(): expected message len %i, was len: %i",
   410              SDL_strlen(probeError),
   411              len);
   412      SDLTest_AssertCheck(SDL_strcmp(lastError, probeError) == 0,
   413              "SDL_GetError(): expected message '%s', was message: '%s'",
   414              probeError,
   415              lastError);
   416    }
   417 
   418    /* Clean up */
   419    SDL_ClearError();
   420    SDLTest_AssertPass("SDL_ClearError()");
   421 
   422    return TEST_COMPLETED;
   423 }
   424 
   425 /* !
   426  * \brief Tests SDL_GetPowerInfo
   427  * \sa
   428  * http://wiki.libsdl.org/moin.cgi/SDL_GetPowerInfo
   429  */
   430 int platform_testGetPowerInfo(void *arg)
   431 {
   432    SDL_PowerState state;
   433    SDL_PowerState stateAgain;
   434    int secs;
   435    int secsAgain;
   436    int pct;
   437    int pctAgain;
   438 
   439    state = SDL_GetPowerInfo(&secs, &pct);
   440    SDLTest_AssertPass("SDL_GetPowerInfo()");
   441    SDLTest_AssertCheck(
   442        state==SDL_POWERSTATE_UNKNOWN ||
   443        state==SDL_POWERSTATE_ON_BATTERY ||
   444        state==SDL_POWERSTATE_NO_BATTERY ||
   445        state==SDL_POWERSTATE_CHARGING ||
   446        state==SDL_POWERSTATE_CHARGED,
   447        "SDL_GetPowerInfo(): state %i is one of the expected values",
   448        (int)state);
   449 
   450    if (state==SDL_POWERSTATE_ON_BATTERY)
   451    {
   452       SDLTest_AssertCheck(
   453          secs >= 0,
   454          "SDL_GetPowerInfo(): on battery, secs >= 0, was: %i",
   455          secs);
   456       SDLTest_AssertCheck(
   457          (pct >= 0) && (pct <= 100),
   458          "SDL_GetPowerInfo(): on battery, pct=[0,100], was: %i",
   459          pct);
   460    }
   461 
   462    if (state==SDL_POWERSTATE_UNKNOWN ||
   463        state==SDL_POWERSTATE_NO_BATTERY)
   464    {
   465       SDLTest_AssertCheck(
   466          secs == -1,
   467          "SDL_GetPowerInfo(): no battery, secs == -1, was: %i",
   468          secs);
   469       SDLTest_AssertCheck(
   470          pct == -1,
   471          "SDL_GetPowerInfo(): no battery, pct == -1, was: %i",
   472          pct);
   473    }
   474 
   475    /* Partial return value variations */
   476    stateAgain = SDL_GetPowerInfo(&secsAgain, NULL);
   477    SDLTest_AssertCheck(
   478         state==stateAgain,
   479         "State %i returned when only 'secs' requested",
   480         stateAgain);
   481    SDLTest_AssertCheck(
   482         secs==secsAgain,
   483         "Value %i matches when only 'secs' requested",
   484         secsAgain);
   485    stateAgain = SDL_GetPowerInfo(NULL, &pctAgain);
   486    SDLTest_AssertCheck(
   487         state==stateAgain,
   488         "State %i returned when only 'pct' requested",
   489         stateAgain);
   490    SDLTest_AssertCheck(
   491         pct==pctAgain,
   492         "Value %i matches when only 'pct' requested",
   493         pctAgain);
   494    stateAgain = SDL_GetPowerInfo(NULL, NULL);
   495    SDLTest_AssertCheck(
   496         state==stateAgain,
   497         "State %i returned when no value requested",
   498         stateAgain);
   499 
   500    return TEST_COMPLETED;
   501 }
   502 
   503 /* ================= Test References ================== */
   504 
   505 /* Platform test cases */
   506 static const SDLTest_TestCaseReference platformTest1 =
   507         { (SDLTest_TestCaseFp)platform_testTypes, "platform_testTypes", "Tests predefined types", TEST_ENABLED};
   508 
   509 static const SDLTest_TestCaseReference platformTest2 =
   510         { (SDLTest_TestCaseFp)platform_testEndianessAndSwap, "platform_testEndianessAndSwap", "Tests endianess and swap functions", TEST_ENABLED};
   511 
   512 static const SDLTest_TestCaseReference platformTest3 =
   513         { (SDLTest_TestCaseFp)platform_testGetFunctions, "platform_testGetFunctions", "Tests various SDL_GetXYZ functions", TEST_ENABLED};
   514 
   515 static const SDLTest_TestCaseReference platformTest4 =
   516         { (SDLTest_TestCaseFp)platform_testHasFunctions, "platform_testHasFunctions", "Tests various SDL_HasXYZ functions", TEST_ENABLED};
   517 
   518 static const SDLTest_TestCaseReference platformTest5 =
   519         { (SDLTest_TestCaseFp)platform_testGetVersion, "platform_testGetVersion", "Tests SDL_GetVersion function", TEST_ENABLED};
   520 
   521 static const SDLTest_TestCaseReference platformTest6 =
   522         { (SDLTest_TestCaseFp)platform_testSDLVersion, "platform_testSDLVersion", "Tests SDL_VERSION macro", TEST_ENABLED};
   523 
   524 static const SDLTest_TestCaseReference platformTest7 =
   525         { (SDLTest_TestCaseFp)platform_testDefaultInit, "platform_testDefaultInit", "Tests default SDL_Init", TEST_ENABLED};
   526 
   527 static const SDLTest_TestCaseReference platformTest8 =
   528         { (SDLTest_TestCaseFp)platform_testGetSetClearError, "platform_testGetSetClearError", "Tests SDL_Get/Set/ClearError", TEST_ENABLED};
   529 
   530 static const SDLTest_TestCaseReference platformTest9 =
   531         { (SDLTest_TestCaseFp)platform_testSetErrorEmptyInput, "platform_testSetErrorEmptyInput", "Tests SDL_SetError with empty input", TEST_ENABLED};
   532 
   533 static const SDLTest_TestCaseReference platformTest10 =
   534         { (SDLTest_TestCaseFp)platform_testSetErrorInvalidInput, "platform_testSetErrorInvalidInput", "Tests SDL_SetError with invalid input", TEST_ENABLED};
   535 
   536 static const SDLTest_TestCaseReference platformTest11 =
   537         { (SDLTest_TestCaseFp)platform_testGetPowerInfo, "platform_testGetPowerInfo", "Tests SDL_GetPowerInfo function", TEST_ENABLED };
   538 
   539 /* Sequence of Platform test cases */
   540 static const SDLTest_TestCaseReference *platformTests[] =  {
   541     &platformTest1,
   542     &platformTest2,
   543     &platformTest3,
   544     &platformTest4,
   545     &platformTest5,
   546     &platformTest6,
   547     &platformTest7,
   548     &platformTest8,
   549     &platformTest9,
   550     &platformTest10,
   551     &platformTest11,
   552     NULL
   553 };
   554 
   555 /* Platform test suite (global) */
   556 SDLTest_TestSuiteReference platformTestSuite = {
   557     "Platform",
   558     NULL,
   559     platformTests,
   560     NULL
   561 };