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.

156 lines
3.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "unitlib/unitlib.h"
  9. #include "tier0/dbg.h"
  10. #include <string.h>
  11. #include "memdbgon.h"
  12. //-----------------------------------------------------------------------------
  13. //
  14. // Base class for test cases
  15. //
  16. //-----------------------------------------------------------------------------
  17. CTestCase::CTestCase( char const* pName, ITestSuite* pParent )
  18. {
  19. Assert( pName );
  20. m_pName = new char[strlen(pName) + 1];
  21. strcpy( m_pName, pName );
  22. // Only install the test case if it has no parent
  23. if (pParent)
  24. {
  25. pParent->AddTest( this );
  26. }
  27. else
  28. {
  29. UnitTestInstallTestCase( this );
  30. }
  31. }
  32. CTestCase::~CTestCase()
  33. {
  34. if (m_pName)
  35. delete[] m_pName;
  36. }
  37. char const* CTestCase::GetName()
  38. {
  39. return m_pName;
  40. }
  41. //-----------------------------------------------------------------------------
  42. //
  43. // Test suite class
  44. //
  45. //-----------------------------------------------------------------------------
  46. CTestSuite::CTestSuite( char const* pName, ITestSuite* pParent )
  47. {
  48. m_TestCount = 0;
  49. m_ppTestCases = 0;
  50. m_pName = new char[strlen(pName) + 1];
  51. strcpy( m_pName, pName );
  52. // Only install the test case if it has no parent
  53. if (pParent)
  54. {
  55. pParent->AddTest( this );
  56. }
  57. else
  58. {
  59. UnitTestInstallTestCase( this );
  60. }
  61. }
  62. CTestSuite::~CTestSuite()
  63. {
  64. if (m_ppTestCases)
  65. free(m_ppTestCases);
  66. if (m_pName)
  67. delete[] m_pName;
  68. }
  69. char const* CTestSuite::GetName()
  70. {
  71. return m_pName;
  72. }
  73. void CTestSuite::AddTest( ITestCase* pTest )
  74. {
  75. Assert( pTest );
  76. if (!m_ppTestCases)
  77. {
  78. m_ppTestCases = (ITestCase**)malloc( sizeof(ITestCase**) );
  79. }
  80. else
  81. {
  82. m_ppTestCases = (ITestCase**)realloc( m_ppTestCases, (m_TestCount+1) * sizeof(ITestCase**) );
  83. }
  84. m_ppTestCases[m_TestCount++] = pTest;
  85. }
  86. void CTestSuite::RunTest()
  87. {
  88. for ( int i = 0; i < m_TestCount; ++i )
  89. {
  90. m_ppTestCases[i]->RunTest();
  91. }
  92. }
  93. //-----------------------------------------------------------------------------
  94. // This is the main function exported by the unit test library used by
  95. // unit test DLLs to install their test cases into a list to be run
  96. //-----------------------------------------------------------------------------
  97. static int s_TestCount = 0;
  98. static int s_TestAllocated = 0;
  99. static ITestCase** s_ppTestCases = 0;
  100. void UnitTestInstallTestCase( ITestCase* pTest )
  101. {
  102. Assert( pTest );
  103. if (s_TestCount == s_TestAllocated)
  104. {
  105. if (!s_ppTestCases)
  106. {
  107. s_ppTestCases = (ITestCase**)malloc( 16 * sizeof(ITestCase**) );
  108. s_TestAllocated = 16;
  109. }
  110. else
  111. {
  112. s_ppTestCases = (ITestCase**)realloc( s_ppTestCases, s_TestAllocated * 2 * sizeof(ITestCase**) );
  113. s_TestAllocated *= 2;
  114. }
  115. }
  116. s_ppTestCases[s_TestCount++] = pTest;
  117. }
  118. //-----------------------------------------------------------------------------
  119. // These are the methods used by the unit test running program to run all tests
  120. //-----------------------------------------------------------------------------
  121. int UnitTestCount()
  122. {
  123. return s_TestCount;
  124. }
  125. ITestCase* GetUnitTest( int i )
  126. {
  127. Assert( i < s_TestCount );
  128. return s_ppTestCases[i];
  129. }