TestEABase.cpp 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // TestEABase.cpp
  3. //
  4. // Copyright (c) 2003 Electronic Arts, Inc. -- All Rights Reserved.
  5. // Created by Paul Pedriana.
  6. ///////////////////////////////////////////////////////////////////////////////
  7. #include "TestEABase.h"
  8. #include "TestEABase.h" // Intentionally double-include the same header file, to test it.
  9. #include <EABase/eabase.h>
  10. #include <EABase/earesult.h>
  11. #include <EABase/eahave.h>
  12. #include <EABase/nullptr.h>
  13. #include <EABase/eaunits.h>
  14. #include <stddef.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <ctype.h>
  19. #include <cmath>
  20. #include <new>
  21. #include <assert.h>
  22. #include <stdarg.h>
  23. #include <EAMain/EAEntryPointMain.inl>
  24. #include <EATest/EASTLVsnprintf.inl>
  25. #include <EATest/EASTLNewOperatorGuard.inl>
  26. #include <EATest/EATest.h>
  27. #include <EASTL/vector.h>
  28. #include <EASTL/fixed_vector.h>
  29. #include <EASTL/string.h>
  30. #include <EASTL/sort.h>
  31. #include <EAStdC/EAString.h>
  32. #if !defined(EA_COMPILER_NO_STANDARD_CPP_LIBRARY)
  33. EA_DISABLE_ALL_VC_WARNINGS()
  34. #include <iterator>
  35. EA_RESTORE_ALL_VC_WARNINGS()
  36. #endif
  37. #if defined(EA_COMPILER_MSVC) && defined(EA_PLATFORM_MICROSOFT)
  38. EA_DISABLE_ALL_VC_WARNINGS()
  39. #if defined(EA_PLATFORM_XENON)
  40. #define NOD3D
  41. #define NONET
  42. #include <Xtl.h>
  43. #else
  44. #include <Windows.h>
  45. #endif
  46. EA_RESTORE_ALL_VC_WARNINGS()
  47. #elif defined(EA_PLATFORM_ANDROID)
  48. #include <android/log.h>
  49. #endif
  50. #if EA_FP16C
  51. // Include emmintrin.h so that the test code can try to call one of the intrinsics.
  52. #include "emmintrin.h"
  53. #if EA_COMPILER_CLANG
  54. // On some versions of clang immintrin.h needs to be included to pull in f16c operations.
  55. #include "immintrin.h"
  56. #endif
  57. #endif
  58. EA_DISABLE_SN_WARNING(1229) // function is deprecated.
  59. EA_DISABLE_VC_WARNING(4265 4296 4310 4350 4481 4530 4625 4626 4996)
  60. // ------------------------------------------------------------------------
  61. // EA_STATIC_WARNING
  62. //
  63. // ** Temporarily here instead of eabase.h **
  64. //
  65. // Unilaterally prints a message during the compilation pre-processing phase.
  66. // No string quotes are required, and no trailing semicolon should be used.
  67. // As of this writing, clang reports this usage like a warning, but using
  68. // -Wno-#pragma-messages causes both the warning and message to go away.
  69. //
  70. // Example usage:
  71. // EA_STATIC_WARNING(This function is deprecated.)
  72. //
  73. #if defined(_MSC_VER)
  74. #define EA_PRAGMA_MESSAGE(x) __pragma(message(#x))
  75. #define EA_STATIC_WARNING(msg) EA_PRAGMA_MESSAGE(msg)
  76. #elif defined(__clang__) || (defined(__GNUC__) && (EA_COMPILER_VERSION >= 4005)) || defined(__SN_VER__)
  77. #define EA_PRAGMA(x) _Pragma(#x)
  78. #define EA_STATIC_WARNING(msg) EA_PRAGMA(message(#msg))
  79. #else
  80. #define EA_STATIC_WARNING(msg)
  81. #endif
  82. ///////////////////////////////////////////////////////////////////////////////
  83. // Exercise EAHave
  84. ///////////////////////////////////////////////////////////////////////////////
  85. // EA_HAVE_EXTENSIONS_FEATURE
  86. // We don't yet have a test for this.
  87. // EA_HAVE_DINKUMWARE_CPP_LIBRARY
  88. // EA_HAVE_LIBSTDCPP_LIBRARY
  89. // EA_HAVE_LIBCPP_LIBRARY
  90. #if defined(EA_HAVE_DINKUMWARE_CPP_LIBRARY)
  91. #if !defined(_YVALS)
  92. #error
  93. #endif
  94. #elif defined(EA_HAVE_LIBSTDCPP_LIBRARY)
  95. #if !defined(__GLIBCXX__) && !defined(__GLIBCPP__)
  96. #error
  97. #endif
  98. #elif defined(EA_HAVE_LIBCPP_LIBRARY)
  99. #if !defined(_LIBCPP_VERSION)
  100. #error
  101. #endif
  102. #endif
  103. // EA_HAVE_XXX_H
  104. #if defined(EA_HAVE_SYS_TYPES_H)
  105. #include <sys/types.h>
  106. #endif
  107. #if defined(EA_HAVE_IO_H)
  108. #include <io.h>
  109. #endif
  110. #if defined(EA_HAVE_INTTYPES_H)
  111. #include <inttypes.h>
  112. #endif
  113. #if defined(EA_HAVE_UNISTD_H)
  114. #include <unistd.h>
  115. #endif
  116. #if defined(EA_HAVE_SYS_TIME_H)
  117. #include <sys/time.h>
  118. #endif
  119. #if defined(EA_HAVE_SYS_PTRACE_H)
  120. #include <sys/ptrace.h>
  121. #endif
  122. #if defined(EA_HAVE_SYS_STAT_H)
  123. #include <sys/stat.h>
  124. #endif
  125. #if defined(EA_HAVE_LOCALE_H)
  126. #include <locale.h>
  127. #endif
  128. #if defined(EA_HAVE_DIRENT_H)
  129. #include <dirent.h>
  130. #endif
  131. #if defined(EA_HAVE_SIGNAL_H)
  132. #include <signal.h>
  133. #endif
  134. #if defined(EA_HAVE_SYS_SIGNAL_H)
  135. #include <sys/signal.h>
  136. #endif
  137. #if defined(EA_HAVE_PTHREAD_H)
  138. #include <pthread.h>
  139. #endif
  140. #if defined(EA_HAVE_WCHAR_H)
  141. #include <wchar.h>
  142. #endif
  143. #if defined(EA_HAVE_MALLOC_H)
  144. #include <malloc.h>
  145. #endif
  146. #if defined(EA_HAVE_ALLOCA_H)
  147. #include <alloca.h>
  148. #endif
  149. #if defined(EA_HAVE_EXECINFO_H)
  150. #include <execinfo.h>
  151. #endif
  152. #if defined(EA_HAVE_SEMAPHORE_H)
  153. #include <semaphore.h>
  154. #endif
  155. #if defined(EA_HAVE_CPP11_CONTAINERS)
  156. #include <array>
  157. #include <forward_list>
  158. #include <unordered_set>
  159. #include <unordered_map>
  160. #endif
  161. #if defined(EA_HAVE_CPP11_ATOMIC)
  162. #include <atomic>
  163. #endif
  164. #if defined(EA_HAVE_CPP11_CONDITION_VARIABLE)
  165. #include <condition_variable>
  166. #endif
  167. #if defined(EA_HAVE_CPP11_MUTEX)
  168. #include <mutex>
  169. #endif
  170. #if defined(EA_HAVE_CPP11_THREAD)
  171. #if defined(_MSC_VER) && defined(EA_COMPILER_NO_EXCEPTIONS) || defined(EA_COMPILER_NO_UNWIND)
  172. // Skip this #include, as VC++ has a bug: <concrt.h> (included by <future>) fails to compile when exceptions are disabled.
  173. #else
  174. #include <thread>
  175. #endif
  176. #endif
  177. #if defined(EA_HAVE_CPP11_FUTURE)
  178. #if defined(_MSC_VER) && defined(EA_COMPILER_NO_EXCEPTIONS) || defined(EA_COMPILER_NO_UNWIND)
  179. // Skip this #include, as VC++ has a bug: <concrt.h> (included by <future>) fails to compile when exceptions are disabled.
  180. #else
  181. #include <future>
  182. #endif
  183. #endif
  184. #if defined(EA_HAVE_CPP11_TYPE_TRAITS)
  185. #include <type_traits>
  186. #endif
  187. #if defined(EA_HAVE_CPP11_TUPLES)
  188. #include <tuple>
  189. #endif
  190. #if defined(EA_HAVE_CPP11_REGEX)
  191. #include <regex>
  192. #endif
  193. #if defined(EA_HAVE_CPP11_RANDOM)
  194. #include <random>
  195. #endif
  196. #if defined(EA_HAVE_CPP11_CHRONO)
  197. #include <chrono>
  198. #endif
  199. #if defined(EA_HAVE_CPP11_SCOPED_ALLOCATOR)
  200. #include <scoped_allocator>
  201. #endif
  202. #if defined(EA_HAVE_CPP11_INITIALIZER_LIST)
  203. #include <initializer_list>
  204. #else
  205. // If there is no initializer_list support the the following should succeed.
  206. // The following is disabled because EASTL defines initializer_list itself and that can collide with this:
  207. // namespace std{ template<class E> class initializer_list{ }; }
  208. #endif
  209. #if defined(EA_HAVE_CPP11_SYSTEM_ERROR)
  210. #include <system_error>
  211. #endif
  212. #if defined(EA_HAVE_CPP11_CODECVT)
  213. #include <codecvt>
  214. #endif
  215. #if defined(EA_HAVE_CPP11_TYPEINDEX)
  216. #include <typeindex>
  217. #endif
  218. // EA_HAVE_XXX_IMPL
  219. #if defined(EA_HAVE_inet_ntop_IMPL)
  220. #include <arpa/inet.h>
  221. #endif
  222. #if defined(EA_HAVE_time_IMPL)
  223. #include <time.h>
  224. #endif
  225. #if defined(EA_HAVE_clock_gettime_IMPL)
  226. #include <time.h>
  227. #endif
  228. #if defined(EA_HAVE_getcwd_IMPL)
  229. #if defined(EA_PLATFORM_MICROSOFT)
  230. #include <direct.h>
  231. #else
  232. #include <unistd.h>
  233. #endif
  234. #endif
  235. #if defined(EA_HAVE_std_terminate_IMPL)
  236. #include <exception>
  237. #endif
  238. #if defined(EA_HAVE_CPP11_ITERATOR_IMPL)
  239. #include <iterator>
  240. #endif
  241. #if defined(EA_HAVE_CPP11_SMART_POINTER_IMPL)
  242. #include <memory>
  243. #endif
  244. #if defined(EA_HAVE_CPP11_FUNCTIONAL_IMPL)
  245. #include <functional>
  246. void BindTestFunction(int /*n1*/, int /*n2*/, int /*n3*/, const int& /*n4*/, int /*n5*/)
  247. {
  248. }
  249. struct BindTestStruct
  250. {
  251. void Test(int /*n1*/, int /*n2*/) const
  252. {
  253. }
  254. };
  255. #endif
  256. #if defined(EA_HAVE_CPP11_EXCEPTION_IMPL)
  257. #include <exception>
  258. #endif
  259. EA_DISABLE_SN_WARNING(1229) // function is deprecated.
  260. // Some CPU/Compiler combinations don't support arbitrary alignment declarations.
  261. // In particular some ARM compilers often don't. You can use EAStdC's EAAlignment to
  262. // achieve arbitrary alignment if EA_ALIGN doesn't work.
  263. #if (EA_ALIGN_MAX_AUTOMATIC < 64)
  264. #define ALIGNMENT_AMOUNT_64 EA_ALIGN_MAX_AUTOMATIC
  265. #else
  266. #define ALIGNMENT_AMOUNT_64 64
  267. #endif
  268. #if (EA_ALIGN_MAX_AUTOMATIC < 32)
  269. #define ALIGNMENT_AMOUNT_32 EA_ALIGN_MAX_AUTOMATIC
  270. #else
  271. #define ALIGNMENT_AMOUNT_32 32
  272. #endif
  273. #if (EA_ALIGN_MAX_AUTOMATIC < 16)
  274. #define ALIGNMENT_AMOUNT_16 EA_ALIGN_MAX_AUTOMATIC
  275. #else
  276. #define ALIGNMENT_AMOUNT_16 16
  277. #endif
  278. // EA_OVERRIDE
  279. struct OverrideBase { virtual ~OverrideBase(){} virtual void f(int){} };
  280. struct OverrideDerived : public OverrideBase { void f(int) EA_OVERRIDE {} };
  281. // EA_INHERITANCE_FINAL
  282. struct FinalBase EA_INHERITANCE_FINAL { virtual ~FinalBase(){} virtual void f() EA_INHERITANCE_FINAL; };
  283. // EA_SEALED
  284. struct SealedBase EA_SEALED { virtual ~SealedBase(){} virtual void f() EA_SEALED; };
  285. // EA_ABSTRACT
  286. struct AbstractBase EA_ABSTRACT {virtual ~AbstractBase(){} virtual void f(){} };
  287. // EA_CONSTEXPR / EA_COMPILER_NO_CONSTEXPR
  288. EA_CONSTEXPR int GetValue(){ return 37; }
  289. // EA_EXTERN_TEMPLATE / EA_COMPILER_NO_EXTERN_TEMPLATE
  290. template struct EASTL_API eabase_template<char>;
  291. // Forward declarations
  292. template<class T>
  293. bool VerifyValue(T v1, T v2);
  294. void DoError(int& nErrorCount, const char* pMessage = NULL);
  295. int Stricmp(const char* pString1, const char* pString2);
  296. int TestEABase();
  297. int TestEAResult();
  298. int TestEAPlatform();
  299. bool TestNU();
  300. int TestEACompiler();
  301. int TestEACompilerTraits();
  302. template<class T>
  303. bool VerifyValue(T v1, T v2)
  304. {
  305. return (v1 == v2);
  306. }
  307. // Test EA_PLATFORM_XXX support
  308. // We don't do anything with the defined values below. We are just doing basic testing
  309. // of the usage of #if EA_PLATFORM_XXX
  310. #if EA_PLATFORM_XENON
  311. #define EA_PLATFORM_XENON_OK
  312. #elif EA_PLATFORM_PS3
  313. #define EA_PLATFORM_PS3_OK
  314. #elif EA_PLATFORM_WIN64
  315. #define EA_PLATFORM_WIN64_OK
  316. #elif EA_PLATFORM_WIN32
  317. #define EA_PLATFORM_WIN64_OK
  318. #elif EA_PLATFORM_WINDOWS
  319. #define EA_PLATFORM_WINDOWS_OK
  320. #elif EA_PLATFORM_POSIX
  321. #define EA_PLATFORM_POSIX_OK
  322. #elif EA_PLATFORM_UNIX
  323. #define EA_PLATFORM_UNIX_OK
  324. #elif EA_PLATFORM_APPLE
  325. #define EA_PLATFORM_APPLE_OK
  326. #elif EA_PLATFORM_CONSOLE
  327. #define EA_PLATFORM_CONSOLE_OK
  328. #elif EA_PLATFORM_DESKTOP
  329. #define EA_PLATFORM_DESKTOP_OK
  330. #else
  331. #define EA_PLATFORM_OK
  332. #endif
  333. /* Test EA_DISABLE_WARNING */
  334. EA_DISABLE_VC_WARNING(4548 4127)
  335. EA_DISABLE_ALL_VC_WARNINGS()
  336. EA_RESTORE_ALL_VC_WARNINGS()
  337. EA_DISABLE_GCC_WARNING(-Wuninitialized)
  338. EA_DISABLE_SN_WARNING(1787)
  339. EA_DISABLE_ALL_SN_WARNINGS()
  340. EA_RESTORE_ALL_SN_WARNINGS()
  341. EA_DISABLE_GHS_WARNING(123)
  342. EA_DISABLE_EDG_WARNING(193)
  343. EA_DISABLE_CW_WARNING(10317)
  344. EA_DISABLE_ALL_CW_WARNINGS()
  345. EA_RESTORE_ALL_CW_WARNINGS()
  346. /* Test EA_DISABLE_WARNING */
  347. EA_RESTORE_VC_WARNING()
  348. EA_RESTORE_GCC_WARNING()
  349. EA_RESTORE_SN_WARNING()
  350. EA_RESTORE_GHS_WARNING()
  351. EA_RESTORE_EDG_WARNING()
  352. EA_RESTORE_CW_WARNING(10317)
  353. void DoError(int& nErrorCount, const char* pMessage)
  354. {
  355. ++nErrorCount;
  356. if(pMessage)
  357. EA::EAMain::Report("Test error: %s\n", pMessage);
  358. }
  359. int Stricmp(const char* pString1, const char* pString2)
  360. {
  361. char c1, c2;
  362. while((c1 = (char)tolower(*pString1++)) == (c2 = (char)tolower(*pString2++)))
  363. {
  364. if(c1 == 0)
  365. return 0;
  366. }
  367. return (c1 - c2);
  368. }
  369. // EA_PURE
  370. static EA_PURE bool PureFunction()
  371. {
  372. return (strlen("abc") == 3);
  373. }
  374. // EA_WEAK
  375. EA_WEAK int gWeakVariable = 1;
  376. // EA_NO_VTABLE
  377. struct EA_NO_VTABLE NoVTable1
  378. {
  379. virtual ~NoVTable1(){}
  380. virtual void InterfaceFunction()
  381. {
  382. }
  383. };
  384. EA_STRUCT_NO_VTABLE(NoVTable2)
  385. {
  386. virtual ~NoVTable2(){}
  387. virtual void InterfaceFunction()
  388. {
  389. }
  390. };
  391. class NoVTable1Subclass : public NoVTable1
  392. {
  393. virtual void InterfaceFunction()
  394. {
  395. }
  396. };
  397. class NoVTable2Subclass : public NoVTable2
  398. {
  399. virtual void InterfaceFunction()
  400. {
  401. }
  402. };
  403. struct ClassWithDefaultCtor
  404. {
  405. ClassWithDefaultCtor(int x = 0)
  406. { char buffer[16]; sprintf(buffer, "%d", x); }
  407. };
  408. struct ClassWithoutDefaultCtor
  409. {
  410. ClassWithoutDefaultCtor(int x)
  411. { char buffer[16]; sprintf(buffer, "%d", x); }
  412. };
  413. struct InitPriorityTestClass
  414. {
  415. int mX;
  416. InitPriorityTestClass(int x = 0) { mX = x; }
  417. };
  418. struct OffsetofTestClass // Intentionally a non-pod.
  419. {
  420. int32_t mX;
  421. int32_t mY;
  422. OffsetofTestClass(int32_t x = 0) : mX(x), mY(0) { }
  423. };
  424. struct SizeofMemberTestClass // Intentionally a non-pod.
  425. {
  426. int32_t mX;
  427. int32_t mY;
  428. SizeofMemberTestClass(int32_t x = 0) : mX(x), mY(0) { }
  429. };
  430. // EA_INIT_PRIORITY
  431. InitPriorityTestClass gInitPriorityTestClass0 EA_INIT_PRIORITY(2000);
  432. #ifndef __SNC__ // SNC is broken with respect to constructors.
  433. InitPriorityTestClass gInitPriorityTestClass1 EA_INIT_PRIORITY(2000) (1);
  434. #endif
  435. // EA_MAY_ALIAS
  436. #ifndef __SNC__ // SNC doesn't support this usage.
  437. void* EA_MAY_ALIAS gPtr0 = NULL;
  438. #endif
  439. typedef void* EA_MAY_ALIAS pvoid_may_alias;
  440. pvoid_may_alias gPtr1 = NULL;
  441. // EA_NO_INLINE
  442. static EA_NO_INLINE void DoNothingInline()
  443. {
  444. }
  445. // EA_PREFIX_NO_INLINE / EA_POSTFIX_NO_INLINE
  446. static void EA_PREFIX_NO_INLINE DoNothingPrefixInline() EA_POSTFIX_NO_INLINE;
  447. static void DoNothingPrefixInline()
  448. {
  449. }
  450. // EA_FORCE_INLINE
  451. static EA_FORCE_INLINE void DoNothingForceInline()
  452. {
  453. }
  454. // EA_PREFIX_FORCE_INLINE / EA_POSTFIX_FORCE_INLINE
  455. static void EA_PREFIX_FORCE_INLINE DoNothingPrefixForceInline() EA_POSTFIX_FORCE_INLINE;
  456. static void DoNothingPrefixForceInline()
  457. {
  458. }
  459. // static_asset at global scope
  460. // Should succeed.
  461. static_assert(sizeof(int32_t) == 4, "static_assert failure");
  462. // Should fail.
  463. //static_assert(sizeof(int32_t) == 8, "static_assert failure");
  464. // EA_STATIC_WARNING
  465. EA_DISABLE_CLANG_WARNING(-W#pragma-messages) // Clang treats messages as warnings.
  466. EA_STATIC_WARNING(EA_STATIC_WARNING test)
  467. EA_RESTORE_CLANG_WARNING()
  468. // EA_OPTIMIZE_OFF / EA_OPTIMIZE_ON
  469. EA_OPTIMIZE_OFF()
  470. static EA_NO_INLINE int DisabledOptimizations(int x)
  471. {
  472. return x * 37;
  473. }
  474. EA_OPTIMIZE_ON()
  475. // EA_UNUSED
  476. static void FunctionWithUnusedVariables(int x)
  477. {
  478. int y = 0;
  479. EA_UNUSED(x);
  480. EA_UNUSED(y);
  481. }
  482. // EA_NON_COPYABLE / EANonCopyable
  483. struct NonCopyableA
  484. {
  485. NonCopyableA(){}
  486. int x;
  487. EA_NON_COPYABLE(NonCopyableA)
  488. };
  489. EA_DISABLE_VC_WARNING(4625 4626) // C4625: A copy constructor was not accessible in a base class and was therefore not generated for a derived class. C4626: An assignment operator was not accessible in a base class and was therefore not generated for a derived class.
  490. struct NonCopyableB : public EANonCopyable
  491. {
  492. #if !EA_COMPILER_NO_DELETED_FUNCTIONS
  493. NonCopyableB& operator=(NonCopyableB&& other) = delete;
  494. #endif
  495. int x;
  496. };
  497. EA_RESTORE_VC_WARNING()
  498. // Exercize the case of using EA_NON_COPYABLE when
  499. struct NonDefaultConstructibleBase
  500. {
  501. int mX;
  502. NonDefaultConstructibleBase(int x) : mX(x){}
  503. };
  504. struct NonCopyableSubclass : public NonDefaultConstructibleBase
  505. {
  506. NonCopyableSubclass(int x) : NonDefaultConstructibleBase(x){}
  507. EA_NON_COPYABLE(NonCopyableSubclass)
  508. };
  509. // EA_COMPILER_NO_DEFAULTED_FUNCTIONS
  510. // EA_COMPILER_NO_DELETED_FUNCTIONS
  511. //
  512. // We currently test only the ability of the compiler to build the code,
  513. // and don't test if the compiler built it correctly.
  514. struct DefaultedDeletedTest
  515. {
  516. #if defined(EA_COMPILER_NO_DEFAULTED_FUNCTIONS)
  517. DefaultedDeletedTest(){}
  518. #else
  519. DefaultedDeletedTest() = default;
  520. #endif
  521. #if defined(EA_COMPILER_NO_DEFAULTED_FUNCTIONS)
  522. ~DefaultedDeletedTest(){}
  523. #else
  524. ~DefaultedDeletedTest() = delete;
  525. #endif
  526. };
  527. struct EA_FUNCTION_DELETE_Test
  528. {
  529. int x;
  530. EA_FUNCTION_DELETE_Test(int xValue) : x(xValue){}
  531. private: // For portability with pre-C++11 compilers, make the function private.
  532. void foo() EA_FUNCTION_DELETE;
  533. };
  534. #if !defined(EA_COMPILER_NO_USER_DEFINED_LITERALS)
  535. // Conversion example
  536. inline long double operator"" _deg(long double degrees)
  537. { return (degrees * 3.141592) / 180; }
  538. // Custom type example
  539. struct UDLTest
  540. {
  541. UDLTest() : mX(0){}
  542. UDLTest(uint64_t x) : mX(x){}
  543. uint64_t mX;
  544. };
  545. UDLTest operator"" _udl(unsigned long long x) // The type must be unsigned long long and can't be uint64_t, as uint64_t might be unsigned long int.
  546. { return UDLTest(x); }
  547. #endif
  548. #if !defined(EA_COMPILER_NO_INLINE_NAMESPACES)
  549. namespace INSNamespace
  550. {
  551. inline namespace INSNamespace_1
  552. {
  553. template <typename T>
  554. class A;
  555. }
  556. template <typename T>
  557. int g(T){ return 37; }
  558. }
  559. struct INSClass{ };
  560. namespace INSNamespace
  561. {
  562. template<>
  563. class A<INSClass>{ };
  564. }
  565. #endif
  566. #if !defined(EA_COMPILER_NO_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  567. struct FunctionTemplateTest
  568. {
  569. template<typename T = int>
  570. static T AddOne(T value)
  571. { return value + 1; }
  572. };
  573. #endif
  574. #if !defined(EA_COMPILER_NO_NOEXCEPT)
  575. int NoExceptTestFunction() EA_NOEXCEPT
  576. { return 37; }
  577. struct NoExceptTestStruct
  578. {
  579. int mX;
  580. NoExceptTestStruct() : mX(37) {}
  581. };
  582. template <class T>
  583. int NoExceptTestTemplate() EA_NOEXCEPT_IF(EA_NOEXCEPT_EXPR(T()))
  584. { T t; return t.mX; }
  585. #endif
  586. // The following function defintions are intended to generate compilation errors if EA_CHAR16_NATIVE or EA_CHAR32_NATIVE is set to 1 when it should be 0.
  587. // i.e. if the types are not actually native then their will be a function redefinition error generated.
  588. void NoopTakingString(const wchar_t *)
  589. {
  590. }
  591. #if EA_WCHAR_UNIQUE
  592. #if EA_WCHAR_SIZE == 2
  593. // This definition should not conflict with the wchar_t defintion because char16_t should be unique.
  594. void NoopTakingString(const char16_t *)
  595. {
  596. #if !EA_CHAR16_NATIVE
  597. #error Expected EA_CHAR16_NATIVE to be 1.
  598. #endif
  599. }
  600. #else
  601. // This definition should not conflict with the wchar_t defintion because char32_t should be unique.
  602. void NoopTakingString(const char32_t *)
  603. {
  604. #if !EA_CHAR32_NATIVE
  605. #error Expected EA_CHAR32_NATIVE to be 1.
  606. #endif
  607. }
  608. #endif
  609. #endif
  610. int TestEABase()
  611. {
  612. int nErrorCount(0);
  613. DoNothingInline();
  614. // Test NULL
  615. {
  616. if(!VerifyValue<void*>(NULL, (void*)0))
  617. DoError(nErrorCount, "unspecified test");
  618. }
  619. // Verify sized type sizes
  620. {
  621. if(!VerifyValue<size_t>(sizeof(int8_t), 1))
  622. DoError(nErrorCount, "int8_t size test");
  623. if(!VerifyValue<size_t>(sizeof(uint8_t), 1))
  624. DoError(nErrorCount, "uint8_t size test");
  625. if(!VerifyValue<size_t>(sizeof(int16_t), 2))
  626. DoError(nErrorCount, "int16_t size test");
  627. if(!VerifyValue<size_t>(sizeof(uint16_t), 2))
  628. DoError(nErrorCount, "uint16_t size test");
  629. if(!VerifyValue<size_t>(sizeof(int32_t), 4))
  630. DoError(nErrorCount, "int32_t size test");
  631. if(!VerifyValue<size_t>(sizeof(uint32_t), 4))
  632. DoError(nErrorCount, "uint32_t size test");
  633. if(!VerifyValue<size_t>(sizeof(int64_t), 8))
  634. DoError(nErrorCount, "int64_t size test");
  635. if(!VerifyValue<size_t>(sizeof(uint64_t), 8))
  636. DoError(nErrorCount, "uint64_t size test");
  637. #if !defined(FLT_EVAL_METHOD)
  638. #error EABase should always define FLT_EVAL_METHOD
  639. DoError(nErrorCount, "FLT_EVAL_METHOD test: not defined.");
  640. #else
  641. #if (FLT_EVAL_METHOD == -1)
  642. // In this case the C99 standard states that the
  643. // precision of float_t and double_t is indeterminable.
  644. #elif (FLT_EVAL_METHOD == 0)
  645. if(!VerifyValue<size_t>(sizeof(float_t), sizeof(float)))
  646. DoError(nErrorCount, "float_t size test");
  647. if(!VerifyValue<size_t>(sizeof(double_t), sizeof(double)))
  648. DoError(nErrorCount, "double_t size test");
  649. #elif (FLT_EVAL_METHOD == 1)
  650. if(!VerifyValue<size_t>(sizeof(float_t), sizeof(double)))
  651. DoError(nErrorCount, "float_t size test");
  652. if(!VerifyValue<size_t>(sizeof(double_t), sizeof(double)))
  653. DoError(nErrorCount, "double_t size test");
  654. #elif (FLT_EVAL_METHOD == 2)
  655. if(!VerifyValue<size_t>(sizeof(float_t), sizeof(long double)))
  656. DoError(nErrorCount, "float_t size test");
  657. if(!VerifyValue<size_t>(sizeof(double_t), sizeof(long double)))
  658. DoError(nErrorCount, "double_t size test");
  659. #else
  660. DoError(nErrorCount, "FLT_EVAL_METHOD test: invalid value.");
  661. #endif
  662. #endif
  663. if(sizeof(bool8_t) != 1)
  664. DoError(nErrorCount, "bool8_t size test");
  665. if(!VerifyValue<size_t>(sizeof(intptr_t), sizeof(void*)))
  666. DoError(nErrorCount, "intptr_t size test");
  667. if(!VerifyValue<size_t>(sizeof(uintptr_t), sizeof(void*)))
  668. DoError(nErrorCount, "uintptr_t size test");
  669. if(!VerifyValue<size_t>(sizeof(ssize_t), sizeof(size_t)))
  670. DoError(nErrorCount, "ssize_t size test");
  671. EA_DISABLE_VC_WARNING(6326)
  672. const ssize_t ss(1); // Verify that ssize_t is a signed type.
  673. if(ssize_t((ss ^ ss) - 1) >= 0)
  674. DoError(nErrorCount, "ssize_t sign test");
  675. EA_RESTORE_VC_WARNING()
  676. if(!VerifyValue<size_t>(sizeof(char8_t), 1))
  677. DoError(nErrorCount, "char8_t size test");
  678. if(!VerifyValue<size_t>(sizeof(char16_t), 2))
  679. DoError(nErrorCount, "char16_t size test");
  680. if(!VerifyValue<size_t>(sizeof(char32_t), 4))
  681. DoError(nErrorCount, "char32_t size test");
  682. #if (EA_WCHAR_SIZE == 2) || (EA_WCHAR_SIZE == 4)
  683. if(!VerifyValue<size_t>(sizeof(wchar_t), EA_WCHAR_SIZE))
  684. DoError(nErrorCount, "EA_WCHAR_SIZE test");
  685. #else
  686. DoError(nErrorCount, "EA_WCHAR_SIZE test");
  687. #endif
  688. }
  689. // Test CHAR8_MIN, etc.
  690. {
  691. // The C standard allows compilers/platforms to use -127 as the min 8 bit value, but we've never seen it in modern systems.
  692. static_assert(((((CHAR8_MIN == -128) && (CHAR8_MAX == 127))) || ((CHAR8_MIN == 0) && (CHAR8_MAX == 255))), "CHAR8_MAX failure");
  693. static_assert(((((CHAR16_MIN == -32768) && (CHAR16_MAX == 32767))) || ((CHAR16_MIN == 0) && (CHAR16_MAX == 65535))), "CHAR16_MAX failure");
  694. static_assert(((((CHAR32_MIN == -INT64_C(2147483648)) && (CHAR32_MAX == INT64_C(2147483647)))) || ((CHAR32_MIN == 0) && (CHAR32_MAX == INT64_C(4294967295)))), "CHAR32_MAX failure");
  695. }
  696. // Test char8_t, char16_t, char32_t string literals.
  697. {
  698. const char8_t* p8 = EA_CHAR8("abc");
  699. const char8_t c8 = EA_CHAR8('a');
  700. #ifdef EA_CHAR16
  701. const char16_t* p16 = EA_CHAR16("abc"); // Under GCC, this assumes compiling with -fshort-wchar
  702. const char16_t c16 = EA_CHAR16('\x3001');
  703. #else
  704. const char16_t* p16 = NULL;
  705. const char16_t c16 = static_cast<char16_t>('X');
  706. #endif
  707. #ifdef EA_CHAR32
  708. const char32_t* p32 = EA_CHAR32("abc");
  709. const char32_t c32 = EA_CHAR32('\x3001');
  710. #else
  711. 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).
  712. #ifdef EA_CHAR16
  713. const char32_t c32 = EA_CHAR16('\x3001'); // 16 bit should silently convert to 32 bit.
  714. #else
  715. const char32_t c32 = static_cast<char16_t>('X'); // 16 bit should silently convert to 32 bit.
  716. #endif
  717. #endif
  718. const wchar_t* pW = EA_WCHAR("abc");
  719. const wchar_t cW = EA_WCHAR('\x3001');
  720. EA_UNUSED(p8);
  721. EA_UNUSED(c8);
  722. EA_UNUSED(p16);
  723. EA_UNUSED(c16);
  724. EA_UNUSED(p32);
  725. EA_UNUSED(c32);
  726. EA_UNUSED(pW);
  727. EA_UNUSED(cW);
  728. }
  729. // Verify sized type signs
  730. {
  731. int8_t i8(1);
  732. if(int8_t((i8 ^ i8) - 1) >= 0)
  733. DoError(nErrorCount, "int8_t sign test");
  734. uint8_t u8(1);
  735. if(uint8_t((u8 ^ u8) - 1) <= 0)
  736. DoError(nErrorCount, "uint8_t sign test");
  737. int16_t i16(1);
  738. if(int16_t((i16 ^ i16) - 1) >= 0)
  739. DoError(nErrorCount, "int16_t sign test");
  740. uint16_t u16(1);
  741. if(uint16_t((u16 ^ u16) - 1) <= 0)
  742. DoError(nErrorCount, "uint16_t sign test");
  743. int32_t i32(1);
  744. if(int32_t((i32 ^ i32) - 1) >= 0)
  745. DoError(nErrorCount, "int32_t sign test");
  746. uint32_t u32(1);
  747. if(uint32_t((u32 ^ u32) - 1) <= 0)
  748. DoError(nErrorCount, "uint32_t sign test");
  749. int64_t i64(1);
  750. if(int64_t((i64 ^ i64) - 1) >= 0)
  751. DoError(nErrorCount, "int64_t sign test");
  752. uint64_t u64(1);
  753. if(uint64_t((u64 ^ u64) - 1) <= 0)
  754. DoError(nErrorCount, "uint64_t sign test");
  755. intptr_t ip(1);
  756. if(intptr_t((ip ^ ip) - 1) >= 0)
  757. DoError(nErrorCount, "intptr_t sign test");
  758. uintptr_t up(1);
  759. if(uintptr_t((up ^ up) - 1) <= 0)
  760. DoError(nErrorCount, "uintptr_t sign test");
  761. // The following sign tests have been disabled, as the determination of
  762. // the sign of type char and wchar_t are in the hands of the compiler and
  763. // the user's configuration of that compiler.
  764. //char8_t c8(1); // We expect it to be signed, though the need for such a requirement is debateable.
  765. //if(char8_t((c8 ^ c8) - 1) >= 0)
  766. // DoError(nErrorCount, "char8_t sign test");
  767. //char16_t c16(1); // We expect it to be unsigned
  768. //if(char16_t((c16 ^ c16) - 1) <= 0)
  769. // DoError(nErrorCount, "char16_t sign test");
  770. //char32_t c32(1); // We expect it to be unsigned
  771. //if(char32_t((c32 ^ c32) - 1) <= 0)
  772. // DoError(nErrorCount, "char32_t sign test");
  773. }
  774. //Test Constant macros
  775. {
  776. char buffer[256];
  777. const int8_t i8Min = INT8_C(-128); // Strictly speaking, the C language standard allows this to be -127 as well.
  778. const int8_t i8Max = INT8_C(127);
  779. const uint8_t u8Min = UINT8_C(0);
  780. const uint8_t u8Max = UINT8_C(255);
  781. const int16_t i16Min = INT16_C(-32767) - 1;
  782. const int16_t i16Max = INT16_C( 32767);
  783. const uint16_t u16Min = UINT16_C(0);
  784. const uint16_t u16Max = UINT16_C(65535);
  785. const int32_t i32Min = INT32_C(-2147483647) - 1;
  786. const int32_t i32Max = INT32_C( 2147483647);
  787. const uint32_t u32Min = UINT32_C(0);
  788. const uint32_t u32Max = UINT32_C(4294967295);
  789. #if defined(__GNUC__) && (__GNUC__ < 4) // If using a broken version of UINT64_C/INT64_C macros...
  790. const int64_t i64Min = -9223372036854775807LL - 1;
  791. const int64_t i64Max = 9223372036854775807LL;
  792. const uint64_t u64Min = UINT64_C(0);
  793. const uint64_t u64Max = 18446744073709551615ULL;
  794. #else
  795. const int64_t i64Min = INT64_C(-9223372036854775807) - 1;
  796. const int64_t i64Max = INT64_C( 9223372036854775807);
  797. const uint64_t u64Min = UINT64_C(0);
  798. const uint64_t u64Max = UINT64_C(18446744073709551615);
  799. #endif
  800. sprintf(buffer, "%d %d %u %u %d %d %u %u %d %d %u %u %" SCNd64" %" SCNd64" %" SCNu64" %" SCNu64,
  801. (int)i8Min, (int)i8Max, (unsigned)u8Min, (unsigned)u8Max,
  802. (int)i16Min, (int)i16Max, (unsigned)u16Min, (unsigned)u16Max,
  803. (int)i32Min, (int)i32Max, (unsigned)u32Min, (unsigned)u32Max,
  804. i64Min, i64Max, u64Min, u64Max);
  805. if(strcmp(buffer, "-128 127 0 255 -32768 32767 0 65535 -2147483648 2147483647 0 4294967295 -9223372036854775808 9223372036854775807 0 18446744073709551615"))
  806. DoError(nErrorCount, "INT_C test");
  807. EA_DISABLE_VC_WARNING(6326)
  808. // Verify the use of hex numbers with INT64_C
  809. const int64_t i64Hex = INT64_C(0x1111111122222222);
  810. if(i64Hex != INT64_C(1229782938533634594))
  811. DoError(nErrorCount, "INT64_C hex error");
  812. EA_RESTORE_VC_WARNING()
  813. // Verify the use of hex numbers with UINT64_C
  814. const uint64_t u64Hex = UINT64_C(0xaaaaaaaabbbbbbbb);
  815. #if defined(__GNUC__) && (__GNUC__ < 4) // If using a broken version of UINT64_C/INT64_C macros...
  816. const uint64_t temp = 12297829382759365563ULL;
  817. #else
  818. const uint64_t temp = UINT64_C(12297829382759365563);
  819. #endif
  820. EA_DISABLE_VC_WARNING(6326)
  821. if(u64Hex != temp)
  822. DoError(nErrorCount, "UINT64_C hex error");
  823. EA_RESTORE_VC_WARNING()
  824. // Verify that the compiler both allows division with uint64_t but
  825. // also that it allows it via UINT64_MAX. A bad implementation of
  826. // UINT64_MAX would cause the code below to mis-execute or not compile.
  827. EA_DISABLE_VC_WARNING(6326)
  828. const uint64_t resultUint64 = UINT64_MAX / 2;
  829. if(resultUint64 != UINT64_C(9223372036854775807))
  830. DoError(nErrorCount, "UINT64_MAX error");
  831. EA_RESTORE_VC_WARNING()
  832. }
  833. //Test sized printf format specifiers
  834. {
  835. char buffer[256];
  836. int8_t d8(INT8_MAX), i8(INT8_MIN), o8(INT8_MAX);
  837. uint8_t u8(UINT8_MAX), x8(UINT8_MAX), X8(UINT8_MAX);
  838. sprintf(buffer, "%" PRId8 " %" PRIi8 " %" PRIo8 " %" PRIu8 " %" PRIx8 " %" PRIX8, d8, i8, o8, u8, x8, X8);
  839. #ifdef EA_COMPILER_GNUC
  840. if(Stricmp(buffer, "127 -128 177 255 ff FF"))
  841. DoError(nErrorCount, "PRI8 test"); // This is known to fail with compilers such as VC++ which don't support %hh.
  842. #endif
  843. int16_t d16(INT16_MAX), i16(INT16_MIN), o16(INT16_MAX);
  844. uint16_t u16(UINT16_MAX), x16(UINT16_MAX), X16(UINT16_MAX);
  845. sprintf(buffer, "%" PRId16 " %" PRIi16 " %" PRIo16 " %" PRIu16 " %" PRIx16 " %" PRIX16, d16, i16, o16, u16, x16, X16);
  846. if(Stricmp(buffer, "32767 -32768 77777 65535 ffff FFFF"))
  847. DoError(nErrorCount, "PRI16 test");
  848. int32_t d32(INT32_MAX), i32(INT32_MIN), o32(INT32_MAX);
  849. uint32_t u32(UINT32_MAX), x32(UINT32_MAX), X32(UINT32_MAX);
  850. sprintf(buffer, "%" PRId32 " %" PRIi32 " %" PRIo32 " %" PRIu32 " %" PRIx32 " %" PRIX32, d32, i32, o32, u32, x32, X32);
  851. if(Stricmp(buffer, "2147483647 -2147483648 17777777777 4294967295 ffffffff FFFFFFFF"))
  852. DoError(nErrorCount, "PRI32 test");
  853. int64_t d64(INT64_MAX), i64(INT64_MIN), o64(INT64_MAX);
  854. uint64_t u64(UINT64_MAX), x64(UINT64_MAX), X64(UINT64_MAX);
  855. sprintf(buffer, "%" PRId64 " %" PRIi64 " %" PRIo64 " %" PRIu64 " %" PRIx64 " %" PRIX64, d64, i64, o64, u64, x64, X64);
  856. if(Stricmp(buffer, "9223372036854775807 -9223372036854775808 777777777777777777777 18446744073709551615 ffffffffffffffff FFFFFFFFFFFFFFFF"))
  857. DoError(nErrorCount, "PRI64 test");
  858. // Many compilers give warnings for the following code because they
  859. // recognize that a pointer is being formatted as an integer.
  860. // This is what we want to do and what the C99 standard intends here.
  861. #if defined(_MSC_VER) && (_MSC_VER >= 1300)
  862. #pragma warning(disable: 4313) // Warning C4313: 'sprintf' : '%d' in format string conflicts with argument 1 of type 'void *'
  863. #pragma warning(disable: 4777) // Warning C4777: 'sprintf' : format string '%lld' requires an argument of type '__int64', but variadic argument 1 has type 'intptr_t'
  864. #endif
  865. #if !defined(__GNUC__) // GCC generates warnings here which we can't work around.
  866. 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;
  867. 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);
  868. #if (EA_PLATFORM_PTR_SIZE == 4)
  869. if(Stricmp(buffer, "2147483647 -2147483648 17777777777 4294967295 ffffffff FFFFFFFF"))
  870. DoError(nErrorCount, "PRIPTR test");
  871. #else // EA_PLATFORM_PTR_SIZE == 8
  872. if(Stricmp(buffer, "2147483647 -2147483648 17777777777 18446744073709551615 ffffffffffffffff FFFFFFFFFFFFFFFF"))
  873. DoError(nErrorCount, "PRIPTR test");
  874. #endif
  875. #endif
  876. #if defined(_MSC_VER) && (_MSC_VER >= 1300)
  877. #pragma warning(default: 4313)
  878. #pragma warning(default: 4777)
  879. #endif
  880. }
  881. //Test sized scanf format specifiers
  882. {
  883. int numMatched = 0;
  884. #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.
  885. int8_t d8, i8, o8;
  886. uint8_t u8, x8;
  887. numMatched = sscanf("127 -127 177 255 ff", "%" SCNd8 " %" SCNi8 " %" SCNo8 " %" SCNu8 " %" SCNx8, &d8, &i8, &o8, &u8, &x8);
  888. if((numMatched != 5) || (d8 != 127) || (i8 != -127) || (o8 != 127) || (u8 != 255) || (x8 != 255))
  889. DoError(nErrorCount, "SCN8 test"); // This is known to fail with compilers such as VC++ which don't support %hh.
  890. #endif
  891. int16_t d16, i16, o16;
  892. uint16_t u16, x16;
  893. numMatched = sscanf("32767 -32768 77777 65535 ffff", "%" SCNd16 " %" SCNi16 " %" SCNo16 " %" SCNu16 " %" SCNx16, &d16, &i16, &o16, &u16, &x16);
  894. if((numMatched != 5) || (d16 != 32767) || (i16 != -32768) || (o16 != 32767) || (u16 != 65535) || (x16 != 65535))
  895. DoError(nErrorCount, "SCN16 test");
  896. int32_t d32, i32, o32;
  897. uint32_t u32, x32;
  898. numMatched = sscanf("2147483647 -2147483648 17777777777 4294967295 ffffffff", "%" SCNd32 " %" SCNi32 " %" SCNo32 " %" SCNu32 " %" SCNx32, &d32, &i32, &o32, &u32, &x32);
  899. if((numMatched != 5) || (d32 != INT32_MAX) || (i32 != INT32_MIN) || (o32 != INT32_MAX) || (u32 != UINT32_MAX) || (x32 != UINT32_MAX))
  900. DoError(nErrorCount, "SCN32 test");
  901. int64_t d64, i64, o64;
  902. uint64_t u64, x64;
  903. numMatched = sscanf("9223372036854775807 -9223372036854775808 777777777777777777777 18446744073709551615 ffffffffffffffff", "%" SCNd64 " %" SCNi64 " %" SCNo64 " %" SCNu64 " %" SCNx64, &d64, &i64, &o64, &u64, &x64);
  904. if((numMatched != 5) || (d64 != INT64_MAX) || (i64 != INT64_MIN) || (o64 != INT64_MAX) || (u64 != UINT64_MAX) || (x64 != UINT64_MAX))
  905. DoError(nErrorCount, "SCN64 test");
  906. // Many compilers give warnings for the following code because they
  907. // recognize that a pointer is being formatted as an integer.
  908. // This is what we want to do and what the C99 standard intends here.
  909. #if !defined(__GNUC__) // GCC generates warnings here which we can't work around.
  910. void *dPtr, *iPtr, *oPtr, *uPtr, *xPtr;
  911. intptr_t dip, iip;
  912. uintptr_t ouip, uuip, xuip;
  913. EA_DISABLE_VC_WARNING(4777) // format string '%lld' requires an argument of type '__int64 *', but variadic argument 1 has type 'intptr_t *'
  914. #if (EA_PLATFORM_PTR_SIZE == 4)
  915. numMatched = sscanf("2147483647 -2147483648 17777777777 4294967295 ffffffff", "%" SCNdPTR " %" SCNiPTR " %" SCNoPTR " %" SCNuPTR " %" SCNxPTR, &dip, &iip, &ouip, &uuip, &xuip);
  916. #else // EA_PLATFORM_PTR_SIZE == 8
  917. numMatched = sscanf("2147483647 -2147483648 17777777777 18446744073709551615 ffffffffffffffff", "%" SCNdPTR " %" SCNiPTR " %" SCNoPTR " %" SCNuPTR " %" SCNxPTR, &dip, &iip, &ouip, &uuip, &xuip);
  918. #endif
  919. EA_RESTORE_VC_WARNING()
  920. dPtr = (void*)dip;
  921. iPtr = (void*)iip;
  922. oPtr = (void*)ouip;
  923. uPtr = (void*)uuip;
  924. xPtr = (void*)xuip;
  925. if((numMatched != 5) || (dPtr != (void*)INT32_MAX) || (iPtr != (void*)INT32_MIN) || (oPtr != (void*)INT32_MAX) || (uPtr != (void*)(uintptr_t)UINT64_MAX) || (xPtr != (void*)(uintptr_t)UINT64_MAX))
  926. DoError(nErrorCount, "SCNPTR test");
  927. #endif
  928. }
  929. // Test min/max
  930. {
  931. // The C standard allows INT8_MIN to be either -127 or -128. So in order to be able
  932. // to test for this in a portable way, we do the logic below whereby we test for
  933. // -127 (which all compiles should support) or -127 - 1 which all compilers should
  934. // support if INT8_MIN isn't -127.
  935. if(!VerifyValue<int8_t>(INT8_MIN, INT8_C(-127)) && !VerifyValue<int8_t>(INT8_MIN, INT8_C(-127) - 1))
  936. DoError(nErrorCount, "INT8_MIN test");
  937. if(!VerifyValue<int8_t>(INT8_MAX, INT8_C(127)))
  938. DoError(nErrorCount, "INT8_MAX test");
  939. if(!VerifyValue<uint8_t>(UINT8_MAX, UINT8_C(255)))
  940. DoError(nErrorCount, "UINT8_MAX test");
  941. if(!VerifyValue<int16_t>(INT16_MIN, INT16_C(-32767)) && !VerifyValue<int16_t>(INT16_MIN, INT16_C(-32767) - 1))
  942. DoError(nErrorCount, "INT16_MIN test");
  943. if(!VerifyValue<int16_t>(INT16_MAX, INT16_C(32767)))
  944. DoError(nErrorCount, "INT16_MAX test");
  945. if(!VerifyValue<uint16_t>(UINT16_MAX, UINT16_C(65535)))
  946. DoError(nErrorCount, "UINT16_MAX test");
  947. if(!VerifyValue<int32_t>(INT32_MIN, INT32_C(-2147483647)) && !VerifyValue<int32_t>(INT32_MIN, INT32_C(-2147483647) - 1))
  948. DoError(nErrorCount, "INT32_MIN test");
  949. if(!VerifyValue<int32_t>(INT32_MAX, INT32_C(2147483647)))
  950. DoError(nErrorCount, "INT32_MAX test");
  951. if(!VerifyValue<uint32_t>(UINT32_MAX, UINT32_C(4294967295)))
  952. DoError(nErrorCount, "UINT32_MAX test");
  953. if(!VerifyValue<int64_t>(INT64_MIN, INT64_C(-9223372036854775807)) && !VerifyValue<int64_t>(INT64_MIN, INT64_C(-9223372036854775807) - 1))
  954. DoError(nErrorCount, "INT64_MIN test");
  955. if(!VerifyValue<uint64_t>(INT64_MAX, INT64_C(9223372036854775807)))
  956. DoError(nErrorCount, "INT64_MAX test");
  957. #if defined(__GNUC__) && (__GNUC__ < 4) // If using a broken version of UINT64_C/INT64_C macros...
  958. const uint64_t temp = 18446744073709551615ULL;
  959. #else
  960. const uint64_t temp = UINT64_C(18446744073709551615);
  961. #endif
  962. if(!VerifyValue<uint64_t>(UINT64_MAX, temp))
  963. DoError(nErrorCount, "UINT64_MAX test");
  964. }
  965. {
  966. NoopTakingString(L"");
  967. // Compilation errors below indicate that the EA_CHAR16/EA_CHAR32 may be incorrectly defined, or EA_CHAR16_NATIVE/EA_CHAR32_NATIVE is incorrect set to 0.
  968. #if EA_WCHAR_SIZE == 2 && defined(EA_CHAR16)
  969. const char16_t *str = EA_CHAR16("");
  970. NoopTakingString(str);
  971. #elif EA_WCHAR_SIZE == 4 && defined(EA_CHAR32)
  972. const char32_t *str = EA_CHAR32("");
  973. NoopTakingString(str);
  974. #endif
  975. }
  976. return nErrorCount;
  977. }
  978. int TestEAResult()
  979. {
  980. int nErrorCount(0);
  981. EA::result_type resultSuccess(EA::SUCCESS);
  982. EA::result_type resultFailure(EA::FAILURE);
  983. EA::result_type resultZero(0); // success
  984. EA::result_type resultNeg(-1); // failure
  985. EA::result_type resultPos(+1); // success
  986. if(!EA_SUCCEEDED(resultSuccess))
  987. DoError(nErrorCount, "EA::SUCCESS test");
  988. if(EA_FAILED(resultSuccess))
  989. DoError(nErrorCount, "EA::SUCCESS test");
  990. if(EA_SUCCEEDED(resultFailure))
  991. DoError(nErrorCount, "EA::FAILURE test");
  992. if(!EA_FAILED(resultFailure))
  993. DoError(nErrorCount, "EA::FAILURE test");
  994. if(!EA_SUCCEEDED(resultZero))
  995. DoError(nErrorCount, "EA::SUCCESS test");
  996. if(EA_FAILED(resultZero))
  997. DoError(nErrorCount, "EA::SUCCESS test");
  998. if(EA_SUCCEEDED(resultNeg))
  999. DoError(nErrorCount, "EA::FAILURE test");
  1000. if(!EA_FAILED(resultNeg))
  1001. DoError(nErrorCount, "EA::FAILURE test");
  1002. if(!EA_SUCCEEDED(resultPos))
  1003. DoError(nErrorCount, "EA::SUCCESS test");
  1004. if(EA_FAILED(resultPos))
  1005. DoError(nErrorCount, "EA::SUCCESS test");
  1006. return nErrorCount;
  1007. }
  1008. int TestEAPlatform()
  1009. {
  1010. int nErrorCount(0);
  1011. // Test EA_PLATFORM_PTR_SIZE
  1012. {
  1013. #ifdef EA_PLATFORM_PTR_SIZE
  1014. if(!VerifyValue<size_t>(EA_PLATFORM_PTR_SIZE, sizeof(void*)))
  1015. DoError(nErrorCount, "EA_PLATFORM_PTR_SIZE test");
  1016. #else
  1017. DoError(nErrorCount, "EA_PLATFORM_PTR_SIZE test");
  1018. #endif
  1019. }
  1020. // Test EA_PLATFORM_NAME
  1021. {
  1022. #ifdef EA_PLATFORM_NAME
  1023. char buffer[256];
  1024. sprintf(buffer, "TestEAPlatform: EA_PLATFORM_NAME: %s\n", EA_PLATFORM_NAME);
  1025. #else
  1026. DoError(nErrorCount, "EA_PLATFORM_NAME test");
  1027. #endif
  1028. }
  1029. // Test EA_PLATFORM_DESCRIPTION
  1030. {
  1031. #ifdef EA_PLATFORM_DESCRIPTION
  1032. char buffer[256];
  1033. sprintf(buffer, "TestEAPlatform: EA_PLATFORM_DESCRIPTION: %s\n", EA_PLATFORM_DESCRIPTION);
  1034. #else
  1035. DoError(nErrorCount, "EA_PLATFORM_DESCRIPTION test");
  1036. #endif
  1037. }
  1038. // Test EA_SYSTEM_LITTLE_ENDIAN / EA_SYSTEM_BIG_ENDIAN
  1039. {
  1040. uint32_t kValue = 0x12345678;
  1041. uint8_t* pValue = (uint8_t*)&kValue;
  1042. #ifdef EA_SYSTEM_LITTLE_ENDIAN
  1043. if(pValue[0] != 0x78)
  1044. DoError(nErrorCount, "EA_SYSTEM_ENDIAN test");
  1045. #elif defined(EA_SYSTEM_BIG_ENDIAN)
  1046. if(pValue[0] != 0x12)
  1047. DoError(nErrorCount, "EA_SYSTEM_ENDIAN test");
  1048. #else
  1049. DoError(nErrorCount, "EA_SYSTEM_ENDIAN test");
  1050. #endif
  1051. }
  1052. // Test EA_ASM_STYLE
  1053. {
  1054. #if defined(EA_PROCESSOR_X86)
  1055. #if defined(EA_ASM_STYLE_ATT)
  1056. asm volatile ("nop");
  1057. #elif defined(EA_ASM_STYLE_INTEL)
  1058. __asm nop
  1059. #endif
  1060. #else
  1061. // Add other processors here.
  1062. #endif
  1063. }
  1064. return nErrorCount;
  1065. }
  1066. // Test compiler limitations
  1067. // Easiest way to come up with tests for some of the more complicated versions
  1068. // of these is to look at the Boost /libs/config/test/*.cxx files. Many of the
  1069. // Boost compiler limitation defines are similar or match exactly to those
  1070. // defined by EABase. See http://www.boost.org if you want to check this out.
  1071. #ifndef EA_COMPILER_NO_STATIC_CONSTANTS // If class member static constants are allowed...
  1072. struct NSC
  1073. {
  1074. static const int x = 10;
  1075. };
  1076. #endif
  1077. #ifndef EA_COMPILER_NO_TEMPLATE_SPECIALIZATION
  1078. // Todo
  1079. #endif
  1080. #ifndef EA_COMPILER_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  1081. // Todo
  1082. #endif
  1083. #ifndef EA_COMPILER_NO_MEMBER_TEMPLATES
  1084. // Todo
  1085. #endif
  1086. #ifndef EA_COMPILER_NO_MEMBER_TEMPLATE_SPECIALIZATION
  1087. // Todo
  1088. #endif
  1089. #ifndef EA_COMPILER_NO_TEMPLATE_TEMPLATES
  1090. // Todo
  1091. #endif
  1092. #ifndef EA_COMPILER_NO_MEMBER_TEMPLATE_FRIENDS
  1093. // Todo
  1094. #endif
  1095. #ifndef EA_COMPILER_NO_VOID_RETURNS
  1096. void TestNVR1();
  1097. void TestNVR();
  1098. void TestNVR1()
  1099. {
  1100. char buffer[8];
  1101. sprintf(buffer, " ");
  1102. }
  1103. void TestNVR()
  1104. {
  1105. return TestNVR1();
  1106. }
  1107. #endif
  1108. #ifndef EA_COMPILER_NO_COVARIANT_RETURN_TYPE
  1109. // Todo
  1110. #endif
  1111. #ifndef EA_COMPILER_NO_DEDUCED_TYPENAME
  1112. // Todo
  1113. #endif
  1114. #ifndef EA_COMPILER_NO_ARGUMENT_DEPENDENT_LOOKUP
  1115. // Todo
  1116. #endif
  1117. #if !defined(EA_COMPILER_NO_EXCEPTION_STD_NAMESPACE) && !defined(EA_COMPILER_NO_STANDARD_CPP_LIBRARY)
  1118. #include <exception>
  1119. static void TestNESN()
  1120. {
  1121. // iPhone gives us this error: Undefined symbols for architecture armv6: std::terminate()
  1122. // Android gives: undefined reference to std::terminate()
  1123. // We could possibly define our own std::terminate, but that might collide in the future unexpectedly.
  1124. #if defined(EA_PLATFORM_IPHONE) || defined(EA_PLATFORM_ANDROID)
  1125. void (*pTerminate)() = NULL;
  1126. #else
  1127. void (*pTerminate)() = std::terminate;
  1128. #endif
  1129. char buffer[32];
  1130. sprintf(buffer, "%p", pTerminate);
  1131. }
  1132. #endif
  1133. #ifndef EA_COMPILER_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
  1134. // Todo
  1135. #endif
  1136. #ifndef EA_COMPILER_NO_EXCEPTIONS
  1137. static bool TestNE()
  1138. {
  1139. EA_DISABLE_VC_WARNING(4571)
  1140. try{
  1141. char buffer1[8];
  1142. sprintf(buffer1, " ");
  1143. throw int(0);
  1144. }
  1145. catch(...){
  1146. char buffer2[8];
  1147. sprintf(buffer2, " "); // If you are tracing this in a debugger and the debugger stops here, then you need to let the app continue.
  1148. }
  1149. return true;
  1150. EA_RESTORE_VC_WARNING()
  1151. }
  1152. #endif
  1153. struct UnwindTest
  1154. {
  1155. static int x;
  1156. enum State{
  1157. kStateNone,
  1158. kStateConstructed,
  1159. kStateDestructed
  1160. };
  1161. UnwindTest()
  1162. { x = kStateConstructed; };
  1163. ~UnwindTest()
  1164. { x = kStateDestructed; };
  1165. };
  1166. int UnwindTest::x = kStateNone;
  1167. #ifndef EA_COMPILER_NO_EXCEPTIONS
  1168. static void TestNU1()
  1169. {
  1170. UnwindTest ut;
  1171. #ifndef EA_COMPILER_NO_EXCEPTIONS
  1172. throw(int(0)); // If you are tracing this in a debugger and the debugger stops here, then you need to let the app continue.
  1173. #endif
  1174. }
  1175. #endif
  1176. bool TestNU()
  1177. {
  1178. bool bReturnValue(false);
  1179. #ifdef EA_COMPILER_NO_EXCEPTIONS
  1180. bReturnValue = true; //Nothing to test, so we just return true.
  1181. #else
  1182. EA_DISABLE_VC_WARNING(4571)
  1183. try
  1184. {
  1185. TestNU1();
  1186. }
  1187. catch(...)
  1188. {
  1189. #ifdef EA_COMPILER_NO_UNWIND
  1190. if(UnwindTest::x == UnwindTest::kStateConstructed)
  1191. bReturnValue = true;
  1192. #else
  1193. if(UnwindTest::x == UnwindTest::kStateDestructed)
  1194. bReturnValue = true;
  1195. #endif
  1196. }
  1197. EA_RESTORE_VC_WARNING()
  1198. #endif
  1199. return bReturnValue;
  1200. }
  1201. #ifndef EA_COMPILER_NO_STANDARD_CPP_LIBRARY
  1202. #include <vector> // We need do nothing more than #include this.
  1203. #endif
  1204. #ifndef EA_COMPILER_NO_COVARIANT_RETURN_TYPE
  1205. // Todo
  1206. #endif
  1207. #ifndef EA_COMPILER_NO_COVARIANT_RETURN_TYPE
  1208. // Todo
  1209. #endif
  1210. #if !defined(EA_COMPILER_NO_TRAILING_RETURN_TYPES)
  1211. // This usage assumes that C++11 auto is supported, which in practice is always the case because
  1212. // the case because otherwise trailing return types wouldn't be as useful.
  1213. static auto AddOne(int i)->int
  1214. {
  1215. return i + 1;
  1216. }
  1217. template <typename T>
  1218. struct AddTwoClass
  1219. {
  1220. typedef float Local_type;
  1221. Local_type AddTwo(T t);
  1222. };
  1223. template <typename T>
  1224. auto AddTwoClass<T>::AddTwo(T t)->Local_type
  1225. {
  1226. return (t + 2.f); // Assumes that t is a numerical type in this case.
  1227. }
  1228. #endif
  1229. #if !defined(EA_COMPILER_NO_VARIADIC_TEMPLATES)
  1230. template<typename...>
  1231. struct VariadicTemplateTuple{};
  1232. template<typename T1, typename T2>
  1233. struct VariadicTemplatePair
  1234. {
  1235. T1 x;
  1236. T1 y;
  1237. };
  1238. template<class ... Args1>
  1239. struct VariadicTemplateZip
  1240. {
  1241. template<class ... Args2> struct with
  1242. {
  1243. typedef VariadicTemplateTuple<VariadicTemplatePair<Args1, Args2> ... > type;
  1244. };
  1245. };
  1246. // VariadicTemplateType is Tuple<Pair<short, unsigned short>, Pair<int, unsigned> >
  1247. typedef VariadicTemplateZip<short, int>::with<unsigned short, unsigned>::type VariadicTemplateType;
  1248. #endif
  1249. #if !defined(EA_COMPILER_NO_TEMPLATE_ALIASES)
  1250. template<typename T>
  1251. using VectorAlias = eastl::vector<T, EASTLAllocatorType>;
  1252. #endif
  1253. #if !defined(EA_COMPILER_NO_VARIABLE_TEMPLATES)
  1254. template<class T>
  1255. constexpr T pi = T(3.1415926535897932385);
  1256. #endif
  1257. int TestEACompiler()
  1258. {
  1259. int nErrorCount(0);
  1260. // As of this writing, eacompiler.h defines at least the following compilers:
  1261. // EA_COMPILER_GNUC
  1262. // EA_COMPILER_BORLANDC
  1263. // EA_COMPILER_INTEL
  1264. // EA_COMPILER_METROWERKS
  1265. // EA_COMPILER_MSVC, EA_COMPILER_MSVC6, EA_COMPILER_MSVC7, EA_COMPILER_MSVC7_1
  1266. // Test EA_COMPILER_NAME
  1267. {
  1268. #ifdef EA_COMPILER_NAME
  1269. char buffer[256];
  1270. sprintf(buffer, "TestEACompiler: EA_COMPILER_NAME: %s\n", EA_COMPILER_NAME);
  1271. #else
  1272. DoError(nErrorCount, "EA_COMPILER_NAME test");
  1273. #endif
  1274. }
  1275. // Test EA_COMPILER_VERSION
  1276. {
  1277. #ifdef EA_COMPILER_VERSION
  1278. char buffer[256];
  1279. sprintf(buffer, "TestEACompiler: EA_COMPILER_VERSION: %d\n", EA_COMPILER_VERSION);
  1280. #else
  1281. DoError(nErrorCount, "EA_COMPILER_VERSION test");
  1282. #endif
  1283. }
  1284. // Test EA_COMPILER_STRING
  1285. {
  1286. #ifdef EA_COMPILER_STRING
  1287. char buffer[256];
  1288. sprintf(buffer, "TestEACompiler: EA_COMPILER_STRING: %s\n", EA_COMPILER_STRING);
  1289. #else
  1290. DoError(nErrorCount, "EA_COMPILER_STRING test");
  1291. #endif
  1292. }
  1293. // Test EA_COMPILER_NO_STATIC_CONSTANTS
  1294. {
  1295. char buffer[256];
  1296. sprintf(buffer, "%d", (int)NSC::x);
  1297. if(buffer[0] != '1')
  1298. DoError(nErrorCount, "EA_COMPILER_NO_STATIC_CONSTANTS test");
  1299. }
  1300. // Test EA_COMPILER_NO_VOID_RETURNS
  1301. #ifndef EA_COMPILER_NO_VOID_RETURNS
  1302. TestNVR1(); // Nothing to test for except successful compilation.
  1303. #endif
  1304. // Test EA_COMPILER_NO_EXCEPTION_STD_NAMESPACE
  1305. #if !defined(EA_COMPILER_NO_EXCEPTION_STD_NAMESPACE) && !defined(EA_COMPILER_NO_STANDARD_CPP_LIBRARY)
  1306. TestNESN();
  1307. #endif
  1308. #if !(defined(EA_PLATFORM_IPHONE) && defined(EA_COMPILER_CLANG)) || defined(__IPHONE_7_0)
  1309. {
  1310. // There was a bug in Apple's exception code in iOS SDK versions
  1311. // prior to 7.0, which is why this test is disabled for versions
  1312. // of the SDK before 7.0.
  1313. // Note that __IPHONE_7_0 will be defined for all future SDKs as
  1314. // well, because Apple simply adds another define with each release
  1315. // and does not remove the old ones.
  1316. // Test EA_COMPILER_NO_EXCEPTIONS
  1317. #ifndef EA_COMPILER_NO_EXCEPTIONS
  1318. if(!TestNE())
  1319. DoError(nErrorCount, "EA_COMPILER_NO_EXCEPTIONS test");
  1320. #endif
  1321. // Test EA_COMPILER_NO_UNWIND
  1322. if(!TestNU())
  1323. DoError(nErrorCount, "EA_COMPILER_NO_UNWIND test");
  1324. }
  1325. #endif
  1326. // Test EA_COMPILER_NO_RVALUE_REFERENCES
  1327. #ifndef EA_COMPILER_NO_RVALUE_REFERENCES
  1328. {
  1329. // Trivial test
  1330. int&& i = 2;
  1331. FunctionWithUnusedVariables(i);
  1332. }
  1333. #endif
  1334. // Test EA_COMPILER_NO_RANGE_BASED_FOR_LOOP
  1335. #if !defined(EA_COMPILER_NO_RANGE_BASED_FOR_LOOP)
  1336. {
  1337. float floatArray[2] = { 0.0f, 1.0f };
  1338. for(float& f : floatArray)
  1339. f += 1.0;
  1340. EATEST_VERIFY(floatArray[1] == 2.0f);
  1341. }
  1342. #endif
  1343. // Test EA_COMPILER_NO_AUTO
  1344. #if !defined(EA_COMPILER_NO_AUTO)
  1345. {
  1346. auto length = strlen("test");
  1347. EATEST_VERIFY(length == 4);
  1348. }
  1349. #endif
  1350. // Test EA_COMPILER_NO_DECLTYPE
  1351. #if !defined(EA_COMPILER_NO_DECLTYPE)
  1352. {
  1353. struct A { double x; };
  1354. int b = 9;
  1355. A a; a.x = 7;
  1356. decltype(b) x2 = 2;
  1357. decltype(a.x) x3 = 3.5;
  1358. EATEST_VERIFY((b + a.x) == 16);
  1359. EATEST_VERIFY((x3 + x2) == 5.5);
  1360. }
  1361. #endif
  1362. // Test EA_COMPILER_NO_LAMBDA_EXPRESSIONS
  1363. #if !defined(EA_COMPILER_NO_LAMBDA_EXPRESSIONS)
  1364. {
  1365. struct LambaTest
  1366. {
  1367. static void SortViaAbs(float* x, size_t n)
  1368. {
  1369. eastl::insertion_sort(x, x + n,
  1370. [](float a, float b)
  1371. { return (a < b); }
  1372. );
  1373. }
  1374. };
  1375. float floatArray[3] = { 0.f, 1.f, 3.f };
  1376. LambaTest::SortViaAbs(floatArray, EAArrayCount(floatArray));
  1377. EATEST_VERIFY(floatArray[1] == 1.f);
  1378. }
  1379. #endif
  1380. // Test EA_COMPILER_NO_TRAILING_RETURN_TYPES
  1381. #if !defined(EA_COMPILER_NO_TRAILING_RETURN_TYPES)
  1382. {
  1383. int x = AddOne(2); // AddOne declared above.
  1384. EATEST_VERIFY(x == 3);
  1385. AddTwoClass<float> a;
  1386. float y = a.AddTwo(2.f);
  1387. EATEST_VERIFY(y == 4.f);
  1388. }
  1389. #endif
  1390. // Test EA_COMPILER_NO_FORWARD_DECLARED_ENUMS
  1391. // Forward declared enum support requires strongly typed enum support.
  1392. #if !defined(EA_COMPILER_NO_FORWARD_DECLARED_ENUMS) && !defined(EA_COMPILER_NO_STRONGLY_TYPED_ENUMS)
  1393. {
  1394. // This happen to be used below in the EA_COMPILER_NO_STRONGLY_TYPED_ENUMS section.
  1395. enum class Color;
  1396. enum class Size : uint8_t;
  1397. enum Distance: uint8_t;
  1398. }
  1399. #endif
  1400. // Test EA_COMPILER_NO_STRONGLY_TYPED_ENUMS
  1401. #if !defined(EA_COMPILER_NO_STRONGLY_TYPED_ENUMS)
  1402. {
  1403. enum class Color { red, blue, green };
  1404. enum class Size : uint8_t { little = 1, med = 1, large = 2 };
  1405. enum Distance : uint8_t { close = 1, faraway = 2 };
  1406. Color c = Color::red;
  1407. EATEST_VERIFY(c != Color::blue);
  1408. Size s = Size::med;
  1409. EATEST_VERIFY(s != Size::large);
  1410. static_assert(sizeof(s) == 1, "EA_COMPILER_NO_STRONGLY_TYPED_ENUMS failure");
  1411. Distance d = close;
  1412. EATEST_VERIFY(d != faraway);
  1413. static_assert(sizeof(d) == 1, "EA_COMPILER_NO_STRONGLY_TYPED_ENUMS failure");
  1414. }
  1415. #endif
  1416. // Test EA_COMPILER_NO_VARIADIC_TEMPLATES
  1417. #if !defined(EA_COMPILER_NO_VARIADIC_TEMPLATES)
  1418. {
  1419. // This uses types defined above.
  1420. VariadicTemplateType x;
  1421. static_assert(sizeof(x) > 0, "EA_COMPILER_NO_VARIADIC_TEMPLATES failure");
  1422. char buffer[32];
  1423. sprintf(buffer, "%p", &x);
  1424. // Ignore the result, as we're just verifying that it compiles.
  1425. }
  1426. #endif
  1427. // Test EA_COMPILER_NO_TEMPLATE_ALIASES
  1428. #if !defined(EA_COMPILER_NO_TEMPLATE_ALIASES)
  1429. {
  1430. // This uses types defined above.
  1431. // Same as vector<int, EASTLAllocatorType> v;
  1432. VectorAlias<int> v;
  1433. EATEST_VERIFY(v.empty());
  1434. }
  1435. #endif
  1436. // Test EA_COMPILER_NO_VARIABLE_TEMPLATES
  1437. #if !defined(EA_COMPILER_NO_VARIABLE_TEMPLATES)
  1438. static_assert(pi<int> == 3, "variable template failure");
  1439. static_assert(pi<intmax_t> == 3, "variable template failure");
  1440. static_assert(pi<double> == 3.1415926535897932385, "variable template failure");
  1441. #endif
  1442. // Test EA_COMPILER_NO_INITIALIZER_LISTS
  1443. #if !defined(EA_COMPILER_NO_INITIALIZER_LISTS)
  1444. {
  1445. int a = { 1 };
  1446. EATEST_VERIFY(a == 1);
  1447. int* e{};
  1448. EATEST_VERIFY(!e);
  1449. double x = double{1};
  1450. EATEST_VERIFY(x == 1.0);
  1451. //Disabled until we have a compiler and standard library that can exercise this.
  1452. //#include <initializer_list>
  1453. //eastl::vector<eastl::string, int> anim = { {"bear", 4}, {"cassowary", 2}, {"tiger", 7} };
  1454. //EATEST_VERIFY(!anim.empty());
  1455. // Other tests to do.
  1456. //std::complex<double> z{1,2};
  1457. //eastl::vector<int>{1, 2, 3, 4};
  1458. //f({"Nicholas","Annemarie"});
  1459. //return { "Norah" };
  1460. }
  1461. #endif
  1462. // Test EA_COMPILER_NO_NORETURN / EA_NORETURN
  1463. #if !defined(EA_COMPILER_NO_NORETURN) && !defined(EA_PLATFORM_KETTLE) // Kettle SDK up to at least v.915 has a broken definition of the exit() function and fails to compile the valid code below.
  1464. {
  1465. struct NoReturnTest
  1466. {
  1467. EA_NORETURN void DoesNotReturn()
  1468. { exit(0); }
  1469. int DoesReturn()
  1470. { return 17; }
  1471. };
  1472. NoReturnTest nrt;
  1473. if(nrt.DoesReturn() == 18)
  1474. nrt.DoesNotReturn();
  1475. }
  1476. #endif
  1477. // Test EA_COMPILER_NO_CARRIES_DEPENDENCY / EA_CARRIES_DEPENDENCY
  1478. #if !defined(EA_COMPILER_NO_CARRIES_DEPENDENCY)
  1479. {
  1480. struct CarriesDependencyTest
  1481. {
  1482. CarriesDependencyTest() : mX(0){}
  1483. EA_CARRIES_DEPENDENCY int* Test1(){ return &mX; }
  1484. void Test2(int* f EA_CARRIES_DEPENDENCY) { char buffer[32]; sprintf(buffer, "%p", f); }
  1485. int mX;
  1486. };
  1487. CarriesDependencyTest cdt;
  1488. cdt.Test2(cdt.Test1());
  1489. }
  1490. #endif
  1491. // Test EA_COMPILER_NO_FALLTHROUGH / EA_FALLTHROUGH
  1492. #if !defined(EA_COMPILER_NO_FALLTHROUGH)
  1493. {
  1494. int i = 1;
  1495. switch (i)
  1496. {
  1497. case 1:
  1498. i++;
  1499. EA_FALLTHROUGH;
  1500. case 2: { i = 42; }
  1501. break;
  1502. }
  1503. EATEST_VERIFY(i == 42);
  1504. }
  1505. #endif
  1506. // Test EA_COMPILER_NO_NODISCARD / EA_NODISCARD
  1507. #if !defined(EA_COMPILER_NO_NODISCARD)
  1508. {
  1509. struct EA_NODISCARD DoNotDiscardMe {};
  1510. auto result = [](void) -> DoNotDiscardMe { return {}; }();
  1511. (void)result; // use the result to avoid [[nodiscard]] compiler warnings
  1512. }
  1513. #endif
  1514. // Test EA_COMPILER_NO_MAYBE_UNUSED / EA_MAYBE_UNUSED
  1515. #if !defined(EA_COMPILER_NO_MAYBE_UNUSED)
  1516. {
  1517. {
  1518. EA_MAYBE_UNUSED int notAlwaysUsed = 42;
  1519. // Do not use expressions below. It defeats the purpose of the test.
  1520. // (void)notAlwaysUsed;
  1521. // EA_UNUSED(notAlwaysUsed);
  1522. }
  1523. {
  1524. [](EA_MAYBE_UNUSED bool b1, EA_MAYBE_UNUSED bool b2) { EA_ASSERT(b1 && b2); }(true, true);
  1525. }
  1526. }
  1527. #endif
  1528. #if !defined(EA_COMPILER_NO_NONSTATIC_MEMBER_INITIALIZERS)
  1529. {
  1530. struct NonstaticInitializerTest
  1531. {
  1532. int a = 7;
  1533. int b = a;
  1534. };
  1535. NonstaticInitializerTest nit;
  1536. EATEST_VERIFY((nit.a == 7) && (nit.b == 7));
  1537. }
  1538. #endif
  1539. #if !defined(EA_COMPILER_NO_RIGHT_ANGLE_BRACKETS)
  1540. {
  1541. eastl::vector<eastl::vector<int>> listList;
  1542. EA_UNUSED(listList);
  1543. }
  1544. #endif
  1545. #if !defined(EA_COMPILER_NO_ALIGNOF)
  1546. {
  1547. char buffer[32];
  1548. sprintf(buffer, "%u", (unsigned)alignof(uint64_t));
  1549. }
  1550. #endif
  1551. #if !defined(EA_COMPILER_NO_ALIGNAS)
  1552. {
  1553. struct alignas(32) AlignAsTest1
  1554. { float mData[4]; };
  1555. struct alignas(uint64_t) AlignAsTest2
  1556. { float mData[4]; };
  1557. char buffer[32];
  1558. sprintf(buffer, "%u %u", (unsigned)EA_ALIGN_OF(AlignAsTest1), (unsigned)EA_ALIGN_OF(AlignAsTest2));
  1559. }
  1560. #endif
  1561. #if !defined(EA_COMPILER_NO_DELEGATING_CONSTRUCTORS)
  1562. {
  1563. struct DCTest
  1564. {
  1565. char mChar;
  1566. double mDouble;
  1567. DCTest() : mChar('\0'), mDouble(1.23){ };
  1568. DCTest(double d, char c): mChar(c), mDouble(d) { }
  1569. DCTest(char c) : DCTest(1.23, c) { }
  1570. DCTest(double d): DCTest(d, 'a') { }
  1571. DCTest(char*): DCTest() { }
  1572. };
  1573. DCTest dcTest(1.5);
  1574. EATEST_VERIFY(dcTest.mDouble == 1.5);
  1575. }
  1576. #endif
  1577. #if !defined(EA_COMPILER_NO_INHERITING_CONSTRUCTORS)
  1578. {
  1579. struct B1{
  1580. B1(int x) : mX(x){}
  1581. int mX;
  1582. };
  1583. struct B2{
  1584. B2(int x = 13, int y = 42) : mX(x), mY(y){}
  1585. int mX, mY;
  1586. };
  1587. struct D1 : B1 {
  1588. using B1::B1;
  1589. };
  1590. struct D2 : B2 {
  1591. using B2::B2;
  1592. };
  1593. D1 d1(3);
  1594. D2 d2a(17, 22);
  1595. D2 d2b;
  1596. EATEST_VERIFY((d1.mX == 3) &&
  1597. (d2a.mX == 17) && (d2a.mY == 22) &&
  1598. (d2b.mX == 13) && (d2b.mY == 42));
  1599. }
  1600. #endif
  1601. #if !defined(EA_COMPILER_NO_USER_DEFINED_LITERALS)
  1602. {
  1603. // The operators are defined above.
  1604. // Conversion example
  1605. double x = 90.0_deg; // x = 1.570796
  1606. EATEST_VERIFY((x > 1.57) && (x < 1.58));
  1607. // Custom type example
  1608. UDLTest y(123_udl);
  1609. EATEST_VERIFY(y.mX == 123);
  1610. }
  1611. #endif
  1612. #if !defined(EA_COMPILER_NO_STANDARD_LAYOUT_TYPES)
  1613. {
  1614. // We don't currently have a good way of testing this without bringing in <type_traits>.
  1615. }
  1616. #endif
  1617. #if !defined(EA_COMPILER_NO_EXTENDED_SIZEOF)
  1618. {
  1619. struct SizeofTest{
  1620. int32_t mMember;
  1621. };
  1622. const size_t testSize = sizeof(SizeofTest::mMember);
  1623. EATEST_VERIFY(testSize == sizeof(int32_t));
  1624. char buffer[32];
  1625. sprintf(buffer, "%u", (unsigned)testSize);
  1626. }
  1627. #endif
  1628. #if !defined(EA_COMPILER_NO_INLINE_NAMESPACES)
  1629. {
  1630. // The namespaces are defined above.
  1631. INSNamespace::A<INSClass> a;
  1632. int result = g(a);
  1633. EATEST_VERIFY(result == 37);
  1634. }
  1635. #endif
  1636. #if !defined(EA_COMPILER_NO_UNRESTRICTED_UNIONS)
  1637. {
  1638. struct Point {
  1639. int mX, mY;
  1640. Point(int x = 0, int y = 0) : mX(x), mY(y) {}
  1641. };
  1642. union U {
  1643. int z;
  1644. double w;
  1645. Point p; // Illegal in C++03; legal in C++11.
  1646. U() { new(&p) Point(); } // Due to the Point member, a constructor definition is now required.
  1647. };
  1648. }
  1649. #endif
  1650. #if !defined(EA_COMPILER_NO_EXPLICIT_CONVERSION_OPERATORS)
  1651. {
  1652. // bool cast test
  1653. struct Testable
  1654. {
  1655. explicit operator bool() const
  1656. { return false; }
  1657. Testable() : mX(37) { }
  1658. int mX;
  1659. };
  1660. Testable a;
  1661. if(a)
  1662. EATEST_VERIFY(a.mX == 37);
  1663. // Class cast test
  1664. struct Y {
  1665. int mY;
  1666. Y(int y = 0) : mY(y) { }
  1667. };
  1668. struct Z {
  1669. int mZ;
  1670. Z(int z = 0) : mZ(z) { }
  1671. explicit operator Y() const { return Y(mZ); }
  1672. };
  1673. Z z(3);
  1674. Y y1(z); // Direct initialization
  1675. Y y2 = (Y)z; // Cast notation
  1676. EATEST_VERIFY((z.mZ == 3) && (y1.mY == 3) && (y2.mY == 3));
  1677. }
  1678. #endif
  1679. #if !defined(EA_COMPILER_NO_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  1680. {
  1681. // FunctionTemplateTest is declared above.
  1682. int result = FunctionTemplateTest::AddOne((int)3);
  1683. EATEST_VERIFY(result == 4);
  1684. }
  1685. #endif
  1686. #if !defined(EA_COMPILER_NO_LOCAL_CLASS_TEMPLATE_PARAMETERS)
  1687. {
  1688. struct LocalStruct{};
  1689. eastl::fixed_vector<LocalStruct, 2, false> localStructArray;
  1690. EATEST_VERIFY(localStructArray.empty());
  1691. }
  1692. #endif
  1693. #if !defined(EA_COMPILER_NO_NOEXCEPT)
  1694. {
  1695. EATEST_VERIFY(NoExceptTestFunction() == 37);
  1696. EATEST_VERIFY(NoExceptTestTemplate<NoExceptTestStruct>() == 37);
  1697. }
  1698. #endif
  1699. #if !defined(EA_COMPILER_NO_RAW_LITERALS)
  1700. {
  1701. // Older versions of GCC are preventing us from using " below in str1. Due to the way the preprocessor
  1702. // works, it encounters what it sees as a string problem before it handles the #if above. No #ifdefs
  1703. // can make this problem go away.
  1704. const char str1[] = R"(This slash is just a slash: \ This quote is just a quote: ' )";
  1705. const char str2[] = R"delimiter(This slash is just a slash: \ This paren is just a paren: ) )delimiter";
  1706. EA_UNUSED(str1);
  1707. EA_UNUSED(str2);
  1708. static_assert(EAArrayCount(str1) == 61, "EA_COMPILER_NO_RAW_LITERALS failure.");
  1709. static_assert(EAArrayCount(str2) == 61, "EA_COMPILER_NO_RAW_LITERALS failure.");
  1710. }
  1711. #endif
  1712. #if !defined(EA_COMPILER_NO_UNICODE_STRING_LITERALS)
  1713. {
  1714. const char8_t str1[] = u8"Unicode: \u2018."; // This assumes that \u and \U are supported by the compiler.
  1715. const char16_t str2[] = u"Unicode: \U00002018.";
  1716. const char32_t str3[] = U"Unicode: \U00022018.";
  1717. static_assert(EAArrayCount(str1) == 14, "EA_COMPILER_NO_UNICODE_STRING_LITERALS failure.");
  1718. static_assert(EAArrayCount(str2) == 12, "EA_COMPILER_NO_UNICODE_STRING_LITERALS failure.");
  1719. static_assert(EAArrayCount(str3) == 12, "EA_COMPILER_NO_UNICODE_STRING_LITERALS failure.");
  1720. }
  1721. #endif
  1722. #if !defined(EA_COMPILER_NO_UNICODE_CHAR_NAME_LITERALS)
  1723. {
  1724. const char8_t str1[] = "\u2018\u2019";
  1725. static_assert(EAArrayCount(str1) == 7, "EA_COMPILER_NO_UNICODE_CHAR_NAME_LITERALS failure.");
  1726. #if (EA_WCHAR_SIZE >= 2)
  1727. const wchar_t str2[] = L"\U00002018\U00002019";
  1728. static_assert(EAArrayCount(str2) == 3, "EA_COMPILER_NO_UNICODE_CHAR_NAME_LITERALS failure."); // This test assumes that wchar_t is a 16bit or greater value.
  1729. #endif
  1730. #if defined(EA_CHAR16_NATIVE) && EA_CHAR16_NATIVE
  1731. const char16_t str3[] = u"\U00002018\U00002019";
  1732. static_assert(EAArrayCount(str3) == 3, "EA_COMPILER_NO_UNICODE_CHAR_NAME_LITERALS failure.");
  1733. #endif
  1734. }
  1735. #endif
  1736. #ifndef EA_COMPILER_NO_RVALUE_REFERENCES
  1737. {
  1738. const int MAX_ARR_SIZE = 4096;
  1739. struct StructWithArray { int arr[MAX_ARR_SIZE]; };
  1740. static_assert(EAArrayCount(StructWithArray().arr) == MAX_ARR_SIZE, "");
  1741. }
  1742. #endif
  1743. #if !defined(EA_COMPILER_NO_UNIFIED_INITIALIZATION_SYNTAX)
  1744. {
  1745. struct InitTest1
  1746. {
  1747. int mX;
  1748. double mY;
  1749. };
  1750. struct InitTest2
  1751. {
  1752. InitTest2(int x, double y) : mX{x}, mY{y} {}
  1753. int mX;
  1754. double mY;
  1755. };
  1756. InitTest1 var1{5, 3.2};
  1757. InitTest2 var2{2, 4.3};
  1758. EATEST_VERIFY(var1.mY == 3.2);
  1759. EATEST_VERIFY(var2.mY == 4.3);
  1760. }
  1761. #endif
  1762. #if !defined(EA_COMPILER_NO_EXTENDED_FRIEND_DECLARATIONS)
  1763. {
  1764. class G;
  1765. class X1 {
  1766. friend G;
  1767. };
  1768. }
  1769. #endif
  1770. #if !defined(EA_COMPILER_NO_THREAD_LOCAL)
  1771. {
  1772. // We don't yet test this because we don't have a sufficient compiler to test it with.
  1773. }
  1774. #endif
  1775. return nErrorCount;
  1776. }
  1777. #if defined(EA_COMPILER_MSVC) && EA_COMPILER_VERSION >= 1900 // VS2015+
  1778. EA_DISABLE_VC_WARNING(5029); // nonstandard extension used: alignment attributes in C++ apply to variables, data members and tag types only
  1779. #endif
  1780. int TestEACompilerTraits()
  1781. {
  1782. int nErrorCount(0);
  1783. // EA_COMPILER_IS_ANSIC
  1784. // EA_COMPILER_IS_C99
  1785. // EA_COMPILER_IS_CPLUSPLUS
  1786. // EA_COMPILER_MANAGED_CPP
  1787. {
  1788. // EA_COMPILER_INTMAX_SIZE
  1789. #if (EA_COMPILER_INTMAX_SIZE == 16)
  1790. /* To do: Test this when we get a machine that supports it (e.g. Linux64)/
  1791. #if defined(__GNUC__)
  1792. #define int128_t __int128_t
  1793. #define uint128_t __uint128_t
  1794. #endif
  1795. int128_t x = UINT128_C(0x12345678123456781234567812345678);
  1796. uint128_t y = (x * 2);
  1797. if(x == (int128_t)y)
  1798. DoError(nErrorCount, "EA_COMPILER_INTMAX_SIZE test");
  1799. */
  1800. #elif (EA_COMPILER_INTMAX_SIZE == 8)
  1801. int64_t x = UINT64_C(0x1234567812345678);
  1802. uint64_t y = (x * 2);
  1803. if(x == (int64_t)y)
  1804. DoError(nErrorCount, "EA_COMPILER_INTMAX_SIZE test");
  1805. #elif (EA_COMPILER_INTMAX_SIZE == 32)
  1806. int32_t x = UINT64_C(0x12345678);
  1807. uint32_t y = (x * 2);
  1808. if(x == (int32_t)y)
  1809. DoError(nErrorCount, "EA_COMPILER_INTMAX_SIZE test");
  1810. #else
  1811. int16_t x = UINT16_C(0x1234);
  1812. uint16_t y = (x * 2);
  1813. if(x == (int16_t)y)
  1814. DoError(nErrorCount, "EA_COMPILER_INTMAX_SIZE test");
  1815. #endif
  1816. }
  1817. {
  1818. // EA_OFFSETOF
  1819. const size_t o = EA_OFFSETOF(OffsetofTestClass, mY);
  1820. EA_DISABLE_VC_WARNING(6326)
  1821. if(o != 4)
  1822. DoError(nErrorCount, "EA_OFFSETOF test");
  1823. EA_RESTORE_VC_WARNING()
  1824. }
  1825. {
  1826. // EA_SIZEOF_MEMBER
  1827. const size_t s = EA_SIZEOF_MEMBER(SizeofMemberTestClass, mY);
  1828. EA_DISABLE_VC_WARNING(6326)
  1829. if(s != 4)
  1830. DoError(nErrorCount, "EA_SIZEOF_MEMBER test");
  1831. EA_RESTORE_VC_WARNING()
  1832. // There have been problems on some platforms (SNC version < 405) where extended sizeof was not properly
  1833. // supported when used within a member function, so we test for that here.
  1834. class TestClass
  1835. {
  1836. public:
  1837. void TestExtendedSizeof(int& nErrorCount)
  1838. {
  1839. EA_DISABLE_VC_WARNING(6326)
  1840. const size_t sizeOfmY = EA_SIZEOF_MEMBER(SizeofMemberTestClass, mY);
  1841. if(sizeOfmY != 4)
  1842. DoError(nErrorCount, "EA_SIZEOF_MEMBER test: within member function");
  1843. EA_RESTORE_VC_WARNING()
  1844. }
  1845. }tc;
  1846. tc.TestExtendedSizeof(nErrorCount);
  1847. }
  1848. { // EA_ALIGN_OF, EA_PREFIX_ALIGN, etc.
  1849. size_t a = EA_ALIGN_OF(int);
  1850. EA_PREFIX_ALIGN(4) int b = 5;
  1851. EA_ALIGN(8) int c;
  1852. int d EA_POSTFIX_ALIGN(8);
  1853. int e EA_POSTFIX_ALIGN(8) = 5;
  1854. int f EA_POSTFIX_ALIGN(8)(5);
  1855. struct EA_ALIGN(8) G { int x; };
  1856. struct EA_PREFIX_ALIGN(8) GG { int x; } EA_POSTFIX_ALIGN(8);
  1857. EA_ALIGNED(int, h, 8) = 5;
  1858. EA_ALIGNED(int, i, ALIGNMENT_AMOUNT_16)(5);
  1859. EA_ALIGNED(int, j[3], ALIGNMENT_AMOUNT_16);
  1860. EA_ALIGNED(int, k[3], ALIGNMENT_AMOUNT_16) = { 1, 2, 3 };
  1861. struct EA_ALIGN(8) L { int x; int y; };
  1862. EA_ALIGN(ALIGNMENT_AMOUNT_32) struct X { int x; int y; } m;
  1863. //int N[3] EA_PACKED; // Some compilers (e.g. GCC) don't support this or ignore this and generate a warning.
  1864. struct P { int x EA_PACKED; int y EA_PACKED; };
  1865. struct Q { int x; int y; } EA_PACKED;
  1866. typedef EA_ALIGNED(int, r, ALIGNMENT_AMOUNT_16);
  1867. r rInstance;
  1868. typedef EA_ALIGNED(Q, X16, ALIGNMENT_AMOUNT_16);
  1869. X16 x16Instance;
  1870. char buffer[256];
  1871. sprintf(buffer, "%p %p %p %p %p %p %p %p %p %p %p %p %p", &a, &b, &c, &d, &e, &f, &h, &i, &j, &k, &m, &rInstance, &x16Instance);
  1872. }
  1873. { // Test EA_ALIGN_OF
  1874. if(EA_ALIGN_OF(int8_t) != sizeof(int8_t)) // This may not be a kosher test.
  1875. DoError(nErrorCount, "EA_ALIGN_OF test (int16_t)");
  1876. if(EA_ALIGN_OF(int16_t) != sizeof(int16_t)) // This may not be a kosher test.
  1877. DoError(nErrorCount, "EA_ALIGN_OF test (int16_t)");
  1878. if(EA_ALIGN_OF(int32_t) != sizeof(int32_t)) // This may not be a kosher test.
  1879. DoError(nErrorCount, "EA_ALIGN_OF test (int32_t)");
  1880. #if !defined(EA_ABI_ARM_APPLE)
  1881. if(EA_ALIGN_OF(int64_t) != sizeof(int64_t)) // This may not be a kosher test.
  1882. DoError(nErrorCount, "EA_ALIGN_OF test (int64_t)");
  1883. #endif
  1884. typedef void (*AlignTestFunctionType)();
  1885. if(EA_ALIGN_OF(AlignTestFunctionType) != sizeof(void*)) // This may not be a kosher test.
  1886. DoError(nErrorCount, "EA_ALIGN_OF test (AlignTestFunctionType)");
  1887. }
  1888. { // Test EA_ALIGN
  1889. #ifdef EA_ALIGN
  1890. char buffer[32];
  1891. EA_ALIGN(ALIGNMENT_AMOUNT_64) int x(0);
  1892. sprintf(buffer, "%d", x);
  1893. if(buffer[0] != '0')
  1894. DoError(nErrorCount, "EA_ALIGN test 1");
  1895. if((intptr_t)&x & (ALIGNMENT_AMOUNT_64 -1))
  1896. DoError(nErrorCount, "EA_ALIGN test 2");
  1897. EA_ALIGN(ALIGNMENT_AMOUNT_64) ClassWithDefaultCtor cdcA;
  1898. //EA_ALIGN(64) ClassWithoutDefaultCtor cwdcA;
  1899. if((intptr_t)&cdcA & (ALIGNMENT_AMOUNT_64 -1))
  1900. DoError(nErrorCount, "EA_ALIGN test 3");
  1901. EA_ALIGN(ALIGNMENT_AMOUNT_64) ClassWithDefaultCtor cdcB(3);
  1902. if((intptr_t)&cdcB & (ALIGNMENT_AMOUNT_64 -1))
  1903. DoError(nErrorCount, "EA_ALIGN test 4");
  1904. EA_ALIGN(ALIGNMENT_AMOUNT_64) ClassWithoutDefaultCtor cwdcB(3);
  1905. if((intptr_t)&cwdcB & (ALIGNMENT_AMOUNT_64 -1))
  1906. DoError(nErrorCount, "EA_ALIGN test 5");
  1907. #else
  1908. DoError(nErrorCount, "EA_ALIGN test 6");
  1909. #endif
  1910. }
  1911. { // Test EA_PREFIX_ALIGN
  1912. #ifdef EA_PREFIX_ALIGN
  1913. char buffer[32];
  1914. EA_PREFIX_ALIGN(ALIGNMENT_AMOUNT_64) int x(0);
  1915. sprintf(buffer, "%d", x);
  1916. if(buffer[0] != '0')
  1917. DoError(nErrorCount, "EA_PREFIX_ALIGN test 1");
  1918. EA_PREFIX_ALIGN(64) ClassWithDefaultCtor cdcA;
  1919. //EA_PREFIX_ALIGN(64) ClassWithoutDefaultCtor cwdcA;
  1920. EA_PREFIX_ALIGN(64) ClassWithDefaultCtor cdcB(3);
  1921. EA_PREFIX_ALIGN(64) ClassWithoutDefaultCtor cwdcB(3);
  1922. #else
  1923. DoError(nErrorCount, "EA_PREFIX_ALIGN test 2");
  1924. #endif
  1925. }
  1926. { // Test EA_POSTFIX_ALIGN
  1927. #ifdef EA_POSTFIX_ALIGN
  1928. char buffer[32];
  1929. int x EA_POSTFIX_ALIGN(ALIGNMENT_AMOUNT_64) = 0;
  1930. sprintf(buffer, "%d", x);
  1931. if(buffer[0] != '0')
  1932. DoError(nErrorCount, "EA_POSTFIX_ALIGN test 1");
  1933. ClassWithDefaultCtor cdcA EA_POSTFIX_ALIGN(ALIGNMENT_AMOUNT_64);
  1934. //ClassWithoutDefaultCtor cwdcA EA_POSTFIX_ALIGN(64);
  1935. #ifndef __SNC__ // SNC is broken with respect to constructors.
  1936. ClassWithDefaultCtor cdcB EA_POSTFIX_ALIGN(ALIGNMENT_AMOUNT_64)(3);
  1937. ClassWithoutDefaultCtor cwdcB EA_POSTFIX_ALIGN(ALIGNMENT_AMOUNT_64)(3);
  1938. #endif
  1939. #else
  1940. DoError(nErrorCount, "EA_POSTFIX_ALIGN test 2");
  1941. #endif
  1942. }
  1943. { // Test EA_ALIGNED
  1944. #ifdef EA_ALIGNED
  1945. char buffer[64];
  1946. // Verify that a simple declaration works.
  1947. EA_ALIGNED(int, xA, ALIGNMENT_AMOUNT_64); xA = 0;
  1948. sprintf(buffer, "%d", xA);
  1949. if((intptr_t)&xA & (ALIGNMENT_AMOUNT_64 -1))
  1950. DoError(nErrorCount, "EA_ALIGNED test 1");
  1951. // Verify that a declaration with assignment works.
  1952. EA_ALIGNED(int, xB, ALIGNMENT_AMOUNT_64) = 0;
  1953. sprintf(buffer, "%d", xB);
  1954. if((intptr_t)&xB & (ALIGNMENT_AMOUNT_64 -1))
  1955. DoError(nErrorCount, "EA_ALIGNED test 2");
  1956. // Verify that a declaration with construction works.
  1957. EA_ALIGNED(int, xC, ALIGNMENT_AMOUNT_64)(0);
  1958. sprintf(buffer, "%d", xC);
  1959. if((intptr_t)&xC & (ALIGNMENT_AMOUNT_64 -1))
  1960. DoError(nErrorCount, "EA_ALIGNED test 3");
  1961. // Verify that a typedefd declaration works.
  1962. typedef EA_ALIGNED(int, int16, ALIGNMENT_AMOUNT_16);
  1963. int16 n16 = 0;
  1964. sprintf(buffer, "%p", &n16);
  1965. if((intptr_t)&n16 & (ALIGNMENT_AMOUNT_16 - 1))
  1966. DoError(nErrorCount, "EA_ALIGNED test 4");
  1967. // Verify that the following tests compile. These tests are here
  1968. // because the SN compiler (EDG front-end) has some problems with
  1969. // GCC compatibility related to the 'aligned' __attribute__.
  1970. ClassWithDefaultCtor cdc;
  1971. ClassWithoutDefaultCtor cwdc(3);
  1972. sprintf(buffer, "%p%p", &cdc, &cwdc);
  1973. // Verify that regular usage of EA_ALIGNED works.
  1974. EA_ALIGNED(ClassWithDefaultCtor, cdc16A, ALIGNMENT_AMOUNT_16);
  1975. //EA_ALIGNED(ClassWithoutDefaultCtor, cwdcA, 16); // Doesn't have a default ctor, so this can't be done.
  1976. sprintf(buffer, "%p%p", &cdc16A, (void*)NULL);
  1977. // Verify that argument usage of EA_ALIGNED works.
  1978. EA_ALIGNED(ClassWithDefaultCtor, cdcB, ALIGNMENT_AMOUNT_16)(3);
  1979. EA_ALIGNED(ClassWithoutDefaultCtor, cwdcB, ALIGNMENT_AMOUNT_16)(3);
  1980. sprintf(buffer, "%p%p", &cdcB, &cwdcB);
  1981. // Verify that usage of EA_ALIGNED works within a typedef.
  1982. typedef EA_ALIGNED(ClassWithDefaultCtor, ClassWithDefaultCtor16, ALIGNMENT_AMOUNT_16);
  1983. ClassWithDefaultCtor16 cdcC(3);
  1984. typedef EA_ALIGNED(ClassWithoutDefaultCtor, ClassWithoutDefaultCtor16, ALIGNMENT_AMOUNT_16);
  1985. ClassWithoutDefaultCtor16 cwdcC(3);
  1986. sprintf(buffer, "%p%p", &cdcC, &cwdcC);
  1987. #else
  1988. DoError(nErrorCount, "EA_ALIGNED test");
  1989. #endif
  1990. }
  1991. { // Test EA_NO_INLINE / EA_PREFIX_NO_INLINE / EA_POSTFIX_NO_INLINE
  1992. DoNothingInline();
  1993. DoNothingPrefixInline();
  1994. }
  1995. { // Test EA_FORCE_INLINE / EA_PREFIX_FORCE_INLINE / EA_POSTFIX_FORCE_INLINE
  1996. DoNothingForceInline();
  1997. DoNothingPrefixForceInline();
  1998. }
  1999. { // Test EA_PACKED
  2000. #ifdef EA_PACKED
  2001. char buffer[32];
  2002. struct X { int x; } EA_PACKED;
  2003. X x = { 0 };
  2004. sprintf(buffer, "%d", x.x);
  2005. if(buffer[0] != '0')
  2006. DoError(nErrorCount, "EA_PACKED test");
  2007. #else
  2008. DoError(nErrorCount, "EA_PACKED test");
  2009. #endif
  2010. }
  2011. { // Test EA_LIKELY
  2012. if(EA_UNLIKELY(nErrorCount > 0))
  2013. {
  2014. if(EA_LIKELY(nErrorCount == 999999)) // Actually this isn't likely, but that's beside the point.
  2015. DoError(nErrorCount, "EA_LIKELY test");
  2016. }
  2017. }
  2018. { // Test EA_INIT_PRIORITY
  2019. // We don't test that the init priority succeeded in modifying the init priority.
  2020. // We merely test that this compiles on all platforms and assume the compiler's
  2021. // support of this is not broken.
  2022. if(gInitPriorityTestClass0.mX != 0)
  2023. DoError(nErrorCount, "EA_INIT_PRIORITY test.");
  2024. #ifndef __SNC__ // SNC is broken with respect to constructors.
  2025. if(gInitPriorityTestClass1.mX != 1)
  2026. DoError(nErrorCount, "EA_INIT_PRIORITY test.");
  2027. #endif
  2028. }
  2029. { // Test EA_MAY_ALIAS
  2030. // We don't test that the init priority succeeded in modifying the init priority.
  2031. // We merely test that this compiles on all platforms and assume the compiler's
  2032. // support of this is not broken.
  2033. #ifndef __SNC__ // SNC doesn't support this usage.
  2034. if(gPtr0 != NULL)
  2035. DoError(nErrorCount, "EA_MAY_ALIAS test.");
  2036. #endif
  2037. if(gPtr1 != NULL)
  2038. DoError(nErrorCount, "EA_MAY_ALIAS test.");
  2039. }
  2040. { // Test EA_ASSUME
  2041. switch (nErrorCount / (nErrorCount + 1))
  2042. {
  2043. case 0:
  2044. Stricmp("nop0", "nop0");
  2045. break;
  2046. case 1:
  2047. Stricmp("nop1", "nop1");
  2048. break;
  2049. default:
  2050. EA_ASSUME(0);
  2051. }
  2052. }
  2053. { // Test EA_PURE
  2054. if(!PureFunction())
  2055. DoError(nErrorCount, "EA_PURE test");
  2056. }
  2057. { // EA_WEAK
  2058. if(gWeakVariable != 1)
  2059. DoError(nErrorCount, "EA_WEAK test");
  2060. }
  2061. { // Test EA_NO_VTABLE
  2062. NoVTable1 nvt1;
  2063. NoVTable2 nvt2;
  2064. nvt1.InterfaceFunction();
  2065. nvt2.InterfaceFunction();
  2066. }
  2067. { // Test EA_WCHAR_SIZE
  2068. EA_DISABLE_VC_WARNING(6326)
  2069. #ifdef EA_WCHAR_SIZE
  2070. if((EA_WCHAR_SIZE != 1) && (EA_WCHAR_SIZE != 2) && (EA_WCHAR_SIZE != 4))
  2071. DoError(nErrorCount, "EA_WCHAR_SIZE test");
  2072. #else
  2073. DoError(nErrorCount, "EA_WCHAR_SIZE test");
  2074. #endif
  2075. EA_RESTORE_VC_WARNING()
  2076. }
  2077. { // Test EA_RESTRICT
  2078. struct TestRestrict{
  2079. static size_t Test(char* EA_RESTRICT p){ return sizeof(p); }
  2080. };
  2081. char* p = NULL;
  2082. if(TestRestrict::Test(p) == 0) // This isn't a real test. If there is a failure, it will happen at compile time.
  2083. DoError(nErrorCount, "EA_RESTRICT test");
  2084. }
  2085. { // Test EA_DEPRECATED
  2086. /* This causes warnings on compilers, so just disable it.
  2087. #if defined(EA_DEPRECATED) && (!defined(__GNUC__) || ((__GNUC__ * 100 + __GNUC_MINOR__) < 402)) // GCC 4.2+ is converting deprecated into an error instead of a warning.
  2088. char buffer[32];
  2089. EA_DEPRECATED int x(0);
  2090. sprintf(buffer, "%d", x); (void)x;
  2091. if(buffer[0] != '0')
  2092. DoError(nErrorCount, "EA_DEPRECATED test");
  2093. #elif !defined (EA_DEPRECATED)
  2094. DoError(nErrorCount, "EA_DEPRECATED test");
  2095. #endif
  2096. */
  2097. }
  2098. { // Test EA_PASCAL
  2099. #ifdef EA_PASCAL
  2100. struct X{ void EA_PASCAL DoNothing(){} };
  2101. X x;
  2102. x.DoNothing();
  2103. #else
  2104. DoError(nErrorCount, "EA_PASCAL test");
  2105. #endif
  2106. }
  2107. { // Test EA_PASCAL_FUNC
  2108. #ifdef EA_PASCAL_FUNC
  2109. struct X{ void EA_PASCAL_FUNC(DoNothing()){} };
  2110. X x;
  2111. x.DoNothing();
  2112. #else
  2113. DoError(nErrorCount, "EA_PASCAL_FUNC test");
  2114. #endif
  2115. }
  2116. // EA_SSE
  2117. // Not sure how to properly test at this time.
  2118. { // EA_FP16C
  2119. #if EA_FP16C
  2120. // For this test just try to call an intrinsic that is only
  2121. // available when FP16C is available. The test can make sure the
  2122. // platform actually supports FP16C when it claims to support it,
  2123. // but it can't verify a platform doesn't support FP16C.
  2124. _mm_cvtph_ps(_mm_set1_epi32(42));
  2125. #endif
  2126. }
  2127. { // EA_IMPORT
  2128. // Not possible to do this because import means it will come from outside.
  2129. //struct X{ EA_IMPORT void DoNothing(){} };
  2130. //X x;
  2131. //x.DoNothing();
  2132. }
  2133. { // EA_EXPORT
  2134. struct X{ EA_EXPORT void DoNothing(){} };
  2135. X x;
  2136. x.DoNothing();
  2137. }
  2138. // EA_PREPROCESSOR_JOIN
  2139. // EA_STRINGIFY
  2140. {
  2141. char buffer[32];
  2142. char bufferExpected[32];
  2143. const int line = (__LINE__ + 2);
  2144. sprintf(buffer, "%s %s", EA_STRINGIFY(EA_PREPROCESSOR_JOIN(test_, __LINE__)), EA_STRINGIFY(__LINE__));
  2145. sprintf(bufferExpected, "test_%d %d", line, line);
  2146. if(strcmp(buffer, bufferExpected) != 0)
  2147. DoError(nErrorCount, "EA_PREPROCESSOR_JOIN/EA_STRINGIFY test");
  2148. }
  2149. { // EAArrayCount
  2150. const int testArray[13] = { 0 };
  2151. const size_t arrayCount = EAArrayCount(testArray);
  2152. EA_DISABLE_VC_WARNING(6326)
  2153. if((arrayCount != 13) || (testArray[0] != 0))
  2154. DoError(nErrorCount, "EAArrayCount test");
  2155. EA_RESTORE_VC_WARNING()
  2156. const float testArray2[EAArrayCount(testArray)] = {};
  2157. static_assert(EAArrayCount(testArray2) == EAArrayCount(testArray), "Array counts should be equivalent.");
  2158. static_assert(EAArrayCount(testArray2) == 13, "Float array should have 13 elements.");
  2159. EA_DISABLE_VC_WARNING(6326)
  2160. if (EAArrayCount(testArray2) != EAArrayCount(testArray))
  2161. DoError(nErrorCount, "EAArrayCount - Array counts should be equivalent.");
  2162. EA_RESTORE_VC_WARNING()
  2163. EA_DISABLE_VC_WARNING(6326)
  2164. if (EAArrayCount(testArray2) != 13)
  2165. DoError(nErrorCount, "EAArrayCount - Float array should have 13 elements.");
  2166. EA_UNUSED(testArray2);
  2167. EA_RESTORE_VC_WARNING()
  2168. // Regresssion of user bug report that static_assert<member array> fails with some C++11 compilers.
  2169. // We revised the templated definition of EAArrayCount to deal with the failure.
  2170. struct Example
  2171. {
  2172. int32_t mItems[7];
  2173. Example()
  2174. { static_assert(EAArrayCount(mItems) == 7, "invalid size"); memset(mItems, 0x77, sizeof(mItems)); } // This was failing with the original templated version of EAArrayCount.
  2175. };
  2176. Example example;
  2177. EATEST_VERIFY(example.mItems[0] == 0x77777777);
  2178. }
  2179. { // static_assert
  2180. // Should succeed.
  2181. static_assert(sizeof(int32_t) == 4, "static_assert failure");
  2182. // Should fail.
  2183. //static_assert(sizeof(int32_t) == 8, "static_assert failure");
  2184. }
  2185. { // EA_OPTIMIZE_OFF / EA_OPTIMIZE_ON
  2186. int result = DisabledOptimizations(2);
  2187. if(result != 2*37)
  2188. DoError(nErrorCount, "EA_OPTIMIZE_OFF test");
  2189. }
  2190. { // EA_UNUSED
  2191. FunctionWithUnusedVariables(3);
  2192. }
  2193. { // EA_EXTERN_TEMPLATE
  2194. eabase_template<char> x;
  2195. x.value = 0;
  2196. if(x.GetValue() != 0)
  2197. DoError(nErrorCount, "EA_EXTERN_TEMPLATE test");
  2198. }
  2199. { // EA_FUNCTION_DELETE
  2200. EA_FUNCTION_DELETE_Test test(17);
  2201. EATEST_VERIFY(test.x == 17);
  2202. }
  2203. { // EA_NON_COPYABLE / EANonCopyable
  2204. NonCopyableA ncA1;
  2205. ncA1.x = 1;
  2206. //NonCopyableA ncA2(ncA1); // Both of these lines should result in
  2207. //ncA1 = ncA1; // compiler errors if enabled.
  2208. EA_UNUSED(ncA1);
  2209. NonCopyableB ncB1;
  2210. ncB1.x = 1;
  2211. //NonCopyableB ncB2(ncB1); // Both of these lines should result in
  2212. //ncB1 = ncB1; // compiler errors if enabled.
  2213. EA_UNUSED(ncB1);
  2214. NonCopyableSubclass ncs1(3);
  2215. //NonCopyableSubclass ncs2(ncs1); // Both of these lines should result in
  2216. //ncs2 = ncs2; // compiler errors if enabled.
  2217. EATEST_VERIFY(ncs1.mX == 3);
  2218. struct NonCopyableLocal
  2219. {
  2220. NonCopyableLocal(){}
  2221. int x;
  2222. EA_NON_COPYABLE(NonCopyableLocal)
  2223. };
  2224. NonCopyableLocal ncLocal1;
  2225. ncLocal1.x = 1;
  2226. //NonCopyableLocal ncLocal2(ncLocal1); // Both of these lines should result in
  2227. //ncLocal1 = ncLocal1; // compiler errors if enabled.
  2228. EA_UNUSED(ncLocal1);
  2229. }
  2230. return nErrorCount;
  2231. }
  2232. #if defined(EA_COMPILER_MSVC) && EA_COMPILER_VERSION >= 1900 // VS2015+
  2233. EA_RESTORE_VC_WARNING();
  2234. #endif
  2235. /////////////////////////////////////////////////
  2236. // nullptr test
  2237. /////////////////////////////////////////////////
  2238. #if !defined(EA_HAVE_nullptr_IMPL)
  2239. #define EA_RTTI_ENABLED 0 // This is something that ideally would be defined in EABase.
  2240. int mfCCount = 0;
  2241. struct C
  2242. {
  2243. void mf()
  2244. {
  2245. mfCCount++;
  2246. }
  2247. };
  2248. int fDoubleCount = 0;
  2249. static void f(double*)
  2250. {
  2251. fDoubleCount++;
  2252. }
  2253. int fIntCount = 0;
  2254. static void f(int)
  2255. {
  2256. fIntCount++;
  2257. }
  2258. int gTCount = 0;
  2259. template<typename T>
  2260. void g(T*)
  2261. {
  2262. gTCount++;
  2263. }
  2264. int hTCount = 0;
  2265. template<typename T>
  2266. void h(T)
  2267. {
  2268. hTCount++;
  2269. }
  2270. #endif
  2271. static int TestNullPtr()
  2272. {
  2273. int nErrorCount(0);
  2274. #if defined(EA_HAVE_nullptr_IMPL) // If the compiler provides a native version...
  2275. // Don't question it. VC++ nullptr -seems- to be not entirely conforming anyway.
  2276. #else
  2277. using namespace std;
  2278. // DoError("TestNullptr\n");
  2279. void* pv = nullptr; // OK
  2280. EATEST_VERIFY(pv == 0);
  2281. EATEST_VERIFY(pv == nullptr);
  2282. #ifndef __MWERKS__ // Currently the Metrowerks compiler crashes on this code.
  2283. EATEST_VERIFY(nullptr == pv);
  2284. #endif
  2285. pv = &pv; // OK
  2286. EATEST_VERIFY(pv != 0);
  2287. #if !defined(__GNUC__) || (__GNUC__ > 3)
  2288. EATEST_VERIFY(pv != nullptr);
  2289. #endif
  2290. const char* pc = nullptr; // OK
  2291. EATEST_VERIFY(pc == 0);
  2292. EATEST_VERIFY(pc == nullptr);
  2293. #ifndef __MWERKS__
  2294. EATEST_VERIFY(nullptr == pc);
  2295. #endif
  2296. C* pC = nullptr; // OK
  2297. EATEST_VERIFY(pC == 0);
  2298. EATEST_VERIFY(pC == nullptr);
  2299. #ifndef __MWERKS__
  2300. EATEST_VERIFY(nullptr == pC);
  2301. #endif
  2302. f(nullptr); // OK. Calls f(double*).
  2303. EATEST_VERIFY(fDoubleCount == 1);
  2304. f(0); // OK. Calls f(int)
  2305. EATEST_VERIFY(fIntCount == 1);
  2306. //g(nullptr); // Not OK. Can't deduce T
  2307. h(0); // OK. Deduces T = int
  2308. EATEST_VERIFY(hTCount == 1);
  2309. h(nullptr); // OK. Deduces T = nullptr_t
  2310. EATEST_VERIFY(hTCount == 2);
  2311. h((float*)nullptr); // OK. Deduces T = float*
  2312. EATEST_VERIFY(hTCount == 3);
  2313. void (C::*pmf)() = 0; // OK
  2314. EATEST_VERIFY(pmf == 0);
  2315. #if !defined(__GNUC__) || (__GNUC__ > 3)
  2316. void (C::*pmf2)() = nullptr; // OK
  2317. EA_UNUSED(pmf2);
  2318. #ifndef __MWERKS__ // CodeWarrior is not following the C++ Standard properly.
  2319. EATEST_VERIFY(pmf2 == 0);
  2320. EATEST_VERIFY(pmf2 == nullptr);
  2321. EATEST_VERIFY(nullptr == pmf2);
  2322. #endif
  2323. #endif
  2324. #if !defined(__GNUC__) || (__GNUC__ > 3)
  2325. void (C::*pmf3)() = &C::mf;
  2326. #ifndef __MWERKS__
  2327. EATEST_VERIFY(pmf3 != nullptr);
  2328. #endif
  2329. #endif
  2330. nullptr_t n1 = nullptr, n2 = nullptr; // OK
  2331. n1 = n2; // OK
  2332. h(n1);
  2333. //const int const0 = 0;
  2334. //if(const0 == nullptr) {} // Not OK.
  2335. //int n = 0;
  2336. //if(n == nullptr) {} // Not OK.
  2337. //nullptr_t* pN = &n1; // Not OK. Address can't be taken.
  2338. EATEST_VERIFY(!nullptr); // Supposedly OK, but VC++ doesn't accept it.
  2339. if(nullptr) // Supposedly OK, but VC++ doesn't accept it.
  2340. EATEST_VERIFY(false);
  2341. int val = 0;
  2342. char* ch3 = val ? nullptr : nullptr; // OK.
  2343. EATEST_VERIFY(ch3 == 0);
  2344. //char* ch4 = val ? 0 : nullptr; // Not OK. Types are not compatible.
  2345. //int n3 = val ? nullptr : nullptr; // Not OK. nullptr can't be converted to int.
  2346. //int n4 = val ? 0 : nullptr; // Not OK. Types are not compatible.
  2347. // void* p = 0;
  2348. // reinterpret_cast<nullptr>(p); // Not OK. But all compilers allow this. A reinterpret_cast cannot be used to convert a value of any type to the type std::nullptr_t.
  2349. //This is supposed to succeed, but we can't make it so, given the conflicting requirements of the C++ and nullptr standards.
  2350. //EATEST_VERIFY(sizeof(nullptr) == sizeof(void*)); // I don't currently have a means to make this work. See the class for why.
  2351. #ifndef __MWERKS__
  2352. nullptr_t n3 = nullptr, n4 = nullptr;
  2353. EATEST_VERIFY(n3 == n4);
  2354. EATEST_VERIFY(!(n3 != n4));
  2355. EATEST_VERIFY(n3 <= n4);
  2356. EATEST_VERIFY(n3 >= n4);
  2357. EATEST_VERIFY(!(n3 < n4));
  2358. EATEST_VERIFY(!(n3 > n4));
  2359. #endif
  2360. #if EA_RTTI_ENABLED
  2361. typeid(nullptr); // OK
  2362. #endif
  2363. #ifndef EA_COMPILER_NO_EXCEPTIONS
  2364. try{
  2365. pv = 0;
  2366. throw nullptr; // OK
  2367. }
  2368. catch(nullptr_t n)
  2369. {
  2370. EATEST_VERIFY(n == pv); // OK
  2371. h(n);
  2372. }
  2373. #endif
  2374. #endif // EA_HAVE_nullptr_IMPL
  2375. return nErrorCount;
  2376. }
  2377. static int TestEAHave()
  2378. {
  2379. int nErrorCount(0);
  2380. // EA_HAVE_XXX_DECL
  2381. //
  2382. // We don't have a simple way to test these, as they indicate the presence of
  2383. // declarations and not necessarily the presence of implementations.
  2384. //
  2385. // EA_HAVE_mkstemps_DECL
  2386. // EA_HAVE_gettimeofday_DECL
  2387. // EA_HAVE_strcasecmp_DECL
  2388. // EA_HAVE_strncasecmp_DECL
  2389. // EA_HAVE_mmap_DECL
  2390. // EA_HAVE_fopen_DECL
  2391. // EA_HAVE_ISNAN(x)
  2392. // EA_HAVE_ISINF(x)
  2393. // EA_HAVE_itoa_DECL
  2394. // EA_HAVE_nanosleep_DECL
  2395. // EA_HAVE_utime_DECL
  2396. // EA_HAVE_ftruncate_DECL
  2397. // EA_HAVE_localtime_DECL
  2398. // EA_HAVE_pthread_getattr_np_DECL
  2399. #if defined(EA_HAVE_ISNAN)
  2400. EATEST_VERIFY(EA_HAVE_ISNAN(1.f) == 0);
  2401. #endif
  2402. #if defined(EA_HAVE_ISINF)
  2403. EATEST_VERIFY(EA_HAVE_ISINF(1.f) == 0);
  2404. #endif
  2405. // EA_HAVE_XXX_IMPL
  2406. #if defined(EA_HAVE_WCHAR_IMPL)
  2407. size_t wlen = wcslen(L"test");
  2408. EATEST_VERIFY(wlen == 4); // Expect success.
  2409. #endif
  2410. #if defined(EA_HAVE_getenv_IMPL)
  2411. char* p = getenv("nonexistent_asdf");
  2412. EATEST_VERIFY(!p); // Expect failure.
  2413. #endif
  2414. #if defined(EA_HAVE_setenv_IMPL)
  2415. // http://pubs.opengroup.org/onlinepubs/009695399/functions/setenv.html
  2416. // int setenv(const char *envname, const char *envval, int overwrite);
  2417. setenv("test_asdf", "value", 0); // We ignore the return value, as we can't tell if the platform allows it.
  2418. #endif
  2419. #if defined(EA_HAVE_unsetenv_IMPL)
  2420. unsetenv("test_asdf"); // Ignore the return value.
  2421. #endif
  2422. #if defined(EA_HAVE_putenv_IMPL)
  2423. // int putenv(char* string);
  2424. char str[] = "a=b";
  2425. #if defined(EA_PLATFORM_MICROSOFT) && defined(EA_COMPILER_MICROSOFT)
  2426. // Microsoft uses _putenv, while others use putenv.
  2427. int putenvSuccess = _putenv(str);
  2428. #else
  2429. int putenvSuccess = putenv(str);
  2430. #endif
  2431. EATEST_VERIFY(putenvSuccess == 0);
  2432. #endif
  2433. #if defined(EA_HAVE_time_IMPL)
  2434. time_t timeResult = time(NULL);
  2435. EATEST_VERIFY(timeResult != 0); // Expect success.
  2436. #endif
  2437. #if defined(EA_HAVE_clock_IMPL)
  2438. // http://www.cplusplus.com/reference/ctime/clock/
  2439. clock_t clockResult = clock();
  2440. EATEST_VERIFY(clockResult != (clock_t) -1); // Expect success.
  2441. #endif
  2442. #if defined(EA_HAVE_fopen_IMPL)
  2443. // We don't have a portable way of testing the success of this, as different platforms have different file systems and requirements.
  2444. // since we want this to fail, we will use a normal Windows path as some platforms /require/ a windows-like mount path else they call abort()
  2445. FILE* pFile = fopen("Q:\\nonexistent_pleasedontexist", "r");
  2446. EATEST_VERIFY(pFile == NULL); // Expect failure.
  2447. if(pFile)
  2448. fclose(pFile);
  2449. #endif
  2450. #if defined(EA_HAVE_inet_ntop_IMPL)
  2451. char inetResult[32];
  2452. const char* pInetNtopResult = inet_ntop(0, "", inetResult, (uint16_t)EAArrayCount(inetResult)); // Cast to uint16_t because different libraries declare this arg differently, and this is a lowest common denominator.
  2453. EATEST_VERIFY(pInetNtopResult == NULL); // Expect failure.
  2454. #endif
  2455. #if defined(EA_HAVE_inet_pton_IMPL)
  2456. char inetPtonResult[32];
  2457. int inetResultVal = inet_pton(0, "", inetPtonResult);
  2458. EATEST_VERIFY(inetResultVal <= 0); // Expect failure.
  2459. #endif
  2460. #if defined(EA_HAVE_clock_gettime_IMPL)
  2461. struct timespec tp;
  2462. int clockGettimeResult = clock_gettime(CLOCK_MONOTONIC, &tp);
  2463. EATEST_VERIFY(clockGettimeResult <= 0); // Expect success or error.
  2464. #endif
  2465. #if defined(EA_HAVE_getcwd_IMPL)
  2466. {
  2467. char cwdBuffer[1];
  2468. char *result = getcwd(cwdBuffer, EAArrayCount(cwdBuffer));
  2469. EA_UNUSED(result);
  2470. }
  2471. #endif
  2472. #if defined(EA_HAVE_tmpnam_IMPL)
  2473. {
  2474. char tmpnamBuffer[L_tmpnam];
  2475. char *result = tmpnam(tmpnamBuffer);
  2476. EA_UNUSED(result);
  2477. }
  2478. #endif
  2479. #if defined(EA_HAVE_nullptr_IMPL)
  2480. // This is exercised elsewhere in this test.
  2481. #endif
  2482. #if defined(EA_HAVE_std_terminate_IMPL)
  2483. if(nErrorCount == INT_MIN) // This is impossible.
  2484. std::terminate();
  2485. #endif
  2486. #if defined(EA_HAVE_CPP11_ITERATOR_IMPL)
  2487. // <iterator>: std::begin, std::end, std::prev, std::next, std::move_iterator.
  2488. #if defined(EA_HAVE_CPP11_INITIALIZER_LIST)
  2489. eastl::vector<int> intArray;
  2490. EATEST_VERIFY(std::begin(intArray) == std::end(intArray));
  2491. #endif
  2492. char charArray[16] = { 0 };
  2493. EATEST_VERIFY(std::begin(charArray) != std::end(charArray));
  2494. #endif
  2495. #if defined(EA_HAVE_CPP11_SMART_POINTER_IMPL)
  2496. // std::weak_ptr, std::shared_ptr, std::unique_ptr, std::bad_weak_ptr
  2497. std::shared_ptr<int> spInt;
  2498. std::weak_ptr<int> wpInt;
  2499. std::unique_ptr<int> upInt;
  2500. //std::bad_weak_ptr<int> bwpInt;
  2501. #endif
  2502. #if defined(EA_HAVE_CPP11_FUNCTIONAL_IMPL) && !defined(EA_PLATFORM_ANDROID) // Our Android build system is failing to link _1, _2, etc.
  2503. // function, mem_fn, bad_function_call, is_bind_expression, is_placeholder, reference_wrapper, hash, bind, ref, cref.
  2504. // It turns out that all compiler/library combinations that support this also support C++11 auto, so we can use it.
  2505. #if !defined(EA_PLATFORM_ANDROID) // Our Android build system is failing to link _1, _2, etc.
  2506. using namespace std::placeholders; //for _1, _2, _3...
  2507. int n = 7;
  2508. auto f = std::bind(BindTestFunction, _2, _1, 42, std::cref(n), n);
  2509. f(1, 2, 1001); // 1 is bound by _2, 2 is bound by _1, 1001 is unused
  2510. BindTestStruct bts;
  2511. auto f2 = std::bind(&BindTestStruct::Test, bts, 95, _1);
  2512. f2(5);
  2513. #endif
  2514. std::hash<uint32_t> hash32;
  2515. EATEST_VERIFY(hash32(37) == hash32(37));
  2516. #endif
  2517. #if defined(EA_HAVE_CPP11_EXCEPTION_IMPL)
  2518. // current_exception, rethrow_exception, exception_ptr, make_exception_ptr
  2519. #if !defined(EA_COMPILER_NO_EXCEPTIONS)
  2520. EA_DISABLE_VC_WARNING(4571)
  2521. if(nErrorCount == 9999999) // This will never be true.
  2522. {
  2523. std::exception_ptr ep = std::make_exception_ptr(std::logic_error("logic_error"));
  2524. try {
  2525. std::rethrow_exception(ep);
  2526. }
  2527. catch (...) {
  2528. ep = std::current_exception();
  2529. std::rethrow_exception(ep);
  2530. }
  2531. }
  2532. EA_RESTORE_VC_WARNING()
  2533. #endif
  2534. #endif
  2535. #if defined(EA_HAVE_CPP11_TYPE_TRAITS)
  2536. // Some form of type traits have been supported by compilers since well before C++11. But C++11 introduced
  2537. // a number of type traits that weren't previously supported by compilers. We require that full C++11 type
  2538. // traits be supported. See the C++11 Standard, section 20.9.2.
  2539. // We currently test a sampling of specific traits that didn't exist in preliminary standard library versions.
  2540. bool ttResult = std::is_nothrow_move_constructible<int>::value;
  2541. EATEST_VERIFY(ttResult);
  2542. ttResult = std::is_standard_layout<int>::value;
  2543. EATEST_VERIFY(ttResult);
  2544. #endif
  2545. return nErrorCount;
  2546. }
  2547. static int TestEAAlignment()
  2548. {
  2549. // This test does a couple of allocations and for each allocation it determines
  2550. // the minimal alignment. If this (local) minimum is less than the global minimum
  2551. // then the global minimum is updated. After all the allocation sizes and iterations
  2552. // it checks this minimum to make sure that the EABase EA_PLATFORM_MIN_MALLOC_ALIGNMENT
  2553. // is at least that number, since you would never want to ask for finer grained
  2554. // allocations as malloc can't give them.
  2555. int nErrorCount(0);
  2556. const size_t MAX_SIZE = 128;
  2557. const size_t NUM_ITERATIONS = 32;
  2558. size_t minAlignment = MAX_SIZE;
  2559. for(size_t size = 1; size <= MAX_SIZE; ++size)
  2560. {
  2561. for(size_t iteration = 0; iteration < NUM_ITERATIONS; ++iteration)
  2562. {
  2563. void* ptr = malloc(size);
  2564. size_t address = static_cast<size_t>(reinterpret_cast<uintptr_t>(ptr));
  2565. size_t alignment = MAX_SIZE;
  2566. do
  2567. {
  2568. if((address & (alignment - 1)) == 0)
  2569. {
  2570. break;
  2571. }
  2572. else
  2573. {
  2574. alignment >>= 1;
  2575. }
  2576. } while(alignment > 0);
  2577. if(alignment < minAlignment)
  2578. minAlignment = alignment;
  2579. free(ptr);
  2580. }
  2581. }
  2582. EATEST_VERIFY_F(EA_PLATFORM_MIN_MALLOC_ALIGNMENT <= minAlignment,
  2583. "'EA_PLATFORM_MIN_MALLOC_ALIGNMENT=%d' <= 'minAlignment=%d' failure on '%s'",
  2584. EA_PLATFORM_MIN_MALLOC_ALIGNMENT, minAlignment, EA_PLATFORM_DESCRIPTION);
  2585. return nErrorCount;
  2586. }
  2587. #include <EABase/eastdarg.h>
  2588. #include <EAStdC/EASprintf.h>
  2589. #include <EAStdC/EAString.h>
  2590. static void TestEAStdargReferenceHelp(char* p, va_list_reference args)
  2591. {
  2592. EA::StdC::Sprintf(p, "%d", va_arg(args, int));
  2593. }
  2594. static void TestEAStdargReference(char* p1, char* p2, ...) // Must be called with two ints for ...
  2595. {
  2596. va_list args;
  2597. va_start(args, p2);
  2598. TestEAStdargReferenceHelp(p1, args); // We pass args to TestEAStdargReferenceHelp by reference, which results in args being
  2599. TestEAStdargReferenceHelp(p2, args); // modified upon return. So upon this second call args should have used the first int arg.
  2600. va_end(args);
  2601. }
  2602. static void TestEAStdargCopy(char* p1, char* p2, ...) // Must be called with two ints for ...
  2603. {
  2604. va_list args, argsCopy;
  2605. va_start(args, p2);
  2606. va_copy(argsCopy, args);
  2607. EA::StdC::Vsprintf(p1, "%d", args);
  2608. EA::StdC::Vsprintf(p2, "%d", argsCopy);
  2609. va_end(args);
  2610. va_end(argsCopy);
  2611. }
  2612. static int TestEAStdarg()
  2613. {
  2614. int nErrorCount(0);
  2615. // VA_ARG_COUNT
  2616. static_assert(VA_ARG_COUNT() == 0, "VA_ARG_COUNT()");
  2617. static_assert(VA_ARG_COUNT(1) == 1, "VA_ARG_COUNT(1)");
  2618. static_assert(VA_ARG_COUNT(2, 2) == 2, "VA_ARG_COUNT(2)");
  2619. static_assert(VA_ARG_COUNT(3, 3, 3) == 3, "VA_ARG_COUNT(3)");
  2620. static_assert(VA_ARG_COUNT(4, 4, 4, 4) == 4, "VA_ARG_COUNT(4)");
  2621. static_assert(VA_ARG_COUNT(5, 5, 5, 5, 5) == 5, "VA_ARG_COUNT(5)");
  2622. char buffer1[64];
  2623. char buffer2[64];
  2624. // va_copy
  2625. TestEAStdargCopy(buffer1, buffer2, 17, 99);
  2626. EATEST_VERIFY((EA::StdC::AtoI32(buffer1) == 17) && (EA::StdC::AtoI32(buffer2) == 17));
  2627. // va_list_reference
  2628. TestEAStdargReference(buffer1, buffer2, 17, 99);
  2629. EATEST_VERIFY((EA::StdC::AtoI32(buffer1) == 17) && (EA::StdC::AtoI32(buffer2) == 99));
  2630. return nErrorCount;
  2631. }
  2632. static int TestEAUnits()
  2633. {
  2634. int nErrorCount(0);
  2635. static_assert(EA_BYTE(64) == 64, "SI units mismatch");
  2636. static_assert(EA_BYTE(1000) == 1000, "SI units mismatch");
  2637. static_assert(EA_KILOBYTE(1) != EA_KIBIBYTE(1), "SI units mismatch");
  2638. static_assert(EA_MEGABYTE(1) != EA_MEBIBYTE(1), "SI units mismatch");
  2639. static_assert(EA_GIGABYTE(1) != EA_GIBIBYTE(1), "SI units mismatch");
  2640. static_assert((4 % EA_BYTE(4*10)) == 4, "Order of operations error"); //If unit macros aren't enclosed in parentheses, this will cause order of operation problems in this situation.
  2641. static_assert((4 % EA_MEBIBYTE(4)) == 4, "Order of operations error"); //If unit macros aren't enclosed in parentheses, this will cause order of operation problems in this situation.
  2642. #ifndef EA_PROCESSOR_X86
  2643. static_assert(EA_TERABYTE(1) != EA_TEBIBYTE(1), "SI units mismatch");
  2644. static_assert(EA_PETABYTE(1) != EA_PEBIBYTE(1), "SI units mismatch");
  2645. static_assert(EA_EXABYTE(1) != EA_EXBIBYTE(1), "SI units mismatch");
  2646. #endif
  2647. static_assert(EA_KILOBYTE(1) == 1000, "SI units mismatch");
  2648. static_assert(EA_MEGABYTE(2) == EA_KILOBYTE(2) * 1000, "SI units mismatch");
  2649. static_assert(EA_GIGABYTE(3) == EA_MEGABYTE(3) * 1000, "SI units mismatch");
  2650. #ifndef EA_PROCESSOR_X86
  2651. static_assert(EA_TERABYTE(4) == EA_GIGABYTE(4) * 1000, "SI units mismatch");
  2652. static_assert(EA_PETABYTE(5) == EA_TERABYTE(5) * 1000, "SI units mismatch");
  2653. static_assert(EA_EXABYTE(6) == EA_PETABYTE(6) * 1000, "SI units mismatch");
  2654. #endif
  2655. static_assert(EA_KIBIBYTE(1) == 1024, "SI units mismatch");
  2656. static_assert(EA_MEBIBYTE(2) == EA_KIBIBYTE(2) * 1024, "SI units mismatch");
  2657. static_assert(EA_GIBIBYTE(3) == EA_MEBIBYTE(3) * 1024, "SI units mismatch");
  2658. #ifndef EA_PROCESSOR_X86
  2659. static_assert(EA_TEBIBYTE(4) == EA_GIBIBYTE(4) * 1024, "SI units mismatch");
  2660. static_assert(EA_PEBIBYTE(5) == EA_TEBIBYTE(5) * 1024, "SI units mismatch");
  2661. static_assert(EA_EXBIBYTE(6) == EA_PEBIBYTE(6) * 1024, "SI units mismatch");
  2662. #endif
  2663. return nErrorCount;
  2664. }
  2665. template<typename Int128T, typename MakeInt128T>
  2666. static void TestInt128T(MakeInt128T MakeInt128, const char* errorMsg, int& nErrorCount)
  2667. {
  2668. auto VERIFY = [&](bool result) { if(!result) DoError(nErrorCount, errorMsg); };
  2669. const auto TestValue = MakeInt128(0x1234567812345678, 0x1234567812345678);
  2670. const Int128T zero = MakeInt128(0, 0);
  2671. const Int128T one = MakeInt128(0, 1);
  2672. const Int128T two = MakeInt128(0, 2);
  2673. const Int128T big = MakeInt128(0x1234567812345678, 0x1234567812345678);;
  2674. const Int128T negative_one = MakeInt128(0xffffffffffffffff, 0xffffffffffffffff);
  2675. const Int128T half_range = MakeInt128(0x0, 0xffffffffffffffff);
  2676. {
  2677. // Int128T a1 = 42.f;
  2678. // Int128T a2 = 42.0f;
  2679. // Int128T a3 = 42;
  2680. // Int128T a4 = 42u;
  2681. // Int128T a5 = 42ul;
  2682. // Int128T a6 = 42ull;
  2683. }
  2684. // default ctor
  2685. {
  2686. { Int128T a; (void)a; }
  2687. { Int128T a{}; (void)a; }
  2688. static_assert(eastl::is_trivially_default_constructible_v<Int128T>, "128-bit integer failure");
  2689. }
  2690. // operator-
  2691. {
  2692. VERIFY(negative_one == -one);
  2693. }
  2694. // operator~
  2695. {
  2696. auto not_one = ~one;
  2697. VERIFY(not_one == MakeInt128(0xffffffffffffffff, 0xfffffffffffffffe));
  2698. }
  2699. // operator+
  2700. {
  2701. VERIFY(zero == +zero);
  2702. VERIFY(one == +one);
  2703. VERIFY(big == +big);
  2704. }
  2705. // operator+
  2706. // operator-
  2707. // operator*
  2708. // operator/
  2709. // operator%
  2710. {
  2711. auto i = MakeInt128(42, 42);
  2712. i = i + one;
  2713. VERIFY(i == MakeInt128(42, 43));
  2714. i = i - one;
  2715. VERIFY(i == MakeInt128(42, 42));
  2716. i = i * two;
  2717. VERIFY(i == MakeInt128(84, 84));
  2718. i = i / two;
  2719. VERIFY(i == MakeInt128(42, 42));
  2720. }
  2721. // operator== / operator!=
  2722. {
  2723. VERIFY(TestValue == MakeInt128(0x1234567812345678, 0x1234567812345678));
  2724. VERIFY(TestValue == TestValue);
  2725. VERIFY(MakeInt128(0x1, 0x1) != MakeInt128(0x1, 0x2));
  2726. }
  2727. // operator<
  2728. {
  2729. VERIFY(zero < one);
  2730. VERIFY(one < two);
  2731. VERIFY(zero < two);
  2732. VERIFY(zero < big);
  2733. VERIFY(one < big);
  2734. VERIFY(MakeInt128(123, 122) < MakeInt128(123, 123));
  2735. VERIFY(MakeInt128(122, 123) < MakeInt128(123, 123));
  2736. }
  2737. // operator> / operator>=
  2738. {
  2739. VERIFY(TestValue > MakeInt128(0, 0x1234567812345678));
  2740. VERIFY(TestValue >= MakeInt128(0, 0x1234567812345678));
  2741. VERIFY(TestValue >= TestValue);
  2742. VERIFY(TestValue >= TestValue);
  2743. }
  2744. // operator< / operator<=
  2745. {
  2746. VERIFY(MakeInt128(0, 0x1234567812345678) < TestValue);
  2747. VERIFY(MakeInt128(0, 0x1234567812345678) <= TestValue);
  2748. VERIFY(TestValue <= TestValue);
  2749. VERIFY(TestValue <= TestValue);
  2750. }
  2751. // operator++
  2752. {
  2753. auto i = MakeInt128(0, 0);
  2754. VERIFY(i++ == MakeInt128(0, 0));
  2755. VERIFY(++i == MakeInt128(0, 2));
  2756. VERIFY(++i == MakeInt128(0, 3));
  2757. VERIFY(i++ == MakeInt128(0, 3));
  2758. {
  2759. auto n1 = half_range;
  2760. VERIFY(++n1 == MakeInt128(1, 0));
  2761. }
  2762. }
  2763. // operator--
  2764. {
  2765. auto i = MakeInt128(0, 5);
  2766. VERIFY(i-- == MakeInt128(0, 4));
  2767. VERIFY(--i == MakeInt128(0, 4));
  2768. VERIFY(--i == MakeInt128(0, 3));
  2769. VERIFY(i-- == MakeInt128(0, 2));
  2770. {
  2771. auto n1 = MakeInt128(1, 0);
  2772. VERIFY(n1-- == half_range);
  2773. }
  2774. }
  2775. // operator+=
  2776. // operator-=
  2777. // operator*=
  2778. // operator/=
  2779. // operator%=
  2780. {
  2781. auto n = MakeInt128(0, 5);
  2782. n += MakeInt128(0, 15);
  2783. VERIFY(n == MakeInt128(0, 20));
  2784. n -= MakeInt128(0, 18);
  2785. VERIFY(n == MakeInt128(0, 2));
  2786. n *= MakeInt128(0, 2);
  2787. VERIFY(n == MakeInt128(0, 4));
  2788. n /= MakeInt128(0, 2);
  2789. VERIFY(n == MakeInt128(0, 2));
  2790. n %= MakeInt128(0, 2);
  2791. VERIFY(n == MakeInt128(0, 0));
  2792. }
  2793. // operator>>
  2794. // operator<<
  2795. // operator>>=
  2796. // operator<<=
  2797. {
  2798. auto n = MakeInt128(0, 0x4);
  2799. {
  2800. auto a = n >> 1;
  2801. VERIFY(a == MakeInt128(0, 0x2));
  2802. a >>= 1;
  2803. VERIFY(a == MakeInt128(0, 0x1));
  2804. }
  2805. {
  2806. auto a = n << 1;
  2807. VERIFY(a == MakeInt128(0, 0x8));
  2808. a <<= 1;
  2809. VERIFY(a == MakeInt128(0, 0x10));
  2810. }
  2811. {
  2812. auto a = half_range;
  2813. a <<= 1;
  2814. VERIFY(a == MakeInt128(0x1, 0xfffffffffffffffe));
  2815. }
  2816. {
  2817. auto a = half_range;
  2818. a >>= 1;
  2819. VERIFY(a == MakeInt128(0x0, 0x7fffffffffffffff));
  2820. }
  2821. }
  2822. // operator^
  2823. // operator|
  2824. // operator&
  2825. // operator^=
  2826. // operator|=
  2827. // operator&=
  2828. {
  2829. const auto n1 = MakeInt128(0xAAAAAAAAAAAAAAAA, 0xAAAAAAAAAAAAAAAA);
  2830. const auto n2 = MakeInt128(0x5555555555555555, 0x5555555555555555);
  2831. {
  2832. auto i = n1 ^ n2;
  2833. VERIFY(i == negative_one);
  2834. auto n3 = n1;
  2835. n3 ^= n2;
  2836. VERIFY(n3 == negative_one);
  2837. }
  2838. {
  2839. auto i = n1 | n2;
  2840. VERIFY(i == negative_one);
  2841. auto n3 = n1;
  2842. n3 |= n2;
  2843. VERIFY(n3 == negative_one);
  2844. }
  2845. {
  2846. auto i = n1 & n2;
  2847. VERIFY(i == zero);
  2848. auto n3 = n1;
  2849. n3 &= n2;
  2850. VERIFY(n3 == zero);
  2851. }
  2852. }
  2853. // Test loop counter
  2854. {
  2855. {
  2856. int counter = 0;
  2857. Int128T i = MakeInt128(0,0);
  2858. for (; i < MakeInt128(0,10); i++)
  2859. counter++;
  2860. VERIFY(i == MakeInt128(0, counter));
  2861. }
  2862. {
  2863. // int counter = 0;
  2864. // for (Int128T i = 0; i < 10; i++)
  2865. // counter++;
  2866. // VERIFY(i == counter);
  2867. }
  2868. }
  2869. }
  2870. static int TestEAInt128_t()
  2871. {
  2872. int nErrorCount(0);
  2873. TestInt128T<uint128_t>(UINT128_C, "uint128_t test failure", nErrorCount);
  2874. TestInt128T<int128_t>(INT128_C, "int128_t test failure", nErrorCount);
  2875. return nErrorCount;
  2876. }
  2877. // EA_WCHAR_UNIQUE
  2878. template <typename T>
  2879. struct wchar_unique { enum { value = 1 }; }; // if wchar_t is unique then wchar_unique<wchar_t>::value should be 1
  2880. template <> struct wchar_unique<char8_t> { enum { value = 0 }; }; // if wchar_unique is not unique then it should match one of the specializations and the value will be 0.
  2881. template <> struct wchar_unique<char16_t> { enum { value = 0 }; };
  2882. template <> struct wchar_unique<char32_t> { enum { value = 0 }; };
  2883. #if EA_WCHAR_UNIQUE
  2884. static_assert( wchar_unique<wchar_t>::value == 1, "WCHAR_UNIQUE appears to be incorrectly defined to 1 by EABase" );
  2885. #else
  2886. static_assert( wchar_unique<wchar_t>::value == 0, "WCHAR_UNIQUE appears to be incorrectly defined to 0 by EABase" );
  2887. #endif
  2888. //
  2889. // Tests for EA_IS_ENABLED
  2890. //
  2891. #define EABASE_TEST_FEATURE_A EA_ENABLED
  2892. #if EA_IS_ENABLED(EABASE_TEST_FEATURE_A)
  2893. // Feature A is enabled
  2894. #else
  2895. #error Error EABASE_TEST_FEATURE_A should be enabled.
  2896. #endif
  2897. // Make sure it is possible to successfully negate the test.
  2898. #if !EA_IS_ENABLED(EABASE_TEST_FEATURE_A)
  2899. #error Error EABASE_TEST_FEATURE_A should be enabled.
  2900. #endif
  2901. #define EABASE_TEST_FEATURE_B EA_DISABLED
  2902. #if EA_IS_ENABLED(EABASE_TEST_FEATURE_B)
  2903. #error Error EABASE_TEST_FEATURE_B should be disabled.
  2904. #endif
  2905. // Make sure it is possible to successfully negate the test.
  2906. #if !EA_IS_ENABLED(EABASE_TEST_FEATURE_B)
  2907. // Feature B is not enabled
  2908. #else
  2909. #error Error EABASE_TEST_FEATURE_B should be disabled.
  2910. #endif
  2911. // The test below should cause compilation to fail if it is uncommented. However we can't
  2912. // obviously enable the test because it will break the build. It should be tested manually
  2913. // if changes to EA_IS_ENABLED are made.
  2914. //
  2915. // #if EA_IS_ENABLED(EABASE_TEST_FEATURE_WITH_NO_DEFINE)
  2916. // #endif
  2917. int EAMain(int, char**)
  2918. {
  2919. int nErrorCount = 0;
  2920. int nTotalErrorCount = 0;
  2921. nErrorCount = TestEABase();
  2922. EA::EAMain::Report("EABase test failure count: %d\n\n", nErrorCount);
  2923. nTotalErrorCount += nErrorCount;
  2924. nErrorCount = TestEAResult();
  2925. EA::EAMain::Report("EAResult test failure count: %d\n\n", nErrorCount);
  2926. nTotalErrorCount += nErrorCount;
  2927. nErrorCount = TestEAPlatform();
  2928. EA::EAMain::Report("EAPlatform test failure count: %d\n\n", nErrorCount);
  2929. nTotalErrorCount += nErrorCount;
  2930. nErrorCount = TestEACompiler();
  2931. EA::EAMain::Report("EACompiler test failure count: %d\n\n", nErrorCount);
  2932. nTotalErrorCount += nErrorCount;
  2933. nErrorCount = TestEACompilerTraits();
  2934. EA::EAMain::Report("EACompilerTraits test failure count: %d\n\n", nErrorCount);
  2935. nTotalErrorCount += nErrorCount;
  2936. nErrorCount = TestNullPtr();
  2937. EA::EAMain::Report("nullptr test failure count: %d\n\n", nErrorCount);
  2938. nTotalErrorCount += nErrorCount;
  2939. nErrorCount = TestEAHave();
  2940. EA::EAMain::Report("EAHave test failure count: %d\n\n", nErrorCount);
  2941. nTotalErrorCount += nErrorCount;
  2942. nErrorCount = TestEAAlignment();
  2943. EA::EAMain::Report("EAAlignment test failure count: %d\n\n", nErrorCount);
  2944. nTotalErrorCount += nErrorCount;
  2945. nErrorCount = TestEAStdarg();
  2946. EA::EAMain::Report("EAStdarg test failure count: %d\n\n", nErrorCount);
  2947. nTotalErrorCount += nErrorCount;
  2948. nErrorCount = TestEAUnits();
  2949. EA::EAMain::Report("EAUnits test failure count: %d\n\n", nErrorCount);
  2950. nTotalErrorCount += nErrorCount;
  2951. nErrorCount = TestEAInt128_t();
  2952. EA::EAMain::Report("EAInt128_t test failure count: %d\n\n", nErrorCount);
  2953. nTotalErrorCount += nErrorCount;
  2954. if (nTotalErrorCount == 0)
  2955. EA::EAMain::Report("\nAll tests completed successfully.\n");
  2956. else
  2957. EA::EAMain::Report("\nTests failed. Total error count: %d\n", nTotalErrorCount);
  2958. return nTotalErrorCount;
  2959. }
  2960. EA_RESTORE_VC_WARNING() // for the following from above: EA_DISABLE_VC_WARNING(4265 4296 4310 4350 4481 4530 4625 4626 4996)