test/testplatform.c
changeset 1421 638da75f9ab8
child 1424 7a610f25c12f
equal deleted inserted replaced
1420:2405517b5eab 1421:638da75f9ab8
       
     1 
       
     2 #include <stdio.h>
       
     3 
       
     4 #include "SDL.h"
       
     5 #include "SDL_endian.h"
       
     6 #include "SDL_cpuinfo.h"
       
     7 
       
     8 /*
       
     9  * Watcom C flags these as Warning 201: "Unreachable code" if you just
       
    10  *  compare them directly, so we push it through a function to keep the
       
    11  *  compiler quiet.  --ryan.
       
    12  */
       
    13 static int badsize(size_t sizeoftype, size_t hardcodetype)
       
    14 {
       
    15     return sizeoftype != hardcodetype;
       
    16 }
       
    17 
       
    18 int TestTypes(SDL_bool verbose)
       
    19 {
       
    20 	int error = 0;
       
    21 
       
    22 	if ( badsize(sizeof(Uint8), 1) ) {
       
    23 		if ( verbose )
       
    24 			printf("sizeof(Uint8) != 1, instead = %d\n",
       
    25 								sizeof(Uint8));
       
    26 		++error;
       
    27 	}
       
    28 	if ( badsize(sizeof(Uint16), 2) ) {
       
    29 		if ( verbose )
       
    30 			printf("sizeof(Uint16) != 2, instead = %d\n",
       
    31 								sizeof(Uint16));
       
    32 		++error;
       
    33 	}
       
    34 	if ( badsize(sizeof(Uint32), 4) ) {
       
    35 		if ( verbose )
       
    36 			printf("sizeof(Uint32) != 4, instead = %d\n",
       
    37 								sizeof(Uint32));
       
    38 		++error;
       
    39 	}
       
    40 #ifdef SDL_HAS_64BIT_TYPE
       
    41 	if ( badsize(sizeof(Uint64), 8) ) {
       
    42 		if ( verbose )
       
    43 			printf("sizeof(Uint64) != 8, instead = %d\n",
       
    44 								sizeof(Uint64));
       
    45 		++error;
       
    46 	}
       
    47 #else
       
    48 	if ( verbose ) {
       
    49 		printf("WARNING: No 64-bit datatype on this platform\n");
       
    50 	}
       
    51 #endif
       
    52 	if ( verbose && !error )
       
    53 		printf("All data types are the expected size.\n");
       
    54 
       
    55 	return( error ? 1 : 0 );
       
    56 }
       
    57 
       
    58 int TestEndian(SDL_bool verbose)
       
    59 {
       
    60 	int error = 0;
       
    61 	Uint16 value = 0x1234;
       
    62 	int real_byteorder;
       
    63 	Uint16 value16 = 0xCDAB;
       
    64 	Uint16 swapped16 = 0xABCD;
       
    65 	Uint32 value32 = 0xEFBEADDE;
       
    66 	Uint32 swapped32 = 0xDEADBEEF;
       
    67 #ifdef SDL_HAS_64BIT_TYPE
       
    68 	Uint64 value64, swapped64;
       
    69 	value64 = 0xEFBEADDE;
       
    70 	value64 <<= 32;
       
    71 	value64 |= 0xCDAB3412;
       
    72 	swapped64 = 0x1234ABCD;
       
    73 	swapped64 <<= 32;
       
    74 	swapped64 |= 0xDEADBEEF;
       
    75 #endif
       
    76 
       
    77 	if ( verbose ) {
       
    78 		printf("Detected a %s endian machine.\n",
       
    79 			(SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big");
       
    80 	}
       
    81 	if ( *((char *)&value) == '1' ) {
       
    82 		real_byteorder = SDL_BIG_ENDIAN;
       
    83 	} else {
       
    84 		real_byteorder = SDL_LIL_ENDIAN;
       
    85 	}
       
    86 	if ( real_byteorder != SDL_BYTEORDER ) {
       
    87 		if ( verbose ) {
       
    88 			printf("Actually a %s endian machine!\n",
       
    89 				(real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
       
    90 		}
       
    91 		++error;
       
    92 	}
       
    93 	if ( verbose ) {
       
    94 		printf("Value 16 = 0x%X, swapped = 0x%X\n", value16, SDL_Swap16(value16));
       
    95 	}
       
    96 	if ( SDL_Swap16(value16) != swapped16 ) {
       
    97 		if ( verbose ) {
       
    98 			printf("16 bit value swapped incorrectly!\n");
       
    99 		}
       
   100 		++error;
       
   101 	}
       
   102 	if ( verbose ) {
       
   103 		printf("Value 32 = 0x%X, swapped = 0x%X\n", value32, SDL_Swap32(value32));
       
   104 	}
       
   105 	if ( SDL_Swap32(value32) != swapped32 ) {
       
   106 		if ( verbose ) {
       
   107 			printf("32 bit value swapped incorrectly!\n");
       
   108 		}
       
   109 		++error;
       
   110 	}
       
   111 #ifdef SDL_HAS_64BIT_TYPE
       
   112 	if ( verbose ) {
       
   113 		printf("Value 64 = 0x%llX, swapped = 0x%llX\n", value64, SDL_Swap64(value64));
       
   114 	}
       
   115 	if ( SDL_Swap64(value64) != swapped64 ) {
       
   116 		if ( verbose ) {
       
   117 			printf("64 bit value swapped incorrectly!\n");
       
   118 		}
       
   119 		++error;
       
   120 	}
       
   121 #endif
       
   122 	return( error ? 1 : 0 );
       
   123 }
       
   124 
       
   125 
       
   126 int TestCPUInfo(SDL_bool verbose)
       
   127 {
       
   128 	if ( verbose ) {
       
   129 		printf("RDTSC %s\n", SDL_HasRDTSC() ? "detected" : "not detected");
       
   130 		printf("MMX %s\n", SDL_HasMMX() ? "detected" : "not detected");
       
   131 		printf("MMX Ext %s\n", SDL_HasMMXExt() ? "detected" : "not detected");
       
   132 		printf("3DNow %s\n", SDL_Has3DNow() ? "detected" : "not detected");
       
   133 		printf("3DNow Ext %s\n", SDL_Has3DNowExt() ? "detected" : "not detected");
       
   134 		printf("SSE %s\n", SDL_HasSSE() ? "detected" : "not detected");
       
   135 		printf("SSE2 %s\n", SDL_HasSSE2() ? "detected" : "not detected");
       
   136 		printf("AltiVec %s\n", SDL_HasAltiVec() ? "detected" : "not detected");
       
   137 	}
       
   138 	return(0);
       
   139 }
       
   140 
       
   141 int main(int argc, char *argv[])
       
   142 {
       
   143 	SDL_bool verbose = SDL_TRUE;
       
   144 	int status = 0;
       
   145 
       
   146 	if ( argv[1] && (SDL_strcmp(argv[1], "-q") == 0) ) {
       
   147 		verbose = SDL_FALSE;
       
   148 	}
       
   149 	if ( verbose ) {
       
   150 		printf("This system is running %s\n",
       
   151 #if __AIX__
       
   152 			"AIX"
       
   153 #elif __AMIGA__
       
   154 			"AmigaOS"
       
   155 #elif __BEOS__
       
   156 			"BeOS"
       
   157 #elif __BSDI__
       
   158 			"BSDI"
       
   159 #elif __DREAMCAST__
       
   160 			"Dreamcast"
       
   161 #elif __FREEBSD__
       
   162 			"FreeBSD"
       
   163 #elif __HPUX__
       
   164 			"HP-UX"
       
   165 #elif __IRIX__
       
   166 			"Irix"
       
   167 #elif __LINUX__
       
   168 			"Linux"
       
   169 #elif __MINT__
       
   170 			"Atari MiNT"
       
   171 #elif __MACOS__
       
   172 			"MacOS Classic"
       
   173 #elif __MACOSX__
       
   174 			"MacOS X"
       
   175 #elif __NETBSD__
       
   176 			"NetBSD"
       
   177 #elif __OPENBSD__
       
   178 			"OpenBSD"
       
   179 #elif __OS2__
       
   180 			"OS/2"
       
   181 #elif __OSF__
       
   182 			"OSF/1"
       
   183 #elif __QNXNTO__
       
   184 			"QNX Neutrino"
       
   185 #elif __RISCOS__
       
   186 			"RISC OS"
       
   187 #elif __SOLARIS__
       
   188 			"Solaris"
       
   189 #elif __WIN32__
       
   190 #ifdef _WIN32_WCE
       
   191 			"Windows CE"
       
   192 #else
       
   193 			"Windows"
       
   194 #endif
       
   195 #else
       
   196 			"an unknown operating system! (see SDL_platform.h)"
       
   197 #endif
       
   198 		);
       
   199 	}
       
   200 
       
   201 	status += TestTypes(verbose);
       
   202 	status += TestEndian(verbose);
       
   203 	status += TestCPUInfo(verbose);
       
   204 	return status;
       
   205 }