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.

125 lines
2.0 KiB

  1. //========== Copyright � 2005, Valve Corporation, All rights reserved. ========
  2. //
  3. // Purpose: A simple tool for coverage tests
  4. //
  5. //=============================================================================
  6. #ifndef VCOVER_H
  7. #define VCOVER_H
  8. #include "tier1/utlrbtree.h"
  9. #include "vstdlib.h"
  10. #if defined( _WIN32 )
  11. #pragma once
  12. #endif
  13. class CVCoverage
  14. {
  15. public:
  16. CVCoverage() :
  17. m_bActive( false ),
  18. m_depth( 0 ),
  19. m_token( 1 )
  20. {
  21. }
  22. bool IsActive() const
  23. {
  24. return m_bActive;
  25. }
  26. void SetActive( bool bActive )
  27. {
  28. Assert( bActive != m_bActive );
  29. m_bActive = bActive;
  30. if ( bActive )
  31. ++m_token;
  32. }
  33. void Begin()
  34. {
  35. ++m_depth;
  36. }
  37. void End()
  38. {
  39. --m_depth;
  40. }
  41. void Reset()
  42. {
  43. m_locations.RemoveAll();
  44. }
  45. bool ShouldCover( unsigned token ) const
  46. {
  47. return ( m_bActive && m_depth > 0 && token != m_token );
  48. }
  49. unsigned Cover( const char *pszFile, int line )
  50. {
  51. Location_t location = { pszFile, line };
  52. m_locations.Insert( location );
  53. return m_token;
  54. }
  55. void Report()
  56. {
  57. for ( int i = m_locations.FirstInorder(); i != m_locations.InvalidIndex(); i = m_locations.NextInorder( i ) )
  58. {
  59. Msg( "%s(%d) :\n", m_locations[i].pszFile, m_locations[i].line );
  60. }
  61. }
  62. private:
  63. struct Location_t
  64. {
  65. const char *pszFile;
  66. int line;
  67. };
  68. class CLocationLess
  69. {
  70. public:
  71. CLocationLess( int ignored ) {}
  72. bool operator!() { return false; }
  73. bool operator()( const Location_t &lhs, const Location_t &rhs ) const
  74. {
  75. if ( lhs.line < rhs.line )
  76. {
  77. return true;
  78. }
  79. return CaselessStringLessThan( lhs.pszFile, rhs.pszFile );
  80. }
  81. };
  82. bool m_bActive;
  83. int m_depth;
  84. unsigned m_token;
  85. CUtlRBTree< Location_t, unsigned short, CLocationLess > m_locations;
  86. };
  87. VSTDLIB_INTERFACE CVCoverage g_VCoverage;
  88. #ifdef VCOVER_ENABLED
  89. #define VCOVER() \
  90. do \
  91. { \
  92. static token; \
  93. if ( g_VCoverage.ShouldCover( token ) ) \
  94. { \
  95. token = g_VCoverage.Cover( __FILE__, __LINE__ ); \
  96. } \
  97. } while( 0 )
  98. #else
  99. #define VCOVER() ((void)0)
  100. #endif
  101. #endif // VCOVER_H