TestEABase.cpp 98 KB

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