test/testplatform.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 09:56:26 +0000
changeset 3312 c91cb3f0204e
parent 3292 245a7d79577c
child 3319 63d4517fc4ac
permissions -rw-r--r--
Fixed bug #705

Sami N 2009-02-21 11:15:39 PST

Patches two tests that had broken format strings in their *printf()'s.
slouken@1421
     1
slouken@1421
     2
#include <stdio.h>
slouken@1421
     3
slouken@1421
     4
#include "SDL.h"
slouken@1421
     5
#include "SDL_endian.h"
slouken@1421
     6
#include "SDL_cpuinfo.h"
slouken@1421
     7
slouken@1421
     8
/*
slouken@1421
     9
 * Watcom C flags these as Warning 201: "Unreachable code" if you just
slouken@1421
    10
 *  compare them directly, so we push it through a function to keep the
slouken@1421
    11
 *  compiler quiet.  --ryan.
slouken@1421
    12
 */
slouken@1895
    13
static int
slouken@1895
    14
badsize(size_t sizeoftype, size_t hardcodetype)
slouken@1421
    15
{
slouken@1421
    16
    return sizeoftype != hardcodetype;
slouken@1421
    17
}
slouken@1421
    18
slouken@1895
    19
int
slouken@1895
    20
TestTypes(SDL_bool verbose)
slouken@1421
    21
{
slouken@1895
    22
    int error = 0;
slouken@1421
    23
slouken@1895
    24
    if (badsize(sizeof(Uint8), 1)) {
slouken@1895
    25
        if (verbose)
slouken@3312
    26
            printf("sizeof(Uint8) != 1, instead = %lu\n", sizeof(Uint8));
slouken@1895
    27
        ++error;
slouken@1895
    28
    }
slouken@1895
    29
    if (badsize(sizeof(Uint16), 2)) {
slouken@1895
    30
        if (verbose)
slouken@3312
    31
            printf("sizeof(Uint16) != 2, instead = %lu\n", sizeof(Uint16));
slouken@1895
    32
        ++error;
slouken@1895
    33
    }
slouken@1895
    34
    if (badsize(sizeof(Uint32), 4)) {
slouken@1895
    35
        if (verbose)
slouken@3312
    36
            printf("sizeof(Uint32) != 4, instead = %lu\n", sizeof(Uint32));
slouken@1895
    37
        ++error;
slouken@1895
    38
    }
slouken@1421
    39
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
    40
    if (badsize(sizeof(Uint64), 8)) {
slouken@1895
    41
        if (verbose)
slouken@3312
    42
            printf("sizeof(Uint64) != 8, instead = %lu\n", sizeof(Uint64));
slouken@1895
    43
        ++error;
slouken@1895
    44
    }
slouken@1421
    45
#else
slouken@1895
    46
    if (verbose) {
slouken@1895
    47
        printf("WARNING: No 64-bit datatype on this platform\n");
slouken@1895
    48
    }
slouken@1421
    49
#endif
slouken@1895
    50
    if (verbose && !error)
slouken@1895
    51
        printf("All data types are the expected size.\n");
slouken@1421
    52
slouken@1895
    53
    return (error ? 1 : 0);
slouken@1421
    54
}
slouken@1421
    55
slouken@1895
    56
int
slouken@1895
    57
TestEndian(SDL_bool verbose)
slouken@1421
    58
{
slouken@1895
    59
    int error = 0;
slouken@1895
    60
    Uint16 value = 0x1234;
slouken@1895
    61
    int real_byteorder;
slouken@1895
    62
    Uint16 value16 = 0xCDAB;
slouken@1895
    63
    Uint16 swapped16 = 0xABCD;
slouken@1895
    64
    Uint32 value32 = 0xEFBEADDE;
slouken@1895
    65
    Uint32 swapped32 = 0xDEADBEEF;
slouken@1421
    66
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
    67
    Uint64 value64, swapped64;
slouken@1895
    68
    value64 = 0xEFBEADDE;
slouken@1895
    69
    value64 <<= 32;
slouken@1895
    70
    value64 |= 0xCDAB3412;
slouken@1895
    71
    swapped64 = 0x1234ABCD;
slouken@1895
    72
    swapped64 <<= 32;
slouken@1895
    73
    swapped64 |= 0xDEADBEEF;
slouken@1421
    74
#endif
slouken@1421
    75
slouken@1895
    76
    if (verbose) {
slouken@1895
    77
        printf("Detected a %s endian machine.\n",
slouken@1895
    78
               (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
slouken@1895
    79
    }
slouken@1895
    80
    if ((*((char *) &value) >> 4) == 0x1) {
slouken@1895
    81
        real_byteorder = SDL_BIG_ENDIAN;
slouken@1895
    82
    } else {
slouken@1895
    83
        real_byteorder = SDL_LIL_ENDIAN;
slouken@1895
    84
    }
slouken@1895
    85
    if (real_byteorder != SDL_BYTEORDER) {
slouken@1895
    86
        if (verbose) {
slouken@1895
    87
            printf("Actually a %s endian machine!\n",
slouken@1895
    88
                   (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
slouken@1895
    89
        }
slouken@1895
    90
        ++error;
slouken@1895
    91
    }
slouken@1895
    92
    if (verbose) {
slouken@1895
    93
        printf("Value 16 = 0x%X, swapped = 0x%X\n", value16,
slouken@1895
    94
               SDL_Swap16(value16));
slouken@1895
    95
    }
slouken@1895
    96
    if (SDL_Swap16(value16) != swapped16) {
slouken@1895
    97
        if (verbose) {
slouken@1895
    98
            printf("16 bit value swapped incorrectly!\n");
slouken@1895
    99
        }
slouken@1895
   100
        ++error;
slouken@1895
   101
    }
slouken@1895
   102
    if (verbose) {
slouken@1895
   103
        printf("Value 32 = 0x%X, swapped = 0x%X\n", value32,
slouken@1895
   104
               SDL_Swap32(value32));
slouken@1895
   105
    }
slouken@1895
   106
    if (SDL_Swap32(value32) != swapped32) {
slouken@1895
   107
        if (verbose) {
slouken@1895
   108
            printf("32 bit value swapped incorrectly!\n");
slouken@1895
   109
        }
slouken@1895
   110
        ++error;
slouken@1895
   111
    }
slouken@1421
   112
#ifdef SDL_HAS_64BIT_TYPE
slouken@1895
   113
    if (verbose) {
slouken@1615
   114
#ifdef _MSC_VER
slouken@1895
   115
        printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
slouken@1895
   116
               SDL_Swap64(value64));
slouken@1615
   117
#else
slouken@1895
   118
        printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64,
slouken@1895
   119
               SDL_Swap64(value64));
slouken@1615
   120
#endif
slouken@1895
   121
    }
slouken@1895
   122
    if (SDL_Swap64(value64) != swapped64) {
slouken@1895
   123
        if (verbose) {
slouken@1895
   124
            printf("64 bit value swapped incorrectly!\n");
slouken@1895
   125
        }
slouken@1895
   126
        ++error;
slouken@1895
   127
    }
slouken@1421
   128
#endif
slouken@1895
   129
    return (error ? 1 : 0);
slouken@1421
   130
}
slouken@1421
   131
slouken@1421
   132
slouken@1895
   133
int
slouken@1895
   134
TestCPUInfo(SDL_bool verbose)
slouken@1421
   135
{
slouken@1895
   136
    if (verbose) {
slouken@1895
   137
        printf("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
slouken@1895
   138
        printf("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
slouken@1895
   139
        printf("MMX Ext %s\n", SDL_HasMMXExt()? "detected" : "not detected");
slouken@1895
   140
        printf("3DNow %s\n", SDL_Has3DNow()? "detected" : "not detected");
slouken@1895
   141
        printf("3DNow Ext %s\n",
slouken@1895
   142
               SDL_Has3DNowExt()? "detected" : "not detected");
slouken@1895
   143
        printf("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
slouken@1895
   144
        printf("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
slouken@1895
   145
        printf("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
slouken@1895
   146
    }
slouken@1895
   147
    return (0);
slouken@1421
   148
}
slouken@1421
   149
slouken@1895
   150
int
slouken@1895
   151
main(int argc, char *argv[])
slouken@1421
   152
{
slouken@1895
   153
    SDL_bool verbose = SDL_TRUE;
slouken@1895
   154
    int status = 0;
slouken@1421
   155
slouken@1895
   156
    if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
slouken@1895
   157
        verbose = SDL_FALSE;
slouken@1895
   158
    }
slouken@1895
   159
    if (verbose) {
slouken@1895
   160
        printf("This system is running %s\n",
slouken@1421
   161
#if __AIX__
slouken@1895
   162
               "AIX"
slouken@3292
   163
#elif __HAIKU__
slouken@3292
   164
/* Haiku must appear here before BeOS, since it also defines __BEOS__ */
slouken@3292
   165
               "Haiku"
slouken@1421
   166
#elif __BEOS__
slouken@1895
   167
               "BeOS"
slouken@1421
   168
#elif __BSDI__
slouken@1895
   169
               "BSDI"
slouken@1421
   170
#elif __DREAMCAST__
slouken@1895
   171
               "Dreamcast"
slouken@1421
   172
#elif __FREEBSD__
slouken@1895
   173
               "FreeBSD"
slouken@1421
   174
#elif __HPUX__
slouken@1895
   175
               "HP-UX"
slouken@1421
   176
#elif __IRIX__
slouken@1895
   177
               "Irix"
slouken@1421
   178
#elif __LINUX__
slouken@1895
   179
               "Linux"
slouken@1421
   180
#elif __MINT__
slouken@1895
   181
               "Atari MiNT"
slouken@1421
   182
#elif __MACOS__
slouken@1895
   183
               "MacOS Classic"
slouken@1421
   184
#elif __MACOSX__
slouken@1895
   185
               "Mac OS X"
slouken@1421
   186
#elif __NETBSD__
slouken@1895
   187
               "NetBSD"
slouken@1421
   188
#elif __OPENBSD__
slouken@1895
   189
               "OpenBSD"
slouken@1421
   190
#elif __OS2__
slouken@1895
   191
               "OS/2"
slouken@1421
   192
#elif __OSF__
slouken@1895
   193
               "OSF/1"
slouken@1421
   194
#elif __QNXNTO__
slouken@1895
   195
               "QNX Neutrino"
slouken@1421
   196
#elif __RISCOS__
slouken@1895
   197
               "RISC OS"
slouken@1421
   198
#elif __SOLARIS__
slouken@1895
   199
               "Solaris"
slouken@1421
   200
#elif __WIN32__
slouken@1421
   201
#ifdef _WIN32_WCE
slouken@1895
   202
               "Windows CE"
slouken@1421
   203
#else
slouken@1895
   204
               "Windows"
slouken@1421
   205
#endif
slouken@2765
   206
#elif __IPHONEOS__
slouken@2765
   207
               "iPhone OS"
slouken@1421
   208
#else
slouken@1895
   209
               "an unknown operating system! (see SDL_platform.h)"
slouken@1421
   210
#endif
slouken@1895
   211
            );
slouken@1895
   212
    }
slouken@1421
   213
slouken@1895
   214
    status += TestTypes(verbose);
slouken@1895
   215
    status += TestEndian(verbose);
slouken@1895
   216
    status += TestCPUInfo(verbose);
slouken@1895
   217
    return status;
slouken@1421
   218
}