test.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. namespace UnitTesting
  28. {
  29. //-----------------------------------------------------------------------------
  30. TestRegistry *TestRegistry::_list = 0;
  31. //-----------------------------------------------------------------------------
  32. static const int MaxMarginCount = 32;
  33. static const int MaxMarginValue = 128;
  34. static int _Margin[MaxMarginCount] = { 3 };
  35. static int* _MarginPtr = _Margin;
  36. static char _MarginString[MaxMarginValue];
  37. static void _printMargin()
  38. {
  39. if (*_MarginPtr)
  40. ::fwrite(_MarginString,1,*_MarginPtr,stdout);
  41. }
  42. void UnitMargin::Push(int margin)
  43. {
  44. if (_MarginPtr < _Margin + MaxMarginCount) {
  45. *++_MarginPtr = (margin < MaxMarginValue)? margin: MaxMarginValue;
  46. memset(_MarginString,' ',*_MarginPtr);
  47. }
  48. }
  49. void UnitMargin::Pop()
  50. {
  51. if (_MarginPtr > _Margin) {
  52. _MarginPtr--;
  53. memset(_MarginString,' ',*_MarginPtr);
  54. }
  55. }
  56. int UnitMargin::Current()
  57. {
  58. return *_MarginPtr;
  59. }
  60. void UnitPrint(const char* str)
  61. {
  62. static bool lineStart = true;
  63. Platform::outputDebugString(str);
  64. // Need to scan for '\n' in order to support margins
  65. const char* ptr = str, *itr = ptr;
  66. for (; *itr != 0; itr++)
  67. if (*itr == '\n')
  68. {
  69. if (lineStart)
  70. _printMargin();
  71. ::fwrite(ptr,1,itr - ptr + 1,stdout);
  72. ptr = itr + 1;
  73. lineStart = true;
  74. }
  75. // End the line with a carriage return unless the
  76. // line ends with a line continuation char.
  77. if (ptr != itr) {
  78. if (lineStart)
  79. _printMargin();
  80. if (itr[-1] == '\\') {
  81. ::fwrite(ptr,1,itr - ptr - 1,stdout);
  82. lineStart = false;
  83. }
  84. else {
  85. ::fwrite(ptr,1,itr - ptr,stdout);
  86. ::fwrite("\n",1,1,stdout);
  87. lineStart = true;
  88. }
  89. }
  90. else {
  91. ::fwrite("\n",1,1,stdout);
  92. lineStart = true;
  93. }
  94. ::fflush(stdout);
  95. }
  96. //-----------------------------------------------------------------------------
  97. UnitTest::UnitTest() {
  98. _testCount = 0;
  99. _failureCount = 0;
  100. _warningCount = 0;
  101. _lastTestResult = true;
  102. }
  103. void UnitTest::fail(const char* msg)
  104. {
  105. Con::warnf("** Failed: %s",msg);
  106. dFetchAndAdd( _failureCount, 1 );
  107. }
  108. void UnitTest::warn(const char* msg)
  109. {
  110. Con::warnf("** Warning: %s",msg);
  111. dFetchAndAdd( _warningCount, 1 );
  112. }
  113. //-----------------------------------------------------------------------------
  114. TestRegistry::TestRegistry(const char* name, bool interactive, const char *className)
  115. {
  116. // Check that no existing test uses the same class-name; this is guaranteed
  117. // to lead to funkiness.
  118. TestRegistry *walk = _list;
  119. while(walk)
  120. {
  121. if(walk->_className)
  122. {
  123. AssertFatal(dStricmp(className, walk->_className), "TestRegistry::TestRegistry - got two unit tests with identical class names; they must have unique class names!");
  124. }
  125. walk = walk->_next;
  126. }
  127. // Add us to the list.
  128. _next = _list;
  129. _list = this;
  130. // And fill in our fields.
  131. _name = name;
  132. _className = className;
  133. _isInteractive = interactive;
  134. }
  135. DynamicTestRegistration::DynamicTestRegistration( const char *name, UnitTest *test ) : TestRegistry( name, false, NULL ), mUnitTest( test )
  136. {
  137. }
  138. DynamicTestRegistration::~DynamicTestRegistration()
  139. {
  140. // Un-link ourselves from the test registry
  141. TestRegistry *walk = _list;
  142. // Easy case!
  143. if( walk == this )
  144. _list = _next;
  145. else
  146. {
  147. // Search for us and remove
  148. while( ( walk != 0 ) && ( walk->_next != 0 ) && ( walk->_next != this ) )
  149. walk = walk->_next;
  150. // When this loop is broken, walk will be the unit test in the list previous to this one
  151. if( walk != 0 && walk->_next != 0 )
  152. walk->_next = walk->_next->_next;
  153. }
  154. }
  155. //-----------------------------------------------------------------------------
  156. TestRun::TestRun()
  157. {
  158. _subCount = 0;
  159. _testCount = 0;
  160. _failureCount = 0;
  161. _warningCount = 0;
  162. }
  163. void TestRun::printStats()
  164. {
  165. Con::printf("-- %d test%s run (with %d sub-test%s)",
  166. _testCount,(_testCount != 1)? "s": "",
  167. _subCount,(_subCount != 1)? "s": "");
  168. if (_testCount)
  169. {
  170. if (_failureCount)
  171. Con::printf("** %d reported failure%s",
  172. _failureCount,(_failureCount != 1)? "s": "");
  173. else if (_warningCount)
  174. Con::printf("** %d reported warning%s",
  175. _warningCount,(_warningCount != 1)? "s": "");
  176. else
  177. Con::printf("-- No reported failures");
  178. }
  179. }
  180. void TestRun::test(TestRegistry* reg)
  181. {
  182. Con::printf("-- Testing: %s %s",reg->getName(), reg->isInteractive() ? "(interactive)" : "" );
  183. UnitMargin::Push(_Margin[0]);
  184. // Run the test.
  185. UnitTest* test = reg->newTest();
  186. test->run();
  187. UnitMargin::Pop();
  188. // Update stats.
  189. _failureCount += test->getFailureCount();
  190. _subCount += test->getTestCount();
  191. _warningCount += test->getWarningCount();
  192. _testCount++;
  193. // Don't forget to delete the test!
  194. delete test;
  195. }
  196. // [tom, 2/5/2007] To provide a predictable environment for the tests, this
  197. // now changes the current directory to the executable's directory before
  198. // running the tests. The previous current directory is restored on exit.
  199. bool TestRun::test(const char* module, bool skipInteractive)
  200. {
  201. StringTableEntry cwdSave = Platform::getCurrentDirectory();
  202. int len = strlen(module);
  203. const char *skipMsg = skipInteractive ? "(skipping interactive tests)" : "";
  204. // Indicate to the user what we're up to.
  205. if (!len)
  206. Con::printf("-- Running all unit tests %s", skipMsg);
  207. else
  208. Con::printf("-- Running %s tests %s",module, skipMsg);
  209. for (TestRegistry* itr = TestRegistry::getFirst(); itr; itr = itr->getNext())
  210. {
  211. if (!len || !dStrnicmp(module,itr->getName(),len))
  212. {
  213. // Skip the test if it's interactive and we're in skipinteractive mode.
  214. if(skipInteractive && itr->isInteractive())
  215. continue;
  216. // Otherwise, run the test!
  217. Platform::setCurrentDirectory(Platform::getMainDotCsDir());
  218. test(itr);
  219. }
  220. }
  221. // Print out a nice report on how we did.
  222. printStats();
  223. Platform::setCurrentDirectory(cwdSave);
  224. // And indicate our failure situation in the return value.
  225. return !_failureCount;
  226. }
  227. } // Namespace