Team Fortress 2 Source Code as on 22/4/2020
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.

154 lines
3.1 KiB

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