test/testplatform.c
author Paul Hunkin <paul@bieh.net>
Mon, 17 May 2010 15:14:34 +1200
changeset 4695 b7918263da92
parent 3687 8e5186a99af4
child 5121 87f452305db4
permissions -rw-r--r--
Initial commit, adding __ANDROID__ to the platform list
     1 
     2 #include <stdio.h>
     3 
     4 #include "SDL.h"
     5 #include "SDL_endian.h"
     6 #include "SDL_cpuinfo.h"
     7 #include "SDL_assert.h"
     8 
     9 /*
    10  * Watcom C flags these as Warning 201: "Unreachable code" if you just
    11  *  compare them directly, so we push it through a function to keep the
    12  *  compiler quiet.  --ryan.
    13  */
    14 static int
    15 badsize(size_t sizeoftype, size_t hardcodetype)
    16 {
    17     return sizeoftype != hardcodetype;
    18 }
    19 
    20 int
    21 TestTypes(SDL_bool verbose)
    22 {
    23     int error = 0;
    24 
    25     if (badsize(sizeof(Uint8), 1)) {
    26         if (verbose)
    27             printf("sizeof(Uint8) != 1, instead = %u\n",
    28                    (unsigned int)sizeof(Uint8));
    29         ++error;
    30     }
    31     if (badsize(sizeof(Uint16), 2)) {
    32         if (verbose)
    33             printf("sizeof(Uint16) != 2, instead = %u\n",
    34                    (unsigned int)sizeof(Uint16));
    35         ++error;
    36     }
    37     if (badsize(sizeof(Uint32), 4)) {
    38         if (verbose)
    39             printf("sizeof(Uint32) != 4, instead = %u\n",
    40                    (unsigned int)sizeof(Uint32));
    41         ++error;
    42     }
    43 #ifdef SDL_HAS_64BIT_TYPE
    44     if (badsize(sizeof(Uint64), 8)) {
    45         if (verbose)
    46             printf("sizeof(Uint64) != 8, instead = %u\n",
    47                    (unsigned int)sizeof(Uint64));
    48         ++error;
    49     }
    50 #else
    51     if (verbose) {
    52         printf("WARNING: No 64-bit datatype on this platform\n");
    53     }
    54 #endif
    55     if (verbose && !error)
    56         printf("All data types are the expected size.\n");
    57 
    58     return (error ? 1 : 0);
    59 }
    60 
    61 int
    62 TestEndian(SDL_bool verbose)
    63 {
    64     int error = 0;
    65     Uint16 value = 0x1234;
    66     int real_byteorder;
    67     Uint16 value16 = 0xCDAB;
    68     Uint16 swapped16 = 0xABCD;
    69     Uint32 value32 = 0xEFBEADDE;
    70     Uint32 swapped32 = 0xDEADBEEF;
    71 #ifdef SDL_HAS_64BIT_TYPE
    72     Uint64 value64, swapped64;
    73     value64 = 0xEFBEADDE;
    74     value64 <<= 32;
    75     value64 |= 0xCDAB3412;
    76     swapped64 = 0x1234ABCD;
    77     swapped64 <<= 32;
    78     swapped64 |= 0xDEADBEEF;
    79 #endif
    80 
    81     if (verbose) {
    82         printf("Detected a %s endian machine.\n",
    83                (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
    84     }
    85     if ((*((char *) &value) >> 4) == 0x1) {
    86         real_byteorder = SDL_BIG_ENDIAN;
    87     } else {
    88         real_byteorder = SDL_LIL_ENDIAN;
    89     }
    90     if (real_byteorder != SDL_BYTEORDER) {
    91         if (verbose) {
    92             printf("Actually a %s endian machine!\n",
    93                    (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
    94         }
    95         ++error;
    96     }
    97     if (verbose) {
    98         printf("Value 16 = 0x%X, swapped = 0x%X\n", value16,
    99                SDL_Swap16(value16));
   100     }
   101     if (SDL_Swap16(value16) != swapped16) {
   102         if (verbose) {
   103             printf("16 bit value swapped incorrectly!\n");
   104         }
   105         ++error;
   106     }
   107     if (verbose) {
   108         printf("Value 32 = 0x%X, swapped = 0x%X\n", value32,
   109                SDL_Swap32(value32));
   110     }
   111     if (SDL_Swap32(value32) != swapped32) {
   112         if (verbose) {
   113             printf("32 bit value swapped incorrectly!\n");
   114         }
   115         ++error;
   116     }
   117 #ifdef SDL_HAS_64BIT_TYPE
   118     if (verbose) {
   119 #ifdef _MSC_VER
   120         printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
   121                SDL_Swap64(value64));
   122 #else
   123         printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64,
   124                SDL_Swap64(value64));
   125 #endif
   126     }
   127     if (SDL_Swap64(value64) != swapped64) {
   128         if (verbose) {
   129             printf("64 bit value swapped incorrectly!\n");
   130         }
   131         ++error;
   132     }
   133 #endif
   134     return (error ? 1 : 0);
   135 }
   136 
   137 
   138 int
   139 TestCPUInfo(SDL_bool verbose)
   140 {
   141     if (verbose) {
   142         printf("CPU count: %d\n", SDL_GetCPUCount());
   143         printf("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
   144         printf("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
   145         printf("MMX Ext %s\n", SDL_HasMMXExt()? "detected" : "not detected");
   146         printf("3DNow %s\n", SDL_Has3DNow()? "detected" : "not detected");
   147         printf("3DNow Ext %s\n",
   148                SDL_Has3DNowExt()? "detected" : "not detected");
   149         printf("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
   150         printf("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
   151         printf("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
   152     }
   153     return (0);
   154 }
   155 
   156 int
   157 TestAssertions(SDL_bool verbose)
   158 {
   159     SDL_assert(1);
   160     SDL_assert_release(1);
   161     SDL_assert_paranoid(1);
   162     SDL_assert(0 || 1);
   163     SDL_assert_release(0 || 1);
   164     SDL_assert_paranoid(0 || 1);
   165 
   166 #if 0   /* enable this to test assertion failures. */
   167     SDL_assert_release(1 == 2);
   168     SDL_assert_release(5 < 4);
   169     SDL_assert_release(0 && "This is a test");
   170 #endif
   171     SDL_assert_release(0 && "This is a test");
   172 
   173     return (0);
   174 }
   175 
   176 int
   177 main(int argc, char *argv[])
   178 {
   179     SDL_bool verbose = SDL_TRUE;
   180     int status = 0;
   181 
   182     if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
   183         verbose = SDL_FALSE;
   184     }
   185     if (verbose) {
   186         printf("This system is running %s\n", SDL_GetPlatform());
   187     }
   188 
   189     status += TestTypes(verbose);
   190     status += TestEndian(verbose);
   191     status += TestCPUInfo(verbose);
   192     status += TestAssertions(verbose);
   193 
   194     return status;
   195 }