test/testplatform.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 30 Oct 2016 21:01:46 +0100
changeset 10564 46cfe31e501a
parent 9998 f67cf37e9cd4
child 10587 513f0e80a7dd
permissions -rw-r--r--
Added call to SDL_HasAVX2() in platform test program.
slouken@5535
     1
/*
slouken@9998
     2
  Copyright (C) 1997-2016 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");
philipp@10564
   148
        SDL_Log("AVX2 %s\n", SDL_HasAVX2()? "detected" : "not detected");
slouken@7823
   149
        SDL_Log("System RAM %d MB\n", SDL_GetSystemRAM());
slouken@1895
   150
    }
slouken@1895
   151
    return (0);
slouken@1421
   152
}
slouken@1421
   153
slouken@1895
   154
int
slouken@3663
   155
TestAssertions(SDL_bool verbose)
slouken@3663
   156
{
slouken@3663
   157
    SDL_assert(1);
slouken@3663
   158
    SDL_assert_release(1);
slouken@3663
   159
    SDL_assert_paranoid(1);
slouken@3663
   160
    SDL_assert(0 || 1);
slouken@3663
   161
    SDL_assert_release(0 || 1);
slouken@3663
   162
    SDL_assert_paranoid(0 || 1);
slouken@3663
   163
slouken@3663
   164
#if 0   /* enable this to test assertion failures. */
slouken@3663
   165
    SDL_assert_release(1 == 2);
slouken@3663
   166
    SDL_assert_release(5 < 4);
slouken@3663
   167
    SDL_assert_release(0 && "This is a test");
slouken@3663
   168
#endif
slouken@3663
   169
slouken@5542
   170
    {
philipp@9383
   171
        const SDL_AssertData *item = SDL_GetAssertionReport();
slouken@5542
   172
        while (item) {
aschiffler@7639
   173
            SDL_Log("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
slouken@5542
   174
                item->condition, item->function, item->filename,
slouken@5542
   175
                item->linenum, item->trigger_count,
slouken@5542
   176
                item->always_ignore ? "yes" : "no");
slouken@5542
   177
            item = item->next;
slouken@5542
   178
        }
slouken@5542
   179
    }
slouken@3663
   180
    return (0);
slouken@3663
   181
}
slouken@3663
   182
slouken@3663
   183
int
slouken@1895
   184
main(int argc, char *argv[])
slouken@1421
   185
{
slouken@1895
   186
    SDL_bool verbose = SDL_TRUE;
slouken@1895
   187
    int status = 0;
slouken@1421
   188
aschiffler@7639
   189
    /* Enable standard application logging */
aschiffler@7639
   190
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   191
slouken@1895
   192
    if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
slouken@1895
   193
        verbose = SDL_FALSE;
slouken@1895
   194
    }
slouken@1895
   195
    if (verbose) {
aschiffler@7639
   196
        SDL_Log("This system is running %s\n", SDL_GetPlatform());
slouken@1895
   197
    }
slouken@1421
   198
slouken@1895
   199
    status += TestTypes(verbose);
slouken@1895
   200
    status += TestEndian(verbose);
slouken@1895
   201
    status += TestCPUInfo(verbose);
slouken@3663
   202
    status += TestAssertions(verbose);
slouken@3663
   203
slouken@1895
   204
    return status;
slouken@1421
   205
}