TestEABase.cpp 99 KB

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