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