fct.h 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964
  1. /*
  2. ====================================================================
  3. Copyright (c) 2008 Ian Blumel. All rights reserved.
  4. FCTX (Fast C Test) Unit Testing Framework
  5. Copyright (c) 2008, Ian Blumel (ian.blumel@gmail.com)
  6. All rights reserved.
  7. This license is based on the BSD License.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are
  10. met:
  11. * Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in
  15. the documentation and/or other materials provided with the
  16. distribution.
  17. * Neither the name of, Ian Blumel, nor the names of its
  18. contributors may be used to endorse or promote products derived
  19. from this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  23. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  24. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ====================================================================
  32. File: fct.h
  33. */
  34. #if !defined(FCT_INCLUDED__IMB)
  35. #define FCT_INCLUDED__IMB
  36. /* Configuration Values. You can over-ride these values in your own
  37. header, then include this header. For example, in your file, myfct.h,
  38. #define FCT_DEFAULT_LOGGER "standard"
  39. #include "fct.h"
  40. then if your unit tests included, myfct.h, you would default to work
  41. with a standard logger. */
  42. #if !defined(FCT_DEFAULT_LOGGER)
  43. # define FCT_DEFAULT_LOGGER "standard"
  44. #endif /* !FCT_DEFAULT_LOGGER */
  45. #define FCT_VERSION_MAJOR 1
  46. #define FCT_VERSION_MINOR 6
  47. #define FCT_VERSION_MICRO 1
  48. #define _FCT_QUOTEME(x) #x
  49. #define FCT_QUOTEME(x) _FCT_QUOTEME(x)
  50. #define FCT_VERSION_STR (FCT_QUOTEME(FCT_VERSION_MAJOR) "."\
  51. FCT_QUOTEME(FCT_VERSION_MINOR) "."\
  52. FCT_QUOTEME(FCT_VERSION_MICRO))
  53. #include <string.h>
  54. #include <assert.h>
  55. #include <stdarg.h>
  56. #include <stdlib.h>
  57. #ifdef PRINTF_STDLIB
  58. #include <stdio.h>
  59. #endif
  60. #ifdef PRINTF_CUSTOM
  61. #include "tinystdio.h"
  62. #endif
  63. #include <time.h>
  64. #include <float.h>
  65. #include <math.h>
  66. #include <ctype.h>
  67. #define FCT_MAX_NAME 256
  68. #define FCT_MAX_LOG_LINE 256
  69. #define nbool_t int
  70. #define FCT_TRUE 1
  71. #define FCT_FALSE 0
  72. #define FCTMIN(x, y) ( x < y) ? (x) : (y)
  73. #ifndef __INTEL_COMPILER
  74. /* Use regular assertions for non-Intel compilers */
  75. #define FCT_ASSERT(expr) assert(expr)
  76. #else
  77. /* Silence Intel warnings on assert(expr && "str") or assert("str") */
  78. #define FCT_ASSERT(expr) do { \
  79. _Pragma("warning(push,disable:279)"); \
  80. assert(expr); \
  81. _Pragma("warning(pop)"); \
  82. } while (0)
  83. #endif
  84. #if defined(__cplusplus)
  85. #define FCT_EXTERN_C extern "C"
  86. #else
  87. #define FCT_EXTERN_C
  88. #endif
  89. /* Forward declarations. The following forward declarations are required
  90. because there is a inter-relationship between certain objects that
  91. just can not be untwined. */
  92. typedef struct _fct_logger_evt_t fct_logger_evt_t;
  93. typedef struct _fct_logger_i fct_logger_i;
  94. typedef struct _fct_logger_types_t fct_logger_types_t;
  95. typedef struct _fct_standard_logger_t fct_standard_logger_t;
  96. typedef struct _fct_junit_logger_t fct_junit_logger_t;
  97. typedef struct _fct_minimal_logger_t fct_minimal_logger_t;
  98. typedef struct _fctchk_t fctchk_t;
  99. typedef struct _fct_test_t fct_test_t;
  100. typedef struct _fct_ts_t fct_ts_t;
  101. typedef struct _fctkern_t fctkern_t;
  102. /* Forward declare some functions used throughout. */
  103. static fct_logger_i*
  104. fct_standard_logger_new(void);
  105. static fct_logger_i*
  106. fct_minimal_logger_new(void);
  107. static fct_junit_logger_t *
  108. fct_junit_logger_new(void);
  109. static void
  110. fct_logger__del(fct_logger_i *logger);
  111. static void
  112. fct_logger__on_chk(fct_logger_i *self, fctchk_t const *chk);
  113. static void
  114. fct_logger__on_test_start(fct_logger_i *logger, fct_test_t const *test);
  115. static void
  116. fct_logger__on_test_end(fct_logger_i *logger, fct_test_t *test);
  117. static void
  118. fct_logger__on_test_suite_start(fct_logger_i *logger, fct_ts_t const *ts);
  119. static void
  120. fct_logger__on_test_suite_end(fct_logger_i *logger, fct_ts_t const *ts);
  121. static void
  122. fct_logger__on_test_suite_skip(
  123. fct_logger_i *logger,
  124. char const *condition,
  125. char const *name
  126. );
  127. static void
  128. fct_logger__on_test_skip(
  129. fct_logger_i *logger,
  130. char const *condition,
  131. char const *name
  132. );
  133. static void
  134. fct_logger__on_warn(fct_logger_i *logger, char const *warn);
  135. /* Explicitly indicate a no-op */
  136. #define fct_pass()
  137. #define fct_unused(x) (void)(x)
  138. /* This is just a little trick to let me put comments inside of macros. I
  139. really only want to bother with this when we are "unwinding" the macros
  140. for debugging purposes. */
  141. #if defined(FCT_CONF_UNWIND)
  142. # define _fct_cmt(string) {char*_=string;}
  143. #else
  144. # define _fct_cmt(string)
  145. #endif
  146. /*
  147. --------------------------------------------------------
  148. UTILITIES
  149. --------------------------------------------------------
  150. */
  151. /* STDIO and STDERR redirect support */
  152. #define FCT_PIPE_RESERVE_BYTES_DEFAULT 512
  153. static int fct_stdout_pipe[2];
  154. static int fct_stderr_pipe[2];
  155. static int fct_saved_stdout;
  156. static int fct_saved_stderr;
  157. /* Platform indepedent pipe functions. TODO: Look to figure this out in a way
  158. that follows the ISO C++ conformant naming convention. */
  159. #if defined(WIN32)
  160. # include <io.h>
  161. # include <fcntl.h>
  162. # define _fct_pipe(_PFDS_) \
  163. _pipe((_PFDS_), FCT_PIPE_RESERVE_BYTES_DEFAULT, _O_TEXT)
  164. # define _fct_dup _dup
  165. # define _fct_dup2 _dup2
  166. # define _fct_close _close
  167. # define _fct_read _read
  168. /* Until I can figure a better way to do this, rely on magic numbers. */
  169. # define STDOUT_FILENO 1
  170. # define STDERR_FILENO 2
  171. #else
  172. # include <unistd.h>
  173. # define _fct_pipe pipe
  174. # define _fct_dup dup
  175. # define _fct_dup2 dup2
  176. # define _fct_close close
  177. # define _fct_read read
  178. #endif /* WIN32 */
  179. static void
  180. fct_switch_std_to_buffer(int std_pipe[2], FILE *out, int fileno_, int *save_handle)
  181. {
  182. fflush(out);
  183. *save_handle = _fct_dup(fileno_);
  184. if ( _fct_pipe(std_pipe) != 0 )
  185. {
  186. exit(1);
  187. }
  188. _fct_dup2(std_pipe[1], fileno_);
  189. _fct_close(std_pipe[1]);
  190. }
  191. static void
  192. fct_switch_std_to_std(FILE *out, int fileno_, int save_handle)
  193. {
  194. fflush(out);
  195. _fct_dup2(save_handle, fileno_);
  196. }
  197. #define FCT_SWITCH_STDOUT_TO_BUFFER() \
  198. fct_switch_std_to_buffer(fct_stdout_pipe, stdout, STDOUT_FILENO, &fct_saved_stdout)
  199. #define FCT_SWITCH_STDOUT_TO_STDOUT() \
  200. fct_switch_std_to_std(stdout, STDOUT_FILENO, fct_saved_stdout)
  201. #define FCT_SWITCH_STDERR_TO_BUFFER() \
  202. fct_switch_std_to_buffer(fct_stderr_pipe, stderr, STDERR_FILENO, &fct_saved_stderr)
  203. #define FCT_SWITCH_STDERR_TO_STDERR() \
  204. fct_switch_std_to_std(stderr, STDERR_FILENO, fct_saved_stderr)
  205. /* Utility for truncated, safe string copies. The NUM
  206. should be the length of DST plus the null-termintor. */
  207. static void
  208. fctstr_safe_cpy(char *dst, char const *src, size_t num)
  209. {
  210. FCT_ASSERT( dst != NULL );
  211. FCT_ASSERT( src != NULL );
  212. FCT_ASSERT( num > 0 );
  213. #if defined(WIN32) && _MSC_VER >= 1400
  214. strncpy_s(dst, num, src, _TRUNCATE);
  215. #else
  216. strncpy(dst, src, num);
  217. #endif
  218. dst[num-1] = '\0';
  219. }
  220. /* Isolate the vsnprintf implementation */
  221. static int
  222. fct_vsnprintf(char *buffer,
  223. size_t buffer_len,
  224. char const *format,
  225. va_list args)
  226. {
  227. int count =0;
  228. /* Older microsoft compilers where not ANSI compliant with this
  229. function and you had to use _vsnprintf. I will assume that newer
  230. Microsoft Compilers start implementing vsnprintf. */
  231. #if defined(_MSC_VER) && (_MSC_VER < 1400)
  232. count = _vsnprintf(buffer, buffer_len, format, args);
  233. #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
  234. count = vsnprintf_s(buffer, buffer_len, _TRUNCATE, format, args);
  235. #else
  236. count = vsnprintf(buffer, buffer_len, format, args);
  237. #endif
  238. return count;
  239. }
  240. /* Isolate the snprintf implemenation. */
  241. static int
  242. fct_snprintf(char *buffer, size_t buffer_len, char const *format, ...)
  243. {
  244. int count =0;
  245. va_list args;
  246. va_start(args, format);
  247. count =fct_vsnprintf(buffer, buffer_len, format, args);
  248. va_end(args);
  249. return count;
  250. }
  251. /* Helper to for cloning strings on the heap. Returns NULL for
  252. an out of memory condition. */
  253. static char*
  254. fctstr_clone(char const *s)
  255. {
  256. char *k =NULL;
  257. size_t klen =0;
  258. FCT_ASSERT( s != NULL && "invalid arg");
  259. klen = strlen(s)+1;
  260. k = (char*)malloc(sizeof(char)*klen+1);
  261. fctstr_safe_cpy(k, s, klen);
  262. return k;
  263. }
  264. /* Clones and returns a lower case version of the original string. */
  265. static char*
  266. fctstr_clone_lower(char const *s)
  267. {
  268. char *k =NULL;
  269. size_t klen =0;
  270. size_t i;
  271. if ( s == NULL )
  272. {
  273. return NULL;
  274. }
  275. klen = strlen(s)+1;
  276. k = (char*)malloc(sizeof(char)*klen+1);
  277. for ( i=0; i != klen; ++i )
  278. {
  279. k[i] = (char)tolower(s[i]);
  280. }
  281. return k;
  282. }
  283. /* A very, very simple "filter". This just compares the supplied prefix
  284. against the test_str, to see if they both have the same starting
  285. characters. If they do we return true, otherwise we return false. If the
  286. prefix is a blank string or NULL, then it will return FCT_TRUE.*/
  287. static nbool_t
  288. fct_filter_pass(char const *prefix, char const *test_str)
  289. {
  290. nbool_t is_match = FCT_FALSE;
  291. char const *prefix_p;
  292. char const *test_str_p;
  293. /* If you got nothing to test against, why test? */
  294. FCT_ASSERT( test_str != NULL );
  295. /* When the prefix is NULL or blank, we always return FCT_TRUE. */
  296. if ( prefix == NULL || prefix[0] == '\0' )
  297. {
  298. return FCT_TRUE;
  299. }
  300. /* Iterate through both character arrays at the same time. We are
  301. going to play a game and see if we can beat the house. */
  302. for ( prefix_p = prefix, test_str_p = test_str;
  303. *prefix_p != '\0' && *test_str_p != '\0';
  304. ++prefix_p, ++test_str_p )
  305. {
  306. is_match = *prefix_p == *test_str_p;
  307. if ( !is_match )
  308. {
  309. break; /* Quit the first time we don't match. */
  310. }
  311. }
  312. /* If the iterator for the test_str is pointing at the null char, and
  313. the iterator for the prefix string is not, then the prefix string is
  314. larger than the actual test string, and therefore we failed to pass the
  315. filter. */
  316. if ( *test_str_p == '\0' && *prefix_p != '\0' )
  317. {
  318. return FCT_FALSE;
  319. }
  320. /* is_match will be set to the either FCT_TRUE if we kicked of the loop
  321. early because our filter ran out of characters or FCT_FALSE if we
  322. encountered a mismatch before our filter ran out of characters. */
  323. return is_match;
  324. }
  325. /* Routine checks if two strings are equal. Taken from
  326. http://publications.gbdirect.co.uk/c_book/chapter5/character_handling.html
  327. */
  328. static int
  329. fctstr_eq(char const *s1, char const *s2)
  330. {
  331. if ( s1 == s2 )
  332. {
  333. return 1;
  334. }
  335. if ( (s1 == NULL && s2 != NULL)
  336. || (s1 != NULL && s2 == NULL) )
  337. {
  338. return 0;
  339. }
  340. while (*s1 == *s2)
  341. {
  342. if (*s1 == '\0')
  343. return 1;
  344. s1++;
  345. s2++;
  346. }
  347. /* Difference detected! */
  348. return 0;
  349. }
  350. static int
  351. fctstr_ieq(char const *s1, char const *s2)
  352. {
  353. if ( s1 == s2 )
  354. {
  355. return 1;
  356. }
  357. if ( (s1 == NULL && s2 != NULL)
  358. || (s1 != NULL && s2 == NULL) )
  359. {
  360. return 0;
  361. }
  362. while (tolower(*s1) == tolower(*s2))
  363. {
  364. if (*s1 == '\0')
  365. return 1;
  366. s1++;
  367. s2++;
  368. }
  369. /* Difference detected! */
  370. return 0;
  371. }
  372. /* Returns 1 if the STR contains the CHECK_INCL substring. NULL's
  373. are handled, and NULL always INCLUDES NULL. This check is case
  374. sensitive. If two strings point to the same place they are
  375. included. */
  376. static int
  377. fctstr_incl(char const *str, char const *check_incl)
  378. {
  379. static char const *blank_s = "";
  380. char const *found = NULL;
  381. if ( str == NULL )
  382. {
  383. str = blank_s;
  384. }
  385. if ( check_incl == NULL )
  386. {
  387. check_incl = blank_s;
  388. }
  389. if ( str == check_incl )
  390. {
  391. return 1;
  392. }
  393. found = strstr(str, check_incl);
  394. return found != NULL;
  395. }
  396. /* Does a case insensitive include check. */
  397. static int
  398. fctstr_iincl(char const *str, char const *check_incl)
  399. {
  400. /* Going to do this with a memory allocation to save coding
  401. time. In the future this can be rewritten. Both clone_lower
  402. and _incl are NULL tolerant. */
  403. char *lstr = fctstr_clone_lower(str);
  404. char *lcheck_incl = fctstr_clone_lower(check_incl);
  405. int found = fctstr_incl(lstr, lcheck_incl);
  406. free(lstr);
  407. free(lcheck_incl);
  408. return found;
  409. }
  410. /* Returns true if STR starts with CHECK. NULL and NULL is consider
  411. true. */
  412. static int
  413. fctstr_startswith(char const *str, char const *check)
  414. {
  415. char const *sp;
  416. if ( str == NULL && check == NULL )
  417. {
  418. return 1;
  419. }
  420. else if ( ((str == NULL) && (check != NULL))
  421. || ((str != NULL) && (check == NULL)) )
  422. {
  423. return 0;
  424. }
  425. sp = strstr(str, check);
  426. return sp == str;
  427. }
  428. /* Case insenstive variant of fctstr_startswith. */
  429. static int
  430. fctstr_istartswith(char const *str, char const *check)
  431. {
  432. /* Taking the lazy approach for now. */
  433. char *istr = fctstr_clone_lower(str);
  434. char *icheck = fctstr_clone_lower(check);
  435. /* TODO: check for memory. */
  436. int startswith = fctstr_startswith(istr, icheck);
  437. free(istr);
  438. free(icheck);
  439. return startswith;
  440. }
  441. /* Returns true if the given string ends with the given
  442. check. Treats NULL as a blank string, and as such, will
  443. pass the ends with (a blank string endswith a blank string). */
  444. static int
  445. fctstr_endswith(char const *str, char const *check)
  446. {
  447. size_t check_i;
  448. size_t str_i;
  449. if ( str == NULL && check == NULL )
  450. {
  451. return 1;
  452. }
  453. else if ( ((str == NULL) && (check != NULL))
  454. || ((str != NULL) && (check == NULL)) )
  455. {
  456. return 0;
  457. }
  458. check_i = strlen(check);
  459. str_i = strlen(str);
  460. if ( str_i < check_i )
  461. {
  462. return 0; /* Can't do it string is too small. */
  463. }
  464. for ( ; check_i != 0; --check_i, --str_i)
  465. {
  466. if ( str[str_i] != check[check_i] )
  467. {
  468. return 0; /* Found a case where they are not equal. */
  469. }
  470. }
  471. /* Exahausted check against string, can only be true. */
  472. return 1;
  473. }
  474. static int
  475. fctstr_iendswith(char const *str, char const *check)
  476. {
  477. size_t check_i;
  478. size_t str_i;
  479. if ( str == NULL && check == NULL )
  480. {
  481. return 1;
  482. }
  483. else if ( ((str == NULL) && (check != NULL))
  484. || ((str != NULL) && (check == NULL)) )
  485. {
  486. return 0;
  487. }
  488. check_i = strlen(check);
  489. str_i = strlen(str);
  490. if ( str_i < check_i )
  491. {
  492. return 0; /* Can't do it string is too small. */
  493. }
  494. for ( ; check_i != 0; --check_i, --str_i)
  495. {
  496. if ( tolower(str[str_i]) != tolower(check[check_i]) )
  497. {
  498. return 0; /* Found a case where they are not equal. */
  499. }
  500. }
  501. /* Exahausted check against string, can only be true. */
  502. return 1;
  503. }
  504. /* Use this with the _end variant to get the
  505. STARTSWITH ........................................ END
  506. effect. Assumes that the line will be maxwidth in characters. The
  507. maxwidth can't be greater than FCT_DOTTED_MAX_LEN. */
  508. #define FCT_DOTTED_MAX_LEN 256
  509. static void
  510. fct_dotted_line_start(size_t maxwidth, char const *startwith)
  511. {
  512. char line[FCT_DOTTED_MAX_LEN];
  513. size_t len =0;
  514. size_t line_len =0;
  515. memset(line, '.', sizeof(char)*maxwidth);
  516. len = strlen(startwith);
  517. line_len = FCTMIN(maxwidth-1, len);
  518. memcpy(line, startwith, sizeof(char)*line_len);
  519. if ( len < maxwidth-1)
  520. {
  521. line[len] = ' ';
  522. }
  523. line[maxwidth-1] = '\0';
  524. fputs(line, stdout);
  525. }
  526. static void
  527. fct_dotted_line_end(char const *endswith)
  528. {
  529. printf(" %s\n", endswith);
  530. }
  531. /*
  532. --------------------------------------------------------
  533. TIMER
  534. --------------------------------------------------------
  535. This is a low-res implementation at the moment.
  536. We will improve this in the future, and isolate the
  537. implementation from the rest of the code.
  538. */
  539. typedef struct _fct_timer_t fct_timer_t;
  540. struct _fct_timer_t
  541. {
  542. clock_t start;
  543. clock_t stop;
  544. double duration;
  545. };
  546. static void
  547. fct_timer__init(fct_timer_t *timer)
  548. {
  549. FCT_ASSERT(timer != NULL);
  550. memset(timer, 0, sizeof(fct_timer_t));
  551. }
  552. static void
  553. fct_timer__start(fct_timer_t *timer)
  554. {
  555. FCT_ASSERT(timer != NULL);
  556. timer->start = clock();
  557. }
  558. static void
  559. fct_timer__stop(fct_timer_t *timer)
  560. {
  561. FCT_ASSERT(timer != NULL);
  562. timer->stop = clock();
  563. timer->duration = (double) (timer->stop - timer->start) / CLOCKS_PER_SEC;
  564. }
  565. /* Returns the time in seconds. */
  566. static double
  567. fct_timer__duration(fct_timer_t const *timer)
  568. {
  569. FCT_ASSERT( timer != NULL );
  570. return timer->duration;
  571. }
  572. /*
  573. --------------------------------------------------------
  574. GENERIC LIST
  575. --------------------------------------------------------
  576. */
  577. /* For now we will just keep it at a linear growth rate. */
  578. #define FCT_LIST_GROWTH_FACTOR 2
  579. /* Starting size for the list, to keep it simple we will start
  580. at a reasonable size. */
  581. #define FCT_LIST_DEFAULT_START_SZ 8
  582. /* Helper macros for quickly iterating through a list. You should be able
  583. to do something like,
  584. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, my_list)
  585. {
  586. fct_logger__on_blah(logger);
  587. }
  588. FCT_NLIST_FOREACH_END();
  589. */
  590. #define FCT_NLIST_FOREACH_BGN(Type, Var, List)\
  591. {\
  592. if ( List != NULL ) {\
  593. size_t item_i##Var;\
  594. size_t num_items##Var = fct_nlist__size(List);\
  595. for( item_i##Var =0; item_i##Var != num_items##Var; ++item_i##Var )\
  596. {\
  597. Type Var = (Type) fct_nlist__at((List), item_i##Var);
  598. #define FCT_NLIST_FOREACH_END() }}}
  599. /* Used to manage a list of loggers. This works mostly like
  600. the STL vector, where the array grows as more items are
  601. appended. */
  602. typedef struct _fct_nlist_t fct_nlist_t;
  603. struct _fct_nlist_t
  604. {
  605. /* Item's are stored as pointers to void. */
  606. void **itm_list;
  607. /* Indicates the number of element's in the array. */
  608. size_t avail_itm_num;
  609. /* Indicates the number of actually elements in the array. */
  610. size_t used_itm_num;
  611. };
  612. typedef void (*fct_nlist_on_del_t)(void*);
  613. /* Clears the contents of the list, and sets the list count to 0. The
  614. actual count remains unchanged. If on_del is supplied it is executed
  615. against each list element. */
  616. static void
  617. fct_nlist__clear(fct_nlist_t *list, fct_nlist_on_del_t on_del)
  618. {
  619. size_t itm_i__ =0;
  620. FCT_ASSERT( list != NULL );
  621. if ( on_del != NULL )
  622. {
  623. for ( itm_i__=0; itm_i__ != list->used_itm_num; ++itm_i__ )
  624. {
  625. on_del(list->itm_list[itm_i__]);
  626. }
  627. }
  628. list->used_itm_num =0;
  629. }
  630. /* If you used init, then close with final. This is useful for
  631. working with structures that live on the stack. */
  632. static void
  633. fct_nlist__final(fct_nlist_t *list, fct_nlist_on_del_t on_del)
  634. {
  635. FCT_ASSERT( list != NULL );
  636. fct_nlist__clear(list, on_del);
  637. free(list->itm_list);
  638. }
  639. static int
  640. fct_nlist__init2(fct_nlist_t *list, size_t start_sz)
  641. {
  642. FCT_ASSERT( list != NULL );
  643. if ( start_sz == 0 )
  644. {
  645. list->itm_list = NULL;
  646. }
  647. else
  648. {
  649. list->itm_list = (void**)malloc(sizeof(void*)*start_sz);
  650. if ( list->itm_list == NULL )
  651. {
  652. return 0;
  653. }
  654. }
  655. /* If these are both 0, then they are equal and that means
  656. that the first append operation will allocate memory. The beauty
  657. here is that if the list remains empty, then we save a malloc.
  658. Empty lists are relatively common in FCT (consider an error list). */
  659. list->avail_itm_num = start_sz;
  660. list->used_itm_num =0;
  661. return 1;
  662. }
  663. /* Initializes a list. Useful for populating existing structures.
  664. Returns 0 if there was an error allocating memory. Returns 1 otherwise. */
  665. #define fct_nlist__init(_LIST_PTR_) \
  666. (fct_nlist__init2((_LIST_PTR_), FCT_LIST_DEFAULT_START_SZ))
  667. /* Returns the number of elements within the list. */
  668. static size_t
  669. fct_nlist__size(fct_nlist_t const *list)
  670. {
  671. FCT_ASSERT( list != NULL );
  672. return list->used_itm_num;
  673. }
  674. /* Returns the item at idx, asserts otherwise. */
  675. static void*
  676. fct_nlist__at(fct_nlist_t const *list, size_t idx)
  677. {
  678. FCT_ASSERT( list != NULL );
  679. FCT_ASSERT( idx < list->used_itm_num );
  680. return list->itm_list[idx];
  681. }
  682. static void
  683. fct_nlist__append(fct_nlist_t *list, void *itm)
  684. {
  685. FCT_ASSERT( list != NULL );
  686. /* If we ran out of room, then the last increment should be equal to the
  687. available space, in this case we need to grow a little more. If this
  688. list started as size 0, then we should encounter the same effect as
  689. "running out of room." */
  690. if ( list->used_itm_num == list->avail_itm_num )
  691. {
  692. /* Use multiple and add, since the avail_itm_num could be 0. */
  693. list->avail_itm_num = list->avail_itm_num*FCT_LIST_GROWTH_FACTOR+\
  694. FCT_LIST_GROWTH_FACTOR;
  695. list->itm_list = (void**)realloc(
  696. list->itm_list, sizeof(void*)*list->avail_itm_num
  697. );
  698. FCT_ASSERT( list->itm_list != NULL && "memory check");
  699. }
  700. list->itm_list[list->used_itm_num] = itm;
  701. ++(list->used_itm_num);
  702. }
  703. /*
  704. -----------------------------------------------------------
  705. A SINGLE CHECK
  706. -----------------------------------------------------------
  707. This defines a single check. It indicates what the check was,
  708. and where it occurred. A "Test" object will have-a bunch
  709. of "checks".
  710. */
  711. struct _fctchk_t
  712. {
  713. /* This string that represents the condition. */
  714. char cndtn[FCT_MAX_LOG_LINE];
  715. /* These indicate where the condition occurred. */
  716. char file[FCT_MAX_LOG_LINE];
  717. int lineno;
  718. nbool_t is_pass;
  719. /* This is a message that we can "format into", if
  720. no format string is specified this should be
  721. equivalent to the cntdn. */
  722. char msg[FCT_MAX_LOG_LINE];
  723. };
  724. #define fctchk__is_pass(_CHK_) ((_CHK_)->is_pass)
  725. #define fctchk__file(_CHK_) ((_CHK_)->file)
  726. #define fctchk__lineno(_CHK_) ((_CHK_)->lineno)
  727. #define fctchk__cndtn(_CHK_) ((_CHK_)->cndtn)
  728. #define fctchk__msg(_CHK_) ((_CHK_)->msg)
  729. static fctchk_t*
  730. fctchk_new(int is_pass,
  731. char const *cndtn,
  732. char const *file,
  733. int lineno,
  734. char const *format,
  735. va_list args)
  736. {
  737. fctchk_t *chk = NULL;
  738. FCT_ASSERT( cndtn != NULL );
  739. FCT_ASSERT( file != NULL );
  740. FCT_ASSERT( lineno > 0 );
  741. chk = (fctchk_t*)calloc(1, sizeof(fctchk_t));
  742. if ( chk == NULL )
  743. {
  744. return NULL;
  745. }
  746. fctstr_safe_cpy(chk->cndtn, cndtn, FCT_MAX_LOG_LINE);
  747. fctstr_safe_cpy(chk->file, file, FCT_MAX_LOG_LINE);
  748. chk->lineno = lineno;
  749. chk->is_pass =is_pass;
  750. if ( format != NULL )
  751. {
  752. fct_vsnprintf(chk->msg, FCT_MAX_LOG_LINE, format, args);
  753. }
  754. else
  755. {
  756. /* Default to make the condition be the message, if there was no format
  757. specified. */
  758. fctstr_safe_cpy(chk->msg, cndtn, FCT_MAX_LOG_LINE);
  759. }
  760. return chk;
  761. }
  762. /* Cleans up a "check" object. If the `chk` is NULL, this function does
  763. nothing. */
  764. static void
  765. fctchk__del(fctchk_t *chk)
  766. {
  767. if ( chk == NULL )
  768. {
  769. return;
  770. }
  771. free( chk );
  772. }
  773. /*
  774. -----------------------------------------------------------
  775. A TEST
  776. -----------------------------------------------------------
  777. A suite will have-a list of tests. Where each test will have-a
  778. list of failed and passed checks.
  779. */
  780. struct _fct_test_t
  781. {
  782. /* List of failed and passed "checks" (fctchk_t). Two seperate
  783. lists make it faster to determine how many checks passed and how
  784. many checks failed. */
  785. fct_nlist_t failed_chks;
  786. fct_nlist_t passed_chks;
  787. /* To store the test run time */
  788. fct_timer_t timer;
  789. /* The name of the test case. */
  790. char name[FCT_MAX_NAME];
  791. };
  792. #define fct_test__name(_TEST_) ((_TEST_)->name)
  793. /* Clears the failed tests ... partly for internal testing. */
  794. #define fct_test__clear_failed(test) \
  795. fct_nlist__clear(test->failed_chks, (fct_nlist_on_del_t)fctchk__del);\
  796. static void
  797. fct_test__del(fct_test_t *test)
  798. {
  799. if (test == NULL )
  800. {
  801. return;
  802. }
  803. fct_nlist__final(&(test->passed_chks), (fct_nlist_on_del_t)fctchk__del);
  804. fct_nlist__final(&(test->failed_chks), (fct_nlist_on_del_t)fctchk__del);
  805. free(test);
  806. }
  807. static fct_test_t*
  808. fct_test_new(char const *name)
  809. {
  810. nbool_t ok =FCT_FALSE;
  811. fct_test_t *test =NULL;
  812. test = (fct_test_t*)malloc(sizeof(fct_test_t));
  813. if ( test == NULL )
  814. {
  815. return NULL;
  816. }
  817. fctstr_safe_cpy(test->name, name, FCT_MAX_NAME);
  818. /* Failures are an exception, so lets not allocate up
  819. the list until we need to. */
  820. fct_nlist__init2(&(test->failed_chks), 0);
  821. if (!fct_nlist__init(&(test->passed_chks)))
  822. {
  823. ok =FCT_FALSE;
  824. goto finally;
  825. }
  826. fct_timer__init(&(test->timer));
  827. ok =FCT_TRUE;
  828. finally:
  829. if ( !ok )
  830. {
  831. fct_test__del(test);
  832. test =NULL;
  833. }
  834. return test;
  835. }
  836. static void
  837. fct_test__start_timer(fct_test_t *test)
  838. {
  839. FCT_ASSERT( test != NULL );
  840. fct_timer__start(&(test->timer));
  841. }
  842. static void
  843. fct_test__stop_timer(fct_test_t *test)
  844. {
  845. FCT_ASSERT( test != NULL );
  846. fct_timer__stop(&(test->timer));
  847. }
  848. static double
  849. fct_test__duration(fct_test_t const *test)
  850. {
  851. FCT_ASSERT( test != NULL );
  852. return fct_timer__duration(&(test->timer));
  853. }
  854. static nbool_t
  855. fct_test__is_pass(fct_test_t const *test)
  856. {
  857. FCT_ASSERT( test != NULL );
  858. return fct_nlist__size(&(test->failed_chks)) == 0;
  859. }
  860. static void
  861. fct_test__add(fct_test_t *test, fctchk_t *chk)
  862. {
  863. FCT_ASSERT( test != NULL );
  864. FCT_ASSERT( chk != NULL );
  865. if ( fctchk__is_pass(chk) )
  866. {
  867. fct_nlist__append(&(test->passed_chks), (void*)chk);
  868. }
  869. else
  870. {
  871. fct_nlist__append(&(test->failed_chks), (void*)chk);
  872. }
  873. }
  874. /* Returns the number of checks made throughout the test. */
  875. static size_t
  876. fct_test__chk_cnt(fct_test_t const *test)
  877. {
  878. FCT_ASSERT( test != NULL );
  879. return fct_nlist__size(&(test->failed_chks)) \
  880. + fct_nlist__size(&(test->passed_chks));
  881. }
  882. /*
  883. -----------------------------------------------------------
  884. TEST SUITE (TS)
  885. -----------------------------------------------------------
  886. */
  887. /* The different types of 'modes' that a test suite can be in.
  888. While the test suite is iterating through all the tests, its "State"
  889. can change from "setup mode", to "test mode" to "tear down" mode.
  890. These help to indicate what mode are currently in. Think of it as a
  891. basic FSM.
  892. if the count was 0 end
  893. +--------->---------------------> ending_mode-----+-+
  894. | ^ |
  895. ^ | ^
  896. start | [if no more tests] |
  897. | | | |
  898. +-count_mode -> setup_mode -> test_mode -> teardown_mode->-+
  899. | ^ | |
  900. | +-----------<---------------+ |
  901. +----------->---[if fct_req fails]--------+
  902. */
  903. enum ts_mode
  904. {
  905. ts_mode_cnt, /* To setup when done counting. */
  906. ts_mode_setup, /* To test when done setup. */
  907. ts_mode_teardown, /* To ending mode, when no more tests. */
  908. ts_mode_test, /* To tear down mode. */
  909. ts_mode_ending, /* To ... */
  910. ts_mode_end, /* .. The End. */
  911. ts_mode_abort /* Abort */
  912. };
  913. /* Types of states the test could be in. */
  914. typedef enum
  915. {
  916. fct_test_status_SUCCESS,
  917. fct_test_status_FAILURE
  918. } fct_test_status;
  919. struct _fct_ts_t
  920. {
  921. /* For counting our 'current' test number, and the total number of
  922. tests. */
  923. int curr_test_num;
  924. int total_test_num;
  925. /* Keeps track of the current state of the object while it is walking
  926. through its "FSM" */
  927. enum ts_mode mode;
  928. /* The name of the test suite. */
  929. char name[FCT_MAX_NAME];
  930. /* List of tests that where executed within the test suite. */
  931. fct_nlist_t test_list;
  932. };
  933. #define fct_ts__is_setup_mode(ts) ((ts)->mode == ts_mode_setup)
  934. #define fct_ts__is_teardown_mode(ts) ((ts)->mode == ts_mode_teardown)
  935. #define fct_ts__is_test_mode(ts) ((ts)->mode == ts_mode_test)
  936. #define fct_ts__is_ending_mode(ts) ((ts)->mode == ts_mode_ending)
  937. #define fct_ts__is_end(ts) ((ts)->mode == ts_mode_end)
  938. #define fct_ts__is_cnt_mode(ts) ((ts)->mode == ts_mode_cnt)
  939. #define fct_ts__is_abort_mode(ts) ((ts)->mode == ts_mode_abort)
  940. /* This cndtn is set when we have iterated through all the tests, and
  941. there was nothing more to do. */
  942. #define fct_ts__ending(ts) ((ts)->mode = ts_mode_ending)
  943. /* Flag a test suite as complete. It will no longer accept any more tests. */
  944. #define fct_ts__end(ts) ((ts)->mode = ts_mode_end)
  945. #define fct_ts__name(ts) ((ts)->name)
  946. static void
  947. fct_ts__del(fct_ts_t *ts)
  948. {
  949. if ( ts == NULL )
  950. {
  951. return;
  952. }
  953. fct_nlist__final(&(ts->test_list), (fct_nlist_on_del_t)fct_test__del);
  954. free(ts);
  955. }
  956. static fct_ts_t *
  957. fct_ts_new(char const *name)
  958. {
  959. fct_ts_t *ts =NULL;
  960. ts = (fct_ts_t*)calloc(1, sizeof(fct_ts_t));
  961. FCT_ASSERT( ts != NULL );
  962. fctstr_safe_cpy(ts->name, name, FCT_MAX_NAME);
  963. ts->mode = ts_mode_cnt;
  964. fct_nlist__init(&(ts->test_list));
  965. return ts;
  966. }
  967. static nbool_t
  968. fct_ts__is_more_tests(fct_ts_t const *ts)
  969. {
  970. FCT_ASSERT( ts != NULL );
  971. FCT_ASSERT( !fct_ts__is_end(ts) );
  972. return ts->curr_test_num < ts->total_test_num;
  973. }
  974. /* Indicates that we have started a test case. */
  975. static void
  976. fct_ts__test_begin(fct_ts_t *ts)
  977. {
  978. FCT_ASSERT( !fct_ts__is_end(ts) );
  979. ++(ts->curr_test_num);
  980. }
  981. /* Takes OWNERSHIP of a test object, and warehouses it for later stat
  982. generation. */
  983. static void
  984. fct_ts__add_test(fct_ts_t *ts, fct_test_t *test)
  985. {
  986. FCT_ASSERT( ts != NULL && "invalid arg");
  987. FCT_ASSERT( test != NULL && "invalid arg");
  988. FCT_ASSERT( !fct_ts__is_end(ts) );
  989. fct_nlist__append(&(ts->test_list), test);
  990. }
  991. static void
  992. fct_ts__test_end(fct_ts_t *ts)
  993. {
  994. FCT_ASSERT( ts != NULL );
  995. /* After a test has completed, move to teardown mode. */
  996. ts->mode = ts_mode_teardown;
  997. }
  998. /* Increments the internal count by 1. */
  999. static void
  1000. fct_ts__inc_total_test_num(fct_ts_t *ts)
  1001. {
  1002. FCT_ASSERT( ts != NULL );
  1003. FCT_ASSERT( fct_ts__is_cnt_mode(ts) );
  1004. FCT_ASSERT( !fct_ts__is_end(ts) );
  1005. ++(ts->total_test_num);
  1006. }
  1007. /* Flags the end of the setup, which implies we are going to move into
  1008. setup mode. You must be already in setup mode for this to work! */
  1009. static void
  1010. fct_ts__setup_end(fct_ts_t *ts)
  1011. {
  1012. if ( ts->mode != ts_mode_abort )
  1013. {
  1014. ts->mode = ts_mode_test;
  1015. }
  1016. }
  1017. static fct_test_t *
  1018. fct_ts__make_abort_test(fct_ts_t *ts)
  1019. {
  1020. char setup_testname[FCT_MAX_LOG_LINE+1] = {'\0'};
  1021. char const *suitename = fct_ts__name(ts);
  1022. fct_snprintf(setup_testname, FCT_MAX_LOG_LINE, "setup_%s", suitename);
  1023. return fct_test_new(setup_testname);
  1024. }
  1025. /* Flags a pre-mature abort of a setup (like a failed fct_req). */
  1026. static void
  1027. fct_ts__setup_abort(fct_ts_t *ts)
  1028. {
  1029. FCT_ASSERT( ts != NULL );
  1030. ts->mode = ts_mode_abort;
  1031. }
  1032. /* Flags the end of the teardown, which implies we are going to move
  1033. into setup mode (for the next 'iteration'). */
  1034. static void
  1035. fct_ts__teardown_end(fct_ts_t *ts)
  1036. {
  1037. if ( ts->mode == ts_mode_abort )
  1038. {
  1039. return; /* Because we are aborting . */
  1040. }
  1041. /* We have to decide if we should keep on testing by moving into tear down
  1042. mode or if we have reached the real end and should be moving into the
  1043. ending mode. */
  1044. if ( fct_ts__is_more_tests(ts) )
  1045. {
  1046. ts->mode = ts_mode_setup;
  1047. }
  1048. else
  1049. {
  1050. ts->mode = ts_mode_ending;
  1051. }
  1052. }
  1053. /* Flags the end of the counting, and proceeding to the first setup.
  1054. Consider the special case when a test suite has NO tests in it, in
  1055. that case we will have a current count that is zero, in which case
  1056. we can skip right to 'ending'. */
  1057. static void
  1058. fct_ts__cnt_end(fct_ts_t *ts)
  1059. {
  1060. FCT_ASSERT( ts != NULL );
  1061. FCT_ASSERT( fct_ts__is_cnt_mode(ts) );
  1062. FCT_ASSERT( !fct_ts__is_end(ts) );
  1063. if (ts->total_test_num == 0 )
  1064. {
  1065. ts->mode = ts_mode_ending;
  1066. }
  1067. else
  1068. {
  1069. ts->mode = ts_mode_setup;
  1070. }
  1071. }
  1072. static nbool_t
  1073. fct_ts__is_test_cnt(fct_ts_t const *ts, int test_num)
  1074. {
  1075. FCT_ASSERT( ts != NULL );
  1076. FCT_ASSERT( 0 <= test_num );
  1077. FCT_ASSERT( test_num < ts->total_test_num );
  1078. FCT_ASSERT( !fct_ts__is_end(ts) );
  1079. /* As we roll through the tests we increment the count. With this
  1080. count we can decide if we need to execute a test or not. */
  1081. return test_num == ts->curr_test_num;
  1082. }
  1083. /* Returns the # of tests on the FCT TS object. This is the actual
  1084. # of tests executed. */
  1085. static size_t
  1086. fct_ts__tst_cnt(fct_ts_t const *ts)
  1087. {
  1088. FCT_ASSERT( ts != NULL );
  1089. FCT_ASSERT(
  1090. fct_ts__is_end(ts)
  1091. && "can't count number of tests executed until the test suite ends"
  1092. );
  1093. return fct_nlist__size(&(ts->test_list));
  1094. }
  1095. /* Returns the # of tests in the TS object that passed. */
  1096. static size_t
  1097. fct_ts__tst_cnt_passed(fct_ts_t const *ts)
  1098. {
  1099. size_t tally =0;
  1100. FCT_ASSERT( ts != NULL );
  1101. FCT_ASSERT( fct_ts__is_end(ts) );
  1102. FCT_NLIST_FOREACH_BGN(fct_test_t*, test, &(ts->test_list))
  1103. {
  1104. if ( fct_test__is_pass(test) )
  1105. {
  1106. tally += 1;
  1107. }
  1108. }
  1109. FCT_NLIST_FOREACH_END();
  1110. return tally;
  1111. }
  1112. /* Returns the # of checks made throughout a test suite. */
  1113. static size_t
  1114. fct_ts__chk_cnt(fct_ts_t const *ts)
  1115. {
  1116. size_t tally =0;
  1117. FCT_ASSERT( ts != NULL );
  1118. FCT_NLIST_FOREACH_BGN(fct_test_t *, test, &(ts->test_list))
  1119. {
  1120. tally += fct_test__chk_cnt(test);
  1121. }
  1122. FCT_NLIST_FOREACH_END();
  1123. return tally;
  1124. }
  1125. /* Currently the duration is simply a sum of all the tests. */
  1126. static double
  1127. fct_ts__duration(fct_ts_t const *ts)
  1128. {
  1129. double tally =0.0;
  1130. FCT_ASSERT( ts != NULL );
  1131. FCT_NLIST_FOREACH_BGN(fct_test_t *, test, &(ts->test_list))
  1132. {
  1133. tally += fct_test__duration(test);
  1134. }
  1135. FCT_NLIST_FOREACH_END();
  1136. return tally;
  1137. }
  1138. /*
  1139. --------------------------------------------------------
  1140. FCT COMMAND LINE OPTION INITIALIZATION (fctcl_init)
  1141. --------------------------------------------------------
  1142. Structure used for command line initialization. To keep it clear that we do
  1143. not delete the char*'s present on this structure.
  1144. */
  1145. typedef enum
  1146. {
  1147. FCTCL_STORE_UNDEFINED,
  1148. FCTCL_STORE_TRUE,
  1149. FCTCL_STORE_VALUE
  1150. } fctcl_store_t;
  1151. typedef struct _fctcl_init_t
  1152. {
  1153. /* What to parse for this option. --long versus -s. */
  1154. char const *long_opt; /* i.e. --help */
  1155. char const *short_opt; /* i.e. -h */
  1156. /* What action to take when the option is activated. */
  1157. fctcl_store_t action;
  1158. /* The help string for the action. */
  1159. char const *help;
  1160. } fctcl_init_t;
  1161. /* Use when defining the option list. */
  1162. #define FCTCL_INIT_NULL \
  1163. {NULL, NULL, FCTCL_STORE_UNDEFINED, NULL}
  1164. /*
  1165. --------------------------------------------------------
  1166. FCT COMMAND LINE OPTION (fctcl)
  1167. --------------------------------------------------------
  1168. Specifies the command line configuration options. Use this
  1169. to help initialize the fct_clp (command line parser).
  1170. */
  1171. /* Handy strings for storing "true" and "false". We can reference
  1172. these strings throughout the parse operation and not have to
  1173. worry about dealing with memory. */
  1174. #define FCTCL_TRUE_STR "1"
  1175. typedef struct _fctcl_t
  1176. {
  1177. /* What to parse for this option. --long versus -s. */
  1178. char *long_opt; /* i.e. --help */
  1179. char *short_opt; /* i.e. -h */
  1180. /* What action to take when the option is activated. */
  1181. fctcl_store_t action;
  1182. /* The help string for the action. */
  1183. char *help;
  1184. /* The result. */
  1185. char *value;
  1186. } fctcl_t;
  1187. #define fctcl_new() ((fctcl_t*)calloc(1, sizeof(fctcl_t)))
  1188. static void
  1189. fctcl__del(fctcl_t *clo)
  1190. {
  1191. if ( clo == NULL )
  1192. {
  1193. return;
  1194. }
  1195. if ( clo->long_opt )
  1196. {
  1197. free(clo->long_opt);
  1198. }
  1199. if ( clo->short_opt)
  1200. {
  1201. free(clo->short_opt);
  1202. }
  1203. if ( clo->value )
  1204. {
  1205. free(clo->value);
  1206. }
  1207. if ( clo->help )
  1208. {
  1209. free(clo->help);
  1210. }
  1211. free(clo);
  1212. }
  1213. static fctcl_t*
  1214. fctcl_new2(fctcl_init_t const *clo_init)
  1215. {
  1216. fctcl_t *clone = NULL;
  1217. int ok =0;
  1218. clone = fctcl_new();
  1219. if ( clone == NULL )
  1220. {
  1221. return NULL;
  1222. }
  1223. clone->action = clo_init->action;
  1224. if ( clo_init->help == NULL )
  1225. {
  1226. clone->help = NULL;
  1227. }
  1228. else
  1229. {
  1230. clone->help = fctstr_clone(clo_init->help);
  1231. if ( clone->help == NULL )
  1232. {
  1233. ok =0;
  1234. goto finally;
  1235. }
  1236. }
  1237. if ( clo_init->long_opt == NULL )
  1238. {
  1239. clone->long_opt = NULL;
  1240. }
  1241. else
  1242. {
  1243. clone->long_opt = fctstr_clone(clo_init->long_opt);
  1244. if ( clone->long_opt == NULL )
  1245. {
  1246. ok = 0;
  1247. goto finally;
  1248. }
  1249. }
  1250. if ( clo_init->short_opt == NULL )
  1251. {
  1252. clone->short_opt = NULL;
  1253. }
  1254. else
  1255. {
  1256. clone->short_opt = fctstr_clone(clo_init->short_opt);
  1257. if ( clone->short_opt == NULL )
  1258. {
  1259. ok =0;
  1260. goto finally;
  1261. }
  1262. }
  1263. ok = 1;
  1264. finally:
  1265. if ( !ok )
  1266. {
  1267. fctcl__del(clone);
  1268. clone = NULL;
  1269. }
  1270. return clone;
  1271. }
  1272. static int
  1273. fctcl__is_option(fctcl_t const *clo, char const *option)
  1274. {
  1275. FCT_ASSERT( clo != NULL );
  1276. if ( option == NULL )
  1277. {
  1278. return 0;
  1279. }
  1280. return ((clo->long_opt != NULL
  1281. && fctstr_eq(clo->long_opt, option))
  1282. ||
  1283. (clo->short_opt != NULL
  1284. && fctstr_eq(clo->short_opt, option))
  1285. );
  1286. }
  1287. #define fctcl__set_value(_CLO_, _VAL_) \
  1288. (_CLO_)->value = fctstr_clone((_VAL_));
  1289. /*
  1290. --------------------------------------------------------
  1291. FCT COMMAND PARSER (fct_clp)
  1292. --------------------------------------------------------
  1293. */
  1294. #define FCT_CLP_MAX_ERR_MSG_LEN 256
  1295. typedef struct _fct_clp_t
  1296. {
  1297. /* List of command line options. */
  1298. fct_nlist_t clo_list;
  1299. /* List of parameters (not options). */
  1300. fct_nlist_t param_list;
  1301. char error_msg[FCT_CLP_MAX_ERR_MSG_LEN];
  1302. int is_error;
  1303. } fct_clp_t;
  1304. static void
  1305. fct_clp__final(fct_clp_t *clp)
  1306. {
  1307. fct_nlist__final(&(clp->clo_list), (fct_nlist_on_del_t)fctcl__del);
  1308. fct_nlist__final(&(clp->param_list), (fct_nlist_on_del_t)free);
  1309. }
  1310. /* Add an configuration options. */
  1311. static int
  1312. fct_clp__add_options(fct_clp_t *clp, fctcl_init_t const *options)
  1313. {
  1314. fctcl_init_t const *pclo =NULL;
  1315. int ok;
  1316. for ( pclo = options; pclo->action != FCTCL_STORE_UNDEFINED; ++pclo )
  1317. {
  1318. fctcl_t *cpy = fctcl_new2(pclo);
  1319. if ( cpy == NULL )
  1320. {
  1321. ok = 0;
  1322. goto finally;
  1323. }
  1324. fct_nlist__append(&(clp->clo_list), (void*)cpy);
  1325. }
  1326. ok =1;
  1327. finally:
  1328. return ok;
  1329. }
  1330. /* Returns false if we ran out of memory. */
  1331. static int
  1332. fct_clp__init(fct_clp_t *clp, fctcl_init_t const *options)
  1333. {
  1334. int ok =0;
  1335. FCT_ASSERT( clp != NULL );
  1336. /* It is just much saner to manage a clone of the options. Then we know
  1337. who is in charge of the memory. */
  1338. ok = fct_nlist__init(&(clp->clo_list));
  1339. if ( !ok )
  1340. {
  1341. goto finally;
  1342. }
  1343. if ( options != NULL )
  1344. {
  1345. ok = fct_clp__add_options(clp, options);
  1346. if ( !ok )
  1347. {
  1348. goto finally;
  1349. }
  1350. }
  1351. ok = fct_nlist__init(&(clp->param_list));
  1352. if ( !ok )
  1353. {
  1354. goto finally;
  1355. }
  1356. ok =1;
  1357. finally:
  1358. if ( !ok )
  1359. {
  1360. fct_clp__final(clp);
  1361. }
  1362. return ok;
  1363. }
  1364. /* Parses the command line arguments. Use fct_clp__is_error and
  1365. fct_clp__get_error to figure out if something went awry. */
  1366. static void
  1367. fct_clp__parse(fct_clp_t *clp, int argc, char const *argv[])
  1368. {
  1369. int argi =1;
  1370. int is_option =0;
  1371. char *arg =NULL;
  1372. char *token =NULL;
  1373. char *next_token =NULL;
  1374. clp->error_msg[0] = '\0';
  1375. clp->is_error =0;
  1376. while ( argi < argc )
  1377. {
  1378. is_option =0;
  1379. token =NULL;
  1380. next_token = NULL;
  1381. arg = fctstr_clone(argv[argi]);
  1382. #if defined(_MSC_VER) && _MSC_VER > 1300
  1383. token = strtok_s(arg, "=", &next_token);
  1384. #else
  1385. token = strtok(arg, "=");
  1386. next_token = strtok(NULL, "=");
  1387. #endif
  1388. FCT_NLIST_FOREACH_BGN(fctcl_t*, pclo, &(clp->clo_list))
  1389. {
  1390. /* Need to reset for each search. strtok below is destructive. */
  1391. if ( fctcl__is_option(pclo, token) )
  1392. {
  1393. is_option =1;
  1394. if ( pclo->action == FCTCL_STORE_VALUE )
  1395. {
  1396. /* If this is --xxxx=value then the next strtok should succeed.
  1397. Otherwise, we need to chew up the next argument. */
  1398. if ( next_token != NULL && strlen(next_token) > 0 )
  1399. {
  1400. fctcl__set_value(pclo, next_token);
  1401. }
  1402. else
  1403. {
  1404. ++argi; /* Chew up the next value */
  1405. if ( argi >= argc )
  1406. {
  1407. /* error */
  1408. fct_snprintf(
  1409. clp->error_msg,
  1410. FCT_CLP_MAX_ERR_MSG_LEN,
  1411. "missing argument for %s",
  1412. token
  1413. );
  1414. clp->is_error =1;
  1415. break;
  1416. }
  1417. fctcl__set_value(pclo, argv[argi]);
  1418. }
  1419. }
  1420. else if (pclo->action == FCTCL_STORE_TRUE)
  1421. {
  1422. fctcl__set_value(pclo, FCTCL_TRUE_STR);
  1423. }
  1424. else
  1425. {
  1426. FCT_ASSERT("undefined action requested");
  1427. }
  1428. break; /* No need to parse this argument further. */
  1429. }
  1430. }
  1431. FCT_NLIST_FOREACH_END();
  1432. /* If we have an error, exit. */
  1433. if ( clp->is_error )
  1434. {
  1435. break;
  1436. }
  1437. /* If we walked through all the options, and didn't find
  1438. anything, then we must have a parameter. Forget the fact that
  1439. an unknown option will be treated like a parameter... */
  1440. if ( !is_option )
  1441. {
  1442. fct_nlist__append(&(clp->param_list), arg);
  1443. arg =NULL; /* Owned by the nlist */
  1444. }
  1445. ++argi;
  1446. if ( arg != NULL )
  1447. {
  1448. free(arg);
  1449. arg =NULL;
  1450. }
  1451. }
  1452. }
  1453. static fctcl_t const*
  1454. fct_clp__get_clo(fct_clp_t const *clp, char const *option)
  1455. {
  1456. fctcl_t const *found =NULL;
  1457. FCT_NLIST_FOREACH_BGN(fctcl_t const*, pclo, &(clp->clo_list))
  1458. {
  1459. if ( fctcl__is_option(pclo, option) )
  1460. {
  1461. found = pclo;
  1462. break;
  1463. }
  1464. }
  1465. FCT_NLIST_FOREACH_END();
  1466. return found;
  1467. }
  1468. #define fct_clp__optval(_CLP_, _OPTION_) \
  1469. fct_clp__optval2((_CLP_), (_OPTION_), NULL)
  1470. /* Returns the value parsed at the command line, and equal to OPTION.
  1471. If the value wasn't parsed, the DEFAULT_VAL is returned instead. */
  1472. static char const*
  1473. fct_clp__optval2(fct_clp_t *clp, char const *option, char const *default_val)
  1474. {
  1475. fctcl_t const *clo =NULL;
  1476. FCT_ASSERT( clp != NULL );
  1477. FCT_ASSERT( option != NULL );
  1478. clo = fct_clp__get_clo(clp, option);
  1479. if ( clo == NULL || clo->value == NULL)
  1480. {
  1481. return default_val;
  1482. }
  1483. return clo->value;
  1484. }
  1485. /* Mainly used for unit tests. */
  1486. static int
  1487. fct_clp__is_param(fct_clp_t *clp, char const *param)
  1488. {
  1489. if ( clp == NULL || param == NULL )
  1490. {
  1491. return 0;
  1492. }
  1493. FCT_NLIST_FOREACH_BGN(char *, aparam, &(clp->param_list))
  1494. {
  1495. if ( fctstr_eq(aparam, param) )
  1496. {
  1497. return 1;
  1498. }
  1499. }
  1500. FCT_NLIST_FOREACH_END();
  1501. return 0;
  1502. }
  1503. #define fct_clp__is_error(_CLP_) ((_CLP_)->is_error)
  1504. #define fct_clp__get_error(_CLP_) ((_CLP_)->error_msg);
  1505. #define fct_clp__num_clo(_CLP_) \
  1506. (fct_nlist__size(&((_CLP_)->clo_list)))
  1507. #define fct_clp__param_cnt(_CLP_) \
  1508. (fct_nlist__size(&((_CLP_)->param_list)))
  1509. /* Returns a *reference* to the parameter at _IDX_. Do not modify
  1510. its contents. */
  1511. #define fct_clp__param_at(_CLP_, _IDX_) \
  1512. ((char const*)fct_nlist__at(&((_CLP_)->param_list), (_IDX_)))
  1513. /* Returns true if the given option was on the command line.
  1514. Use either the long or short option name to check against. */
  1515. #define fct_clp__is(_CLP_, _OPTION_) \
  1516. (fct_clp__optval((_CLP_), (_OPTION_)) != NULL)
  1517. /*
  1518. --------------------------------------------------------
  1519. FCT NAMESPACE
  1520. --------------------------------------------------------
  1521. The macros below start to pollute the watch window with
  1522. lots of "system" variables. This NAMESPACE is an
  1523. attempt to hide all the "system" variables in one place.
  1524. */
  1525. typedef struct _fct_namespace_t
  1526. {
  1527. /* The currently active test suite. */
  1528. fct_ts_t *ts_curr;
  1529. int ts_is_skip_suite;
  1530. char const *ts_skip_cndtn;
  1531. /* Current test name. */
  1532. char const* curr_test_name;
  1533. fct_test_t *curr_test;
  1534. const char *test_skip_cndtn;
  1535. int test_is_skip;
  1536. /* Counts the number of tests in a test suite. */
  1537. int test_num;
  1538. /* Set at the end of the test suites. */
  1539. size_t num_total_failed;
  1540. } fct_namespace_t;
  1541. static void
  1542. fct_namespace_init(fct_namespace_t *ns)
  1543. {
  1544. FCT_ASSERT( ns != NULL && "invalid argument!");
  1545. memset(ns, 0, sizeof(fct_namespace_t));
  1546. }
  1547. /*
  1548. --------------------------------------------------------
  1549. FCT KERNAL
  1550. --------------------------------------------------------
  1551. The "fctkern" is a singleton that is defined throughout the
  1552. system.
  1553. */
  1554. struct _fctkern_t
  1555. {
  1556. /* Holds variables used throughout MACRO MAGIC. In order to reduce
  1557. the "noise" in the watch window during a debug trace. */
  1558. fct_namespace_t ns;
  1559. /* Command line parsing. */
  1560. fct_clp_t cl_parser;
  1561. /* Hold onto the command line arguments. */
  1562. int cl_argc;
  1563. char const **cl_argv;
  1564. /* Track user options. */
  1565. fctcl_init_t const *cl_user_opts;
  1566. /* Tracks the delay parsing. */
  1567. int cl_is_parsed;
  1568. /* This is an list of loggers that can be used in the fct system. */
  1569. fct_nlist_t logger_list;
  1570. /* Array of custom types, you have built-in system ones and you
  1571. have optionally supplied user ones.. */
  1572. fct_logger_types_t *lt_usr;
  1573. fct_logger_types_t *lt_sys;
  1574. /* This is a list of prefix's that can be used to determine if a
  1575. test is should be run or not. */
  1576. fct_nlist_t prefix_list;
  1577. /* This is a list of test suites that where generated throughout the
  1578. testing process. */
  1579. fct_nlist_t ts_list;
  1580. /* Records what we expect to fail. */
  1581. size_t num_expected_failures;
  1582. };
  1583. #define FCT_OPT_VERSION "--version"
  1584. #define FCT_OPT_VERSION_SHORT "-v"
  1585. #define FCT_OPT_HELP "--help"
  1586. #define FCT_OPT_HELP_SHORT "-h"
  1587. #define FCT_OPT_LOGGER "--logger"
  1588. #define FCT_OPT_LOGGER_SHORT "-l"
  1589. static fctcl_init_t FCT_CLP_OPTIONS[] =
  1590. {
  1591. /* Totally unsafe, since we are assuming we can clean out this data,
  1592. what I need to do is have an "initialization" object, full of
  1593. const objects. But for now, this should work. */
  1594. {
  1595. FCT_OPT_VERSION,
  1596. FCT_OPT_VERSION_SHORT,
  1597. FCTCL_STORE_TRUE,
  1598. "Displays the FCTX version number and exits."
  1599. },
  1600. {
  1601. FCT_OPT_HELP,
  1602. FCT_OPT_HELP_SHORT,
  1603. FCTCL_STORE_TRUE,
  1604. "Shows this help."
  1605. },
  1606. {
  1607. FCT_OPT_LOGGER,
  1608. FCT_OPT_LOGGER_SHORT,
  1609. FCTCL_STORE_VALUE,
  1610. NULL
  1611. },
  1612. FCTCL_INIT_NULL /* Sentinel */
  1613. };
  1614. typedef fct_logger_i* (*fct_logger_new_fn)(void);
  1615. struct _fct_logger_types_t
  1616. {
  1617. char const *name;
  1618. fct_logger_new_fn logger_new_fn;
  1619. char const *desc;
  1620. };
  1621. static fct_logger_types_t FCT_LOGGER_TYPES[] =
  1622. {
  1623. {
  1624. "standard",
  1625. (fct_logger_new_fn)fct_standard_logger_new,
  1626. "the basic fctx logger"
  1627. },
  1628. {
  1629. "minimal",
  1630. (fct_logger_new_fn)fct_minimal_logger_new,
  1631. "the least amount of logging information."
  1632. },
  1633. {
  1634. "junit",
  1635. (fct_logger_new_fn)fct_junit_logger_new,
  1636. "junit compatable xml"
  1637. },
  1638. {NULL, (fct_logger_new_fn)NULL, NULL} /* Sentinel */
  1639. };
  1640. /* Returns the number of filters defined for the fct kernal. */
  1641. #define fctkern__filter_cnt(_NK_) (fct_nlist__size(&((_NK_)->prefix_list)))
  1642. static void
  1643. fctkern__add_logger(fctkern_t *nk, fct_logger_i *logger_owns)
  1644. {
  1645. FCT_ASSERT(nk != NULL && "invalid arg");
  1646. FCT_ASSERT(logger_owns != NULL && "invalid arg");
  1647. fct_nlist__append(&(nk->logger_list), logger_owns);
  1648. }
  1649. static void
  1650. fctkern__write_help(fctkern_t *nk, FILE *out)
  1651. {
  1652. fct_clp_t *clp = &(nk->cl_parser);
  1653. fprintf(out, "test.exe [options] prefix_filter ...\n\n");
  1654. FCT_NLIST_FOREACH_BGN(fctcl_t*, clo, &(clp->clo_list))
  1655. {
  1656. if ( clo->short_opt != NULL )
  1657. {
  1658. fprintf(out, "%s, %s\n", clo->short_opt, clo->long_opt);
  1659. }
  1660. else
  1661. {
  1662. fprintf(out, "%s\n", clo->long_opt);
  1663. }
  1664. if ( !fctstr_ieq(clo->long_opt, FCT_OPT_LOGGER) )
  1665. {
  1666. /* For now lets not get to fancy with the text wrapping. */
  1667. fprintf(out, " %s\n", clo->help);
  1668. }
  1669. else
  1670. {
  1671. fct_logger_types_t *types[2];
  1672. int type_i;
  1673. fct_logger_types_t *itr;
  1674. types[0] = nk->lt_sys;
  1675. types[1] = nk->lt_usr;
  1676. fputs(" Sets the logger. The types of loggers currently "
  1677. "available are,\n", out);
  1678. for (type_i =0; type_i != 2; ++type_i )
  1679. {
  1680. for ( itr=types[type_i]; itr && itr->name != NULL; ++itr )
  1681. {
  1682. fprintf(out, " =%s : %s\n", itr->name, itr->desc);
  1683. }
  1684. }
  1685. fprintf(out, " default is '%s'.\n", FCT_DEFAULT_LOGGER);
  1686. }
  1687. }
  1688. FCT_NLIST_FOREACH_END();
  1689. fputs("\n", out);
  1690. }
  1691. /* Appends a prefix filter that is used to determine if a test can
  1692. be executed or not. If the test starts with the same characters as
  1693. the prefix, then it should be "runnable". The prefix filter must be
  1694. a non-NULL, non-Blank string. */
  1695. static void
  1696. fctkern__add_prefix_filter(fctkern_t *nk, char const *prefix_filter)
  1697. {
  1698. char *filter =NULL;
  1699. size_t filter_len =0;
  1700. FCT_ASSERT( nk != NULL && "invalid arg" );
  1701. FCT_ASSERT( prefix_filter != NULL && "invalid arg" );
  1702. FCT_ASSERT( strlen(prefix_filter) > 0 && "invalid arg" );
  1703. /* First we make a copy of the prefix, then we store it away
  1704. in our little list. */
  1705. filter_len = strlen(prefix_filter);
  1706. filter = (char*)malloc(sizeof(char)*(filter_len+1));
  1707. fctstr_safe_cpy(filter, prefix_filter, filter_len+1);
  1708. fct_nlist__append(&(nk->prefix_list), (void*)filter);
  1709. }
  1710. /* Cleans up the contents of a fctkern. NULL does nothing. */
  1711. static void
  1712. fctkern__final(fctkern_t *nk)
  1713. {
  1714. if ( nk == NULL )
  1715. {
  1716. return;
  1717. }
  1718. fct_clp__final(&(nk->cl_parser));
  1719. fct_nlist__final(&(nk->logger_list), (fct_nlist_on_del_t)fct_logger__del);
  1720. /* The prefix list is a list of malloc'd strings. */
  1721. fct_nlist__final(&(nk->prefix_list), (fct_nlist_on_del_t)free);
  1722. fct_nlist__final(&(nk->ts_list), (fct_nlist_on_del_t)fct_ts__del);
  1723. }
  1724. #define fctkern__cl_is_parsed(_NK_) ((_NK_)->cl_is_parsed)
  1725. static int
  1726. fctkern__cl_is(fctkern_t *nk, char const *opt_str)
  1727. {
  1728. FCT_ASSERT( opt_str != NULL );
  1729. return opt_str[0] != '\0'
  1730. && fct_clp__is(&(nk->cl_parser), opt_str);
  1731. }
  1732. /* Returns the command line value given by OPT_STR. If OPT_STR was not defined
  1733. at the command line, DEF_STR is returned (you can use NULL for the DEF_STR).
  1734. The result returned should not be mofidied, and MAY even be the same pointer
  1735. to DEF_STR. */
  1736. static char const *
  1737. fctkern__cl_val2(fctkern_t *nk, char const *opt_str, char const *def_str)
  1738. {
  1739. FCT_ASSERT( opt_str != NULL );
  1740. if ( nk == NULL )
  1741. {
  1742. return NULL;
  1743. }
  1744. return fct_clp__optval2(&(nk->cl_parser), opt_str, def_str);
  1745. }
  1746. /* Selects a logger from the list based on the selection name.
  1747. May return NULL if the name doesn't exist in the list. */
  1748. static fct_logger_i*
  1749. fckern_sel_log(fct_logger_types_t *search, char const *sel_logger)
  1750. {
  1751. fct_logger_types_t *iter;
  1752. FCT_ASSERT(search != NULL);
  1753. FCT_ASSERT(sel_logger != NULL);
  1754. FCT_ASSERT(strlen(sel_logger) > 0);
  1755. for ( iter = search; iter->name != NULL; ++iter)
  1756. {
  1757. if ( fctstr_ieq(iter->name, sel_logger) )
  1758. {
  1759. return iter->logger_new_fn();
  1760. }
  1761. }
  1762. return NULL;
  1763. }
  1764. static int
  1765. fctkern__cl_parse_config_logger(fctkern_t *nk)
  1766. {
  1767. fct_logger_i *logger =NULL;
  1768. char const *sel_logger =NULL;
  1769. char const *def_logger =FCT_DEFAULT_LOGGER;
  1770. sel_logger = fctkern__cl_val2(nk, FCT_OPT_LOGGER, def_logger);
  1771. FCT_ASSERT(sel_logger != NULL && "should never be NULL");
  1772. /* First search the user selected types, then search the
  1773. built-in types. */
  1774. if ( nk->lt_usr != NULL )
  1775. {
  1776. logger = fckern_sel_log(nk->lt_usr, sel_logger);
  1777. }
  1778. if ( nk->lt_sys != NULL && logger == NULL )
  1779. {
  1780. logger = fckern_sel_log(nk->lt_sys, sel_logger);
  1781. }
  1782. if ( logger == NULL )
  1783. {
  1784. /* No logger configured, you must have supplied an invalid selection. */
  1785. fprintf(stderr, "error: unknown logger selected - '%s'", sel_logger);
  1786. return 0;
  1787. }
  1788. fctkern__add_logger(nk, logger);
  1789. logger = NULL; /* owned by nk. */
  1790. return 1;
  1791. }
  1792. /* Call this if you want to (re)parse the command line options with a new
  1793. set of options. Returns -1 if you are to abort with EXIT_SUCCESS, returns
  1794. 0 if you are to abort with EXIT_FAILURE and returns 1 if you are to continue. */
  1795. static int
  1796. fctkern__cl_parse(fctkern_t *nk)
  1797. {
  1798. int status =0;
  1799. size_t num_params =0;
  1800. size_t param_i =0;
  1801. if ( nk == NULL )
  1802. {
  1803. return 0;
  1804. }
  1805. if ( nk->cl_user_opts != NULL )
  1806. {
  1807. if ( !fct_clp__add_options(&(nk->cl_parser), nk->cl_user_opts) )
  1808. {
  1809. status =0;
  1810. goto finally;
  1811. }
  1812. }
  1813. /* You want to add the "house options" after the user defined ones. The
  1814. options are stored as a list so it means that any option listed after
  1815. the above ones won't get parsed. */
  1816. if ( !fct_clp__add_options(&(nk->cl_parser), FCT_CLP_OPTIONS) )
  1817. {
  1818. status =0;
  1819. goto finally;
  1820. }
  1821. fct_clp__parse(&(nk->cl_parser), nk->cl_argc, nk->cl_argv);
  1822. if ( fct_clp__is_error(&(nk->cl_parser)) )
  1823. {
  1824. char *err = fct_clp__get_error(&(nk->cl_parser));
  1825. fprintf(stderr, "error: %s", err);
  1826. status =0;
  1827. goto finally;
  1828. }
  1829. num_params = fct_clp__param_cnt(&(nk->cl_parser));
  1830. for ( param_i =0; param_i != num_params; ++param_i )
  1831. {
  1832. char const *param = fct_clp__param_at(&(nk->cl_parser), param_i);
  1833. fctkern__add_prefix_filter(nk, param);
  1834. }
  1835. if ( fctkern__cl_is(nk, FCT_OPT_VERSION) )
  1836. {
  1837. (void)printf("Built using FCTX version %s.\n", FCT_VERSION_STR);
  1838. status = -1;
  1839. goto finally;
  1840. }
  1841. if ( fctkern__cl_is(nk, FCT_OPT_HELP) )
  1842. {
  1843. fctkern__write_help(nk, stdout);
  1844. status = -1;
  1845. goto finally;
  1846. }
  1847. if ( !fctkern__cl_parse_config_logger(nk) )
  1848. {
  1849. status = -1;
  1850. goto finally;
  1851. }
  1852. status =1;
  1853. nk->cl_is_parsed =1;
  1854. finally:
  1855. return status;
  1856. }
  1857. /* Parses the command line and sets up the framework. The argc and argv
  1858. should be directly from the program's main. */
  1859. static int
  1860. fctkern__init(fctkern_t *nk, int argc, const char *argv[])
  1861. {
  1862. if ( argc == 0 && argv == NULL )
  1863. {
  1864. return 0;
  1865. }
  1866. memset(nk, 0, sizeof(fctkern_t));
  1867. fct_clp__init(&(nk->cl_parser), NULL);
  1868. fct_nlist__init(&(nk->logger_list));
  1869. nk->lt_usr = NULL; /* Supplied via 'install' mechanics. */
  1870. nk->lt_sys = FCT_LOGGER_TYPES;
  1871. fct_nlist__init2(&(nk->prefix_list), 0);
  1872. fct_nlist__init2(&(nk->ts_list), 0);
  1873. nk->cl_is_parsed =0;
  1874. /* Save a copy of the arguments. We do a delay parse of the command
  1875. line arguments in order to allow the client code to optionally configure
  1876. the command line parser.*/
  1877. nk->cl_argc = argc;
  1878. nk->cl_argv = argv;
  1879. fct_namespace_init(&(nk->ns));
  1880. return 1;
  1881. }
  1882. /* Takes OWNERSHIP of the test suite after we have finished executing
  1883. its contents. This way we can build up all kinds of summaries at the end
  1884. of a run. */
  1885. static void
  1886. fctkern__add_ts(fctkern_t *nk, fct_ts_t *ts)
  1887. {
  1888. FCT_ASSERT( nk != NULL );
  1889. FCT_ASSERT( ts != NULL );
  1890. fct_nlist__append(&(nk->ts_list), ts);
  1891. }
  1892. /* Returns FCT_TRUE if the supplied test_name passes the filters set on
  1893. this test suite. If there are no filters, we return FCT_TRUE always. */
  1894. static nbool_t
  1895. fctkern__pass_filter(fctkern_t *nk, char const *test_name)
  1896. {
  1897. size_t prefix_i =0;
  1898. size_t prefix_list_size =0;
  1899. FCT_ASSERT( nk != NULL && "invalid arg");
  1900. FCT_ASSERT( test_name != NULL );
  1901. FCT_ASSERT( strlen(test_name) > 0 );
  1902. prefix_list_size = fctkern__filter_cnt(nk);
  1903. /* If there is no filter list, then we return FCT_TRUE always. */
  1904. if ( prefix_list_size == 0 )
  1905. {
  1906. return FCT_TRUE;
  1907. }
  1908. /* Iterate through the prefix filter list, and see if we have
  1909. anything that does not pass. All we require is ONE item that
  1910. passes the test in order for us to succeed here. */
  1911. for ( prefix_i = 0; prefix_i != prefix_list_size; ++prefix_i )
  1912. {
  1913. char const *prefix = (char const*)fct_nlist__at(
  1914. &(nk->prefix_list), prefix_i
  1915. );
  1916. nbool_t pass = fct_filter_pass(prefix, test_name);
  1917. if ( pass )
  1918. {
  1919. return FCT_TRUE;
  1920. }
  1921. }
  1922. /* Otherwise, we never managed to find a prefix that satisfied the
  1923. supplied test name. Therefore we have failed to pass to the filter
  1924. list test. */
  1925. return FCT_FALSE;
  1926. }
  1927. /* Returns the number of tests that were performed. */
  1928. static size_t
  1929. fctkern__tst_cnt(fctkern_t const *nk)
  1930. {
  1931. size_t tally =0;
  1932. FCT_ASSERT( nk != NULL );
  1933. FCT_NLIST_FOREACH_BGN(fct_ts_t *, ts, &(nk->ts_list))
  1934. {
  1935. tally += fct_ts__tst_cnt(ts);
  1936. }
  1937. FCT_NLIST_FOREACH_END();
  1938. return tally;
  1939. }
  1940. /* Returns the number of tests that passed. */
  1941. static size_t
  1942. fctkern__tst_cnt_passed(fctkern_t const *nk)
  1943. {
  1944. size_t tally =0;
  1945. FCT_ASSERT( nk != NULL );
  1946. FCT_NLIST_FOREACH_BGN(fct_ts_t*, ts, &(nk->ts_list))
  1947. {
  1948. tally += fct_ts__tst_cnt_passed(ts);
  1949. }
  1950. FCT_NLIST_FOREACH_END();
  1951. return tally;
  1952. }
  1953. /* Returns the number of tests that failed. */
  1954. #define fctkern__tst_cnt_failed(nk) \
  1955. (fctkern__tst_cnt(nk) - fctkern__tst_cnt_passed(nk))
  1956. /* Returns the number of checks made throughout the entire test. */
  1957. #if defined(FCT_USE_TEST_COUNT)
  1958. static size_t
  1959. fctkern__chk_cnt(fctkern_t const *nk)
  1960. {
  1961. size_t tally =0;
  1962. FCT_ASSERT( nk != NULL );
  1963. FCT_NLIST_FOREACH_BGN(fct_ts_t *, ts, &(nk->ts_list))
  1964. {
  1965. tally += fct_ts__chk_cnt(ts);
  1966. }
  1967. FCT_NLIST_FOREACH_END();
  1968. return tally;
  1969. }
  1970. #endif /* FCT_USE_TEST_COUNT */
  1971. /* Indicates the very end of all the tests. */
  1972. #define fctkern__end(nk) /* unused */
  1973. static void
  1974. fctkern__log_suite_start(fctkern_t *nk, fct_ts_t const *ts)
  1975. {
  1976. FCT_ASSERT( nk != NULL );
  1977. FCT_ASSERT( ts != NULL );
  1978. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &(nk->logger_list))
  1979. {
  1980. fct_logger__on_test_suite_start(logger, ts);
  1981. }
  1982. FCT_NLIST_FOREACH_END();
  1983. }
  1984. static void
  1985. fctkern__log_suite_end(fctkern_t *nk, fct_ts_t const *ts)
  1986. {
  1987. FCT_ASSERT( nk != NULL );
  1988. FCT_ASSERT( ts != NULL );
  1989. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &(nk->logger_list))
  1990. {
  1991. fct_logger__on_test_suite_end(logger, ts);
  1992. }
  1993. FCT_NLIST_FOREACH_END();
  1994. }
  1995. static void
  1996. fctkern__log_suite_skip(fctkern_t *nk, char const *condition, char const *name)
  1997. {
  1998. if ( nk == NULL )
  1999. {
  2000. return;
  2001. }
  2002. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &(nk->logger_list))
  2003. {
  2004. fct_logger__on_test_suite_skip(logger, condition, name);
  2005. }
  2006. FCT_NLIST_FOREACH_END();
  2007. }
  2008. static void
  2009. fctkern__log_test_skip(fctkern_t *nk, char const *condition, char const *name)
  2010. {
  2011. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &(nk->logger_list))
  2012. {
  2013. fct_logger__on_test_skip(logger, condition, name);
  2014. }
  2015. FCT_NLIST_FOREACH_END();
  2016. }
  2017. /* Use this for displaying information about a "Check" (i.e.
  2018. a condition). */
  2019. static void
  2020. fctkern__log_chk(fctkern_t *nk, fctchk_t const *chk)
  2021. {
  2022. FCT_ASSERT( nk != NULL );
  2023. FCT_ASSERT( chk != NULL );
  2024. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &(nk->logger_list))
  2025. {
  2026. fct_logger__on_chk(logger, chk);
  2027. }
  2028. FCT_NLIST_FOREACH_END();
  2029. }
  2030. /* Use this for displaying warning messages. */
  2031. static void
  2032. fctkern__log_warn(fctkern_t *nk, char const *warn)
  2033. {
  2034. FCT_ASSERT( nk != NULL );
  2035. FCT_ASSERT( warn != NULL );
  2036. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &(nk->logger_list))
  2037. {
  2038. fct_logger__on_warn(logger, warn);
  2039. }
  2040. FCT_NLIST_FOREACH_END();
  2041. }
  2042. /* Called whenever a test is started. */
  2043. static void
  2044. fctkern__log_test_start(fctkern_t *nk, fct_test_t const *test)
  2045. {
  2046. FCT_ASSERT( nk != NULL );
  2047. FCT_ASSERT( test != NULL );
  2048. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &(nk->logger_list))
  2049. {
  2050. fct_logger__on_test_start(logger, test);
  2051. }
  2052. FCT_NLIST_FOREACH_END();
  2053. }
  2054. static void
  2055. fctkern__log_test_end(fctkern_t *nk, fct_test_t *test)
  2056. {
  2057. FCT_ASSERT( nk != NULL );
  2058. FCT_ASSERT( test != NULL );
  2059. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &(nk->logger_list))
  2060. {
  2061. fct_logger__on_test_end(logger, test);
  2062. }
  2063. FCT_NLIST_FOREACH_END();
  2064. }
  2065. #define fctkern__log_start(_NK_) \
  2066. {\
  2067. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &((_NK_)->logger_list))\
  2068. {\
  2069. fct_logger__on_fctx_start(logger, (_NK_));\
  2070. }\
  2071. FCT_NLIST_FOREACH_END();\
  2072. }
  2073. #define fctkern__log_end(_NK_) \
  2074. {\
  2075. FCT_NLIST_FOREACH_BGN(fct_logger_i*, logger, &((_NK_)->logger_list))\
  2076. {\
  2077. fct_logger__on_fctx_end(logger, (_NK_));\
  2078. }\
  2079. FCT_NLIST_FOREACH_END();\
  2080. }
  2081. /*
  2082. -----------------------------------------------------------
  2083. LOGGER INTERFACE
  2084. Defines an interface to a logging system. A logger
  2085. must define the following functions in order to hook
  2086. into the logging system.
  2087. See the "Standard Logger" and "Minimal Logger" as examples
  2088. of the implementation.
  2089. -----------------------------------------------------------
  2090. */
  2091. /* Common event argument. The values of the each event may or may not be
  2092. defined depending on the event in question. */
  2093. struct _fct_logger_evt_t
  2094. {
  2095. fctkern_t const *kern;
  2096. fctchk_t const *chk;
  2097. fct_test_t const *test;
  2098. fct_ts_t const *ts;
  2099. char const *msg;
  2100. char const *cndtn;
  2101. char const *name;
  2102. };
  2103. typedef struct _fct_logger_i_vtable_t
  2104. {
  2105. /* 1
  2106. * Fired when an "fct_chk*" (check) function is completed. The event
  2107. * will contain a reference to the "chk" object created.
  2108. * */
  2109. void (*on_chk)(fct_logger_i *logger, fct_logger_evt_t const *e);
  2110. /* 2
  2111. * Fired when a test starts and before any checks are made. The
  2112. * event will have its "test" object set. */
  2113. void (*on_test_start)(
  2114. fct_logger_i *logger,
  2115. fct_logger_evt_t const *e
  2116. );
  2117. /* 3 */
  2118. void (*on_test_end)(
  2119. fct_logger_i *logger,
  2120. fct_logger_evt_t const *e
  2121. );
  2122. /* 4 */
  2123. void (*on_test_suite_start)(
  2124. fct_logger_i *logger,
  2125. fct_logger_evt_t const *e
  2126. );
  2127. /* 5 */
  2128. void (*on_test_suite_end)(
  2129. fct_logger_i *logger,
  2130. fct_logger_evt_t const *e
  2131. );
  2132. /* 6 */
  2133. void (*on_fctx_start)(
  2134. fct_logger_i *logger,
  2135. fct_logger_evt_t const *e
  2136. );
  2137. /* 7 */
  2138. void (*on_fctx_end)(
  2139. fct_logger_i *logger,
  2140. fct_logger_evt_t const *e
  2141. );
  2142. /* 8
  2143. Called when the logger object must "clean up". */
  2144. void (*on_delete)(
  2145. fct_logger_i *logger,
  2146. fct_logger_evt_t const *e
  2147. );
  2148. /* 9 */
  2149. void (*on_warn)(
  2150. fct_logger_i *logger,
  2151. fct_logger_evt_t const *e
  2152. );
  2153. /* -- new in 1.2 -- */
  2154. /* 10 */
  2155. void (*on_test_suite_skip)(
  2156. fct_logger_i *logger,
  2157. fct_logger_evt_t const *e
  2158. );
  2159. /* 11 */
  2160. void (*on_test_skip)(
  2161. fct_logger_i *logger,
  2162. fct_logger_evt_t const *e
  2163. );
  2164. } fct_logger_i_vtable_t;
  2165. #define _fct_logger_head \
  2166. fct_logger_i_vtable_t vtable; \
  2167. fct_logger_evt_t evt
  2168. struct _fct_logger_i
  2169. {
  2170. _fct_logger_head;
  2171. };
  2172. static void
  2173. fct_logger__stub(fct_logger_i *l, fct_logger_evt_t const *e)
  2174. {
  2175. fct_unused(l);
  2176. fct_unused(e);
  2177. }
  2178. static fct_logger_i_vtable_t fct_logger_default_vtable =
  2179. {
  2180. fct_logger__stub, /* 1. on_chk */
  2181. fct_logger__stub, /* 2. on_test_start */
  2182. fct_logger__stub, /* 3. on_test_end */
  2183. fct_logger__stub, /* 4. on_test_suite_start */
  2184. fct_logger__stub, /* 5. on_test_suite_end */
  2185. fct_logger__stub, /* 6. on_fctx_start */
  2186. fct_logger__stub, /* 7. on_fctx_end */
  2187. fct_logger__stub, /* 8. on_delete */
  2188. fct_logger__stub, /* 9. on_warn */
  2189. fct_logger__stub, /* 10. on_test_suite_skip */
  2190. fct_logger__stub, /* 11. on_test_skip */
  2191. };
  2192. /* Initializes the elements of a logger interface so they are at their
  2193. standard values. */
  2194. static void
  2195. fct_logger__init(fct_logger_i *logger)
  2196. {
  2197. FCT_ASSERT( logger != NULL );
  2198. memcpy(
  2199. &(logger->vtable),
  2200. &fct_logger_default_vtable,
  2201. sizeof(fct_logger_i_vtable_t)
  2202. );
  2203. memset(&(logger->evt),0, sizeof(fct_logger_evt_t));
  2204. }
  2205. static void
  2206. fct_logger__del(fct_logger_i *logger)
  2207. {
  2208. if ( logger )
  2209. {
  2210. logger->vtable.on_delete(logger, &(logger->evt));
  2211. }
  2212. }
  2213. static void
  2214. fct_logger__on_test_start(fct_logger_i *logger, fct_test_t const *test)
  2215. {
  2216. logger->evt.test = test;
  2217. logger->vtable.on_test_start(logger, &(logger->evt));
  2218. }
  2219. static void
  2220. fct_logger__on_test_end(fct_logger_i *logger, fct_test_t *test)
  2221. {
  2222. logger->evt.test = test;
  2223. logger->vtable.on_test_end(logger, &(logger->evt));
  2224. }
  2225. static void
  2226. fct_logger__on_test_suite_start(fct_logger_i *logger, fct_ts_t const *ts)
  2227. {
  2228. logger->evt.ts = ts;
  2229. logger->vtable.on_test_suite_start(logger, &(logger->evt));
  2230. }
  2231. static void
  2232. fct_logger__on_test_suite_end(fct_logger_i *logger, fct_ts_t const *ts)
  2233. {
  2234. logger->evt.ts = ts;
  2235. logger->vtable.on_test_suite_end(logger, &(logger->evt));
  2236. }
  2237. static void
  2238. fct_logger__on_test_suite_skip(
  2239. fct_logger_i *logger,
  2240. char const *condition,
  2241. char const *name
  2242. )
  2243. {
  2244. logger->evt.cndtn = condition;
  2245. logger->evt.name = name;
  2246. logger->vtable.on_test_suite_skip(logger, &(logger->evt));
  2247. }
  2248. static void
  2249. fct_logger__on_test_skip(
  2250. fct_logger_i *logger,
  2251. char const *condition,
  2252. char const *name
  2253. )
  2254. {
  2255. logger->evt.cndtn = condition;
  2256. logger->evt.name = name;
  2257. logger->vtable.on_test_skip(logger, &(logger->evt));
  2258. }
  2259. static void
  2260. fct_logger__on_chk(fct_logger_i *logger, fctchk_t const *chk)
  2261. {
  2262. logger->evt.chk = chk;
  2263. logger->vtable.on_chk(logger, &(logger->evt));
  2264. }
  2265. /* When we start all our tests. */
  2266. #define fct_logger__on_fctx_start(LOGGER, KERN) \
  2267. (LOGGER)->evt.kern = (KERN);\
  2268. (LOGGER)->vtable.on_fctx_start((LOGGER), &((LOGGER)->evt));
  2269. /* When we have reached the end of ALL of our testing. */
  2270. #define fct_logger__on_fctx_end(LOGGER, KERN) \
  2271. (LOGGER)->evt.kern = (KERN);\
  2272. (LOGGER)->vtable.on_fctx_end((LOGGER), &((LOGGER)->evt));
  2273. static void
  2274. fct_logger__on_warn(fct_logger_i *logger, char const *msg)
  2275. {
  2276. logger->evt.msg = msg;
  2277. logger->vtable.on_warn(logger, &(logger->evt));
  2278. }
  2279. /* Commmon routine to record strings representing failures. The
  2280. chk should be a failure before we call this, and the list is a list
  2281. of char*'s that will eventually be free'd by the logger. */
  2282. static void
  2283. fct_logger_record_failure(fctchk_t const* chk, fct_nlist_t* fail_list)
  2284. {
  2285. /* For now we will truncate the string to some set amount, later
  2286. we can work out a dynamic string object. */
  2287. char *str = (char*)malloc(sizeof(char)*FCT_MAX_LOG_LINE);
  2288. FCT_ASSERT( str != NULL );
  2289. fct_snprintf(
  2290. str,
  2291. FCT_MAX_LOG_LINE,
  2292. "%s(%d):\n %s",
  2293. fctchk__file(chk),
  2294. fctchk__lineno(chk),
  2295. fctchk__msg(chk)
  2296. );
  2297. /* Append it to the listing ... */
  2298. fct_nlist__append(fail_list, (void*)str);
  2299. }
  2300. /* Another common routine, to print the failures at the end of a run. */
  2301. static void
  2302. fct_logger_print_failures(fct_nlist_t const *fail_list)
  2303. {
  2304. puts(
  2305. "\n----------------------------------------------------------------------------\n"
  2306. );
  2307. puts("FAILED TESTS\n\n");
  2308. FCT_NLIST_FOREACH_BGN(char *, cndtn_str, fail_list)
  2309. {
  2310. printf("%s\n", cndtn_str);
  2311. }
  2312. FCT_NLIST_FOREACH_END();
  2313. puts("\n");
  2314. }
  2315. /*
  2316. -----------------------------------------------------------
  2317. MINIMAL LOGGER
  2318. -----------------------------------------------------------
  2319. At the moment the MINIMAL LOGGER is currently disabled. Hope
  2320. to bring it back online soon. The only reason it is
  2321. disabled is that we don't currently have the ability to specify
  2322. loggers.
  2323. */
  2324. /* Minimal logger, reports the minimum amount of information needed
  2325. to determine "something is happening". */
  2326. struct _fct_minimal_logger_t
  2327. {
  2328. _fct_logger_head;
  2329. /* A list of char*'s that needs to be cleaned up. */
  2330. fct_nlist_t failed_cndtns_list;
  2331. };
  2332. static void
  2333. fct_minimal_logger__on_chk(
  2334. fct_logger_i *self_,
  2335. fct_logger_evt_t const *e
  2336. )
  2337. {
  2338. fct_minimal_logger_t *self = (fct_minimal_logger_t*)self_;
  2339. if ( fctchk__is_pass(e->chk) )
  2340. {
  2341. fputs(".", stdout);
  2342. }
  2343. else
  2344. {
  2345. fputs("x", stdout);
  2346. fct_logger_record_failure(e->chk, &(self->failed_cndtns_list));
  2347. }
  2348. }
  2349. static void
  2350. fct_minimal_logger__on_fctx_end(
  2351. fct_logger_i *self_,
  2352. fct_logger_evt_t const *e
  2353. )
  2354. {
  2355. fct_minimal_logger_t *self = (fct_minimal_logger_t*)self_;
  2356. fct_unused(e);
  2357. if ( fct_nlist__size(&(self->failed_cndtns_list)) >0 )
  2358. {
  2359. fct_logger_print_failures(&(self->failed_cndtns_list));
  2360. }
  2361. }
  2362. static void
  2363. fct_minimal_logger__on_delete(
  2364. fct_logger_i *self_,
  2365. fct_logger_evt_t const *e
  2366. )
  2367. {
  2368. fct_minimal_logger_t *self = (fct_minimal_logger_t*)self_;
  2369. fct_unused(e);
  2370. fct_nlist__final(&(self->failed_cndtns_list), free);
  2371. free(self);
  2372. }
  2373. fct_logger_i*
  2374. fct_minimal_logger_new(void)
  2375. {
  2376. fct_minimal_logger_t *self = (fct_minimal_logger_t*)\
  2377. calloc(1,sizeof(fct_minimal_logger_t));
  2378. if ( self == NULL )
  2379. {
  2380. return NULL;
  2381. }
  2382. fct_logger__init((fct_logger_i*)self);
  2383. self->vtable.on_chk = fct_minimal_logger__on_chk;
  2384. self->vtable.on_fctx_end = fct_minimal_logger__on_fctx_end;
  2385. self->vtable.on_delete = fct_minimal_logger__on_delete;
  2386. fct_nlist__init2(&(self->failed_cndtns_list), 0);
  2387. return (fct_logger_i*)self;
  2388. }
  2389. /*
  2390. -----------------------------------------------------------
  2391. STANDARD LOGGER
  2392. -----------------------------------------------------------
  2393. */
  2394. struct _fct_standard_logger_t
  2395. {
  2396. _fct_logger_head;
  2397. /* Start time. For now we use the low-accuracy time_t version. */
  2398. fct_timer_t timer;
  2399. /* A list of char*'s that needs to be cleaned up. */
  2400. fct_nlist_t failed_cndtns_list;
  2401. };
  2402. #define FCT_STANDARD_LOGGER_MAX_LINE 68
  2403. /* When a failure occurrs, we will record the details so we can display
  2404. them when the log "finishes" up. */
  2405. static void
  2406. fct_standard_logger__on_chk(
  2407. fct_logger_i *logger_,
  2408. fct_logger_evt_t const *e
  2409. )
  2410. {
  2411. fct_standard_logger_t *logger = (fct_standard_logger_t*)logger_;
  2412. /* Only record failures. */
  2413. if ( !fctchk__is_pass(e->chk) )
  2414. {
  2415. fct_logger_record_failure(e->chk, &(logger->failed_cndtns_list));
  2416. }
  2417. }
  2418. static void
  2419. fct_standard_logger__on_test_skip(
  2420. fct_logger_i* logger_,
  2421. fct_logger_evt_t const *e
  2422. )
  2423. {
  2424. char const *condition = e->cndtn;
  2425. char const *name = e->name;
  2426. char msg[256] = {'\0'};
  2427. fct_unused(logger_);
  2428. fct_unused(condition);
  2429. fct_snprintf(msg, sizeof(msg), "%s (%s)", name, condition);
  2430. msg[sizeof(msg)-1] = '\0';
  2431. fct_dotted_line_start(FCT_STANDARD_LOGGER_MAX_LINE, msg);
  2432. fct_dotted_line_end("- SKIP -");
  2433. }
  2434. static void
  2435. fct_standard_logger__on_test_start(
  2436. fct_logger_i *logger_,
  2437. fct_logger_evt_t const *e
  2438. )
  2439. {
  2440. fct_unused(logger_);
  2441. fct_dotted_line_start(
  2442. FCT_STANDARD_LOGGER_MAX_LINE,
  2443. fct_test__name(e->test)
  2444. );
  2445. }
  2446. static void
  2447. fct_standard_logger__on_test_end(
  2448. fct_logger_i *logger_,
  2449. fct_logger_evt_t const *e
  2450. )
  2451. {
  2452. nbool_t is_pass;
  2453. fct_unused(logger_);
  2454. is_pass = fct_test__is_pass(e->test);
  2455. fct_dotted_line_end((is_pass) ? "PASS" : "FAIL ***" );
  2456. }
  2457. static void
  2458. fct_standard_logger__on_fctx_start(
  2459. fct_logger_i *logger_,
  2460. fct_logger_evt_t const *e
  2461. )
  2462. {
  2463. fct_standard_logger_t *logger = (fct_standard_logger_t*)logger_;
  2464. fct_unused(e);
  2465. fct_timer__start(&(logger->timer));
  2466. }
  2467. static void
  2468. fct_standard_logger__on_fctx_end(
  2469. fct_logger_i *logger_,
  2470. fct_logger_evt_t const *e
  2471. )
  2472. {
  2473. fct_standard_logger_t *logger = (fct_standard_logger_t*)logger_;
  2474. nbool_t is_success =1;
  2475. double elasped_time =0;
  2476. size_t num_tests =0;
  2477. size_t num_passed =0;
  2478. fct_timer__stop(&(logger->timer));
  2479. is_success = fct_nlist__size(&(logger->failed_cndtns_list)) ==0;
  2480. if ( !is_success )
  2481. {
  2482. fct_logger_print_failures(&(logger->failed_cndtns_list));
  2483. }
  2484. puts(
  2485. "\n----------------------------------------------------------------------------\n"
  2486. );
  2487. num_tests = fctkern__tst_cnt(e->kern);
  2488. num_passed = fctkern__tst_cnt_passed(e->kern);
  2489. printf(
  2490. "%s (%lu/%lu tests",
  2491. (is_success) ? "PASSED" : "FAILED",
  2492. (unsigned long) num_passed,
  2493. (unsigned long) num_tests
  2494. );
  2495. elasped_time = fct_timer__duration(&(logger->timer));
  2496. if ( elasped_time > 0.0000001 )
  2497. {
  2498. printf(" in %.6fs)\n", elasped_time);
  2499. }
  2500. else
  2501. {
  2502. /* Don't bother displaying the time to execute. */
  2503. puts(")\n");
  2504. }
  2505. }
  2506. static void
  2507. fct_standard_logger__on_delete(
  2508. fct_logger_i *logger_,
  2509. fct_logger_evt_t const *e
  2510. )
  2511. {
  2512. fct_standard_logger_t *logger = (fct_standard_logger_t*)logger_;
  2513. fct_unused(e);
  2514. fct_nlist__final(&(logger->failed_cndtns_list), free);
  2515. free(logger);
  2516. logger_ =NULL;
  2517. }
  2518. static void
  2519. fct_standard_logger__on_warn(
  2520. fct_logger_i* logger_,
  2521. fct_logger_evt_t const *e
  2522. )
  2523. {
  2524. fct_unused(logger_);
  2525. (void)printf("WARNING: %s", e->msg);
  2526. }
  2527. fct_logger_i*
  2528. fct_standard_logger_new(void)
  2529. {
  2530. fct_standard_logger_t *logger = (fct_standard_logger_t *)calloc(
  2531. 1, sizeof(fct_standard_logger_t)
  2532. );
  2533. if ( logger == NULL )
  2534. {
  2535. return NULL;
  2536. }
  2537. fct_logger__init((fct_logger_i*)logger);
  2538. logger->vtable.on_chk = fct_standard_logger__on_chk;
  2539. logger->vtable.on_test_start = fct_standard_logger__on_test_start;
  2540. logger->vtable.on_test_end = fct_standard_logger__on_test_end;
  2541. logger->vtable.on_fctx_start = fct_standard_logger__on_fctx_start;
  2542. logger->vtable.on_fctx_end = fct_standard_logger__on_fctx_end;
  2543. logger->vtable.on_delete = fct_standard_logger__on_delete;
  2544. logger->vtable.on_warn = fct_standard_logger__on_warn;
  2545. logger->vtable.on_test_skip = fct_standard_logger__on_test_skip;
  2546. fct_nlist__init2(&(logger->failed_cndtns_list), 0);
  2547. fct_timer__init(&(logger->timer));
  2548. return (fct_logger_i*)logger;
  2549. }
  2550. /*
  2551. -----------------------------------------------------------
  2552. JUNIT LOGGER
  2553. -----------------------------------------------------------
  2554. */
  2555. /* JUnit logger */
  2556. struct _fct_junit_logger_t
  2557. {
  2558. _fct_logger_head;
  2559. };
  2560. static void
  2561. fct_junit_logger__on_test_suite_start(
  2562. fct_logger_i *l,
  2563. fct_logger_evt_t const *e
  2564. )
  2565. {
  2566. fct_unused(l);
  2567. fct_unused(e);
  2568. FCT_SWITCH_STDOUT_TO_BUFFER();
  2569. FCT_SWITCH_STDERR_TO_BUFFER();
  2570. }
  2571. static void
  2572. fct_junit_logger__on_test_suite_end(
  2573. fct_logger_i *logger_,
  2574. fct_logger_evt_t const *e
  2575. )
  2576. {
  2577. fct_ts_t const *ts = e->ts; /* Test Suite */
  2578. nbool_t is_pass;
  2579. double elasped_time = 0;
  2580. char std_buffer[1024];
  2581. int read_length;
  2582. int first_out_line;
  2583. fct_unused(logger_);
  2584. elasped_time = fct_ts__duration(ts);
  2585. FCT_SWITCH_STDOUT_TO_STDOUT();
  2586. FCT_SWITCH_STDERR_TO_STDERR();
  2587. /* opening testsuite tag */
  2588. printf("\t<testsuite errors=\"%lu\" failures=\"0\" tests=\"%lu\" "
  2589. "name=\"%s\" time=\"%.4f\">\n",
  2590. (unsigned long) fct_ts__tst_cnt(ts)
  2591. - fct_ts__tst_cnt_passed(ts),
  2592. (unsigned long) fct_ts__tst_cnt(ts),
  2593. fct_ts__name(ts),
  2594. elasped_time);
  2595. FCT_NLIST_FOREACH_BGN(fct_test_t*, test, &(ts->test_list))
  2596. {
  2597. is_pass = fct_test__is_pass(test);
  2598. /* opening testcase tag */
  2599. if (is_pass)
  2600. {
  2601. printf("\t\t<testcase name=\"%s\" time=\"%.3f\"",
  2602. fct_test__name(test),
  2603. fct_test__duration(test)
  2604. );
  2605. }
  2606. else
  2607. {
  2608. printf("\t\t<testcase name=\"%s\" time=\"%.3f\">\n",
  2609. fct_test__name(test),
  2610. fct_test__duration(test)
  2611. );
  2612. }
  2613. FCT_NLIST_FOREACH_BGN(fctchk_t*, chk, &(test->failed_chks))
  2614. {
  2615. /* error tag */
  2616. printf("\t\t\t<error message=\"%s\" "
  2617. "type=\"fctx\">", chk->msg);
  2618. printf("file:%s, line:%d", chk->file, chk->lineno);
  2619. printf("</error>\n");
  2620. }
  2621. FCT_NLIST_FOREACH_END();
  2622. /* closing testcase tag */
  2623. if (is_pass)
  2624. {
  2625. printf(" />\n");
  2626. }
  2627. else
  2628. {
  2629. printf("\t\t</testcase>\n");
  2630. }
  2631. }
  2632. FCT_NLIST_FOREACH_END();
  2633. /* print the std streams */
  2634. first_out_line = 1;
  2635. printf("\t\t<system-out>\n\t\t\t<![CDATA[");
  2636. while ( (read_length = _fct_read(fct_stdout_pipe[0], std_buffer, 1024)) > 0)
  2637. {
  2638. if (first_out_line)
  2639. {
  2640. printf("\n");
  2641. first_out_line = 0;
  2642. }
  2643. printf("%.*s", read_length, std_buffer);
  2644. }
  2645. printf("]]>\n\t\t</system-out>\n");
  2646. first_out_line = 1;
  2647. printf("\t\t<system-err>\n\t\t\t<![CDATA[");
  2648. while ((read_length = _fct_read(fct_stderr_pipe[0], std_buffer, 1024)) > 0)
  2649. {
  2650. if (first_out_line)
  2651. {
  2652. printf("\n");
  2653. first_out_line = 0;
  2654. }
  2655. printf("%.*s", read_length, std_buffer);
  2656. }
  2657. printf("]]>\n\t\t</system-err>\n");
  2658. /* closing testsuite tag */
  2659. printf("\t</testsuite>\n");
  2660. }
  2661. static void
  2662. fct_junit_logger__on_fct_start(
  2663. fct_logger_i *logger_,
  2664. fct_logger_evt_t const *e
  2665. )
  2666. {
  2667. fct_unused(logger_);
  2668. fct_unused(e);
  2669. printf("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
  2670. printf("<testsuites>\n");
  2671. }
  2672. static void
  2673. fct_junit_logger__on_fctx_end(
  2674. fct_logger_i *logger_,
  2675. fct_logger_evt_t const *e
  2676. )
  2677. {
  2678. fct_unused(logger_);
  2679. fct_unused(e);
  2680. printf("</testsuites>\n");
  2681. }
  2682. static void
  2683. fct_junit_logger__on_delete(
  2684. fct_logger_i *logger_,
  2685. fct_logger_evt_t const *e
  2686. )
  2687. {
  2688. fct_junit_logger_t *logger = (fct_junit_logger_t*)logger_;
  2689. fct_unused(e);
  2690. free(logger);
  2691. logger_ =NULL;
  2692. }
  2693. fct_junit_logger_t *
  2694. fct_junit_logger_new(void)
  2695. {
  2696. fct_junit_logger_t *logger =
  2697. (fct_junit_logger_t *)calloc(1, sizeof(fct_junit_logger_t));
  2698. if ( logger == NULL )
  2699. {
  2700. return NULL;
  2701. }
  2702. fct_logger__init((fct_logger_i*)logger);
  2703. logger->vtable.on_test_suite_start = fct_junit_logger__on_test_suite_start;
  2704. logger->vtable.on_test_suite_end = fct_junit_logger__on_test_suite_end;
  2705. logger->vtable.on_fctx_start = fct_junit_logger__on_fct_start;
  2706. logger->vtable.on_fctx_end = fct_junit_logger__on_fctx_end;
  2707. logger->vtable.on_delete = fct_junit_logger__on_delete;
  2708. return logger;
  2709. }
  2710. /*
  2711. ------------------------------------------------------------
  2712. MACRO MAGIC
  2713. ------------------------------------------------------------
  2714. This is where the show begins!
  2715. */
  2716. /* This macro invokes a bunch of functions that need to be referenced in
  2717. order to avoid a "unreferenced local function has been removed" warning.
  2718. The logical acrobatics below try and make it appear to the compiler that
  2719. they are needed, but at runtime, only the cheap, first call is made. */
  2720. #define FCT_REFERENCE_FUNCS() \
  2721. {\
  2722. int check = 0 && fctstr_ieq(NULL, NULL);\
  2723. if ( check ) {\
  2724. _fct_cmt("not to be executed");\
  2725. (void)_fct_chk_empty_str(NULL);\
  2726. (void)_fct_chk_full_str(NULL);\
  2727. (void)fct_test__start_timer(NULL);\
  2728. (void)fct_test__stop_timer(NULL);\
  2729. (void)fct_ts_new(NULL);\
  2730. (void)fct_ts__test_begin(NULL);\
  2731. (void)fct_ts__add_test(NULL, NULL);\
  2732. (void)fct_ts__test_end(NULL);\
  2733. (void)fct_ts__inc_total_test_num(NULL);\
  2734. (void)fct_ts__make_abort_test(NULL);\
  2735. (void)fct_ts__setup_abort(NULL);\
  2736. (void)fct_ts__setup_end(NULL);\
  2737. (void)fct_ts__teardown_end(NULL);\
  2738. (void)fct_ts__cnt_end(NULL);\
  2739. (void)fct_ts__is_test_cnt(NULL, 0);\
  2740. (void)fct_xchk_fn(0, "");\
  2741. (void)fct_xchk2_fn(NULL, 0, "");\
  2742. (void)fctkern__cl_parse(NULL);\
  2743. (void)fctkern__add_ts(NULL, NULL);\
  2744. (void)fctkern__pass_filter(NULL, NULL);\
  2745. (void)fctkern__log_suite_start(NULL, NULL);\
  2746. (void)fctkern__log_suite_end(NULL, NULL);\
  2747. (void)fctkern__log_test_skip(NULL, NULL, NULL);\
  2748. (void)fctkern__log_test_start(NULL, NULL);\
  2749. (void)fctkern__log_test_end(NULL, NULL);\
  2750. (void)fctstr_endswith(NULL,NULL);\
  2751. (void)fctstr_iendswith(NULL,NULL);\
  2752. (void)fctstr_ieq(NULL,NULL);\
  2753. (void)fctstr_incl(NULL, NULL);\
  2754. (void)fctstr_iincl(NULL, NULL);\
  2755. (void)fctstr_iendswith(NULL,NULL);\
  2756. (void)fctstr_istartswith(NULL,NULL);\
  2757. (void)fctstr_clone_lower(NULL);\
  2758. (void)fctstr_startswith(NULL,NULL);\
  2759. (void)fctkern__init(NULL, 0, NULL);\
  2760. (void)fctkern__cl_is(NULL, "");\
  2761. (void)fctkern__cl_val2(NULL, NULL, NULL);\
  2762. fctkern__log_suite_skip(NULL, NULL, NULL);\
  2763. (void)fct_clp__is_param(NULL,NULL);\
  2764. _fct_cmt("should never construct an object");\
  2765. (void)fct_test_new(NULL);\
  2766. (void)fct_ts__chk_cnt(NULL);\
  2767. }\
  2768. }
  2769. #define FCT_INIT(_ARGC_, _ARGV_) \
  2770. fctkern_t fctkern__; \
  2771. fctkern_t* fctkern_ptr__ = &fctkern__; \
  2772. FCT_REFERENCE_FUNCS(); \
  2773. if ( !fctkern__init(fctkern_ptr__, argc, (const char **)argv) ) {\
  2774. (void)fprintf( \
  2775. stderr, "FATAL ERROR: Unable to initialize FCTX Kernel."\
  2776. ); \
  2777. exit(EXIT_FAILURE); \
  2778. } \
  2779. #define FCT_FINAL() \
  2780. fctkern_ptr__->ns.num_total_failed = fctkern__tst_cnt_failed( \
  2781. (fctkern_ptr__) \
  2782. ); \
  2783. fctkern__log_end(fctkern_ptr__); \
  2784. fctkern__end(fctkern_ptr__); \
  2785. fctkern__final(fctkern_ptr__); \
  2786. FCT_ASSERT( !((int)fctkern_ptr__->ns.num_total_failed < 0) \
  2787. && "or we got truncated!"); \
  2788. if ( fctkern_ptr__->ns.num_total_failed == \
  2789. fctkern_ptr__->num_expected_failures) { \
  2790. fctkern_ptr__->ns.num_total_failed = 0; \
  2791. } \
  2792. #define FCT_NUM_FAILED() \
  2793. fctkern_ptr__->ns.num_total_failed \
  2794. /* Typically used internally only, this mentions to FCTX that you EXPECT
  2795. to _NUM_FAILS_. If you the expected matches the actual, a 0 value is returned
  2796. from the program. */
  2797. #define FCT_EXPECTED_FAILURES(_NUM_FAILS_) \
  2798. ((fctkern_ptr__->num_expected_failures = (_NUM_FAILS_)))
  2799. #define FCT_BGN_FN(_FNNAME_) \
  2800. int _FNNAME_(int argc, char* argv[])\
  2801. { \
  2802. FCT_INIT(argc, argv)
  2803. #define FCT_END_FN() FCT_END()
  2804. /* This defines our start. The fctkern__ is a kernal object
  2805. that lives throughout the lifetime of our program. The
  2806. fctkern_ptr__ makes it easier to abstract out macros. */
  2807. #define FCT_BGN() FCT_BGN_FN(main)
  2808. /* Silence Intel complaints about unspecified operand order in user's code */
  2809. #ifndef __INTEL_COMPILER
  2810. # define FCT_END_WARNINGFIX_BGN
  2811. # define FCT_END_WARNINGFIX_END
  2812. #else
  2813. # define FCT_END_WARNINGFIX_BGN _Pragma("warning(push,disable:981)");
  2814. # define FCT_END_WARNINGFIX_END _Pragma("warning(pop)");
  2815. #endif
  2816. /* Ends the test suite by returning the number failed. The "chk_cnt" call is
  2817. made in order allow strict compilers to pass when it encounters unreferenced
  2818. functions. */
  2819. #define FCT_END() \
  2820. { \
  2821. FCT_END_WARNINGFIX_BGN \
  2822. FCT_FINAL(); \
  2823. return FCT_NUM_FAILED();\
  2824. FCT_END_WARNINGFIX_END \
  2825. }\
  2826. }
  2827. #define fctlog_install(_CUST_LOGGER_LIST_) \
  2828. fctkern_ptr__->lt_usr = (_CUST_LOGGER_LIST_)
  2829. /* Re-parses the command line options with the addition of user defined
  2830. options. */
  2831. #define fctcl_install(_CLO_INIT_) \
  2832. {\
  2833. fctkern_ptr__->cl_user_opts = (_CLO_INIT_);\
  2834. _fct_cmt("Delay parse in order to allow for user customization.");\
  2835. if ( !fctkern__cl_is_parsed((fctkern_ptr__)) ) {\
  2836. int status = fctkern__cl_parse((fctkern_ptr__));\
  2837. _fct_cmt("Need to parse command line before we start logger.");\
  2838. fctkern__log_start((fctkern_ptr__));\
  2839. switch( status ) {\
  2840. case -1:\
  2841. case 0:\
  2842. fctkern__final(fctkern_ptr__);\
  2843. exit( (status == 0) ? (EXIT_FAILURE) : (EXIT_SUCCESS) );\
  2844. break;\
  2845. default:\
  2846. fct_pass();\
  2847. }\
  2848. }\
  2849. }
  2850. #define fctcl_is(_OPT_STR_) (fctkern__cl_is(fctkern_ptr__, (_OPT_STR_)))
  2851. #define fctcl_val(_OPT_STR_) (fctcl_val2((_OPT_STR_), NULL))
  2852. #define fctcl_val2(_OPT_STR_, _DEF_STR_) \
  2853. (fctkern__cl_val2(fctkern_ptr__, (_OPT_STR_), (_DEF_STR_)))
  2854. /* We delay the first parse of the command line until we get the first
  2855. test fixture. This allows the user to possibly add their own parse
  2856. specification. */
  2857. #define FCT_FIXTURE_SUITE_BGN(_NAME_) \
  2858. {\
  2859. fctkern_ptr__->ns.ts_curr = fct_ts_new( #_NAME_ );\
  2860. _fct_cmt("Delay parse in order to allow for user customization.");\
  2861. if ( !fctkern__cl_is_parsed((fctkern_ptr__)) ) {\
  2862. int status = fctkern__cl_parse((fctkern_ptr__));\
  2863. _fct_cmt("Need to parse command line before we start logger.");\
  2864. fctkern__log_start((fctkern_ptr__));\
  2865. switch( status ) {\
  2866. case -1:\
  2867. case 0:\
  2868. fct_ts__del((fctkern_ptr__->ns.ts_curr));\
  2869. fctkern__final(fctkern_ptr__);\
  2870. exit( (status == 0) ? (EXIT_FAILURE) : (EXIT_SUCCESS) );\
  2871. break;\
  2872. default:\
  2873. fct_pass();\
  2874. }\
  2875. }\
  2876. if ( fctkern_ptr__->ns.ts_curr == NULL ) {\
  2877. fctkern__log_warn((fctkern_ptr__), "out of memory");\
  2878. }\
  2879. else\
  2880. {\
  2881. fctkern__log_suite_start((fctkern_ptr__), fctkern_ptr__->ns.ts_curr);\
  2882. for (;;)\
  2883. {\
  2884. fctkern_ptr__->ns.test_num = -1;\
  2885. if ( fct_ts__is_ending_mode(fctkern_ptr__->ns.ts_curr) \
  2886. || fct_ts__is_abort_mode(fctkern_ptr__->ns.ts_curr) )\
  2887. {\
  2888. _fct_cmt("flag the test suite as complete.");\
  2889. fct_ts__end(fctkern_ptr__->ns.ts_curr);\
  2890. break;\
  2891. }
  2892. /* Closes off a "Fixture" test suite. */
  2893. #define FCT_FIXTURE_SUITE_END() \
  2894. if ( fct_ts__is_cnt_mode(fctkern_ptr__->ns.ts_curr) )\
  2895. {\
  2896. fct_ts__cnt_end(fctkern_ptr__->ns.ts_curr);\
  2897. }\
  2898. }\
  2899. fctkern__add_ts((fctkern_ptr__), fctkern_ptr__->ns.ts_curr);\
  2900. fctkern__log_suite_end((fctkern_ptr__), fctkern_ptr__->ns.ts_curr);\
  2901. fct_ts__end(fctkern_ptr__->ns.ts_curr);\
  2902. fctkern_ptr__->ns.ts_curr = NULL;\
  2903. }\
  2904. }
  2905. #define FCT_FIXTURE_SUITE_BGN_IF(_CONDITION_, _NAME_) \
  2906. fctkern_ptr__->ns.ts_is_skip_suite = !(_CONDITION_);\
  2907. fctkern_ptr__->ns.ts_skip_cndtn = #_CONDITION_;\
  2908. if ( fctkern_ptr__->ns.ts_is_skip_suite ) {\
  2909. fctkern__log_suite_skip((fctkern_ptr__), #_CONDITION_, #_NAME_);\
  2910. }\
  2911. FCT_FIXTURE_SUITE_BGN(_NAME_);
  2912. #define FCT_FIXTURE_SUITE_END_IF() \
  2913. FCT_FIXTURE_SUITE_END();\
  2914. fctkern_ptr__->ns.ts_is_skip_suite =0;\
  2915. fctkern_ptr__->ns.ts_skip_cndtn =NULL;\
  2916. #define FCT_SETUP_BGN()\
  2917. if ( fct_ts__is_setup_mode(fctkern_ptr__->ns.ts_curr) ) {
  2918. #define FCT_SETUP_END() \
  2919. fct_ts__setup_end(fctkern_ptr__->ns.ts_curr); }
  2920. #define FCT_TEARDOWN_BGN() \
  2921. if ( fct_ts__is_teardown_mode(fctkern_ptr__->ns.ts_curr) ) {\
  2922. #define FCT_TEARDOWN_END() \
  2923. fct_ts__teardown_end(fctkern_ptr__->ns.ts_curr); \
  2924. continue; \
  2925. }
  2926. /* Lets you create a test suite, where maybe you don't want a fixture. We
  2927. do it by 'stubbing' out the setup/teardown logic. */
  2928. #define FCT_SUITE_BGN(Name) \
  2929. FCT_FIXTURE_SUITE_BGN(Name) {\
  2930. FCT_SETUP_BGN() {_fct_cmt("stubbed"); } FCT_SETUP_END()\
  2931. FCT_TEARDOWN_BGN() {_fct_cmt("stubbed");} FCT_TEARDOWN_END()\
  2932. #define FCT_SUITE_END() } FCT_FIXTURE_SUITE_END()
  2933. #define FCT_SUITE_BGN_IF(_CONDITION_, _NAME_) \
  2934. FCT_FIXTURE_SUITE_BGN_IF(_CONDITION_, (_NAME_)) {\
  2935. FCT_SETUP_BGN() {_fct_cmt("stubbed"); } FCT_SETUP_END()\
  2936. FCT_TEARDOWN_BGN() {_fct_cmt("stubbed");} FCT_TEARDOWN_END()\
  2937. #define FCT_SUITE_END_IF() } FCT_FIXTURE_SUITE_END_IF()
  2938. typedef enum
  2939. {
  2940. FCT_TEST_END_FLAG_Default = 0x0000,
  2941. FCT_TEST_END_FLAG_ClearFail = 0x0001
  2942. } FCT_TEST_END_FLAG;
  2943. #define FCT_TEST_BGN_IF(_CONDITION_, _NAME_) { \
  2944. fctkern_ptr__->ns.test_is_skip = !(_CONDITION_);\
  2945. fctkern_ptr__->ns.test_skip_cndtn = #_CONDITION_;\
  2946. FCT_TEST_BGN(_NAME_) {\
  2947. #define FCT_TEST_END_IF() \
  2948. } FCT_TEST_END();\
  2949. fctkern_ptr__->ns.test_is_skip = 0;\
  2950. fctkern_ptr__->ns.test_skip_cndtn = NULL;\
  2951. }
  2952. /* Depending on whether or not we are counting the tests, we will have to
  2953. first determine if the test is the "current" count. Then we have to determine
  2954. if we can pass the filter. Finally we will execute everything so that when a
  2955. check fails, we can "break" out to the end of the test. And in between all
  2956. that we do a memory check and fail a test if we can't build a fct_test
  2957. object (should be rare). */
  2958. #define FCT_TEST_BGN(_NAME_) \
  2959. {\
  2960. fctkern_ptr__->ns.curr_test_name = #_NAME_;\
  2961. ++(fctkern_ptr__->ns.test_num);\
  2962. if ( fct_ts__is_cnt_mode(fctkern_ptr__->ns.ts_curr) )\
  2963. {\
  2964. fct_ts__inc_total_test_num(fctkern_ptr__->ns.ts_curr);\
  2965. }\
  2966. else if ( fct_ts__is_test_mode(fctkern_ptr__->ns.ts_curr) \
  2967. && fct_ts__is_test_cnt(fctkern_ptr__->ns.ts_curr, fctkern_ptr__->ns.test_num) )\
  2968. {\
  2969. fct_ts__test_begin(fctkern_ptr__->ns.ts_curr);\
  2970. if ( fctkern__pass_filter(fctkern_ptr__, fctkern_ptr__->ns.curr_test_name ) )\
  2971. {\
  2972. fctkern_ptr__->ns.curr_test = fct_test_new( fctkern_ptr__->ns.curr_test_name );\
  2973. if ( fctkern_ptr__->ns.curr_test == NULL ) {\
  2974. fctkern__log_warn(fctkern_ptr__, "out of memory");\
  2975. } else if ( fctkern_ptr__->ns.ts_is_skip_suite \
  2976. || fctkern_ptr__->ns.test_is_skip ) {\
  2977. fct_ts__test_begin(fctkern_ptr__->ns.ts_curr);\
  2978. fctkern__log_test_skip(\
  2979. fctkern_ptr__,\
  2980. fctkern_ptr__->ns.curr_test_name,\
  2981. (fctkern_ptr__->ns.test_is_skip) ?\
  2982. (fctkern_ptr__->ns.test_skip_cndtn) :\
  2983. (fctkern_ptr__->ns.ts_skip_cndtn)\
  2984. );\
  2985. fct_ts__test_end(fctkern_ptr__->ns.ts_curr);\
  2986. continue;\
  2987. } else {\
  2988. fctkern__log_test_start(fctkern_ptr__, fctkern_ptr__->ns.curr_test);\
  2989. fct_test__start_timer(fctkern_ptr__->ns.curr_test);\
  2990. for (;;) \
  2991. {
  2992. #define FCT_TEST_END() \
  2993. break;\
  2994. }\
  2995. fct_test__stop_timer(fctkern_ptr__->ns.curr_test);\
  2996. }\
  2997. fct_ts__add_test(fctkern_ptr__->ns.ts_curr, fctkern_ptr__->ns.curr_test);\
  2998. fctkern__log_test_end(fctkern_ptr__, fctkern_ptr__->ns.curr_test);\
  2999. }\
  3000. fct_ts__test_end(fctkern_ptr__->ns.ts_curr);\
  3001. continue;\
  3002. }\
  3003. }\
  3004. /*
  3005. ---------------------------------------------------------
  3006. CHECKING MACROS
  3007. ----------------------------------------------------------
  3008. The chk variants will continue on while the req variants will abort
  3009. a test if a chk condition fails. The req variants are useful when you
  3010. no longer want to keep checking conditions because a critical condition
  3011. is not being met. */
  3012. /* To support older compilers that do not have macro variable argument lists
  3013. we have to use a function. The macro manages to store away the line/file
  3014. location into a global before it runs this function, a trick I picked up from
  3015. the error handling in the APR library. The unfortunate thing is that we can
  3016. not carry forth the actual test through a "stringize" operation, but if you
  3017. wanted to do that you should use fct_chk. */
  3018. static int fct_xchk_lineno =0;
  3019. static char const *fct_xchk_file = NULL;
  3020. static fct_test_t *fct_xchk_test = NULL;
  3021. static fctkern_t *fct_xchk_kern =NULL;
  3022. static int
  3023. _fct_xchk_fn_varg(
  3024. char const *condition,
  3025. int is_pass,
  3026. char const *format,
  3027. va_list args
  3028. )
  3029. {
  3030. fctchk_t *chk =NULL;
  3031. chk = fctchk_new(
  3032. is_pass,
  3033. condition,
  3034. fct_xchk_file,
  3035. fct_xchk_lineno,
  3036. format,
  3037. args
  3038. );
  3039. if ( chk == NULL )
  3040. {
  3041. fctkern__log_warn(fct_xchk_kern, "out of memory (aborting test)");
  3042. goto finally;
  3043. }
  3044. fct_test__add(fct_xchk_test, chk);
  3045. fctkern__log_chk(fct_xchk_kern, chk);
  3046. finally:
  3047. fct_xchk_lineno =0;
  3048. fct_xchk_file =NULL;
  3049. fct_xchk_test =NULL;
  3050. fct_xchk_kern =NULL;
  3051. return is_pass;
  3052. }
  3053. static int
  3054. fct_xchk2_fn(const char *condition, int is_pass, char const *format, ...)
  3055. {
  3056. int r =0;
  3057. va_list args;
  3058. va_start(args, format);
  3059. r = _fct_xchk_fn_varg(condition, is_pass, format, args);
  3060. va_end(args);
  3061. return r;
  3062. }
  3063. static int
  3064. fct_xchk_fn(int is_pass, char const *format, ...)
  3065. {
  3066. int r=0;
  3067. va_list args;
  3068. va_start(args, format);
  3069. r = _fct_xchk_fn_varg("<none-from-xchk>", is_pass, format, args);
  3070. va_end(args);
  3071. return r;
  3072. }
  3073. /* Call this with the following argument list:
  3074. fct_xchk(test_condition, format_str, ...)
  3075. the bulk of this macro presets some globals to allow us to support
  3076. variable argument lists on older compilers. The idea came from the APR
  3077. libraries error checking routines. */
  3078. #define fct_xchk fct_xchk_kern = fctkern_ptr__,\
  3079. fct_xchk_test = fctkern_ptr__->ns.curr_test,\
  3080. fct_xchk_lineno =__LINE__,\
  3081. fct_xchk_file=__FILE__,\
  3082. fct_xchk_fn
  3083. #define fct_xchk2 fct_xchk_kern = fctkern_ptr__,\
  3084. fct_xchk_test = fctkern_ptr__->ns.curr_test,\
  3085. fct_xchk_lineno =__LINE__,\
  3086. fct_xchk_file=__FILE__,\
  3087. fct_xchk2_fn
  3088. /* This checks the condition and reports the condition as a string
  3089. if it fails. */
  3090. #define fct_chk(_CNDTN_) (fct_xchk((_CNDTN_) ? 1 : 0, #_CNDTN_))
  3091. #define _fct_req(_CNDTN_) \
  3092. if ( !(fct_xchk((_CNDTN_) ? 1 : 0, #_CNDTN_)) ) { break; }
  3093. /* When in test mode, construct a mock test object for fct_xchk to operate
  3094. with. If we fail a setup up, then we go directly to a teardown mode. */
  3095. #define fct_req(_CNDTN_) \
  3096. if ( fct_ts__is_test_mode(fctkern_ptr__->ns.ts_curr) ) { \
  3097. _fct_req((_CNDTN_)); \
  3098. } \
  3099. else if ( fct_ts__is_setup_mode(fctkern_ptr__->ns.ts_curr) \
  3100. || fct_ts__is_teardown_mode(fctkern_ptr__->ns.ts_curr) ) { \
  3101. fctkern_ptr__->ns.curr_test = fct_ts__make_abort_test( \
  3102. fctkern_ptr__->ns.ts_curr \
  3103. ); \
  3104. if ( !(fct_xchk((_CNDTN_) ? 1 : 0, #_CNDTN_)) ) { \
  3105. fct_ts__setup_abort(fctkern_ptr__->ns.ts_curr); \
  3106. fct_ts__add_test( \
  3107. fctkern_ptr__->ns.ts_curr, fctkern_ptr__->ns.curr_test \
  3108. ); \
  3109. } \
  3110. } else { \
  3111. assert("invalid condition for fct_req!"); \
  3112. _fct_req((_CNDTN_)); \
  3113. }
  3114. #define fct_chk_eq_dbl(V1, V2) \
  3115. fct_xchk(\
  3116. ((int)(fabs((V1)-(V2)) < DBL_EPSILON)),\
  3117. "chk_eq_dbl: %f != %f",\
  3118. (V1),\
  3119. (V2)\
  3120. )
  3121. #define fct_chk_neq_dbl(V1, V2) \
  3122. fct_xchk(\
  3123. ((int)(fabs((V1)-(V2)) >= DBL_EPSILON)),\
  3124. "chk_neq_dbl: %f == %f",\
  3125. (V1),\
  3126. (V2)\
  3127. )
  3128. #define fct_chk_eq_str(V1, V2) \
  3129. fct_xchk(fctstr_eq((V1), (V2)),\
  3130. "chk_eq_str: '%s' != '%s'",\
  3131. (V1),\
  3132. (V2)\
  3133. )
  3134. #define fct_chk_neq_str(V1, V2) \
  3135. fct_xchk(!fctstr_eq((V1), (V2)),\
  3136. "chk_neq_str: '%s' == '%s'",\
  3137. (V1),\
  3138. (V2)\
  3139. )
  3140. /* To quiet warnings with GCC, who think we are being silly and passing
  3141. in NULL to strlen, we will filter the predicate through these little
  3142. functions */
  3143. static int
  3144. _fct_chk_empty_str(char const *s)
  3145. {
  3146. if ( s == NULL )
  3147. {
  3148. return 1;
  3149. }
  3150. return strlen(s) ==0;
  3151. }
  3152. static int
  3153. _fct_chk_full_str(char const *s)
  3154. {
  3155. if ( s == NULL )
  3156. {
  3157. return 0;
  3158. }
  3159. return strlen(s) >0;
  3160. }
  3161. #define fct_chk_empty_str(V) \
  3162. fct_xchk(_fct_chk_empty_str((V)),\
  3163. "string not empty: '%s'",\
  3164. (V)\
  3165. )
  3166. #define fct_chk_full_str(V) \
  3167. fct_xchk(_fct_chk_full_str((V)),\
  3168. "string is full: '%s'",\
  3169. (V)\
  3170. )
  3171. #define fct_chk_eq_istr(V1, V2) \
  3172. fct_xchk(fctstr_ieq((V1), (V2)),\
  3173. "chk_eq_str: '%s' != '%s'",\
  3174. (V1),\
  3175. (V2)\
  3176. )
  3177. #define fct_chk_neq_istr(V1, V2) \
  3178. fct_xchk(!fctstr_ieq((V1), (V2)),\
  3179. "chk_neq_str: '%s' == '%s'",\
  3180. (V1),\
  3181. (V2)\
  3182. )
  3183. #define fct_chk_endswith_str(STR, CHECK)\
  3184. fct_xchk(fctstr_endswith((STR),(CHECK)),\
  3185. "fct_chk_endswith_str: '%s' doesn't end with '%s'",\
  3186. (STR),\
  3187. (CHECK)\
  3188. )
  3189. #define fct_chk_iendswith_str(STR, CHECK)\
  3190. fct_xchk(fctstr_iendswith((STR), (CHECK)),\
  3191. "fch_chk_iendswith_str: '%s' doesn't end with '%s'.",\
  3192. (STR),\
  3193. (CHECK)\
  3194. )
  3195. #define fct_chk_excl_str(STR, CHECK_EXCLUDE) \
  3196. fct_xchk(!fctstr_incl((STR), (CHECK_EXCLUDE)),\
  3197. "fct_chk_excl_str: '%s' is included in '%s'",\
  3198. (STR),\
  3199. (CHECK_EXCLUDE)\
  3200. )
  3201. #define fct_chk_excl_istr(ISTR, ICHECK_EXCLUDE) \
  3202. fct_xchk(!fctstr_iincl((ISTR), (ICHECK_EXCLUDE)),\
  3203. "fct_chk_excl_istr (case insensitive): '%s' is "\
  3204. "included in'%s'",\
  3205. (ISTR),\
  3206. (ICHECK_EXCLUDE)\
  3207. )
  3208. #define fct_chk_incl_str(STR, CHECK_INCLUDE) \
  3209. fct_xchk(fctstr_incl((STR), (CHECK_INCLUDE)),\
  3210. "fct_chk_incl_str: '%s' does not include '%s'",\
  3211. (STR),\
  3212. (CHECK_INCLUDE)\
  3213. )
  3214. #define fct_chk_incl_istr(ISTR, ICHECK_INCLUDE) \
  3215. fct_xchk(fctstr_iincl((ISTR), (ICHECK_INCLUDE)),\
  3216. "fct_chk_incl_istr (case insensitive): '%s' does "\
  3217. "not include '%s'",\
  3218. (ISTR),\
  3219. (ICHECK_INCLUDE)\
  3220. )
  3221. #define fct_chk_startswith_str(STR, CHECK)\
  3222. fct_xchk(fctstr_startswith((STR), (CHECK)),\
  3223. "'%s' does not start with '%s'",\
  3224. (STR),\
  3225. (CHECK)\
  3226. )
  3227. #define fct_chk_startswith_istr(STR, CHECK)\
  3228. fct_xchk(fctstr_istartswith((STR), (CHECK)),\
  3229. "case insensitive check: '%s' does not start with '%s'",\
  3230. (STR),\
  3231. (CHECK)\
  3232. )
  3233. #define fct_chk_eq_int(V1, V2) \
  3234. fct_xchk(\
  3235. ((V1) == (V2)),\
  3236. "chq_eq_int: %d != %d",\
  3237. (V1),\
  3238. (V2)\
  3239. )
  3240. #define fct_chk_neq_int(V1, V2) \
  3241. fct_xchk(\
  3242. ((V1) != (V2)),\
  3243. "chq_neq_int: %d == %d",\
  3244. (V1),\
  3245. (V2)\
  3246. )
  3247. #define fct_chk_ex(EXCEPTION, CODE) \
  3248. { \
  3249. bool pass_chk_ex = false; \
  3250. try { \
  3251. CODE; \
  3252. pass_chk_ex = false; \
  3253. } catch ( EXCEPTION ) { \
  3254. pass_chk_ex = true; \
  3255. } catch ( ... ) { \
  3256. pass_chk_ex = false; \
  3257. } \
  3258. fct_xchk( \
  3259. pass_chk_ex, \
  3260. "%s exception not generated", \
  3261. #EXCEPTION \
  3262. ); \
  3263. } \
  3264. /*
  3265. ---------------------------------------------------------
  3266. GUT CHECK MACROS
  3267. ----------------------------------------------------------
  3268. The following macros are used to help check the "guts" of
  3269. the FCT, and to confirm that it all works according to spec.
  3270. */
  3271. /* Generates a message to STDERR and exits the application with a
  3272. non-zero number. */
  3273. #define _FCT_GUTCHK(_CNDTN_) \
  3274. if ( !(_CNDTN_) ) {\
  3275. fprintf(stderr, "gutchk fail: '" #_CNDTN_ "' was not true.\n");\
  3276. exit(1);\
  3277. }\
  3278. else {\
  3279. fprintf(stdout, "gutchk pass: '" #_CNDTN_ "'\n");\
  3280. }
  3281. /*
  3282. ---------------------------------------------------------
  3283. MULTI-FILE TEST SUITE MACROS
  3284. ----------------------------------------------------------
  3285. I struggled trying to figure this out in a way that was
  3286. as simple as possible. I wanted to be able to define
  3287. the test suite in one object file, then refer it within
  3288. the other one within the minimum amount of typing.
  3289. Unfortunately without resorting to some supermacro
  3290. work, I could only find a happy comprimise.
  3291. See test_multi.c for an example.
  3292. */
  3293. /* The following macros are used in your separate object
  3294. file to define your test suite. */
  3295. #define FCTMF_FIXTURE_SUITE_BGN(NAME) \
  3296. void NAME (fctkern_t *fctkern_ptr__) {\
  3297. FCT_REFERENCE_FUNCS();\
  3298. FCT_FIXTURE_SUITE_BGN( NAME ) {
  3299. #define FCTMF_FIXTURE_SUITE_END() \
  3300. } FCT_FIXTURE_SUITE_END();\
  3301. }
  3302. #define FCTMF_SUITE_BGN(NAME) \
  3303. void NAME (fctkern_t *fctkern_ptr__) {\
  3304. FCT_REFERENCE_FUNCS();\
  3305. FCT_SUITE_BGN( NAME ) {
  3306. #define FCTMF_SUITE_END() \
  3307. } FCT_SUITE_END(); \
  3308. }
  3309. /* Deprecated, no longer required. */
  3310. #define FCTMF_SUITE_DEF(NAME)
  3311. /* Executes a test suite defined by FCTMF_SUITE* */
  3312. #define FCTMF_SUITE_CALL(NAME) {\
  3313. void NAME (fctkern_t *);\
  3314. NAME (fctkern_ptr__);\
  3315. }
  3316. /*
  3317. ---------------------------------------------------------
  3318. FCT QUICK TEST API
  3319. ----------------------------------------------------------
  3320. The goal of these little macros is to try and get you
  3321. up and running with a test as quick as possible.
  3322. The basic idea is that there is one test per test suite.
  3323. */
  3324. #define FCT_QTEST_BGN(NAME) \
  3325. FCT_SUITE_BGN(NAME) {\
  3326. FCT_TEST_BGN(NAME) {\
  3327. #define FCT_QTEST_END() \
  3328. } FCT_TEST_END();\
  3329. } FCT_SUITE_END();
  3330. #define FCT_QTEST_BGN_IF(_CONDITION_, _NAME_) \
  3331. FCT_SUITE_BGN(_NAME_) {\
  3332. FCT_TEST_BGN_IF(_CONDITION_, _NAME_) {\
  3333. #define FCT_QTEST_END_IF() \
  3334. } FCT_TEST_END_IF();\
  3335. } FCT_SUITE_END();
  3336. #endif /* !FCT_INCLUDED__IMB */