test/test-automation/testplatform/testplatform.c
author Andreas Schiffler
Sun, 17 Jul 2011 20:59:34 -0700
changeset 5766 8cc2ae74cd83
parent 5763 7e74c43ecda2
child 5774 4f9f82aa1a33
permissions -rw-r--r--
Fixed SDL_SetError() by making NULL fmt a no-op, update test automation
     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/SDL.h>
     9 
    10 #include "../SDL_test.h"
    11 
    12 /* Test cases */
    13 static const TestCaseReference test1 =
    14 		(TestCaseReference){ "platform_testTypes", "Tests predefined types", TEST_ENABLED, 0, 0 };
    15 
    16 static const TestCaseReference test2 =
    17 		(TestCaseReference){ "platform_testEndianessAndSwap", "Tests endianess and swap functions", TEST_ENABLED, 0, 0 };
    18 
    19 static const TestCaseReference test3 =
    20 		(TestCaseReference){ "platform_testGetFunctions", "Tests various SDL_GetXYZ functions", TEST_ENABLED, 0, 0 };
    21 
    22 static const TestCaseReference test4 =
    23 		(TestCaseReference){ "platform_testHasFunctions", "Tests various SDL_HasXYZ functions", TEST_ENABLED, 0, 0 };
    24 
    25 static const TestCaseReference test5 =
    26 		(TestCaseReference){ "platform_testGetVersion", "Tests SDL_GetVersion function", TEST_ENABLED, 0, 0 };
    27 
    28 static const TestCaseReference test6 =
    29 		(TestCaseReference){ "platform_testSDLVersion", "Tests SDL_VERSION macro", TEST_ENABLED, 0, 0 };
    30 
    31 static const TestCaseReference test7 =
    32 		(TestCaseReference){ "platform_testDefaultInit", "Tests default SDL_Init", TEST_ENABLED, 0, 0 };
    33 
    34 static const TestCaseReference test8 =
    35 		(TestCaseReference){ "platform_testGetSetClearError", "Tests SDL_Get/Set/ClearError", TEST_ENABLED, 0, 0 };
    36 
    37 static const TestCaseReference test9 =
    38 		(TestCaseReference){ "platform_testSetErrorEmptyInput", "Tests SDL_SetError with empty input", TEST_ENABLED, 0, 0 };
    39 
    40 static const TestCaseReference test10 =
    41 		(TestCaseReference){ "platform_testSetErrorInvalidInput", "Tests SDL_SetError with invalid input", TEST_ENABLED, 0, 0 };
    42 
    43 /* Test suite */
    44 extern const TestCaseReference *testSuite[] =  {
    45 	&test1, &test2, &test3, &test4, &test5, &test6, &test7, &test8, &test9, &test10, NULL
    46 };
    47 
    48 TestCaseReference **QueryTestSuite() {
    49 	return (TestCaseReference **)testSuite;
    50 }
    51 
    52 /**
    53  * @brief Compare sizes of types.
    54  *
    55  * @note Watcom C flags these as Warning 201: "Unreachable code" if you just
    56  *  compare them directly, so we push it through a function to keep the
    57  *  compiler quiet.  --ryan.
    58  */
    59 static int _compareSizeOfType( size_t sizeoftype, size_t hardcodetype )
    60 {
    61     return sizeoftype != hardcodetype;
    62 }
    63 
    64 /**
    65  * @brief Tests type sizes.
    66  */
    67 int platform_testTypes(void *arg)
    68 {
    69    int ret;
    70 
    71    ret = _compareSizeOfType( sizeof(Uint8), 1 );
    72    AssertTrue( ret == 0, "sizeof(Uint8) = %lu, expected  1", sizeof(Uint8) );
    73 
    74    ret = _compareSizeOfType( sizeof(Uint16), 2 );
    75    AssertTrue( ret == 0, "sizeof(Uint16) = %lu, expected 2", sizeof(Uint16) );
    76 
    77    ret = _compareSizeOfType( sizeof(Uint32), 4 );
    78    AssertTrue( ret == 0, "sizeof(Uint32) = %lu, expected 4", sizeof(Uint32) );
    79 
    80    ret = _compareSizeOfType( sizeof(Uint64), 8 );
    81    AssertTrue( ret == 0, "sizeof(Uint64) = %lu, expected 8", sizeof(Uint64) );
    82 }
    83 
    84 /**
    85  * @brief Tests platform endianness and SDL_SwapXY functions.
    86  */
    87 int platform_testEndianessAndSwap(void *arg)
    88 {
    89     int real_byteorder;
    90     Uint16 value = 0x1234;
    91     Uint16 value16 = 0xCDAB;
    92     Uint16 swapped16 = 0xABCD;
    93     Uint32 value32 = 0xEFBEADDE;
    94     Uint32 swapped32 = 0xDEADBEEF;
    95 
    96     Uint64 value64, swapped64;
    97     value64 = 0xEFBEADDE;
    98     value64 <<= 32;
    99     value64 |= 0xCDAB3412;
   100     swapped64 = 0x1234ABCD;
   101     swapped64 <<= 32;
   102     swapped64 |= 0xDEADBEEF;
   103     
   104     if ((*((char *) &value) >> 4) == 0x1) {
   105         real_byteorder = SDL_BIG_ENDIAN;
   106     } else {
   107         real_byteorder = SDL_LIL_ENDIAN;
   108     }
   109 
   110     /* Test endianness. */    
   111     AssertTrue( real_byteorder == SDL_BYTEORDER,
   112              "Machine detected as %s endian, appears to be %s endian.",
   113              (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big",
   114              (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big" );
   115 
   116     /* Test 16 swap. */
   117     AssertTrue( SDL_Swap16(value16) == swapped16,
   118              "SDL_Swap16(): 16 bit swapped: 0x%X => 0x%X",
   119              value16, SDL_Swap16(value16) );
   120 
   121     /* Test 32 swap. */
   122     AssertTrue( SDL_Swap32(value32) == swapped32,
   123              "SDL_Swap32(): 32 bit swapped: 0x%X => 0x%X",
   124              value32, SDL_Swap32(value32) );
   125 
   126     /* Test 64 swap. */
   127     AssertTrue( SDL_Swap64(value64) == swapped64,
   128 #ifdef _MSC_VER
   129              "SDL_Swap64(): 64 bit swapped: 0x%I64X => 0x%I64X",
   130 #else
   131              "SDL_Swap64(): 64 bit swapped: 0x%llX => 0x%llX",
   132 #endif
   133              value64, SDL_Swap64(value64) );
   134 }
   135 
   136 /*!
   137  * \brief Tests SDL_GetXYZ() functions
   138  * \sa
   139  * http://wiki.libsdl.org/moin.cgi/SDL_GetPlatform
   140  */
   141 int platform_testGetFunctions (void *arg)
   142 {
   143    char *platform;
   144    char *revision;
   145    int ret;
   146    int len;
   147  
   148    platform = (char *)SDL_GetPlatform();
   149    AssertPass("SDL_GetPlatform()");
   150    AssertTrue(platform != NULL, "SDL_GetPlatform() != NULL");
   151    if (platform != NULL) {
   152      len = strlen(platform);
   153      AssertTrue(len > 0, 
   154              "SDL_GetPlatform(): expected non-empty platform, was platform: '%s', len: %i", 
   155              platform, 
   156              len);
   157    }
   158     
   159    ret = SDL_GetCPUCount();
   160    AssertPass("SDL_GetCPUCount()");
   161 
   162    revision = (char *)SDL_GetRevision();
   163    AssertPass("SDL_GetRevision()");
   164    AssertTrue(revision != NULL, "SDL_GetRevision() != NULL");
   165 
   166    ret = SDL_GetRevisionNumber();
   167    AssertPass("SDL_GetRevisionNumber()");
   168 }
   169 
   170 /*!
   171  * \brief Tests SDL_HasXYZ() functions
   172  */
   173 int platform_testHasFunctions (void *arg)
   174 {
   175    int ret;
   176    
   177    // TODO: independently determine and compare values as well
   178    
   179    ret = SDL_HasRDTSC();
   180    AssertPass("SDL_HasRDTSC()");
   181    
   182    ret = SDL_HasAltiVec();
   183    AssertPass("SDL_HasAltiVec()");
   184 
   185    ret = SDL_HasMMX();
   186    AssertPass("SDL_HasMMX()");
   187 
   188    ret = SDL_Has3DNow();
   189    AssertPass("SDL_Has3DNow()");
   190 
   191    ret = SDL_HasSSE();
   192    AssertPass("SDL_HasSSE()");
   193 
   194    ret = SDL_HasSSE2();
   195    AssertPass("SDL_HasSSE2()");
   196 
   197    ret = SDL_HasSSE3();
   198    AssertPass("SDL_HasSSE3()");
   199 
   200    ret = SDL_HasSSE41();
   201    AssertPass("SDL_HasSSE41()");
   202 
   203    ret = SDL_HasSSE42();
   204    AssertPass("SDL_HasSSE42()");
   205 }
   206 
   207 /*!
   208  * \brief Tests SDL_GetVersion
   209  */
   210 int platform_testGetVersion(void *arg)
   211 {
   212    SDL_version linked;
   213    
   214    SDL_GetVersion(&linked);
   215    AssertTrue( linked.major >= SDL_MAJOR_VERSION,
   216              "SDL_GetVersion(): returned major %i (>= %i)",
   217              linked.major,
   218              SDL_MAJOR_VERSION);   
   219    AssertTrue( linked.minor >= SDL_MINOR_VERSION,
   220              "SDL_GetVersion(): returned minor %i (>= %i)",
   221              linked.minor,
   222              SDL_MINOR_VERSION);   
   223 }
   224 
   225 /*!
   226  * \brief Tests SDL_VERSION macro
   227  */
   228 int platform_testSDLVersion(void *arg)
   229 {
   230    SDL_version compiled;
   231    
   232    SDL_VERSION(&compiled);
   233    AssertTrue( compiled.major >= SDL_MAJOR_VERSION,
   234              "SDL_VERSION() returned major %i (>= %i)",
   235              compiled.major,
   236              SDL_MAJOR_VERSION);   
   237    AssertTrue( compiled.minor >= SDL_MINOR_VERSION,
   238              "SDL_VERSION() returned minor %i (>= %i)",
   239              compiled.minor,
   240              SDL_MINOR_VERSION);   
   241 }
   242 
   243 /*!
   244  * \brief Tests default SDL_Init
   245  */
   246 int platform_testDefaultInit(void *arg)
   247 {
   248    int ret;
   249    int subsystem;
   250    
   251    ret = SDL_Init(0);   
   252    AssertTrue( ret == 0, 
   253              "SDL_Init(0): returned %i, expected 0, error: %s",
   254              ret,
   255              SDL_GetError());
   256              
   257    subsystem = SDL_WasInit(0);
   258    AssertTrue( subsystem == 0, 
   259              "SDL_WasInit(0): returned %i, expected 0",
   260              ret);   
   261              
   262    SDL_Quit();
   263 }
   264 
   265 /*!
   266  * \brief Tests SDL_Get/Set/ClearError
   267  * \sa 
   268  * http://wiki.libsdl.org/moin.cgi/SDL_GetError
   269  * http://wiki.libsdl.org/moin.cgi/SDL_SetError
   270  * http://wiki.libsdl.org/moin.cgi/SDL_ClearError
   271  */
   272 int platform_testGetSetClearError(void *arg)
   273 {
   274    const char *testError = "Testing";
   275    char *lastError;
   276    int len;
   277    
   278    SDL_ClearError();
   279    AssertPass("SDL_ClearError()");
   280    
   281    lastError = (char *)SDL_GetError();   
   282    AssertPass("SDL_GetError()");
   283    AssertTrue(lastError != NULL, 
   284              "SDL_GetError() != NULL");
   285    if (lastError != NULL)
   286    {
   287      len = strlen(lastError);
   288      AssertTrue(len == 0, 
   289              "SDL_GetError(): no message expected, len: %i", len);
   290    }
   291    
   292    SDL_SetError("%s", testError);
   293    AssertPass("SDL_SetError()");
   294    lastError = (char *)SDL_GetError();   
   295    AssertTrue(lastError != NULL, 
   296              "SDL_GetError() != NULL");
   297    if (lastError != NULL)
   298    {
   299      len = strlen(lastError);
   300      AssertTrue(len == strlen(testError), 
   301              "SDL_GetError(): expected message len %i, was len: %i", 
   302              strlen(testError), 
   303              len);
   304      AssertTrue(strcmp(lastError, testError) == 0, 
   305              "SDL_GetError(): expected message %s, was message: %s", 
   306              testError, 
   307              lastError);
   308    }
   309 
   310    // Clean up                
   311    SDL_ClearError();
   312 }
   313 
   314 /*!
   315  * \brief Tests SDL_SetError with empty input
   316  * \sa
   317  * http://wiki.libsdl.org/moin.cgi/SDL_SetError
   318  */
   319 int platform_testSetErrorEmptyInput(void *arg)
   320 {
   321    const char *testError = "";
   322    char *lastError;
   323    int len;
   324    
   325    SDL_SetError("%s", testError);
   326    AssertPass("SDL_SetError()");
   327    lastError = (char *)SDL_GetError();   
   328    AssertTrue(lastError != NULL, 
   329              "SDL_GetError() != NULL");
   330    if (lastError != NULL)
   331    {
   332      len = strlen(lastError);
   333      AssertTrue(len == strlen(testError), 
   334              "SDL_GetError(): expected message len %i, was len: %i", 
   335              strlen(testError), 
   336              len);
   337      AssertTrue(strcmp(lastError, testError) == 0, 
   338              "SDL_GetError(): expected message '%s', was message: '%s'", 
   339              testError, 
   340              lastError);
   341    }
   342 
   343    // Clean up                
   344    SDL_ClearError();
   345 }
   346 
   347 /*!
   348  * \brief Tests SDL_SetError with invalid input
   349  * \sa 
   350  * http://wiki.libsdl.org/moin.cgi/SDL_SetError
   351  */
   352 int platform_testSetErrorInvalidInput(void *arg)
   353 {
   354    const char *testError = NULL;
   355    const char *probeError = "Testing";
   356    char *lastError;
   357    int len;
   358 
   359    // Reset
   360    SDL_ClearError();
   361    
   362    // Check for no-op
   363    SDL_SetError(testError);
   364    AssertPass("SDL_SetError()");
   365    lastError = (char *)SDL_GetError();   
   366    AssertTrue(lastError != NULL, 
   367              "SDL_GetError() != NULL");
   368    if (lastError != NULL)
   369    {
   370      len = strlen(lastError);
   371      AssertTrue(len == 0, 
   372              "SDL_GetError(): expected message len 0, was len: %i", 
   373              0, 
   374              len);
   375      AssertTrue(strcmp(lastError, "") == 0, 
   376              "SDL_GetError(): expected message '', was message: '%s'",           
   377              lastError);
   378    }
   379    
   380    // Set
   381    SDL_SetError(probeError);
   382    
   383    // Check for no-op
   384    SDL_SetError(testError);
   385    AssertPass("SDL_SetError()");
   386    lastError = (char *)SDL_GetError();   
   387    AssertTrue(lastError != NULL, 
   388              "SDL_GetError() != NULL");
   389    if (lastError != NULL)
   390    {
   391      len = strlen(lastError);
   392      AssertTrue(len == strlen(probeError), 
   393              "SDL_GetError(): expected message len %i, was len: %i", 
   394              strlen(probeError), 
   395              len);
   396      AssertTrue(strcmp(lastError, probeError) == 0, 
   397              "SDL_GetError(): expected message '%s', was message: '%s'",
   398              probeError,
   399              lastError);
   400    }
   401 
   402    // Clean up                
   403    SDL_ClearError();
   404 }