testplatform.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. SDL_Log("Value 64 = 0x%"SDL_PRIX64", swapped = 0x%"SDL_PRIX64"\n", value64,
  112. SDL_Swap64(value64));
  113. }
  114. if (SDL_Swap64(value64) != swapped64) {
  115. if (verbose) {
  116. SDL_Log("64 bit value swapped incorrectly!\n");
  117. }
  118. ++error;
  119. }
  120. return (error ? 1 : 0);
  121. }
  122. int
  123. TestCPUInfo(SDL_bool verbose)
  124. {
  125. if (verbose) {
  126. SDL_Log("CPU count: %d\n", SDL_GetCPUCount());
  127. SDL_Log("CPU cache line size: %d\n", SDL_GetCPUCacheLineSize());
  128. SDL_Log("RDTSC %s\n", SDL_HasRDTSC()? "detected" : "not detected");
  129. SDL_Log("AltiVec %s\n", SDL_HasAltiVec()? "detected" : "not detected");
  130. SDL_Log("MMX %s\n", SDL_HasMMX()? "detected" : "not detected");
  131. SDL_Log("3DNow! %s\n", SDL_Has3DNow()? "detected" : "not detected");
  132. SDL_Log("SSE %s\n", SDL_HasSSE()? "detected" : "not detected");
  133. SDL_Log("SSE2 %s\n", SDL_HasSSE2()? "detected" : "not detected");
  134. SDL_Log("SSE3 %s\n", SDL_HasSSE3()? "detected" : "not detected");
  135. SDL_Log("SSE4.1 %s\n", SDL_HasSSE41()? "detected" : "not detected");
  136. SDL_Log("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
  137. SDL_Log("AVX %s\n", SDL_HasAVX()? "detected" : "not detected");
  138. SDL_Log("System RAM %d MB\n", SDL_GetSystemRAM());
  139. }
  140. return (0);
  141. }
  142. int
  143. TestAssertions(SDL_bool verbose)
  144. {
  145. SDL_assert(1);
  146. SDL_assert_release(1);
  147. SDL_assert_paranoid(1);
  148. SDL_assert(0 || 1);
  149. SDL_assert_release(0 || 1);
  150. SDL_assert_paranoid(0 || 1);
  151. #if 0 /* enable this to test assertion failures. */
  152. SDL_assert_release(1 == 2);
  153. SDL_assert_release(5 < 4);
  154. SDL_assert_release(0 && "This is a test");
  155. #endif
  156. {
  157. const SDL_assert_data *item = SDL_GetAssertionReport();
  158. while (item) {
  159. SDL_Log("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\n",
  160. item->condition, item->function, item->filename,
  161. item->linenum, item->trigger_count,
  162. item->always_ignore ? "yes" : "no");
  163. item = item->next;
  164. }
  165. }
  166. return (0);
  167. }
  168. int
  169. main(int argc, char *argv[])
  170. {
  171. SDL_bool verbose = SDL_TRUE;
  172. int status = 0;
  173. /* Enable standard application logging */
  174. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  175. if (argv[1] && (SDL_strcmp(argv[1], "-q") == 0)) {
  176. verbose = SDL_FALSE;
  177. }
  178. if (verbose) {
  179. SDL_Log("This system is running %s\n", SDL_GetPlatform());
  180. }
  181. status += TestTypes(verbose);
  182. status += TestEndian(verbose);
  183. status += TestCPUInfo(verbose);
  184. status += TestAssertions(verbose);
  185. return status;
  186. }