TestEABaseC.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. /////////////////////////////////////////////////////////////////////////////
  4. #include "TestEABase.h"
  5. #include "TestEABase.h" // Intentionally double-include the same header file, to test it.
  6. #include <EABase/eabase.h>
  7. #include <EABase/eahave.h>
  8. #if defined(_MSC_VER)
  9. #pragma warning(disable: 4296 4310 4255) // expression is always true, cast truncates const value.
  10. #endif
  11. #include <stddef.h>
  12. #include <stdio.h>
  13. #include <stdarg.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <assert.h>
  17. #include <math.h>
  18. #if defined(EA_COMPILER_MSVC) && defined(EA_PLATFORM_MICROSOFT)
  19. EA_DISABLE_ALL_VC_WARNINGS()
  20. #if defined(EA_PLATFORM_XENON)
  21. #define NOD3D
  22. #define NONET
  23. #include <Xtl.h>
  24. #else
  25. #pragma warning(disable: 28252)
  26. #pragma warning(disable: 28253)
  27. #ifndef WIN32_LEAN_AND_MEAN
  28. #define WIN32_LEAN_AND_MEAN
  29. #endif
  30. #include <Windows.h>
  31. #endif
  32. EA_RESTORE_ALL_VC_WARNINGS()
  33. #elif defined(EA_PLATFORM_ANDROID)
  34. #include <android/log.h>
  35. #endif
  36. // Some CPU/Compiler combinations don't support arbitrary alignment declarations.
  37. // In particular some ARM compilers often don't. You can use EAStdC's EAAlignment to
  38. // achieve arbitrary alignment if EA_ALIGN doesn't work.
  39. #if (EA_ALIGN_MAX_AUTOMATIC < 64)
  40. #define ALIGNMENT_AMOUNT_64 EA_ALIGN_MAX_AUTOMATIC
  41. #else
  42. #define ALIGNMENT_AMOUNT_64 64
  43. #endif
  44. #if (EA_ALIGN_MAX_AUTOMATIC < 32)
  45. #define ALIGNMENT_AMOUNT_32 EA_ALIGN_MAX_AUTOMATIC
  46. #else
  47. #define ALIGNMENT_AMOUNT_32 32
  48. #endif
  49. #if (EA_ALIGN_MAX_AUTOMATIC < 16)
  50. #define ALIGNMENT_AMOUNT_16 EA_ALIGN_MAX_AUTOMATIC
  51. #else
  52. #define ALIGNMENT_AMOUNT_16 16
  53. #endif
  54. #if defined(__cplusplus)
  55. struct ClassWithDefaultCtor
  56. {
  57. ClassWithDefaultCtor(int x = 0)
  58. { char buffer[16]; sprintf(buffer, "%d", x); }
  59. };
  60. struct ClassWithoutDefaultCtor
  61. {
  62. ClassWithoutDefaultCtor(int x)
  63. { char buffer[16]; sprintf(buffer, "%d", x); }
  64. };
  65. #endif
  66. // Forward declarations
  67. int Stricmp(const char* pString1, const char* pString2);
  68. int TestEABase(void);
  69. int TestEAResult(void);
  70. int TestEAPlatform(void);
  71. int TestNU(void);
  72. int TestEACompiler(void);
  73. int TestEACompilerTraits(void);
  74. int Verify(int bTest, const char* pMessage);
  75. #define DoError(nErrorCount, pMessage) DoErrorC(&nErrorCount, pMessage)
  76. void DoErrorC(int* nErrorCount, const char* pMessage);
  77. /* Test EA_DISABLE_WARNING */
  78. EA_DISABLE_VC_WARNING(4548 4127)
  79. EA_DISABLE_ALL_VC_WARNINGS()
  80. EA_RESTORE_ALL_VC_WARNINGS()
  81. EA_DISABLE_GCC_WARNING(-Wuninitialized)
  82. EA_DISABLE_SN_WARNING(1787)
  83. EA_DISABLE_ALL_SN_WARNINGS()
  84. EA_RESTORE_ALL_SN_WARNINGS()
  85. EA_DISABLE_GHS_WARNING(123)
  86. EA_DISABLE_EDG_WARNING(193)
  87. EA_DISABLE_CW_WARNING(10317)
  88. EA_DISABLE_ALL_CW_WARNINGS()
  89. EA_RESTORE_ALL_CW_WARNINGS()
  90. static void Printf(const char8_t* pFormat, ...)
  91. {
  92. #if defined(_MSC_VER)
  93. #define vsnprintf _vsnprintf
  94. #endif
  95. if(pFormat)
  96. {
  97. char pMessage[512];
  98. int nReturnValue;
  99. va_list arguments;
  100. va_start(arguments, pFormat);
  101. nReturnValue = vsnprintf(pMessage, EAArrayCount(pMessage), pFormat, arguments);
  102. va_end(arguments);
  103. if(nReturnValue > 0)
  104. {
  105. #if defined(EA_PLATFORM_ANDROID)
  106. __android_log_write(ANDROID_LOG_INFO, "EABase.Printf", pMessage);
  107. #else
  108. fputs(pMessage, stdout);
  109. #endif
  110. #if defined(EA_COMPILER_MSVC) && defined(EA_PLATFORM_MICROSOFT)
  111. OutputDebugStringA(pMessage);
  112. #endif
  113. }
  114. }
  115. }
  116. /* Test EA_DISABLE_WARNING */
  117. EA_RESTORE_VC_WARNING()
  118. EA_RESTORE_GCC_WARNING()
  119. EA_RESTORE_SN_WARNING()
  120. EA_RESTORE_GHS_WARNING()
  121. EA_RESTORE_EDG_WARNING()
  122. EA_RESTORE_CW_WARNING(10317)
  123. int Verify(int bTest, const char* pMessage)
  124. {
  125. if(!bTest)
  126. {
  127. if(pMessage)
  128. Printf("Test warning: %s\n", pMessage);
  129. }
  130. return bTest ? 0 : 1;
  131. }
  132. void DoErrorC(int* nErrorCount, const char* pMessage)
  133. {
  134. ++*nErrorCount;
  135. if(pMessage)
  136. Printf("Test error: %s\n", pMessage);
  137. }
  138. int Stricmp(const char* pString1, const char* pString2)
  139. {
  140. char c1, c2;
  141. while((c1 = (char)tolower((int)*pString1++)) == (c2 = (char)tolower((int)*pString2++)))
  142. {
  143. if(c1 == 0)
  144. return 0;
  145. }
  146. return (c1 - c2);
  147. }
  148. // GCC requires that function attributes be declared in the function
  149. // declaration and not in the function definition.
  150. // RVCT seems to require attributes to be in both the declaration
  151. // and definition otherwise you get a "declaration is incompatible" error
  152. int PureFunction(void) EA_PURE;
  153. int PureFunction(void)
  154. #if defined(EA_COMPILER_RVCT)
  155. EA_PURE
  156. #endif
  157. {
  158. return (strlen("abc") == 3);
  159. }
  160. struct InitPriorityTestClass
  161. {
  162. int mX;
  163. };
  164. // EA_MAY_ALIAS
  165. void* EA_MAY_ALIAS gPtr0 = NULL;
  166. typedef void* EA_MAY_ALIAS pvoid_may_alias;
  167. pvoid_may_alias gPtr1 = NULL;
  168. // static_asset at global scope
  169. // Should succeed.
  170. static_assert(sizeof(int32_t) == 4, "static_assert failure");
  171. // Should fail.
  172. //static_assert(sizeof(int32_t) == 8, "static_assert failure");
  173. static size_t RestrictTest(char* EA_RESTRICT p)
  174. {
  175. return sizeof(p);
  176. }
  177. // EA_OPTIMIZE_OFF / EA_OPTIMIZE_ON
  178. EA_OPTIMIZE_OFF()
  179. static EA_NO_INLINE int DisabledOptimizations(int x)
  180. {
  181. return x * 37;
  182. }
  183. EA_OPTIMIZE_ON()
  184. // EA_UNUSED
  185. static void FunctionWithUnusedVariables(int x)
  186. {
  187. int y = 0;
  188. EA_UNUSED(x);
  189. EA_UNUSED(y);
  190. }
  191. int TestEABase(void)
  192. {
  193. int nErrorCount = 0;
  194. // Test NULL
  195. {
  196. EA_DISABLE_VC_WARNING(6326)
  197. Verify(NULL == (void*)0, "unspecified test");
  198. EA_RESTORE_VC_WARNING()
  199. }
  200. // Verify sized type sizes
  201. {
  202. const ssize_t ss = 1; // Verify that ssize_t is a signed type.
  203. Verify(sizeof(int8_t) == 1, "int8_t size test");
  204. Verify(sizeof(uint8_t) == 1, "uint8_t size test");
  205. Verify(sizeof(int16_t) == 2, "int16_t size test");
  206. Verify(sizeof(uint16_t) == 2, "uint16_t size test");
  207. Verify(sizeof(int32_t) == 4, "int32_t size test");
  208. Verify(sizeof(uint32_t) == 4, "uint32_t size test");
  209. Verify(sizeof(int64_t) == 8, "int64_t size test");
  210. Verify(sizeof(uint64_t) == 8, "uint64_t size test");
  211. #if !defined(FLT_EVAL_METHOD)
  212. #error EABase should always define FLT_EVAL_METHOD
  213. Verify(0, "FLT_EVAL_METHOD test: not defined.");
  214. #else
  215. #if !defined(__GNUC__) || defined(__USE_ISOC99) // GCC doesn't define float_t/double_t unless __USE_ISOC99 is defined (compiled with -std=c99)
  216. #if (FLT_EVAL_METHOD == -1)
  217. // In this case the C99 standard states that the
  218. // precision of float_t and double_t is indeterminable.
  219. #elif (FLT_EVAL_METHOD == 0)
  220. Verify(sizeof(float_t) == sizeof(float), "float_t size test");
  221. Verify(sizeof(double_t) == sizeof(double), "double_t size test");
  222. #elif (FLT_EVAL_METHOD == 1)
  223. Verify(sizeof(float_t) == sizeof(double), "float_t size test");
  224. Verify(sizeof(double_t) == sizeof(double), "double_t size test");
  225. #elif (FLT_EVAL_METHOD == 2)
  226. Verify(sizeof(float_t) == sizeof(long double), "float_t size test");
  227. Verify(sizeof(double_t) == sizeof(long double), "double_t size test");
  228. #else
  229. DoError(nErrorCount, "FLT_EVAL_METHOD test: invalid value.");
  230. #endif
  231. #endif
  232. #endif
  233. Verify(sizeof(bool8_t) == 1, "bool8_t size test");
  234. Verify(sizeof(intptr_t) == sizeof(void*), "intptr_t size test");
  235. Verify(sizeof(uintptr_t) == sizeof(void*), "uintptr_t size test");
  236. Verify(sizeof(ssize_t) == sizeof(size_t), "ssize_t size test");
  237. Verify((ssize_t)((ss ^ ss) - 1) < 0, "ssize_t sign test");
  238. Verify(sizeof(char8_t) == 1, "char8_t size test");
  239. Verify(sizeof(char16_t) == 2, "char16_t size test");
  240. Verify(sizeof(char32_t) == 4, "char32_t test");
  241. #if (EA_WCHAR_SIZE == 2) || (EA_WCHAR_SIZE == 4)
  242. Verify(sizeof(wchar_t) == EA_WCHAR_SIZE, "EA_WCHAR_SIZE test");
  243. #else
  244. Verify(0, "EA_WCHAR_SIZE test");
  245. #endif
  246. }
  247. // Test char8_t, char16_t, char32_t string literals.
  248. {
  249. const char8_t* p8 = "abc";
  250. const char8_t c8 = 'a';
  251. #ifdef EA_CHAR16
  252. const char16_t* p16 = EA_CHAR16("abc"); // Under GCC, this assumes compiling with -fshort-wchar
  253. const char16_t c16 = EA_CHAR16('\x3001');
  254. #else
  255. const char16_t* p16 = NULL;
  256. const char16_t c16 = (char16_t)'X';
  257. #endif
  258. #if EA_CHAR16_NATIVE
  259. const char32_t* p32 = EA_CHAR32("abc");
  260. const char32_t c32 = EA_CHAR32('\x3001');
  261. #else
  262. const char32_t p32[] = { 'a', 'b', 'c', '\0' }; // Microsoft doesn't support 32 bit strings here, and GCC doesn't use them when we compile with -fshort-wchar (which we do).
  263. #ifdef EA_CHAR16
  264. const char32_t c32 = EA_CHAR16('\x3001'); // 16 bit should silently convert to 32 bit.
  265. #else
  266. const char32_t c32 = (char16_t)'X'; // 16 bit should silently convert to 32 bit.
  267. #endif
  268. #endif
  269. char buffer[128];
  270. sprintf(buffer, "%p %p %p %p %p %p", &p8, &c8, &p16, &c16, &p32, &c32); // Make possible compiler warnings about unused variables go away.
  271. }
  272. #if 0
  273. // Verify sized type signs
  274. {
  275. int8_t i8(1);
  276. if(int8_t((i8 ^ i8) - 1) >= 0)
  277. DoError(nErrorCount, "int8_t sign test");
  278. uint8_t u8(1);
  279. if(uint8_t((u8 ^ u8) - 1) <= 0)
  280. DoError(nErrorCount, "uint8_t sign test");
  281. int16_t i16(1);
  282. if(int16_t((i16 ^ i16) - 1) >= 0)
  283. DoError(nErrorCount, "int16_t sign test");
  284. uint16_t u16(1);
  285. if(uint16_t((u16 ^ u16) - 1) <= 0)
  286. DoError(nErrorCount, "uint16_t sign test");
  287. int32_t i32(1);
  288. if(int32_t((i32 ^ i32) - 1) >= 0)
  289. DoError(nErrorCount, "int32_t sign test");
  290. uint32_t u32(1);
  291. if(uint32_t((u32 ^ u32) - 1) <= 0)
  292. DoError(nErrorCount, "uint32_t sign test");
  293. int64_t i64(1);
  294. if(int64_t((i64 ^ i64) - 1) >= 0)
  295. DoError(nErrorCount, "int64_t sign test");
  296. uint64_t u64(1);
  297. if(uint64_t((u64 ^ u64) - 1) <= 0)
  298. DoError(nErrorCount, "uint64_t sign test");
  299. intptr_t ip(1);
  300. if(intptr_t((ip ^ ip) - 1) >= 0)
  301. DoError(nErrorCount, "intptr_t sign test");
  302. uintptr_t up(1);
  303. if(uintptr_t((up ^ up) - 1) <= 0)
  304. DoError(nErrorCount, "uintptr_t sign test");
  305. // The following sign tests have been disabled, as the determination of
  306. // the sign of type char and wchar_t are in the hands of the compiler and
  307. // the user's configuration of that compiler.
  308. //char8_t c8(1); // We expect it to be signed, though the need for such a requirement is debateable.
  309. //if(char8_t((c8 ^ c8) - 1) >= 0)
  310. // DoError(nErrorCount, "char8_t sign test");
  311. //char16_t c16(1); // We expect it to be unsigned
  312. //if(char16_t((c16 ^ c16) - 1) <= 0)
  313. // DoError(nErrorCount, "char16_t sign test");
  314. //char32_t c32(1); // We expect it to be unsigned
  315. //if(char32_t((c32 ^ c32) - 1) <= 0)
  316. // DoError(nErrorCount, "char32_t sign test");
  317. }
  318. //Test Constant macros
  319. {
  320. char buffer[256];
  321. const int8_t i8Min = INT8_C(-128); // Strictly speaking, the C language standard allows this to be -127 as well.
  322. const int8_t i8Max = INT8_C(127);
  323. const uint8_t u8Min = UINT8_C(0);
  324. const uint8_t u8Max = UINT8_C(255);
  325. const int16_t i16Min = INT16_C(-32767) - 1;
  326. const int16_t i16Max = INT16_C( 32767);
  327. const uint16_t u16Min = UINT16_C(0);
  328. const uint16_t u16Max = UINT16_C(65535);
  329. const int32_t i32Min = INT32_C(-2147483647) - 1;
  330. const int32_t i32Max = INT32_C( 2147483647);
  331. const uint32_t u32Min = UINT32_C(0);
  332. const uint32_t u32Max = UINT32_C(4294967295);
  333. #if defined(__GNUC__) && (__GNUC__ < 4) // If using a broken version of UINT64_C/INT64_C macros...
  334. const int64_t i64Min = -9223372036854775807LL - 1;
  335. const int64_t i64Max = 9223372036854775807LL;
  336. const uint64_t u64Min = UINT64_C(0);
  337. const uint64_t u64Max = 18446744073709551615ULL;
  338. #else
  339. const int64_t i64Min = INT64_C(-9223372036854775807) - 1;
  340. const int64_t i64Max = INT64_C( 9223372036854775807);
  341. const uint64_t u64Min = UINT64_C(0);
  342. const uint64_t u64Max = UINT64_C(18446744073709551615);
  343. #endif
  344. sprintf(buffer, "%d %d %u %u %d %d %u %u %d %d %u %u %"SCNd64" %"SCNd64" %"SCNu64" %"SCNu64,
  345. (int)i8Min, (int)i8Max, (unsigned)u8Min, (unsigned)u8Max,
  346. (int)i16Min, (int)i16Max, (unsigned)u16Min, (unsigned)u16Max,
  347. (int)i32Min, (int)i32Max, (unsigned)u32Min, (unsigned)u32Max,
  348. i64Min, i64Max, u64Min, u64Max);
  349. if(strcmp(buffer, "-128 127 0 255 -32768 32767 0 65535 -2147483648 2147483647 0 4294967295 -9223372036854775808 9223372036854775807 0 18446744073709551615"))
  350. DoError(nErrorCount, "INT_C test");
  351. // Verify the use of hex numbers with INT64_C
  352. const int64_t i64Hex = INT64_C(0x1111111122222222);
  353. if(i64Hex != INT64_C(1229782938533634594))
  354. DoError(nErrorCount, "INT64_C hex error");
  355. // Verify the use of hex numbers with UINT64_C
  356. const uint64_t u64Hex = UINT64_C(0xaaaaaaaabbbbbbbb);
  357. #if defined(__GNUC__) && (__GNUC__ < 4) // If using a broken version of UINT64_C/INT64_C macros...
  358. const uint64_t temp = 12297829382759365563ULL;
  359. #else
  360. const uint64_t temp = UINT64_C(12297829382759365563);
  361. #endif
  362. if(u64Hex != temp)
  363. DoError(nErrorCount, "UINT64_C hex error");
  364. // Verify that the compiler both allows division with uint64_t but
  365. // also that it allows it via UINT64_MAX. A bad implementation of
  366. // UINT64_MAX would cause the code below to mis-execute or not compile.
  367. const uint64_t resultUint64 = UINT64_MAX / 2;
  368. if(resultUint64 != UINT64_C(9223372036854775807))
  369. DoError(nErrorCount, "UINT64_MAX error");
  370. }
  371. //Test sized Printf format specifiers
  372. {
  373. char buffer[256];
  374. int8_t d8(INT8_MAX), i8(INT8_MIN), o8(INT8_MAX);
  375. uint8_t u8(UINT8_MAX), x8(UINT8_MAX), X8(UINT8_MAX);
  376. sprintf(buffer, "%" PRId8 " %" PRIi8 " %" PRIo8 " %" PRIu8 " %" PRIx8 " %" PRIX8, d8, i8, o8, u8, x8, X8);
  377. #ifdef EA_COMPILER_GNUC
  378. if(Stricmp(buffer, "127 -128 177 255 ff FF"))
  379. DoError(nErrorCount, "PRI8 test"); // This is known to fail with compilers such as VC++ which don't support %hh.
  380. #endif
  381. int16_t d16(INT16_MAX), i16(INT16_MIN), o16(INT16_MAX);
  382. uint16_t u16(UINT16_MAX), x16(UINT16_MAX), X16(UINT16_MAX);
  383. sprintf(buffer, "%" PRId16 " %" PRIi16 " %" PRIo16 " %" PRIu16 " %" PRIx16 " %" PRIX16, d16, i16, o16, u16, x16, X16);
  384. if(Stricmp(buffer, "32767 -32768 77777 65535 ffff FFFF"))
  385. DoError(nErrorCount, "PRI16 test");
  386. int32_t d32(INT32_MAX), i32(INT32_MIN), o32(INT32_MAX);
  387. uint32_t u32(UINT32_MAX), x32(UINT32_MAX), X32(UINT32_MAX);
  388. sprintf(buffer, "%" PRId32 " %" PRIi32 " %" PRIo32 " %" PRIu32 " %" PRIx32 " %" PRIX32, d32, i32, o32, u32, x32, X32);
  389. if(Stricmp(buffer, "2147483647 -2147483648 17777777777 4294967295 ffffffff FFFFFFFF"))
  390. DoError(nErrorCount, "PRI32 test");
  391. int64_t d64(INT64_MAX), i64(INT64_MIN), o64(INT64_MAX);
  392. uint64_t u64(UINT64_MAX), x64(UINT64_MAX), X64(UINT64_MAX);
  393. sprintf(buffer, "%" PRId64 " %" PRIi64 " %" PRIo64 " %" PRIu64 " %" PRIx64 " %" PRIX64, d64, i64, o64, u64, x64, X64);
  394. if(Stricmp(buffer, "9223372036854775807 -9223372036854775808 777777777777777777777 18446744073709551615 ffffffffffffffff FFFFFFFFFFFFFFFF"))
  395. DoError(nErrorCount, "PRI64 test");
  396. // Many compilers give warnings for the following code because they
  397. // recognize that a pointer is being formatted as an integer.
  398. // This is what we want to do and what the C99 standard intends here.
  399. #if defined(_MSC_VER) && (_MSC_VER >= 1300)
  400. #pragma warning(disable: 4313) // Warning C4313: 'sprintf' : '%d' in format string conflicts with argument 1 of type 'void *'
  401. #endif
  402. void *dPtr = (void*)INT32_MAX, *iPtr = (void*)INT32_MIN, *oPtr = (void*)INT32_MAX, *uPtr = (void*)(uintptr_t)UINT64_MAX, *xPtr = (void*)(uintptr_t)UINT64_MAX, *XPtr = (void*)(uintptr_t)UINT64_MAX;
  403. sprintf(buffer, "%" PRIdPTR " %" PRIiPTR " %" PRIoPTR " %" PRIuPTR " %" PRIxPTR " %" PRIXPTR, (intptr_t)dPtr, (intptr_t)iPtr, (uintptr_t)oPtr, (uintptr_t)uPtr, (uintptr_t)xPtr, (uintptr_t)XPtr);
  404. #if (EA_PLATFORM_PTR_SIZE == 4)
  405. if(Stricmp(buffer, "2147483647 -2147483648 17777777777 4294967295 ffffffff FFFFFFFF"))
  406. DoError(nErrorCount, "PRIPTR test");
  407. #else // EA_PLATFORM_PTR_SIZE == 8
  408. if(Stricmp(buffer, "2147483647 -2147483648 17777777777 18446744073709551615 ffffffffffffffff FFFFFFFFFFFFFFFF"))
  409. DoError(nErrorCount, "PRIPTR test");
  410. #endif
  411. #if defined(_MSC_VER) && (_MSC_VER >= 1300)
  412. #pragma warning(default: 4313)
  413. #endif
  414. }
  415. //Test sized scanf format specifiers
  416. {
  417. #ifdef EA_COMPILER_IS_C99 // Enabled for C99 only because this code will simply crash on many platforms if the format specifiers aren't supported.
  418. int8_t d8, i8, o8;
  419. uint8_t u8, x8;
  420. sscanf("127 -127 177 255 ff", "%"SCNd8 " %"SCNi8 " %"SCNo8 " %"SCNu8 " %"SCNx8, &d8, &i8, &o8, &u8, &x8);
  421. if((d8 != 127) || (i8 != -127) || (o8 != 127) || (u8 != 255) || (x8 != 255))
  422. DoError(nErrorCount, "SCN8 test"); // This is known to fail with compilers such as VC++ which don't support %hh.
  423. #endif
  424. int16_t d16, i16, o16;
  425. uint16_t u16, x16;
  426. sscanf("32767 -32768 77777 65535 ffff", "%"SCNd16 " %"SCNi16 " %"SCNo16 " %"SCNu16 " %"SCNx16, &d16, &i16, &o16, &u16, &x16);
  427. if((d16 != 32767) || (i16 != -32768) || (o16 != 32767) || (u16 != 65535) || (x16 != 65535))
  428. DoError(nErrorCount, "SCN16 test");
  429. int32_t d32, i32, o32;
  430. uint32_t u32, x32;
  431. sscanf("2147483647 -2147483648 17777777777 4294967295 ffffffff", "%"SCNd32 " %"SCNi32 " %"SCNo32 " %"SCNu32 " %"SCNx32, &d32, &i32, &o32, &u32, &x32);
  432. if((d32 != INT32_MAX) || (i32 != INT32_MIN) || (o32 != INT32_MAX) || (u32 != UINT32_MAX) || (x32 != UINT32_MAX))
  433. DoError(nErrorCount, "SCN32 test");
  434. int64_t d64, i64, o64;
  435. uint64_t u64, x64;
  436. sscanf("9223372036854775807 -9223372036854775808 777777777777777777777 18446744073709551615 ffffffffffffffff", "%"SCNd64 " %"SCNi64 " %"SCNo64 " %"SCNu64 " %"SCNx64, &d64, &i64, &o64, &u64, &x64);
  437. if((d64 != INT64_MAX) || (i64 != INT64_MIN) || (o64 != INT64_MAX) || (u64 != UINT64_MAX) || (x64 != UINT64_MAX))
  438. DoError(nErrorCount, "SCN64 test");
  439. // Many compilers give warnings for the following code because they
  440. // recognize that a pointer is being formatted as an integer.
  441. // This is what we want to do and what the C99 standard intends here.
  442. void *dPtr, *iPtr, *oPtr, *uPtr, *xPtr;
  443. #if (EA_PLATFORM_PTR_SIZE == 4)
  444. sscanf("2147483647 -2147483648 17777777777 4294967295 ffffffff", "%"SCNdPTR " %"SCNiPTR " %"SCNoPTR " %"SCNuPTR " %"SCNxPTR, (intptr_t*)&dPtr, (intptr_t*)&iPtr, (uintptr_t*)&oPtr, (uintptr_t*)&uPtr, (uintptr_t*)&xPtr);
  445. #else // EA_PLATFORM_PTR_SIZE == 8
  446. sscanf("2147483647 -2147483648 17777777777 18446744073709551615 ffffffffffffffff", "%"SCNdPTR " %"SCNiPTR " %"SCNoPTR " %"SCNuPTR " %"SCNxPTR, (intptr_t*)&dPtr,(intptr_t*)&iPtr, (uintptr_t*)&oPtr, (uintptr_t*)&uPtr, (uintptr_t*)&xPtr);
  447. #endif
  448. if((dPtr != (void*)INT32_MAX) || (iPtr != (void*)INT32_MIN) || (oPtr != (void*)INT32_MAX) || (uPtr != (void*)(uintptr_t)UINT64_MAX) || (xPtr != (void*)(uintptr_t)UINT64_MAX))
  449. DoError(nErrorCount, "SCNPTR test");
  450. }
  451. // Test min/max
  452. {
  453. // The C standard allows INT8_MIN to be either -127 or -128. So in order to be able
  454. // to test for this in a portable way, we do the logic below whereby we test for
  455. // -127 (which all compiles should support) or -127 - 1 which all compilers should
  456. // support if INT8_MIN isn't -127.
  457. if(!Verify(INT8_MIN == INT8_C(-127)) && !Verify(INT8_MIN == INT8_C(-127) - 1))
  458. DoError(nErrorCount, "INT8_MIN test");
  459. if(!Verify(INT8_MAX == INT8_C(127)))
  460. DoError(nErrorCount, "INT8_MAX test");
  461. if(!Verify(UINT8_MAX == UINT8_C(255)))
  462. DoError(nErrorCount, "UINT8_MAX test");
  463. if(!Verify(INT16_MIN == INT16_C(-32767)) && !Verify(INT16_MIN == INT16_C(-32767) - 1))
  464. DoError(nErrorCount, "INT16_MIN test");
  465. if(!Verify(INT16_MAX == INT16_C(32767)))
  466. DoError(nErrorCount, "INT16_MAX test");
  467. if(!Verify(UINT16_MAX == UINT16_C(65535)))
  468. DoError(nErrorCount, "UINT16_MAX test");
  469. if(!Verify(INT32_MIN == INT32_C(-2147483647)) && !Verify(INT32_MIN == INT32_C(-2147483647) - 1))
  470. DoError(nErrorCount, "INT32_MIN test");
  471. if(!Verify(INT32_MAX == INT32_C(2147483647)))
  472. DoError(nErrorCount, "INT32_MAX test");
  473. if(!Verify(UINT32_MAX == UINT32_C(4294967295)))
  474. DoError(nErrorCount, "UINT32_MAX test");
  475. if(!Verify(INT64_MIN == INT64_C(-9223372036854775807)) && !Verify(INT64_MIN == INT64_C(-9223372036854775807) - 1))
  476. DoError(nErrorCount, "INT64_MIN test");
  477. if(!Verify(INT64_MAX == INT64_C(9223372036854775807)))
  478. DoError(nErrorCount, "INT64_MAX test");
  479. #if defined(__GNUC__) && (__GNUC__ < 4) // If using a broken version of UINT64_C/INT64_C macros...
  480. const uint64_t temp = 18446744073709551615ULL;
  481. #else
  482. const uint64_t temp = UINT64_C(18446744073709551615);
  483. #endif
  484. if(!Verify(UINT64_MAX == temp))
  485. DoError(nErrorCount, "UINT64_MAX test");
  486. }
  487. #endif
  488. return nErrorCount;
  489. }
  490. int TestEAPlatform(void)
  491. {
  492. int nErrorCount = 0;
  493. // Test EA_PLATFORM_PTR_SIZE
  494. {
  495. #ifdef EA_PLATFORM_PTR_SIZE
  496. Verify(EA_PLATFORM_PTR_SIZE == sizeof(void*), "EA_PLATFORM_PTR_SIZE test");
  497. #else
  498. DoError(nErrorCount, "EA_PLATFORM_PTR_SIZE test");
  499. #endif
  500. }
  501. // Test EA_PLATFORM_NAME
  502. {
  503. #ifdef EA_PLATFORM_NAME
  504. char buffer[256];
  505. sprintf(buffer, "TestEAPlatform: EA_PLATFORM_NAME: %s\n", EA_PLATFORM_NAME);
  506. #else
  507. DoError(nErrorCount, "EA_PLATFORM_NAME test");
  508. #endif
  509. }
  510. // Test EA_PLATFORM_DESCRIPTION
  511. {
  512. #ifdef EA_PLATFORM_DESCRIPTION
  513. char buffer[256];
  514. sprintf(buffer, "TestEAPlatform: EA_PLATFORM_DESCRIPTION: %s\n", EA_PLATFORM_DESCRIPTION);
  515. #else
  516. DoError(nErrorCount, "EA_PLATFORM_DESCRIPTION test");
  517. #endif
  518. }
  519. // Test EA_SYSTEM_LITTLE_ENDIAN / EA_SYSTEM_BIG_ENDIAN
  520. {
  521. uint32_t kValue = 0x12345678;
  522. uint8_t* pValue = (uint8_t*)&kValue;
  523. #ifdef EA_SYSTEM_LITTLE_ENDIAN
  524. Verify(pValue[0] == 0x78, "EA_SYSTEM_ENDIAN test");
  525. #elif defined(EA_SYSTEM_BIG_ENDIAN)
  526. Verify(pValue[0] == 0x12, "EA_SYSTEM_ENDIAN test");
  527. #else
  528. Verify(0, "EA_SYSTEM_ENDIAN test");
  529. #endif
  530. }
  531. // Test EA_ASM_STYLE
  532. {
  533. #if defined(EA_PROCESSOR_X86)
  534. #if defined(EA_ASM_STYLE_ATT)
  535. asm volatile ("nop");
  536. #elif defined(EA_ASM_STYLE_INTEL)
  537. __asm nop
  538. #endif
  539. #else
  540. // Add other processors here.
  541. #endif
  542. }
  543. return nErrorCount;
  544. }
  545. // Test compiler limitations
  546. // Easiest way to come up with tests for some of the more complicated versions
  547. // of these is to look at the Boost /libs/config/test/*.cxx files. Many of the
  548. // Boost compiler limitation defines are similar or match exactly to those
  549. // defined by EABase. See http://www.boost.org if you want to check this out.
  550. #ifndef EA_COMPILER_NO_STATIC_CONSTANTS // If class member static constants are allowed...
  551. // Todo
  552. #endif
  553. #ifndef EA_COMPILER_NO_TEMPLATE_SPECIALIZATION
  554. // Todo
  555. #endif
  556. #ifndef EA_COMPILER_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  557. // Todo
  558. #endif
  559. #ifndef EA_COMPILER_NO_MEMBER_TEMPLATES
  560. // Todo
  561. #endif
  562. #ifndef EA_COMPILER_NO_MEMBER_TEMPLATE_SPECIALIZATION
  563. // Todo
  564. #endif
  565. #ifndef EA_COMPILER_NO_TEMPLATE_TEMPLATES
  566. // Todo
  567. #endif
  568. #ifndef EA_COMPILER_NO_MEMBER_TEMPLATE_FRIENDS
  569. // Todo
  570. #endif
  571. #ifndef EA_COMPILER_NO_VOID_RETURNS
  572. #endif
  573. #ifndef EA_COMPILER_NO_COVARIANT_RETURN_TYPE
  574. // Todo
  575. #endif
  576. #ifndef EA_COMPILER_NO_DEDUCED_TYPENAME
  577. // Todo
  578. #endif
  579. #ifndef EA_COMPILER_NO_ARGUMENT_DEPENDENT_LOOKUP
  580. // Todo
  581. #endif
  582. // Not applicable to C:
  583. //#ifndef EA_COMPILER_NO_STANDARD_CPP_LIBRARY
  584. // #include <vector>
  585. //#endif
  586. #ifndef EA_COMPILER_NO_COVARIANT_RETURN_TYPE
  587. // Todo
  588. #endif
  589. #ifndef EA_COMPILER_NO_COVARIANT_RETURN_TYPE
  590. // Todo
  591. #endif
  592. #ifndef EA_COMPILER_NO_VARIADIC_MACROS
  593. #define MY_PRINTF(format, ...) Printf(format, __VA_ARGS__)
  594. #endif
  595. int TestEACompiler(void)
  596. {
  597. int nErrorCount = 0;
  598. #if 0
  599. // As of this writing, eacompiler.h defines at least the following compilers:
  600. // EA_COMPILER_GNUC
  601. // EA_COMPILER_BORLANDC
  602. // EA_COMPILER_INTEL
  603. // EA_COMPILER_METROWERKS
  604. // EA_COMPILER_MSVC, EA_COMPILER_MSVC6, EA_COMPILER_MSVC7, EA_COMPILER_MSVC7_1
  605. // Test EA_COMPILER_NAME
  606. {
  607. #ifdef EA_COMPILER_NAME
  608. char buffer[256];
  609. sprintf(buffer, "TestEACompiler: EA_COMPILER_NAME: %s\n", EA_COMPILER_NAME);
  610. #else
  611. DoError(nErrorCount, "EA_COMPILER_NAME test");
  612. #endif
  613. }
  614. // Test EA_COMPILER_VERSION
  615. {
  616. #ifdef EA_COMPILER_VERSION
  617. char buffer[256];
  618. sprintf(buffer, "TestEACompiler: EA_COMPILER_VERSION: %d\n", EA_COMPILER_VERSION);
  619. #else
  620. DoError(nErrorCount, "EA_COMPILER_VERSION test");
  621. #endif
  622. }
  623. // Test EA_COMPILER_STRING
  624. {
  625. #ifdef EA_COMPILER_STRING
  626. char buffer[256];
  627. sprintf(buffer, "TestEACompiler: EA_COMPILER_STRING: %s\n", EA_COMPILER_STRING);
  628. #else
  629. DoError(nErrorCount, "EA_COMPILER_STRING test");
  630. #endif
  631. }
  632. // Test EA_COMPILER_NO_STATIC_CONSTANTS
  633. {
  634. char buffer[256];
  635. sprintf(buffer, "%d", (int)NSC::x);
  636. if(buffer[0] != '1')
  637. DoError(nErrorCount, "EA_COMPILER_NO_STATIC_CONSTANTS test");
  638. }
  639. // Test EA_COMPILER_NO_VOID_RETURNS
  640. #ifndef EA_COMPILER_NO_VOID_RETURNS
  641. TestNVR1(); // Nothing to test for except successful compilation.
  642. #endif
  643. // Test EA_COMPILER_NO_EXCEPTION_STD_NAMESPACE
  644. #ifndef EA_COMPILER_NO_EXCEPTION_STD_NAMESPACE
  645. TestNESN();
  646. #endif
  647. // Test EA_COMPILER_NO_EXCEPTIONS
  648. #ifndef EA_COMPILER_NO_EXCEPTIONS
  649. if(!TestNE())
  650. DoError(nErrorCount, "EA_COMPILER_NO_EXCEPTIONS test");
  651. #endif
  652. // Test EA_COMPILER_NO_UNWIND
  653. if(!TestNU())
  654. DoError(nErrorCount, "EA_COMPILER_NO_UNWIND test");
  655. #endif
  656. return nErrorCount;
  657. }
  658. int TestEACompilerTraits(void)
  659. {
  660. int nErrorCount = 0;
  661. // EA_COMPILER_IS_ANSIC
  662. // EA_COMPILER_IS_C99
  663. // EA_COMPILER_IS_CPLUSPLUS
  664. // EA_COMPILER_MANAGED_CPP
  665. { // Test EA_ALIGN_OF
  666. typedef void (*AlignTestFunctionType)(void);
  667. if(EA_ALIGN_OF(AlignTestFunctionType) != sizeof(void*)) // This may not be a kosher test.
  668. DoError(nErrorCount, "EA_ALIGN_OF test (AlignTestFunctionType)");
  669. if(EA_ALIGN_OF(int8_t) != sizeof(int8_t)) // This may not be a kosher test.
  670. DoError(nErrorCount, "EA_ALIGN_OF test (int16_t)");
  671. if(EA_ALIGN_OF(int16_t) != sizeof(int16_t)) // This may not be a kosher test.
  672. DoError(nErrorCount, "EA_ALIGN_OF test (int16_t)");
  673. if(EA_ALIGN_OF(int32_t) != sizeof(int32_t)) // This may not be a kosher test.
  674. DoError(nErrorCount, "EA_ALIGN_OF test (int32_t)");
  675. #if !defined (EA_ABI_ARM_APPLE)
  676. if(EA_ALIGN_OF(int64_t) != sizeof(int64_t)) // This may not be a kosher test.
  677. DoError(nErrorCount, "EA_ALIGN_OF test (int64_t)");
  678. #endif
  679. }
  680. { // Test EA_PREFIX_ALIGN
  681. #ifdef EA_PREFIX_ALIGN
  682. char buffer[32];
  683. EA_PREFIX_ALIGN(64) int x = 0;
  684. sprintf(buffer, "%d", x);
  685. if(buffer[0] != '0')
  686. DoError(nErrorCount, "EA_PREFIX_ALIGN test");
  687. #if defined(__cplusplus)
  688. EA_PREFIX_ALIGN(64) ClassWithDefaultCtor cdcA;
  689. //EA_PREFIX_ALIGN(64) ClassWithoutDefaultCtor cwdcA;
  690. EA_PREFIX_ALIGN(64) ClassWithDefaultCtor cdcB(3);
  691. EA_PREFIX_ALIGN(64) ClassWithoutDefaultCtor cwdcB(3);
  692. #endif
  693. #else
  694. DoError(nErrorCount, "EA_PREFIX_ALIGN test");
  695. #endif
  696. }
  697. { // Test EA_POSTFIX_ALIGN
  698. #ifdef EA_POSTFIX_ALIGN
  699. char buffer[32];
  700. int x EA_POSTFIX_ALIGN(ALIGNMENT_AMOUNT_64) = 0;
  701. sprintf(buffer, "%d", x);
  702. if(buffer[0] != '0')
  703. DoError(nErrorCount, "EA_POSTFIX_ALIGN test");
  704. #else
  705. DoError(nErrorCount, "EA_POSTFIX_ALIGN test");
  706. #endif
  707. }
  708. { // Test EA_ALIGNED
  709. #ifdef EA_ALIGNED
  710. char buffer[32];
  711. // Verify that a simple declaration works.
  712. EA_ALIGNED(int, xA, ALIGNMENT_AMOUNT_64);
  713. // Verify that a declaration with assignment works.
  714. EA_ALIGNED(int, xB, ALIGNMENT_AMOUNT_64) = 0;
  715. // Verify that a typedefd declaration works.
  716. typedef EA_ALIGNED(int, int16, ALIGNMENT_AMOUNT_16);
  717. int16 n16 = 0;
  718. // Verify that a declaration with construction works.
  719. #if defined(__cplusplus)
  720. EA_ALIGNED(int, xC, ALIGNMENT_AMOUNT_64)(0);
  721. #endif
  722. xA = 0;
  723. sprintf(buffer, "%d", xA);
  724. sprintf(buffer, "%d", xB);
  725. sprintf(buffer, "%p", &n16);
  726. #if defined(__cplusplus)
  727. sprintf(buffer, "%d", xC);
  728. #endif
  729. #if defined(__cplusplus)
  730. // Verify that the following tests compile. These tests are here
  731. // because the SN compiler (EDG front-end) has some problems with
  732. // GCC compatibility related to the 'aligned' __attribute__.
  733. ClassWithDefaultCtor cdc;
  734. ClassWithoutDefaultCtor cwdc(3);
  735. sprintf(buffer, "%p%p", &cdc, &cwdc);
  736. // Verify that regular usage of EA_ALIGNED works.
  737. EA_ALIGNED(ClassWithDefaultCtor, cdc16A, ALIGNMENT_AMOUNT_16);
  738. //EA_ALIGNED(ClassWithoutDefaultCtor, cwdcA, 16); // Doesn't have a default ctor, so this can't be done.
  739. sprintf(buffer, "%p%p", &cdc16A, (void*)NULL);
  740. // Verify that argument usage of EA_ALIGNED works.
  741. EA_ALIGNED(ClassWithDefaultCtor, cdcB, ALIGNMENT_AMOUNT_16)(3);
  742. EA_ALIGNED(ClassWithoutDefaultCtor, cwdcB, ALIGNMENT_AMOUNT_16)(3);
  743. sprintf(buffer, "%p%p", &cdcB, &cwdcB);
  744. // Verify that usage of EA_ALIGNED works within a typedef.
  745. typedef EA_ALIGNED(ClassWithDefaultCtor, ClassWithDefaultCtor16, ALIGNMENT_AMOUNT_16);
  746. ClassWithDefaultCtor16 cdcC(3);
  747. typedef EA_ALIGNED(ClassWithoutDefaultCtor, ClassWithoutDefaultCtor16, ALIGNMENT_AMOUNT_16);
  748. ClassWithoutDefaultCtor16 cwdcC(3);
  749. sprintf(buffer, "%p%p", &cdcC, &cwdcC);
  750. #endif
  751. #else
  752. DoError(nErrorCount, "EA_ALIGNED test");
  753. #endif
  754. }
  755. { // Test EA_PACKED
  756. #if defined(__cplusplus)
  757. #ifdef EA_PACKED
  758. char buffer[32];
  759. struct X { int x; } EA_PACKED;
  760. X x = { 0 };
  761. sprintf(buffer, "%d", x.x);
  762. if(buffer[0] != '0')
  763. DoError(nErrorCount, "EA_PACKED test");
  764. #else
  765. DoError(nErrorCount, "EA_PACKED test");
  766. #endif
  767. #endif
  768. }
  769. { // Test EA_LIKELY
  770. if(EA_UNLIKELY(nErrorCount > 0))
  771. {
  772. if(EA_LIKELY(nErrorCount == 999999)) // Actually this isn't likely, but that's beside the point.
  773. DoError(nErrorCount, "EA_LIKELY test");
  774. }
  775. }
  776. { // Test EA_ASSUME
  777. switch (nErrorCount / (nErrorCount + 1))
  778. {
  779. case 0:
  780. Stricmp("nop0", "nop0");
  781. break;
  782. case 1:
  783. Stricmp("nop1", "nop1");
  784. break;
  785. default:
  786. EA_ASSUME(0);
  787. }
  788. }
  789. { // Test EA_PURE
  790. if(!PureFunction())
  791. DoError(nErrorCount, "EA_PURE test");
  792. }
  793. { // Test EA_WCHAR_SIZE
  794. EA_DISABLE_VC_WARNING(6326)
  795. #ifdef EA_WCHAR_SIZE
  796. if((EA_WCHAR_SIZE != 1) && (EA_WCHAR_SIZE != 2) && (EA_WCHAR_SIZE != 4))
  797. DoError(nErrorCount, "EA_WCHAR_SIZE test");
  798. #else
  799. DoError(nErrorCount, "EA_WCHAR_SIZE test");
  800. #endif
  801. EA_RESTORE_VC_WARNING()
  802. }
  803. { // Test EA_RESTRICT
  804. char* p = NULL;
  805. if(RestrictTest(p) == 0) // This isn't a real test. If there is a failure, it will happen at compile time.
  806. DoError(nErrorCount, "EA_RESTRICT test");
  807. }
  808. { // Test EA_DEPRECATED
  809. /* This causes warnings on compilers, so just disable it.
  810. #if defined(EA_DEPRECATED) && (!defined(__GNUC__) || ((__GNUC__ * 100 + __GNUC_MINOR__) < 402)) // GCC 4.2+ is converting deprecated into an error instead of a warning.
  811. char buffer[32];
  812. EA_DEPRECATED int x = 0;
  813. sprintf(buffer, "%d", x); (void)x;
  814. if(buffer[0] != '0')
  815. DoError(nErrorCount, "EA_DEPRECATED test");
  816. #else
  817. DoError(nErrorCount, "EA_DEPRECATED test");
  818. #endif
  819. */
  820. }
  821. { // Test EA_PASCAL
  822. #if defined(__cplusplus)
  823. #ifdef EA_PASCAL
  824. struct X{ void EA_PASCAL DoNothing(void){} };
  825. X x;
  826. x.DoNothing();
  827. #else
  828. DoError(nErrorCount, "EA_PASCAL test");
  829. #endif
  830. #endif
  831. }
  832. { // Test EA_PASCAL_FUNC
  833. #if defined(__cplusplus)
  834. #ifdef EA_PASCAL_FUNC
  835. struct X{ void EA_PASCAL_FUNC(DoNothing()){} };
  836. X x;
  837. x.DoNothing();
  838. #else
  839. DoError(nErrorCount, "EA_PASCAL_FUNC test");
  840. #endif
  841. #endif
  842. }
  843. // EA_SSE
  844. // Not sure how to properly test at this time.
  845. { // EA_IMPORT
  846. // Not possible to do this because import means it will come from outside.
  847. //struct X{ EA_IMPORT void DoNothing(){} };
  848. //X x;
  849. //x.DoNothing();
  850. }
  851. { // EA_EXPORT
  852. #if defined(__cplusplus)
  853. struct X{ EA_EXPORT void DoNothing(){} };
  854. X x;
  855. x.DoNothing();
  856. #endif
  857. }
  858. // EA_PREPROCESSOR_JOIN
  859. // EA_STRINGIFY
  860. {
  861. char buffer[32];
  862. char bufferExpected[32];
  863. const int line = (__LINE__ + 2);
  864. sprintf(buffer, "%s %s", EA_STRINGIFY(EA_PREPROCESSOR_JOIN(test_, __LINE__)), EA_STRINGIFY(__LINE__));
  865. sprintf(bufferExpected, "test_%d %d", line, line);
  866. if(strcmp(buffer, bufferExpected) != 0)
  867. DoError(nErrorCount, "EA_PREPROCESSOR_JOIN/EA_STRINGIFY test");
  868. }
  869. { // EAArrayCount
  870. const int testArray[13] = { 0 };
  871. const size_t arrayCount = EAArrayCount(testArray);
  872. if((arrayCount != 13) || (testArray[0] != 0))
  873. DoError(nErrorCount, "EAArrayCount test");
  874. }
  875. { // static_assert
  876. // Should succeed.
  877. static_assert(sizeof(int32_t) == 4, "static_assert failure");
  878. // Should fail.
  879. //static_assert(sizeof(int32_t) == 8, "static_assert failure");
  880. }
  881. { // EA_OPTIMIZE_OFF / EA_OPTIMIZE_ON
  882. int result = DisabledOptimizations(2);
  883. if(result != 2*37)
  884. DoError(nErrorCount, "EA_OPTIMIZE_OFF test");
  885. }
  886. { // EA_UNUSED
  887. FunctionWithUnusedVariables(3);
  888. }
  889. return nErrorCount;
  890. }
  891. //
  892. // Tests for EA_IS_ENABLED
  893. //
  894. #define EABASE_TEST_FEATURE_A EA_ENABLED
  895. #if EA_IS_ENABLED(EABASE_TEST_FEATURE_A)
  896. // Feature A is enabled
  897. #else
  898. #error Error EABASE_TEST_FEATURE_A should be enabled.
  899. #endif
  900. // Make sure it is possible to successfully negate the test.
  901. #if !EA_IS_ENABLED(EABASE_TEST_FEATURE_A)
  902. #error Error EABASE_TEST_FEATURE_A should be enabled.
  903. #endif
  904. #define EABASE_TEST_FEATURE_B EA_DISABLED
  905. #if EA_IS_ENABLED(EABASE_TEST_FEATURE_B)
  906. #error Error EABASE_TEST_FEATURE_B should be disabled.
  907. #endif
  908. // Make sure it is possible to successfully negate the test.
  909. #if !EA_IS_ENABLED(EABASE_TEST_FEATURE_B)
  910. // Feature B is not enabled
  911. #else
  912. #error Error EABASE_TEST_FEATURE_B should be disabled.
  913. #endif
  914. // The test below should cause compilation to fail if it is uncommented. However we can't
  915. // obviously enable the test because it will break the build. It should be tested manually
  916. // if changes to EA_IS_ENABLED are made.
  917. //
  918. // #if EA_IS_ENABLED(EABASE_TEST_FEATURE_WITH_NO_DEFINE)
  919. // #endif
  920. int EAMain(int argc, char** argv)
  921. {
  922. int nErrorCount = 0, nTotalErrorCount = 0;
  923. (void)argc;
  924. (void)argv;
  925. nErrorCount = TestEABase();
  926. Printf("EABase test error count: %d\n\n", nErrorCount);
  927. nTotalErrorCount += nErrorCount;
  928. nErrorCount = TestEAPlatform();
  929. Printf("EAPlatform test error count: %d\n\n", nErrorCount);
  930. nTotalErrorCount += nErrorCount;
  931. nErrorCount = TestEACompiler();
  932. Printf("EACompiler test error count: %d\n\n", nErrorCount);
  933. nTotalErrorCount += nErrorCount;
  934. nErrorCount = TestEACompilerTraits();
  935. Printf("EACompilerTraits test error count: %d\n\n", nErrorCount);
  936. nTotalErrorCount += nErrorCount;
  937. if (nTotalErrorCount == 0)
  938. Printf("\nAll tests completed successfully.\n");
  939. else
  940. Printf("\nTests failed. Total error count: %d\n", nTotalErrorCount);
  941. return nTotalErrorCount;
  942. }