test/testplatform.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 11 Mar 2015 21:14:21 +0100
changeset 9380 07b7c1005a23
parent 9356 e87d6e1e812a
child 9383 62164ad0b7d5
permissions -rw-r--r--
Updated internal documentation comments.
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
slouken@1421
    17
/*
slouken@1421
    18
 * Watcom C flags these as Warning 201: "Unreachable code" if you just
slouken@1421
    19
 *  compare them directly, so we push it through a function to keep the
slouken@1421
    20
 *  compiler quiet.  --ryan.
slouken@1421
    21
 */
slouken@1895
    22
static int
slouken@1895
    23
badsize(size_t sizeoftype, size_t hardcodetype)
slouken@1421
    24
{
slouken@1421
    25
    return sizeoftype != hardcodetype;
slouken@1421
    26
}
slouken@1421
    27
slouken@1895
    28
int
slouken@1895
    29
TestTypes(SDL_bool verbose)
slouken@1421
    30
{
slouken@1895
    31
    int error = 0;
slouken@1421
    32
slouken@1895
    33
    if (badsize(sizeof(Uint8), 1)) {
slouken@1895
    34
        if (verbose)
aschiffler@7639
    35
            SDL_Log("sizeof(Uint8) != 1, instead = %u\n",
slouken@3687
    36
                   (unsigned int)sizeof(Uint8));
slouken@1895
    37
        ++error;
slouken@1895
    38
    }
slouken@1895
    39
    if (badsize(sizeof(Uint16), 2)) {
slouken@1895
    40
        if (verbose)
aschiffler@7639
    41
            SDL_Log("sizeof(Uint16) != 2, instead = %u\n",
slouken@3687
    42
                   (unsigned int)sizeof(Uint16));
slouken@1895
    43
        ++error;
slouken@1895
    44
    }
slouken@1895
    45
    if (badsize(sizeof(Uint32), 4)) {
slouken@1895
    46
        if (verbose)
aschiffler@7639
    47
            SDL_Log("sizeof(Uint32) != 4, instead = %u\n",
slouken@3687
    48
                   (unsigned int)sizeof(Uint32));
slouken@1895
    49
        ++error;
slouken@1895
    50
    }
slouken@1895
    51
    if (badsize(sizeof(Uint64), 8)) {
slouken@1895
    52
        if (verbose)
aschiffler@7639
    53
            SDL_Log("sizeof(Uint64) != 8, instead = %u\n",
slouken@3687
    54
                   (unsigned int)sizeof(Uint64));
slouken@1895
    55
        ++error;
slouken@1895
    56
    }
slouken@1895
    57
    if (verbose && !error)
aschiffler@7639
    58
        SDL_Log("All data types are the expected size.\n");
slouken@1421
    59
slouken@1895
    60
    return (error ? 1 : 0);
slouken@1421
    61
}
slouken@1421
    62
slouken@1895
    63
int
slouken@1895
    64
TestEndian(SDL_bool verbose)
slouken@1421
    65
{
slouken@1895
    66
    int error = 0;
slouken@1895
    67
    Uint16 value = 0x1234;
slouken@1895
    68
    int real_byteorder;
slouken@1895
    69
    Uint16 value16 = 0xCDAB;
slouken@1895
    70
    Uint16 swapped16 = 0xABCD;
slouken@1895
    71
    Uint32 value32 = 0xEFBEADDE;
slouken@1895
    72
    Uint32 swapped32 = 0xDEADBEEF;
slouken@1895
    73
    Uint64 value64, swapped64;
slouken@5512
    74
slouken@1895
    75
    value64 = 0xEFBEADDE;
slouken@1895
    76
    value64 <<= 32;
slouken@1895
    77
    value64 |= 0xCDAB3412;
slouken@1895
    78
    swapped64 = 0x1234ABCD;
slouken@1895
    79
    swapped64 <<= 32;
slouken@1895
    80
    swapped64 |= 0xDEADBEEF;
slouken@1421
    81
slouken@1895
    82
    if (verbose) {
aschiffler@7639
    83
        SDL_Log("Detected a %s endian machine.\n",
slouken@1895
    84
               (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
slouken@1895
    85
    }
slouken@1895
    86
    if ((*((char *) &value) >> 4) == 0x1) {
slouken@1895
    87
        real_byteorder = SDL_BIG_ENDIAN;
slouken@1895
    88
    } else {
slouken@1895
    89
        real_byteorder = SDL_LIL_ENDIAN;
slouken@1895
    90
    }
slouken@1895
    91
    if (real_byteorder != SDL_BYTEORDER) {
slouken@1895
    92
        if (verbose) {
aschiffler@7639
    93
            SDL_Log("Actually a %s endian machine!\n",
slouken@1895
    94
                   (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
slouken@1895
    95
        }
slouken@1895
    96
        ++error;
slouken@1895
    97
    }
slouken@1895
    98
    if (verbose) {
aschiffler@7639
    99
        SDL_Log("Value 16 = 0x%X, swapped = 0x%X\n", value16,
slouken@1895
   100
               SDL_Swap16(value16));
slouken@1895
   101
    }
slouken@1895
   102
    if (SDL_Swap16(value16) != swapped16) {
slouken@1895
   103
        if (verbose) {
aschiffler@7639
   104
            SDL_Log("16 bit value swapped incorrectly!\n");
slouken@1895
   105
        }
slouken@1895
   106
        ++error;
slouken@1895
   107
    }
slouken@1895
   108
    if (verbose) {
aschiffler@7639
   109
        SDL_Log("Value 32 = 0x%X, swapped = 0x%X\n", value32,
slouken@1895
   110
               SDL_Swap32(value32));
slouken@1895
   111
    }
slouken@1895
   112
    if (SDL_Swap32(value32) != swapped32) {
slouken@1895
   113
        if (verbose) {
aschiffler@7639
   114
            SDL_Log("32 bit value swapped incorrectly!\n");
slouken@1895
   115
        }
slouken@1895
   116
        ++error;
slouken@1895
   117
    }
slouken@1895
   118
    if (verbose) {
urkle@9260
   119
        SDL_Log("Value 64 = 0x%"SDL_PRIX64", swapped = 0x%"SDL_PRIX64"\n", value64,
slouken@1895
   120
               SDL_Swap64(value64));
slouken@1895
   121
    }
slouken@1895
   122
    if (SDL_Swap64(value64) != swapped64) {
slouken@1895
   123
        if (verbose) {
aschiffler@7639
   124
            SDL_Log("64 bit value swapped incorrectly!\n");
slouken@1895
   125
        }
slouken@1895
   126
        ++error;
slouken@1895
   127
    }
slouken@1895
   128
    return (error ? 1 : 0);
slouken@1421
   129
}
slouken@1421
   130
slouken@1421
   131
slouken@1895
   132
int
slouken@1895
   133
TestCPUInfo(SDL_bool verbose)
slouken@1421
   134
{
slouken@1895
   135
    if (verbose) {
aschiffler@7639
   136
        SDL_Log("CPU count: %d\n", SDL_GetCPUCount());
aschiffler@7639
   137
        SDL_Log("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize());
aschiffler@7639
   138
        SDL_Log("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
aschiffler@7639
   139
        SDL_Log("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
aschiffler@7639
   140
        SDL_Log("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
aschiffler@7639
   141
        SDL_Log("3DNow! %s\n", SDL_Has3DNow()? "detected" : "not detected");
aschiffler@7639
   142
        SDL_Log("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
aschiffler@7639
   143
        SDL_Log("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
aschiffler@7639
   144
        SDL_Log("SSE3 %s\n", SDL_HasSSE3()? "detected" : "not detected");
aschiffler@7639
   145
        SDL_Log("SSE4.1 %s\n", SDL_HasSSE41()? "detected" : "not detected");
aschiffler@7639
   146
        SDL_Log("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
slouken@8148
   147
        SDL_Log("AVX %s\n", SDL_HasAVX()? "detected" : "not detected");
slouken@7823
   148
        SDL_Log("System RAM %d MB\n", SDL_GetSystemRAM());
slouken@1895
   149
    }
slouken@1895
   150
    return (0);
slouken@1421
   151
}
slouken@1421
   152
slouken@1895
   153
int
slouken@3663
   154
TestAssertions(SDL_bool verbose)
slouken@3663
   155
{
slouken@3663
   156
    SDL_assert(1);
slouken@3663
   157
    SDL_assert_release(1);
slouken@3663
   158
    SDL_assert_paranoid(1);
slouken@3663
   159
    SDL_assert(0 || 1);
slouken@3663
   160
    SDL_assert_release(0 || 1);
slouken@3663
   161
    SDL_assert_paranoid(0 || 1);
slouken@3663
   162
slouken@3663
   163
#if 0   /* enable this to test assertion failures. */
slouken@3663
   164
    SDL_assert_release(1 == 2);
slouken@3663
   165
    SDL_assert_release(5 < 4);
slouken@3663
   166
    SDL_assert_release(0 && "This is a test");
slouken@3663
   167
#endif
slouken@3663
   168
slouken@5542
   169
    {
slouken@5542
   170
        const SDL_assert_data *item = SDL_GetAssertionReport();
slouken@5542
   171
        while (item) {
aschiffler@7639
   172
            SDL_Log("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
slouken@5542
   173
                item->condition, item->function, item->filename,
slouken@5542
   174
                item->linenum, item->trigger_count,
slouken@5542
   175
                item->always_ignore ? "yes" : "no");
slouken@5542
   176
            item = item->next;
slouken@5542
   177
        }
slouken@5542
   178
    }
slouken@3663
   179
    return (0);
slouken@3663
   180
}
slouken@3663
   181
slouken@3663
   182
int
slouken@1895
   183
main(int argc, char *argv[])
slouken@1421
   184
{
slouken@1895
   185
    SDL_bool verbose = SDL_TRUE;
slouken@1895
   186
    int status = 0;
slouken@1421
   187
aschiffler@7639
   188
    /* Enable standard application logging */
aschiffler@7639
   189
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   190
slouken@1895
   191
    if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
slouken@1895
   192
        verbose = SDL_FALSE;
slouken@1895
   193
    }
slouken@1895
   194
    if (verbose) {
aschiffler@7639
   195
        SDL_Log("This system is running %s\n", SDL_GetPlatform());
slouken@1895
   196
    }
slouken@1421
   197
slouken@1895
   198
    status += TestTypes(verbose);
slouken@1895
   199
    status += TestEndian(verbose);
slouken@1895
   200
    status += TestCPUInfo(verbose);
slouken@3663
   201
    status += TestAssertions(verbose);
slouken@3663
   202
slouken@1895
   203
    return status;
slouken@1421
   204
}