Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
8.7 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef UNITLIB_H
  9. #define UNITLIB_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "tier0/platform.h"
  14. //-----------------------------------------------------------------------------
  15. // Usage model for the UnitTest library
  16. //
  17. // The general methodology here is that clients are expected to create unit
  18. // test DLLs that statically link to the unit test DLL and which implement
  19. // tests of various libraries. The unit test application will dynamically
  20. // load all DLLs in a directory, which causes them to install their test cases
  21. // into the unit test system. The application then runs through all tests and
  22. // executes them all, displaying the results.
  23. //
  24. // *** NOTE: The test suites are compiled in both debug and release builds,
  25. // even though it's expected to only be useful in debug builds. This is because
  26. // I couldn't come up with a good way of disabling the code in release builds.
  27. // (The only options I could come up with would still compile in the functions,
  28. // just not install them into the unit test library, or would make it so that
  29. // you couldn't step through the unit test code).
  30. //
  31. // Even though this is the case, there's no reason not to add test cases
  32. // directly into your shipping DLLs, as long as you surround the code with
  33. // #ifdef _DEBUG. To error check a project to make sure it's not compiling
  34. // in unit tests in Release build, just don't link in unitlib.lib in Release.
  35. // You can of course also put your test suites into separate DLLs.
  36. //
  37. // All tests inherit from the ITestCase interface. There are two major kinds
  38. // of tests; the first is a single test case meant to run a piece of
  39. // code and check its results match expected values using the Assert macros.
  40. // The second kind is a test suite which is simply a list of other tests.
  41. //
  42. // The following classes and macros are used to easily create unit test cases
  43. // and suites:
  44. //
  45. // Use DEFINE_TESTSUITE to define a particular test suite, and DEFINE_TESTCASE
  46. // to add as many test cases as you like to that test suite, as follows:
  47. //
  48. // DEFINE_TESTSUITE( VectorTestSuite )
  49. //
  50. // DEFINE_TESTCASE( VectorAdditionTest, VectorTestSuite )
  51. // {
  52. // .. test code here ..
  53. // }
  54. //
  55. // Note that the definition of the test suite can occur in a different file
  56. // as the test case. A link error will occur if the test suite to which a
  57. // test case is added has not been defined.
  58. //
  59. // To create a test case that is not part of a suite, use...
  60. //
  61. // DEFINE_TESTCASE_NOSUITE( VectorAdditionTest )
  62. // {
  63. // .. test code here ..
  64. // }
  65. //
  66. // You can also create a suite which is a child of another suite using
  67. //
  68. // DEFINE_SUBSUITE( VectorTestSuite, MathTestSuite )
  69. //
  70. //-----------------------------------------------------------------------------
  71. //-----------------------------------------------------------------------------
  72. // dll export stuff
  73. //-----------------------------------------------------------------------------
  74. #ifdef TIER0_DLL_EXPORT
  75. #define UNITLIB_DLL_EXPORT
  76. #endif
  77. #ifdef UNITLIB_DLL_EXPORT
  78. #define UNITLIB_INTERFACE DLL_EXPORT
  79. #define UNITLIB_CLASS_INTERFACE DLL_CLASS_EXPORT
  80. #define UNITLIB_GLOBAL_INTERFACE DLL_GLOBAL_EXPORT
  81. #else
  82. #define UNITLIB_INTERFACE DLL_IMPORT
  83. #define UNITLIB_CLASS_INTERFACE DLL_CLASS_IMPORT
  84. #define UNITLIB_GLOBAL_INTERFACE DLL_GLOBAL_IMPORT
  85. #endif
  86. //-----------------------------------------------------------------------------
  87. // All unit test libraries can be asked for a unit test
  88. // AppSystem to perform connection
  89. //-----------------------------------------------------------------------------
  90. #define UNITTEST_INTERFACE_VERSION "UnitTestV001"
  91. //-----------------------------------------------------------------------------
  92. //
  93. // NOTE: All classes and interfaces below you shouldn't use directly.
  94. // Use the DEFINE_TESTSUITE and DEFINE_TESTCASE macros instead.
  95. //
  96. //-----------------------------------------------------------------------------
  97. //-----------------------------------------------------------------------------
  98. // Test case + suite interface
  99. //-----------------------------------------------------------------------------
  100. class ITestCase
  101. {
  102. public:
  103. // This returns the test name
  104. virtual char const* GetName() = 0;
  105. // This runs the test
  106. virtual void RunTest() = 0;
  107. };
  108. class ITestSuite : public ITestCase
  109. {
  110. public:
  111. // Add a test to the suite...
  112. virtual void AddTest( ITestCase* pTest ) = 0;
  113. };
  114. //-----------------------------------------------------------------------------
  115. // This is the main function exported by the unit test library used by
  116. // unit test DLLs to install their test cases into a list to be run
  117. //-----------------------------------------------------------------------------
  118. UNITLIB_INTERFACE void UnitTestInstallTestCase( ITestCase* pTest );
  119. //-----------------------------------------------------------------------------
  120. // These are the methods used by the unit test running program to run all tests
  121. //-----------------------------------------------------------------------------
  122. UNITLIB_INTERFACE int UnitTestCount();
  123. UNITLIB_INTERFACE ITestCase* GetUnitTest( int i );
  124. //-----------------------------------------------------------------------------
  125. // Helper for unit test DLLs to expose IAppSystems
  126. //-----------------------------------------------------------------------------
  127. #define USE_UNITTEST_APPSYSTEM( _className ) \
  128. static _className s_UnitTest ## _className; \
  129. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( _className, IAppSystem, UNITTEST_INTERFACE_VERSION, s_UnitTest ## _className );
  130. //-----------------------------------------------------------------------------
  131. // Base class for test cases
  132. //-----------------------------------------------------------------------------
  133. class UNITLIB_CLASS_INTERFACE CTestCase : public ITestCase
  134. {
  135. public:
  136. CTestCase( char const* pName, ITestSuite* pParent = 0 );
  137. ~CTestCase();
  138. // Returns the test name
  139. char const* GetName();
  140. private:
  141. char* m_pName;
  142. };
  143. //-----------------------------------------------------------------------------
  144. // Test suite class
  145. //-----------------------------------------------------------------------------
  146. class UNITLIB_CLASS_INTERFACE CTestSuite : public ITestSuite
  147. {
  148. public:
  149. CTestSuite( char const* pName, ITestSuite* pParent = 0 );
  150. ~CTestSuite();
  151. // This runs the test
  152. void RunTest();
  153. // Add a test to the suite...
  154. void AddTest( ITestCase* pTest );
  155. // Returns the test name
  156. char const* GetName();
  157. protected:
  158. int m_TestCount;
  159. ITestCase** m_ppTestCases;
  160. char* m_pName;
  161. };
  162. #define TESTSUITE_CLASS( _suite ) \
  163. class CTS ## _suite : public CTestSuite \
  164. { \
  165. public: \
  166. CTS ## _suite(); \
  167. };
  168. #define TESTSUITE_ACCESSOR( _suite ) \
  169. CTS ## _suite* GetTS ## _suite() \
  170. { \
  171. static CTS ## _suite s_TS ## _suite; \
  172. return &s_TS ## _suite; \
  173. }
  174. #define FWD_DECLARE_TESTSUITE( _suite ) \
  175. class CTS ## _suite; \
  176. CTS ## _suite* GetTS ## _suite();
  177. #define DEFINE_TESTSUITE( _suite ) \
  178. TESTSUITE_CLASS( _suite ) \
  179. TESTSUITE_ACCESSOR( _suite ) \
  180. CTS ## _suite::CTS ## _suite() : CTestSuite( #_suite ) {}
  181. #define DEFINE_SUBSUITE( _suite, _parent ) \
  182. TESTSUITE_CLASS( _suite ) \
  183. TESTSUITE_ACCESSOR( _suite ) \
  184. FWD_DECLARE_TESTSUITE( _parent ) \
  185. CTS ## _suite::CTS ## _suite() : CTestSuite( #_suite, GetTS ## _parent() ) {}
  186. #define TESTCASE_CLASS( _case ) \
  187. class CTC ## _case : public CTestCase \
  188. { \
  189. public: \
  190. CTC ## _case (); \
  191. void RunTest(); \
  192. };
  193. #define DEFINE_TESTCASE_NOSUITE( _case ) \
  194. TESTCASE_CLASS( _case ) \
  195. CTC ## _case::CTC ## _case () : CTestCase( #_case ) {} \
  196. \
  197. CTC ## _case s_TC ## _case; \
  198. \
  199. void CTC ## _case ::RunTest()
  200. #define DEFINE_TESTCASE( _case, _suite ) \
  201. TESTCASE_CLASS( _case ) \
  202. FWD_DECLARE_TESTSUITE( _suite ) \
  203. CTC ## _case::CTC ## _case () : CTestCase( #_case, GetTS ## _suite() ) {} \
  204. \
  205. CTC ## _case s_TC ## _case; \
  206. \
  207. void CTC ## _case ::RunTest()
  208. #define _Shipping_AssertMsg( _exp, _msg, _executeExp, _bFatal ) \
  209. do { \
  210. if (!(_exp)) \
  211. { \
  212. LoggingResponse_t ret = Log_Assert( "%s (%d) : %s\n", __TFILE__, __LINE__, _msg ); \
  213. _executeExp; \
  214. if ( ret == LR_DEBUGGER ) \
  215. { \
  216. if ( !ShouldUseNewAssertDialog() || DoNewAssertDialog( __TFILE__, __LINE__, _msg ) ) \
  217. DebuggerBreak(); \
  218. if ( _bFatal ) \
  219. _ExitOnFatalAssert( __TFILE__, __LINE__ ); \
  220. } \
  221. } \
  222. } while (0)
  223. #define Shipping_Assert( _exp ) _Shipping_AssertMsg( _exp, _T("Assertion Failed: ") _T(#_exp), ((void)0), false )
  224. #endif // UNITLIB_H