testplatform.c 5.7 KB

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