test/testplatform.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 05 Jun 2013 12:48:44 -0700
changeset 7277 1290cd7f34af
parent 5638 198383360618
child 7517 965d57022c01
permissions -rw-r--r--
Mac: Fix invalid PS3 controller mapping.

PS3 controller had swapped D-Pad down & left buttons, as well as X & Y buttons.
Thanks to Alex Szpakowski for the bug report and fix.

Fixes http://bugzilla.libsdl.org/show_bug.cgi?id=1891
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 #include <stdio.h>
    14 
    15 #include "SDL.h"
    16 #include "SDL_endian.h"
    17 #include "SDL_cpuinfo.h"
    18 #include "SDL_assert.h"
    19 
    20 /*
    21  * Watcom C flags these as Warning 201: "Unreachable code" if you just
    22  *  compare them directly, so we push it through a function to keep the
    23  *  compiler quiet.  --ryan.
    24  */
    25 static int
    26 badsize(size_t sizeoftype, size_t hardcodetype)
    27 {
    28     return sizeoftype != hardcodetype;
    29 }
    30 
    31 int
    32 TestTypes(SDL_bool verbose)
    33 {
    34     int error = 0;
    35 
    36     if (badsize(sizeof(Uint8), 1)) {
    37         if (verbose)
    38             printf("sizeof(Uint8) != 1, instead = %u\n",
    39                    (unsigned int)sizeof(Uint8));
    40         ++error;
    41     }
    42     if (badsize(sizeof(Uint16), 2)) {
    43         if (verbose)
    44             printf("sizeof(Uint16) != 2, instead = %u\n",
    45                    (unsigned int)sizeof(Uint16));
    46         ++error;
    47     }
    48     if (badsize(sizeof(Uint32), 4)) {
    49         if (verbose)
    50             printf("sizeof(Uint32) != 4, instead = %u\n",
    51                    (unsigned int)sizeof(Uint32));
    52         ++error;
    53     }
    54     if (badsize(sizeof(Uint64), 8)) {
    55         if (verbose)
    56             printf("sizeof(Uint64) != 8, instead = %u\n",
    57                    (unsigned int)sizeof(Uint64));
    58         ++error;
    59     }
    60     if (verbose && !error)
    61         printf("All data types are the expected size.\n");
    62 
    63     return (error ? 1 : 0);
    64 }
    65 
    66 int
    67 TestEndian(SDL_bool verbose)
    68 {
    69     int error = 0;
    70     Uint16 value = 0x1234;
    71     int real_byteorder;
    72     Uint16 value16 = 0xCDAB;
    73     Uint16 swapped16 = 0xABCD;
    74     Uint32 value32 = 0xEFBEADDE;
    75     Uint32 swapped32 = 0xDEADBEEF;
    76     Uint64 value64, swapped64;
    77 
    78     value64 = 0xEFBEADDE;
    79     value64 <<= 32;
    80     value64 |= 0xCDAB3412;
    81     swapped64 = 0x1234ABCD;
    82     swapped64 <<= 32;
    83     swapped64 |= 0xDEADBEEF;
    84 
    85     if (verbose) {
    86         printf("Detected a %s endian machine.\n",
    87                (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
    88     }
    89     if ((*((char *) &value) >> 4) == 0x1) {
    90         real_byteorder = SDL_BIG_ENDIAN;
    91     } else {
    92         real_byteorder = SDL_LIL_ENDIAN;
    93     }
    94     if (real_byteorder != SDL_BYTEORDER) {
    95         if (verbose) {
    96             printf("Actually a %s endian machine!\n",
    97                    (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
    98         }
    99         ++error;
   100     }
   101     if (verbose) {
   102         printf("Value 16 = 0x%X, swapped = 0x%X\n", value16,
   103                SDL_Swap16(value16));
   104     }
   105     if (SDL_Swap16(value16) != swapped16) {
   106         if (verbose) {
   107             printf("16 bit value swapped incorrectly!\n");
   108         }
   109         ++error;
   110     }
   111     if (verbose) {
   112         printf("Value 32 = 0x%X, swapped = 0x%X\n", value32,
   113                SDL_Swap32(value32));
   114     }
   115     if (SDL_Swap32(value32) != swapped32) {
   116         if (verbose) {
   117             printf("32 bit value swapped incorrectly!\n");
   118         }
   119         ++error;
   120     }
   121     if (verbose) {
   122 #ifdef _MSC_VER
   123         printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
   124                SDL_Swap64(value64));
   125 #else
   126         printf("Value 64 = 0x%llX, swapped = 0x%llX\n",
   127                (unsigned long long) value64,
   128                (unsigned long long) SDL_Swap64(value64));
   129 #endif
   130     }
   131     if (SDL_Swap64(value64) != swapped64) {
   132         if (verbose) {
   133             printf("64 bit value swapped incorrectly!\n");
   134         }
   135         ++error;
   136     }
   137     return (error ? 1 : 0);
   138 }
   139 
   140 
   141 int
   142 TestCPUInfo(SDL_bool verbose)
   143 {
   144     if (verbose) {
   145         printf("CPU count: %d\n", SDL_GetCPUCount());
   146         printf("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize());
   147         printf("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
   148         printf("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
   149         printf("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
   150         printf("3DNow! %s\n", SDL_Has3DNow()? "detected" : "not detected");
   151         printf("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
   152         printf("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
   153         printf("SSE3 %s\n", SDL_HasSSE3()? "detected" : "not detected");
   154         printf("SSE4.1 %s\n", SDL_HasSSE41()? "detected" : "not detected");
   155         printf("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
   156     }
   157     return (0);
   158 }
   159 
   160 int
   161 TestAssertions(SDL_bool verbose)
   162 {
   163     SDL_assert(1);
   164     SDL_assert_release(1);
   165     SDL_assert_paranoid(1);
   166     SDL_assert(0 || 1);
   167     SDL_assert_release(0 || 1);
   168     SDL_assert_paranoid(0 || 1);
   169 
   170 #if 0   /* enable this to test assertion failures. */
   171     SDL_assert_release(1 == 2);
   172     SDL_assert_release(5 < 4);
   173     SDL_assert_release(0 && "This is a test");
   174 #endif
   175 
   176     {
   177         const SDL_assert_data *item = SDL_GetAssertionReport();
   178         while (item) {
   179             printf("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
   180                 item->condition, item->function, item->filename,
   181                 item->linenum, item->trigger_count,
   182                 item->always_ignore ? "yes" : "no");
   183             item = item->next;
   184         }
   185     }
   186     return (0);
   187 }
   188 
   189 int
   190 main(int argc, char *argv[])
   191 {
   192     SDL_bool verbose = SDL_TRUE;
   193     int status = 0;
   194 
   195     if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
   196         verbose = SDL_FALSE;
   197     }
   198     if (verbose) {
   199         printf("This system is running %s\n", SDL_GetPlatform());
   200     }
   201 
   202     status += TestTypes(verbose);
   203     status += TestEndian(verbose);
   204     status += TestCPUInfo(verbose);
   205     status += TestAssertions(verbose);
   206 
   207     return status;
   208 }