test/testplatform.c
author Edward Rudd <urkle@outoforder.cc>
Wed, 03 Dec 2014 12:23:17 -0500
changeset 9260 039714172dc3
parent 8149 681eb46b8ac4
child 9356 e87d6e1e812a
permissions -rw-r--r--
switch to use SDL_PRI* macros for long long formatting everywhere.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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)
aschiffler@7639
    38
            SDL_Log("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)
aschiffler@7639
    44
            SDL_Log("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)
aschiffler@7639
    50
            SDL_Log("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)
aschiffler@7639
    56
            SDL_Log("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)
aschiffler@7639
    61
        SDL_Log("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) {
aschiffler@7639
    86
        SDL_Log("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) {
aschiffler@7639
    96
            SDL_Log("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) {
aschiffler@7639
   102
        SDL_Log("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) {
aschiffler@7639
   107
            SDL_Log("16 bit value swapped incorrectly!\n");
slouken@1895
   108
        }
slouken@1895
   109
        ++error;
slouken@1895
   110
    }
slouken@1895
   111
    if (verbose) {
aschiffler@7639
   112
        SDL_Log("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) {
aschiffler@7639
   117
            SDL_Log("32 bit value swapped incorrectly!\n");
slouken@1895
   118
        }
slouken@1895
   119
        ++error;
slouken@1895
   120
    }
slouken@1895
   121
    if (verbose) {
urkle@9260
   122
        SDL_Log("Value 64 = 0x%"SDL_PRIX64", swapped = 0x%"SDL_PRIX64"\n", value64,
slouken@1895
   123
               SDL_Swap64(value64));
slouken@1895
   124
    }
slouken@1895
   125
    if (SDL_Swap64(value64) != swapped64) {
slouken@1895
   126
        if (verbose) {
aschiffler@7639
   127
            SDL_Log("64 bit value swapped incorrectly!\n");
slouken@1895
   128
        }
slouken@1895
   129
        ++error;
slouken@1895
   130
    }
slouken@1895
   131
    return (error ? 1 : 0);
slouken@1421
   132
}
slouken@1421
   133
slouken@1421
   134
slouken@1895
   135
int
slouken@1895
   136
TestCPUInfo(SDL_bool verbose)
slouken@1421
   137
{
slouken@1895
   138
    if (verbose) {
aschiffler@7639
   139
        SDL_Log("CPU count: %d\n", SDL_GetCPUCount());
aschiffler@7639
   140
        SDL_Log("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize());
aschiffler@7639
   141
        SDL_Log("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
aschiffler@7639
   142
        SDL_Log("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
aschiffler@7639
   143
        SDL_Log("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
aschiffler@7639
   144
        SDL_Log("3DNow! %s\n", SDL_Has3DNow()? "detected" : "not detected");
aschiffler@7639
   145
        SDL_Log("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
aschiffler@7639
   146
        SDL_Log("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
aschiffler@7639
   147
        SDL_Log("SSE3 %s\n", SDL_HasSSE3()? "detected" : "not detected");
aschiffler@7639
   148
        SDL_Log("SSE4.1 %s\n", SDL_HasSSE41()? "detected" : "not detected");
aschiffler@7639
   149
        SDL_Log("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
slouken@8148
   150
        SDL_Log("AVX %s\n", SDL_HasAVX()? "detected" : "not detected");
slouken@7823
   151
        SDL_Log("System RAM %d MB\n", SDL_GetSystemRAM());
slouken@1895
   152
    }
slouken@1895
   153
    return (0);
slouken@1421
   154
}
slouken@1421
   155
slouken@1895
   156
int
slouken@3663
   157
TestAssertions(SDL_bool verbose)
slouken@3663
   158
{
slouken@3663
   159
    SDL_assert(1);
slouken@3663
   160
    SDL_assert_release(1);
slouken@3663
   161
    SDL_assert_paranoid(1);
slouken@3663
   162
    SDL_assert(0 || 1);
slouken@3663
   163
    SDL_assert_release(0 || 1);
slouken@3663
   164
    SDL_assert_paranoid(0 || 1);
slouken@3663
   165
slouken@3663
   166
#if 0   /* enable this to test assertion failures. */
slouken@3663
   167
    SDL_assert_release(1 == 2);
slouken@3663
   168
    SDL_assert_release(5 < 4);
slouken@3663
   169
    SDL_assert_release(0 && "This is a test");
slouken@3663
   170
#endif
slouken@3663
   171
slouken@5542
   172
    {
slouken@5542
   173
        const SDL_assert_data *item = SDL_GetAssertionReport();
slouken@5542
   174
        while (item) {
aschiffler@7639
   175
            SDL_Log("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
slouken@5542
   176
                item->condition, item->function, item->filename,
slouken@5542
   177
                item->linenum, item->trigger_count,
slouken@5542
   178
                item->always_ignore ? "yes" : "no");
slouken@5542
   179
            item = item->next;
slouken@5542
   180
        }
slouken@5542
   181
    }
slouken@3663
   182
    return (0);
slouken@3663
   183
}
slouken@3663
   184
slouken@3663
   185
int
slouken@1895
   186
main(int argc, char *argv[])
slouken@1421
   187
{
slouken@1895
   188
    SDL_bool verbose = SDL_TRUE;
slouken@1895
   189
    int status = 0;
slouken@1421
   190
aschiffler@7639
   191
    /* Enable standard application logging */
aschiffler@7639
   192
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   193
slouken@1895
   194
    if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
slouken@1895
   195
        verbose = SDL_FALSE;
slouken@1895
   196
    }
slouken@1895
   197
    if (verbose) {
aschiffler@7639
   198
        SDL_Log("This system is running %s\n", SDL_GetPlatform());
slouken@1895
   199
    }
slouken@1421
   200
slouken@1895
   201
    status += TestTypes(verbose);
slouken@1895
   202
    status += TestEndian(verbose);
slouken@1895
   203
    status += TestCPUInfo(verbose);
slouken@3663
   204
    status += TestAssertions(verbose);
slouken@3663
   205
slouken@1895
   206
    return status;
slouken@1421
   207
}