TestEABase.cpp 98 KB

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