testplatform.c 6.0 KB

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