test/testplatform.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 25 Mar 2011 13:47:49 -0700
changeset 5512 56ab281d7d3e
parent 5389 24903690f48a
child 5535 96594ac5fd1a
permissions -rw-r--r--
SDL 1.3 requires a 64-bit type for the platform.
     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     if (badsize(sizeof(Uint64), 8)) {
    44         if (verbose)
    45             printf("sizeof(Uint64) != 8, instead = %u\n",
    46                    (unsigned int)sizeof(Uint64));
    47         ++error;
    48     }
    49     if (verbose && !error)
    50         printf("All data types are the expected size.\n");
    51 
    52     return (error ? 1 : 0);
    53 }
    54 
    55 int
    56 TestEndian(SDL_bool verbose)
    57 {
    58     int error = 0;
    59     Uint16 value = 0x1234;
    60     int real_byteorder;
    61     Uint16 value16 = 0xCDAB;
    62     Uint16 swapped16 = 0xABCD;
    63     Uint32 value32 = 0xEFBEADDE;
    64     Uint32 swapped32 = 0xDEADBEEF;
    65     Uint64 value64, swapped64;
    66 
    67     value64 = 0xEFBEADDE;
    68     value64 <<= 32;
    69     value64 |= 0xCDAB3412;
    70     swapped64 = 0x1234ABCD;
    71     swapped64 <<= 32;
    72     swapped64 |= 0xDEADBEEF;
    73 
    74     if (verbose) {
    75         printf("Detected a %s endian machine.\n",
    76                (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
    77     }
    78     if ((*((char *) &value) >> 4) == 0x1) {
    79         real_byteorder = SDL_BIG_ENDIAN;
    80     } else {
    81         real_byteorder = SDL_LIL_ENDIAN;
    82     }
    83     if (real_byteorder != SDL_BYTEORDER) {
    84         if (verbose) {
    85             printf("Actually a %s endian machine!\n",
    86                    (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
    87         }
    88         ++error;
    89     }
    90     if (verbose) {
    91         printf("Value 16 = 0x%X, swapped = 0x%X\n", value16,
    92                SDL_Swap16(value16));
    93     }
    94     if (SDL_Swap16(value16) != swapped16) {
    95         if (verbose) {
    96             printf("16 bit value swapped incorrectly!\n");
    97         }
    98         ++error;
    99     }
   100     if (verbose) {
   101         printf("Value 32 = 0x%X, swapped = 0x%X\n", value32,
   102                SDL_Swap32(value32));
   103     }
   104     if (SDL_Swap32(value32) != swapped32) {
   105         if (verbose) {
   106             printf("32 bit value swapped incorrectly!\n");
   107         }
   108         ++error;
   109     }
   110     if (verbose) {
   111 #ifdef _MSC_VER
   112         printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
   113                SDL_Swap64(value64));
   114 #else
   115         printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64,
   116                SDL_Swap64(value64));
   117 #endif
   118     }
   119     if (SDL_Swap64(value64) != swapped64) {
   120         if (verbose) {
   121             printf("64 bit value swapped incorrectly!\n");
   122         }
   123         ++error;
   124     }
   125     return (error ? 1 : 0);
   126 }
   127 
   128 
   129 int
   130 TestCPUInfo(SDL_bool verbose)
   131 {
   132     if (verbose) {
   133         printf("CPU count: %d\n", SDL_GetCPUCount());
   134         printf("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize());
   135         printf("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
   136         printf("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
   137         printf("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
   138         printf("3DNow! %s\n", SDL_Has3DNow()? "detected" : "not detected");
   139         printf("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
   140         printf("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
   141         printf("SSE3 %s\n", SDL_HasSSE3()? "detected" : "not detected");
   142         printf("SSE4.1 %s\n", SDL_HasSSE41()? "detected" : "not detected");
   143         printf("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
   144     }
   145     return (0);
   146 }
   147 
   148 int
   149 TestAssertions(SDL_bool verbose)
   150 {
   151     SDL_assert(1);
   152     SDL_assert_release(1);
   153     SDL_assert_paranoid(1);
   154     SDL_assert(0 || 1);
   155     SDL_assert_release(0 || 1);
   156     SDL_assert_paranoid(0 || 1);
   157 
   158 #if 0   /* enable this to test assertion failures. */
   159     SDL_assert_release(1 == 2);
   160     SDL_assert_release(5 < 4);
   161     SDL_assert_release(0 && "This is a test");
   162 #endif
   163 
   164     return (0);
   165 }
   166 
   167 int
   168 main(int argc, char *argv[])
   169 {
   170     SDL_bool verbose = SDL_TRUE;
   171     int status = 0;
   172 
   173     if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
   174         verbose = SDL_FALSE;
   175     }
   176     if (verbose) {
   177         printf("This system is running %s\n", SDL_GetPlatform());
   178     }
   179 
   180     status += TestTypes(verbose);
   181     status += TestEndian(verbose);
   182     status += TestCPUInfo(verbose);
   183     status += TestAssertions(verbose);
   184 
   185     return status;
   186 }