fct.h 107 KB

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