dejagnu.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* DejaGnu unit testing header.
  2. Copyright (C) 2000-2016 Free Software Foundation, Inc.
  3. This file is part of DejaGnu.
  4. DejaGnu is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. DejaGnu is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with DejaGnu; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef __DEJAGNU_H__
  16. #define __DEJAGNU_H__
  17. #include <stdio.h>
  18. #include <stdarg.h>
  19. #include <string.h>
  20. /* If you have problems with DejaGnu dropping failed, untested, or
  21. * unresolved messages generated by a unit testcase, then: */
  22. /* #define _DEJAGNU_WAIT_ */
  23. #ifdef _DEJAGNU_WAIT_
  24. #include <sys/time.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #endif
  28. static int passed;
  29. static int failed;
  30. static int untest;
  31. static int unresolve;
  32. static int xfailed;
  33. static int xpassed;
  34. static char buffer[512];
  35. void
  36. wait (void)
  37. {
  38. #ifdef _DEJAGNU_WAIT_
  39. fd_set rfds;
  40. struct timeval tv;
  41. FD_ZERO (&rfds);
  42. tv.tv_sec = 0;
  43. tv.tv_usec = 1;
  44. select (0, &rfds, NULL, NULL, &tv);
  45. #endif
  46. }
  47. static inline void
  48. pass (const char* fmt, ...)
  49. {
  50. va_list ap;
  51. passed++;
  52. va_start (ap, fmt);
  53. vsnprintf (buffer, sizeof (buffer), fmt, ap);
  54. va_end (ap);
  55. printf ("\tPASSED: %s\n", buffer);
  56. wait ();
  57. }
  58. static inline void
  59. xpass (const char* fmt, ...)
  60. {
  61. va_list ap;
  62. passed++;
  63. va_start (ap, fmt);
  64. vsnprintf (buffer, sizeof (buffer), fmt, ap);
  65. va_end (ap);
  66. printf ("\tXPASSED: %s\n", buffer);
  67. wait ();
  68. }
  69. static inline void
  70. fail (const char* fmt, ...)
  71. {
  72. va_list ap;
  73. failed++;
  74. va_start (ap, fmt);
  75. vsnprintf (buffer, sizeof (buffer), fmt, ap);
  76. va_end (ap);
  77. printf ("\tFAILED: %s\n", buffer);
  78. wait ();
  79. }
  80. static inline void
  81. xfail (const char* fmt, ...)
  82. {
  83. va_list ap;
  84. failed++;
  85. va_start (ap, fmt);
  86. vsnprintf (buffer, sizeof (buffer), fmt, ap);
  87. va_end (ap);
  88. printf ("\tXFAILED: %s\n", buffer);
  89. wait ();
  90. }
  91. static inline void
  92. untested (const char* fmt, ...)
  93. {
  94. va_list ap;
  95. untest++;
  96. va_start (ap, fmt);
  97. vsnprintf (buffer, sizeof (buffer), fmt, ap);
  98. va_end (ap);
  99. printf ("\tUNTESTED: %s\n", buffer);
  100. wait ();
  101. }
  102. static inline void
  103. unresolved (const char* fmt, ...)
  104. {
  105. va_list ap;
  106. unresolve++;
  107. va_start (ap, fmt);
  108. vsnprintf (buffer, sizeof (buffer), fmt, ap);
  109. va_end (ap);
  110. printf ("\tUNRESOLVED: %s\n", buffer);
  111. wait ();
  112. }
  113. static inline void
  114. note (const char* fmt, ...)
  115. {
  116. va_list ap;
  117. va_start (ap, fmt);
  118. vsnprintf (buffer, sizeof (buffer), fmt, ap);
  119. va_end (ap);
  120. printf ("\tNOTE: %s\n", buffer);
  121. wait ();
  122. }
  123. static inline void
  124. totals (void)
  125. {
  126. printf ("\nTotals:\n");
  127. printf ("\t#passed:\t\t%d\n", passed);
  128. printf ("\t#real failed:\t\t%d\n", failed);
  129. if (xfailed)
  130. printf ("\t#expected failures:\t\t%d\n", xfailed);
  131. if (untest)
  132. printf ("\t#untested:\t\t%d\n", untest);
  133. if (unresolve)
  134. printf ("\t#unresolved:\t\t%d\n", unresolve);
  135. }
  136. #ifdef __cplusplus
  137. #include <iostream>
  138. #include <iomanip>
  139. #include <fstream>
  140. #include <string>
  141. const char *outstate_list[] = {
  142. "FAILED: ", "PASSED: ", "UNTESTED: ", "UNRESOLVED: ", "XFAILED: ", "XPASSED: "
  143. };
  144. const char ** outstate = outstate_list;
  145. enum teststate { FAILED, PASSED, UNTESTED, UNRESOLVED, XFAILED, XPASSED} laststate;
  146. class TestState {
  147. private:
  148. teststate laststate;
  149. std::string lastmsg;
  150. public:
  151. TestState (void)
  152. {
  153. passed = 0;
  154. failed = 0;
  155. untest = 0;
  156. xpassed = 0;
  157. xfailed = 0;
  158. unresolve = 0;
  159. }
  160. ~TestState (void) { totals(); }
  161. void testrun (bool b, std::string s)
  162. {
  163. if (b)
  164. pass (s);
  165. else
  166. fail (s);
  167. }
  168. void pass (std::string s)
  169. {
  170. passed++;
  171. laststate = PASSED;
  172. lastmsg = s;
  173. std::cout << "\t" << outstate[PASSED] << s << std::endl;
  174. }
  175. void pass (const char *c)
  176. {
  177. std::string s = c;
  178. pass (s);
  179. }
  180. void xpass (std::string s)
  181. {
  182. xpassed++;
  183. laststate = PASSED;
  184. lastmsg = s;
  185. std::cout << "\t" << outstate[XPASSED] << s << std::endl;
  186. }
  187. void xpass (const char *c)
  188. {
  189. std::string s = c;
  190. xpass (s);
  191. }
  192. void fail (std::string s)
  193. {
  194. failed++;
  195. laststate = FAILED;
  196. lastmsg = s;
  197. std::cout << "\t" << outstate[FAILED] << s << std::endl;
  198. }
  199. void fail (const char *c)
  200. {
  201. std::string s = c;
  202. fail (s);
  203. }
  204. void xfail (std::string s)
  205. {
  206. xfailed++;
  207. laststate = XFAILED;
  208. lastmsg = s;
  209. std::cout << "\t" << outstate[XFAILED] << s << std::endl;
  210. }
  211. void xfail (const char *c)
  212. {
  213. std::string s = c;
  214. xfail (s);
  215. }
  216. void untested (std::string s)
  217. {
  218. untest++;
  219. laststate = UNTESTED;
  220. lastmsg = s;
  221. std::cout << "\t" << outstate[UNTESTED] << s << std::endl;
  222. }
  223. void untested (const char *c)
  224. {
  225. std::string s = c;
  226. untested (s);
  227. }
  228. void unresolved (std::string s)
  229. {
  230. unresolve++;
  231. laststate = UNRESOLVED;
  232. lastmsg = s;
  233. std::cout << "\t" << outstate[UNRESOLVED] << s << std::endl;
  234. }
  235. void unresolved (const char *c)
  236. {
  237. std::string s = c;
  238. unresolved (s);
  239. }
  240. void totals (void)
  241. {
  242. std::cout << "\t#passed:\t\t" << passed << std::endl;
  243. std::cout << "\t#real failed:\t\t" << failed << std::endl;
  244. if (xfailed)
  245. std::cout << "\t#expected failures:\t\t" << xfailed << std::endl;
  246. if (xpassed)
  247. std::cout << "\t#unexpected passes:\t\t" << xpassed << std::endl;
  248. if (untest)
  249. std::cout << "\t#untested:\t\t" << untest << std::endl;
  250. if (unresolve)
  251. std::cout << "\t#unresolved:\t\t" << unresolve << std::endl;
  252. }
  253. // This is so this class can be printed in an ostream.
  254. friend std::ostream & operator << (std::ostream &os, TestState& t)
  255. {
  256. return os << "\t" << outstate[t.laststate] << t.lastmsg ;
  257. }
  258. int GetState (void) { return laststate; }
  259. std::string GetMsg (void) { return lastmsg; }
  260. };
  261. #endif /* __cplusplus */
  262. #endif /* _DEJAGNU_H_ */