TestEABase.cpp 98 KB

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