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