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