test/automated/platform/platform.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 16 Dec 2009 06:53:53 +0000
changeset 3579 3427271a2d75
parent 3481 c32c53fca10d
child 5286 73d27f06a715
permissions -rw-r--r--
Added SDL_GetCPUCount() to see how many cores are available.
     1 /**
     2  * Automated SDL platform test.
     3  *
     4  * Based off of testplatform.c.
     5  *
     6  * Written by Edgar Simo "bobbens"
     7  *
     8  * Released under Public Domain.
     9  */
    10 
    11 
    12 
    13 
    14 #include "SDL.h"
    15 #include "SDL_endian.h"
    16 #include "SDL_cpuinfo.h"
    17 #include "../SDL_at.h"
    18 
    19 
    20 /*
    21  * Prototypes.
    22  */
    23 static int plat_testSize( size_t sizeoftype, size_t hardcodetype );
    24 static void plat_testTypes (void);
    25 
    26 
    27 /**
    28  * @brief Test size.
    29  *
    30  * @note Watcom C flags these as Warning 201: "Unreachable code" if you just
    31  *  compare them directly, so we push it through a function to keep the
    32  *  compiler quiet.  --ryan.
    33  */
    34 static int plat_testSize( size_t sizeoftype, size_t hardcodetype )
    35 {
    36     return sizeoftype != hardcodetype;
    37 }
    38 
    39 
    40 /**
    41  * @brief Tests type size.
    42  */
    43 static void plat_testTypes (void)
    44 {
    45    int ret;
    46 
    47    SDL_ATbegin( "Type size" );
    48 
    49    ret = plat_testSize( sizeof(Uint8), 1 );
    50    if (SDL_ATvassert( ret == 0, "sizeof(Uint8) = %lu instead of 1", sizeof(Uint8) ))
    51       return;
    52 
    53    ret = plat_testSize( sizeof(Uint16), 2 );
    54    if (SDL_ATvassert( ret == 0, "sizeof(Uint16) = %lu instead of 2", sizeof(Uint16) ))
    55       return;
    56 
    57    ret = plat_testSize( sizeof(Uint32), 4 );
    58    if (SDL_ATvassert( ret == 0, "sizeof(Uint32) = %lu instead of 4", sizeof(Uint32) ))
    59       return;
    60 
    61 #ifdef SDL_HAS_64BIT_TYPE
    62    ret = plat_testSize( sizeof(Uint64), 8 );
    63    if (SDL_ATvassert( ret == 0, "sizeof(Uint64) = %lu instead of 8", sizeof(Uint64) ))
    64       return;
    65 #endif /* SDL_HAS_64BIT_TYPE */
    66 
    67    SDL_ATend();
    68 }
    69 
    70 
    71 /**
    72  * @brief Tests platform endianness.
    73  */
    74 static void plat_testEndian (void)
    75 {
    76     Uint16 value = 0x1234;
    77     int real_byteorder;
    78     Uint16 value16 = 0xCDAB;
    79     Uint16 swapped16 = 0xABCD;
    80     Uint32 value32 = 0xEFBEADDE;
    81     Uint32 swapped32 = 0xDEADBEEF;
    82 #ifdef SDL_HAS_64BIT_TYPE
    83     Uint64 value64, swapped64;
    84     value64 = 0xEFBEADDE;
    85     value64 <<= 32;
    86     value64 |= 0xCDAB3412;
    87     swapped64 = 0x1234ABCD;
    88     swapped64 <<= 32;
    89     swapped64 |= 0xDEADBEEF;
    90 #endif
    91 
    92     SDL_ATbegin( "Endianness" );
    93 
    94     /* Test endianness. */
    95     if ((*((char *) &value) >> 4) == 0x1) {
    96         real_byteorder = SDL_BIG_ENDIAN;
    97     } else {
    98         real_byteorder = SDL_LIL_ENDIAN;
    99     }
   100     if (SDL_ATvassert( real_byteorder == SDL_BYTEORDER,
   101              "Machine detected as %s endian but appears to be %s endian.",
   102              (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big",
   103              (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big" ))
   104        return;
   105 
   106     /* Test 16 swap. */
   107     if (SDL_ATvassert( SDL_Swap16(value16) == swapped16,
   108              "16 bit swapped incorrectly: 0x%X => 0x%X",
   109              value16, SDL_Swap16(value16) ))
   110        return;
   111 
   112     /* Test 32 swap. */
   113     if (SDL_ATvassert( SDL_Swap32(value32) == swapped32,
   114              "32 bit swapped incorrectly: 0x%X => 0x%X",
   115              value32, SDL_Swap32(value32) ))
   116        return;
   117 
   118 #ifdef SDL_HAS_64BIT_TYPE
   119     /* Test 64 swap. */
   120     if (SDL_ATvassert( SDL_Swap64(value64) == swapped64,
   121 #ifdef _MSC_VER
   122              "64 bit swapped incorrectly: 0x%I64X => 0x%I64X",
   123 #else
   124              "64 bit swapped incorrectly: 0x%llX => 0x%llX",
   125 #endif
   126              value64, SDL_Swap64(value64) ))
   127        return;
   128 #endif
   129 
   130     SDL_ATend();
   131 }
   132 
   133 
   134 /**
   135  * @brief Platform test entrypoint.
   136  */
   137 #ifdef TEST_STANDALONE
   138 int main( int argc, const char *argv[] )
   139 {
   140    (void) argc;
   141    (void) argv;
   142 #else /* TEST_STANDALONE */
   143 int test_platform (void)
   144 {
   145 #endif /* TEST_STANDALONE */
   146 
   147    SDL_ATinit( "Platform" );
   148 
   149    /* Debug information. */
   150    SDL_ATprintVerbose( 1, "%s System detected\n", SDL_GetPlatform() );
   151    SDL_ATprintVerbose( 1, "System is %s endian\n",
   152 #ifdef SDL_LIL_ENDIAN
   153          "little"
   154 #else
   155          "big"
   156 #endif
   157          );
   158    SDL_ATprintVerbose( 1, "CPU count: %d\n", SDL_GetCPUCount());
   159    SDL_ATprintVerbose( 1, "Available extensions:\n" );
   160    SDL_ATprintVerbose( 1, "   RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected" );
   161    SDL_ATprintVerbose( 1, "   MMX %s\n", SDL_HasMMX()? "detected" : "not detected" );
   162    SDL_ATprintVerbose( 1, "   MMX Ext %s\n", SDL_HasMMXExt()? "detected" : "not detected" );
   163    SDL_ATprintVerbose( 1, "   3DNow %s\n", SDL_Has3DNow()? "detected" : "not detected" );
   164    SDL_ATprintVerbose( 1, "   3DNow Ext %s\n",
   165          SDL_Has3DNowExt()? "detected" : "not detected" );
   166    SDL_ATprintVerbose( 1, "   SSE %s\n", SDL_HasSSE()? "detected" : "not detected" );
   167    SDL_ATprintVerbose( 1, "   SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected" );
   168    SDL_ATprintVerbose( 1, "   AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected" );
   169 
   170    plat_testTypes();
   171    plat_testEndian();
   172 
   173    return SDL_ATfinish();
   174 }