test/testplatform.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 10:32:14 +0000
changeset 3319 63d4517fc4ac
parent 3312 c91cb3f0204e
child 3579 3427271a2d75
permissions -rw-r--r--
Fixed bug #777

Implemented SDL_GetPlatform()
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@3319
   160
        printf("This system is running %s\n", SDL_GetPlatform());
slouken@1895
   161
    }
slouken@1421
   162
slouken@1895
   163
    status += TestTypes(verbose);
slouken@1895
   164
    status += TestEndian(verbose);
slouken@1895
   165
    status += TestCPUInfo(verbose);
slouken@1895
   166
    return status;
slouken@1421
   167
}