test/testplatform.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 19 Apr 2011 11:41:45 -0700
changeset 5542 882871b2db1a
parent 5535 96594ac5fd1a
child 5638 198383360618
permissions -rw-r--r--
Added test of the assertion reporting system
     1 /*
     2   Copyright (C) 1997-2011 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             printf("sizeof(Uint8) != 1, instead = %u\n",
    39                    (unsigned int)sizeof(Uint8));
    40         ++error;
    41     }
    42     if (badsize(sizeof(Uint16), 2)) {
    43         if (verbose)
    44             printf("sizeof(Uint16) != 2, instead = %u\n",
    45                    (unsigned int)sizeof(Uint16));
    46         ++error;
    47     }
    48     if (badsize(sizeof(Uint32), 4)) {
    49         if (verbose)
    50             printf("sizeof(Uint32) != 4, instead = %u\n",
    51                    (unsigned int)sizeof(Uint32));
    52         ++error;
    53     }
    54     if (badsize(sizeof(Uint64), 8)) {
    55         if (verbose)
    56             printf("sizeof(Uint64) != 8, instead = %u\n",
    57                    (unsigned int)sizeof(Uint64));
    58         ++error;
    59     }
    60     if (verbose && !error)
    61         printf("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         printf("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             printf("Actually a %s endian machine!\n",
    97                    (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
    98         }
    99         ++error;
   100     }
   101     if (verbose) {
   102         printf("Value 16 = 0x%X, swapped = 0x%X\n", value16,
   103                SDL_Swap16(value16));
   104     }
   105     if (SDL_Swap16(value16) != swapped16) {
   106         if (verbose) {
   107             printf("16 bit value swapped incorrectly!\n");
   108         }
   109         ++error;
   110     }
   111     if (verbose) {
   112         printf("Value 32 = 0x%X, swapped = 0x%X\n", value32,
   113                SDL_Swap32(value32));
   114     }
   115     if (SDL_Swap32(value32) != swapped32) {
   116         if (verbose) {
   117             printf("32 bit value swapped incorrectly!\n");
   118         }
   119         ++error;
   120     }
   121     if (verbose) {
   122 #ifdef _MSC_VER
   123         printf("Value 64 = 0x%I64X, swapped = 0x%I64X\n", value64,
   124                SDL_Swap64(value64));
   125 #else
   126         printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64,
   127                SDL_Swap64(value64));
   128 #endif
   129     }
   130     if (SDL_Swap64(value64) != swapped64) {
   131         if (verbose) {
   132             printf("64 bit value swapped incorrectly!\n");
   133         }
   134         ++error;
   135     }
   136     return (error ? 1 : 0);
   137 }
   138 
   139 
   140 int
   141 TestCPUInfo(SDL_bool verbose)
   142 {
   143     if (verbose) {
   144         printf("CPU count: %d\n", SDL_GetCPUCount());
   145         printf("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize());
   146         printf("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
   147         printf("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
   148         printf("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
   149         printf("3DNow! %s\n", SDL_Has3DNow()? "detected" : "not detected");
   150         printf("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
   151         printf("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
   152         printf("SSE3 %s\n", SDL_HasSSE3()? "detected" : "not detected");
   153         printf("SSE4.1 %s\n", SDL_HasSSE41()? "detected" : "not detected");
   154         printf("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
   155     }
   156     return (0);
   157 }
   158 
   159 int
   160 TestAssertions(SDL_bool verbose)
   161 {
   162     SDL_assert(1);
   163     SDL_assert_release(1);
   164     SDL_assert_paranoid(1);
   165     SDL_assert(0 || 1);
   166     SDL_assert_release(0 || 1);
   167     SDL_assert_paranoid(0 || 1);
   168 
   169 #if 0   /* enable this to test assertion failures. */
   170     SDL_assert_release(1 == 2);
   171     SDL_assert_release(5 < 4);
   172     SDL_assert_release(0 && "This is a test");
   173 #endif
   174 
   175     {
   176         const SDL_assert_data *item = SDL_GetAssertionReport();
   177         while (item) {
   178             printf("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
   179                 item->condition, item->function, item->filename,
   180                 item->linenum, item->trigger_count,
   181                 item->always_ignore ? "yes" : "no");
   182             item = item->next;
   183         }
   184     }
   185     return (0);
   186 }
   187 
   188 int
   189 main(int argc, char *argv[])
   190 {
   191     SDL_bool verbose = SDL_TRUE;
   192     int status = 0;
   193 
   194     if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
   195         verbose = SDL_FALSE;
   196     }
   197     if (verbose) {
   198         printf("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 }