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