• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

dejagnu.h

Go to the documentation of this file.
00001 /* DejaGnu unit testing header.
00002    Copyright (C) 2000, 2001, 2002, 2004, 2006 Free Software
00003    Foundation, Inc.
00004 
00005 This file is part of DejaGnu.
00006 
00007 DejaGnu is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 DejaGnu is distributed in the hope that it will be useful, but
00013 WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with DejaGnu; if not, write to the Free Software Foundation,
00019 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00020 
00021 #ifndef __DEJAGNU_H__
00022 #define __DEJAGNU_H__
00023 
00024 #include <stdio.h>
00025 #include <stdarg.h>
00026 #include <string.h>
00027 
00028 /* If you have problems with DejaGnu dropping failed, untested, or
00029  * unresolved messages generated by a unit testcase, then:  */
00030 
00031 /* #define _DEJAGNU_WAIT_  */
00032 
00033 #ifdef _DEJAGNU_WAIT_
00034 #include <sys/time.h>
00035 #include <sys/types.h>
00036 #include <unistd.h>
00037 #endif
00038 
00039 static int passed;
00040 static int failed;
00041 static int untest;
00042 static int unresolve;
00043 static int xfailed;
00044 static int xpassed;
00045 
00046 static char buffer[512];
00047 
00048 void
00049 wait (void)
00050 {
00051 #ifdef _DEJAGNU_WAIT_
00052   fd_set rfds;
00053   struct timeval tv;
00054   
00055   FD_ZERO (&rfds);
00056   tv.tv_sec = 0;
00057   tv.tv_usec = 1;
00058   
00059   select (0, &rfds, NULL, NULL, &tv);
00060 #endif
00061 }
00062 
00063 inline void
00064 pass (const char* fmt, ...)
00065 {
00066   va_list ap;
00067         
00068   passed++;
00069   va_start (ap, fmt);
00070   vsnprintf (buffer, sizeof (buffer), fmt, ap);
00071   va_end (ap);
00072   printf ("\tPASSED: %s\n", buffer);
00073   wait ();
00074 }
00075 
00076 inline void
00077 xpass (const char* fmt, ...)
00078 {
00079   va_list ap;
00080         
00081   passed++;
00082   va_start (ap, fmt);
00083   vsnprintf (buffer, sizeof (buffer), fmt, ap);
00084   va_end (ap);
00085   printf ("\tXPASSED: %s\n", buffer);
00086   wait ();
00087 }
00088 
00089 inline void
00090 fail (const char* fmt, ...)
00091 {
00092   va_list ap;
00093   
00094   failed++;
00095   va_start (ap, fmt);
00096   vsnprintf (buffer, sizeof (buffer), fmt, ap);
00097   va_end (ap);
00098   printf ("\tFAILED: %s\n", buffer);
00099   wait ();
00100 }
00101 
00102 inline void
00103 xfail (const char* fmt, ...)
00104 {
00105   va_list ap;
00106   
00107   failed++;
00108   va_start (ap, fmt);
00109   vsnprintf (buffer, sizeof (buffer), fmt, ap);
00110   va_end (ap);
00111   printf ("\tXFAILED: %s\n", buffer);
00112   wait ();
00113 }
00114 
00115 inline void
00116 untested (const char* fmt, ...)
00117 {
00118   va_list ap;
00119   
00120   untest++;
00121   va_start (ap, fmt);
00122   vsnprintf (buffer, sizeof (buffer), fmt, ap);
00123   va_end (ap);
00124   printf ("\tUNTESTED: %s\n", buffer);
00125   wait ();
00126 }
00127 
00128 inline void
00129 unresolved (const char* fmt, ...)
00130 {
00131   va_list ap;
00132   
00133   unresolve++;
00134   va_start (ap, fmt);
00135   vsnprintf (buffer, sizeof (buffer), fmt, ap);
00136   va_end (ap);
00137   printf ("\tUNRESOLVED: %s\n", buffer);
00138   wait ();
00139 }
00140 
00141 inline void
00142 note (const char* fmt, ...)
00143 {
00144   va_list ap;
00145   
00146   va_start (ap, fmt);
00147   vsnprintf (buffer, sizeof (buffer), fmt, ap);
00148   va_end (ap);
00149   printf ("\tNOTE: %s\n", buffer);
00150   wait ();
00151 }
00152 
00153 inline void
00154 totals (void)
00155 {
00156   printf ("\nTotals:\n");
00157   printf ("\t#passed:\t\t%d\n", passed);
00158   printf ("\t#real failed:\t\t%d\n", failed);
00159   if (xfailed)
00160     printf ("\t#expected failures:\t\t%d\n", xfailed);
00161   if (untest)
00162     printf ("\t#untested:\t\t%d\n", untest);
00163   if (unresolve)
00164     printf ("\t#unresolved:\t\t%d\n", unresolve);
00165 }
00166 
00167 #ifdef __cplusplus
00168 
00169 #include <iostream>
00170 #include <iomanip>
00171 #include <fstream>
00172 #include <string>
00173 
00174 const char *outstate_list[] = {
00175   "FAILED: ", "PASSED: ", "UNTESTED: ", "UNRESOLVED: ", "XFAILED: ", "XPASSED: "
00176 };
00177 
00178 const char ** outstate = outstate_list;
00179 
00180 enum teststate { FAILED, PASSED, UNTESTED, UNRESOLVED, XFAILED, XPASSED} laststate;
00181 
00182 class TestState {
00183  private:
00184   teststate laststate;
00185   std::string lastmsg;
00186  public:
00187   TestState (void)
00188     {
00189       passed = 0;
00190       failed = 0;
00191       untest = 0;
00192       xpassed = 0;
00193       xfailed = 0;
00194       unresolve = 0;
00195     }
00196 
00197   ~TestState (void) { totals(); }
00198 
00199   void testrun (bool b, std::string s)
00200     {
00201       if (b)
00202         pass (s);
00203       else
00204         fail (s);
00205     }
00206 
00207     void pass (std::string s)
00208       {
00209         passed++;
00210         laststate = PASSED;
00211         lastmsg = s;
00212         std::cout << "\t" << outstate[PASSED] << s << std::endl;
00213       }
00214 
00215     void pass (const char *c)
00216       {
00217         std::string s = c;
00218         pass (s);
00219       }
00220 
00221     void xpass (std::string s)
00222       {
00223         xpassed++;
00224         laststate = PASSED;
00225         lastmsg = s;
00226         std::cout << "\t" << outstate[XPASSED] << s << std::endl;
00227       }
00228 
00229     void xpass (const char *c)
00230       {
00231         std::string s = c;
00232         xpass (s);
00233       }
00234 
00235     void fail (std::string s)
00236       {
00237         failed++;
00238         laststate = FAILED;
00239         lastmsg = s;
00240         std::cout << "\t" << outstate[FAILED] << s << std::endl;
00241       }
00242 
00243     void fail (const char *c)
00244       {
00245         std::string s = c;
00246         fail (s);
00247       }
00248 
00249     void xfail (std::string s)
00250       {
00251         xfailed++;
00252         laststate = XFAILED;
00253         lastmsg = s;
00254         std::cout << "\t" << outstate[XFAILED] << s << std::endl;
00255       }
00256 
00257     void xfail (const char *c)
00258       {
00259         std::string s = c;
00260         xfail (s);
00261       }
00262 
00263     void untested (std::string s)
00264       {
00265         untest++;
00266         laststate = UNTESTED;
00267         lastmsg = s;
00268         std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
00269       }
00270 
00271     void untested (const char *c)
00272       {
00273         std::string s = c;
00274         untested (s);
00275       }
00276     
00277     void unresolved (std::string s)
00278       {
00279         unresolve++;
00280         laststate = UNRESOLVED;
00281         lastmsg = s;
00282         std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
00283       }
00284 
00285     void unresolved (const char *c)
00286       {
00287         std::string s = c;
00288         unresolved (s);
00289       }
00290 
00291     void totals (void)
00292       {
00293         std::cout << "\t#passed:\t\t" << passed << std::endl;
00294         std::cout << "\t#real failed:\t\t" << failed << std::endl;
00295         if (xfailed)
00296           std::cout << "\t#expected failures:\t\t" << xfailed << std::endl;
00297         if (xpassed)
00298           std::cout << "\t#unexpected passes:\t\t" << xpassed << std::endl;
00299         if (untest)
00300           std::cout << "\t#untested:\t\t" << untest << std::endl;
00301         if (unresolve)
00302           std::cout << "\t#unresolved:\t\t" << unresolve << std::endl;
00303       }
00304     
00305     // This is so this class can be printed in an ostream.
00306     friend std::ostream & operator << (std::ostream &os, TestState& t)
00307       {
00308         return os << "\t" << outstate[t.laststate] << t.lastmsg ;
00309       }
00310     
00311     int GetState (void) { return laststate; }
00312     std::string GetMsg (void) { return lastmsg; }
00313 };
00314 
00315 #endif /* __cplusplus */
00316 #endif /* _DEJAGNU_H_ */

Generated on Thu Sep 2 2010 for Gnash by  doxygen 1.7.1