test.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "core/strings/stringFunctions.h"
  25. #include "console/console.h"
  26. #include "unit/test.h"
  27. #include "core/util/journal/process.h"
  28. namespace UnitTesting
  29. {
  30. //-----------------------------------------------------------------------------
  31. TestRegistry *TestRegistry::_list = 0;
  32. //-----------------------------------------------------------------------------
  33. static const S32 MaxMarginCount = 32;
  34. static const S32 MaxMarginValue = 128;
  35. static S32 _Margin[MaxMarginCount] = { 3 };
  36. static S32* _MarginPtr = _Margin;
  37. static char _MarginString[MaxMarginValue];
  38. static void _printMargin()
  39. {
  40. if (*_MarginPtr)
  41. ::fwrite(_MarginString,1,*_MarginPtr,stdout);
  42. }
  43. void UnitMargin::Push(S32 margin)
  44. {
  45. if (_MarginPtr < _Margin + MaxMarginCount) {
  46. *++_MarginPtr = (margin < MaxMarginValue)? margin: MaxMarginValue;
  47. memset(_MarginString,' ',*_MarginPtr);
  48. }
  49. }
  50. void UnitMargin::Pop()
  51. {
  52. if (_MarginPtr > _Margin) {
  53. _MarginPtr--;
  54. memset(_MarginString,' ',*_MarginPtr);
  55. }
  56. }
  57. S32 UnitMargin::Current()
  58. {
  59. return *_MarginPtr;
  60. }
  61. void UnitPrint(const char* str)
  62. {
  63. static bool lineStart = true;
  64. Platform::outputDebugString(str);
  65. // Need to scan for '\n' in order to support margins
  66. const char* ptr = str, *itr = ptr;
  67. for (; *itr != 0; itr++)
  68. if (*itr == '\n')
  69. {
  70. if (lineStart)
  71. _printMargin();
  72. ::fwrite(ptr,1,itr - ptr + 1,stdout);
  73. ptr = itr + 1;
  74. lineStart = true;
  75. }
  76. // End the line with a carriage return unless the
  77. // line ends with a line continuation char.
  78. if (ptr != itr) {
  79. if (lineStart)
  80. _printMargin();
  81. if (itr[-1] == '\\') {
  82. ::fwrite(ptr,1,itr - ptr - 1,stdout);
  83. lineStart = false;
  84. }
  85. else {
  86. ::fwrite(ptr,1,itr - ptr,stdout);
  87. ::fwrite("\n",1,1,stdout);
  88. lineStart = true;
  89. }
  90. }
  91. else {
  92. ::fwrite("\n",1,1,stdout);
  93. lineStart = true;
  94. }
  95. ::fflush(stdout);
  96. }
  97. //-----------------------------------------------------------------------------
  98. UnitTest::UnitTest() {
  99. _testCount = 0;
  100. _failureCount = 0;
  101. _warningCount = 0;
  102. _lastTestResult = true;
  103. }
  104. void UnitTest::fail(const char* msg)
  105. {
  106. Con::warnf("** Failed: %s",msg);
  107. dFetchAndAdd( _failureCount, 1 );
  108. }
  109. void UnitTest::warn(const char* msg)
  110. {
  111. Con::warnf("** Warning: %s",msg);
  112. dFetchAndAdd( _warningCount, 1 );
  113. }
  114. //-----------------------------------------------------------------------------
  115. TestRegistry::TestRegistry(const char* name, bool interactive, const char *className)
  116. {
  117. // Check that no existing test uses the same class-name; this is guaranteed
  118. // to lead to funkiness.
  119. TestRegistry *walk = _list;
  120. while(walk)
  121. {
  122. if(walk->_className)
  123. {
  124. AssertFatal(dStricmp(className, walk->_className), "TestRegistry::TestRegistry - got two unit tests with identical class names; they must have unique class names!");
  125. }
  126. walk = walk->_next;
  127. }
  128. // Add us to the list.
  129. _next = _list;
  130. _list = this;
  131. // And fill in our fields.
  132. _name = name;
  133. _className = className;
  134. _isInteractive = interactive;
  135. }
  136. DynamicTestRegistration::DynamicTestRegistration( const char *name, UnitTest *test ) : TestRegistry( name, false, NULL ), mUnitTest( test )
  137. {
  138. }
  139. DynamicTestRegistration::~DynamicTestRegistration()
  140. {
  141. // Un-link ourselves from the test registry
  142. TestRegistry *walk = _list;
  143. // Easy case!
  144. if( walk == this )
  145. _list = _next;
  146. else
  147. {
  148. // Search for us and remove
  149. while( ( walk != 0 ) && ( walk->_next != 0 ) && ( walk->_next != this ) )
  150. walk = walk->_next;
  151. // When this loop is broken, walk will be the unit test in the list previous to this one
  152. if( walk != 0 && walk->_next != 0 )
  153. walk->_next = walk->_next->_next;
  154. }
  155. }
  156. //-----------------------------------------------------------------------------
  157. TestRun::TestRun()
  158. {
  159. _subCount = 0;
  160. _testCount = 0;
  161. _failureCount = 0;
  162. _warningCount = 0;
  163. }
  164. void TestRun::printStats()
  165. {
  166. Con::printf("-- %d test%s run (with %d sub-test%s)",
  167. _testCount,(_testCount != 1)? "s": "",
  168. _subCount,(_subCount != 1)? "s": "");
  169. if (_testCount)
  170. {
  171. if (_failureCount)
  172. Con::printf("** %d reported failure%s",
  173. _failureCount,(_failureCount != 1)? "s": "");
  174. else if (_warningCount)
  175. Con::printf("** %d reported warning%s",
  176. _warningCount,(_warningCount != 1)? "s": "");
  177. else
  178. Con::printf("-- No reported failures");
  179. }
  180. }
  181. void TestRun::test(TestRegistry* reg)
  182. {
  183. Con::printf("-- Testing: %s %s",reg->getName(), reg->isInteractive() ? "(interactive)" : "" );
  184. UnitMargin::Push(_Margin[0]);
  185. // Run the test.
  186. UnitTest* test = reg->newTest();
  187. test->run();
  188. UnitMargin::Pop();
  189. // Update stats.
  190. _failureCount += test->getFailureCount();
  191. _subCount += test->getTestCount();
  192. _warningCount += test->getWarningCount();
  193. _testCount++;
  194. // Don't forget to delete the test!
  195. delete test;
  196. }
  197. // [tom, 2/5/2007] To provide a predictable environment for the tests, this
  198. // now changes the current directory to the executable's directory before
  199. // running the tests. The previous current directory is restored on exit.
  200. bool TestRun::test(const char* module, bool skipInteractive)
  201. {
  202. StringTableEntry cwdSave = Platform::getCurrentDirectory();
  203. S32 len = strlen(module);
  204. const char *skipMsg = skipInteractive ? "(skipping interactive tests)" : "";
  205. // Indicate to the user what we're up to.
  206. if (!len)
  207. Con::printf("-- Running all unit tests %s", skipMsg);
  208. else
  209. Con::printf("-- Running %s tests %s",module, skipMsg);
  210. for (TestRegistry* itr = TestRegistry::getFirst(); itr; itr = itr->getNext())
  211. {
  212. if (!len || !dStrnicmp(module,itr->getName(),len))
  213. {
  214. // Skip the test if it's interactive and we're in skipinteractive mode.
  215. if(skipInteractive && itr->isInteractive())
  216. continue;
  217. // Otherwise, run the test!
  218. Platform::setCurrentDirectory(Platform::getMainDotCsDir());
  219. test(itr);
  220. }
  221. }
  222. // Print out a nice report on how we did.
  223. printStats();
  224. Platform::setCurrentDirectory(cwdSave);
  225. // sanity check for avoid Process::requestShutdown() called on some tests
  226. Process::processEvents();
  227. // And indicate our failure situation in the return value.
  228. return !_failureCount;
  229. }
  230. } // Namespace