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