home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stl2vac.zip / STLport-4_5_3.zip / STLport-4.5.3 / test / eh / nc_alloc.h < prev    next >
C/C++ Source or Header  |  2000-12-07  |  5KB  |  198 lines

  1. /***********************************************************************************
  2.     TestController.h
  3.     
  4.         SUMMARY: An "faux-singleton" object to encapsulate a hodgepodge of state and
  5.             functionality relating to the test suite. Probably should be broken
  6.             into smaller pieces.
  7.  
  8.  * Copyright (c) 1997
  9.  * Mark of the Unicorn, Inc.
  10.  *
  11.  * Permission to use, copy, modify, distribute and sell this software
  12.  * and its documentation for any purpose is hereby granted without fee,
  13.  * provided that the above copyright notice appear in all copies and
  14.  * that both that copyright notice and this permission notice appear
  15.  * in supporting documentation.  Mark of the Unicorn makes no
  16.  * representations about the suitability of this software for any
  17.  * purpose.  It is provided "as is" without express or implied warranty.
  18.         
  19. ***********************************************************************************/
  20. #if !INCLUDED_MOTU_nc_alloc
  21. #define INCLUDED_MOTU_nc_alloc 1
  22.  
  23. #include "Prefix.h"
  24.  
  25. # if defined (EH_NEW_HEADERS)
  26. #  include <utility>
  27. # else
  28. #  include <pair.h>
  29. # endif
  30.  
  31. extern long alloc_count;
  32. extern long object_count;
  33.  
  34. struct TestController
  35. {
  36.     // Report that the current test has succeeded.
  37.     static void ReportSuccess(int);
  38.  
  39.     //
  40.     // Leak detection
  41.     //
  42.     
  43.     // Turn the recording of the addresses of individual allocated
  44.     // blocks on or off. If not called, allocations will only be
  45.     // counted, but deallocations won't be checked for validity.
  46.     static void TrackAllocations( bool );
  47.     static bool TrackingEnabled();
  48.     
  49.     // Call this to begin a new leak-detection cycle. Resets all
  50.     // allocation counts, etc.
  51.     static void BeginLeakDetection();
  52.     
  53.     // Returns true iff leak detection is currently in effect
  54.     static bool LeakDetectionEnabled();
  55.     
  56.     // Ends leak detection and reports any resource leaks.
  57.     // Returns true if any occurred.
  58.     static bool ReportLeaked();
  59.  
  60.     //
  61.     // Exception-safety
  62.     //
  63.     
  64.     // Don't test for exception-safety
  65.     static void TurnOffExceptions();
  66.     
  67.     // Set operator new to fail on the nth invocation
  68.     static void SetFailureCountdown( long n );
  69.     
  70.     // Set operator new to never fail.
  71.     static void CancelFailureCountdown();
  72.  
  73.     // Throws an exception if the count has been reached. Call this
  74.     // before every operation that might fail in the real world.
  75.     static void maybe_fail(long);
  76.  
  77.     //
  78.     // Managing verbose feedback.
  79.     //
  80.  
  81.     // Call to begin a strong, weak, or const test. If verbose
  82.     // reporting is enabled, prints the test category.
  83.     static void SetCurrentTestCategory( const char* str );
  84.  
  85.     // Call to set the name of the container being tested.
  86.     static void SetCurrentContainer( const char* str );
  87.  
  88.     // Sets the name of the current test.
  89.     static void SetCurrentTestName(const char* str);
  90.  
  91.     // Turn verbose reporting on or off.
  92.     static void SetVerbose(bool val);
  93.  
  94. private:
  95.     enum { kNotInExceptionTest = -1 };
  96.     
  97.     static void ClearAllocationSet();
  98.     static void EndLeakDetection();
  99.     static void PrintTestName( bool err=false );
  100.     
  101.     static long& Failure_threshold();
  102.     static long possible_failure_count;
  103.     static const char* current_test;
  104.     static const char* current_test_category;
  105.     static const char* current_container;
  106.     static bool  nc_verbose;
  107.     static bool  never_fail;
  108.     static bool track_allocations;
  109.     static bool leak_detection_enabled;
  110. };
  111.  
  112. extern TestController gTestController;
  113.  
  114. //
  115. // inline implementations
  116. //
  117.  
  118. inline void simulate_possible_failure()
  119. {
  120.    gTestController.maybe_fail(0);
  121. }
  122.  
  123. inline void simulate_constructor()
  124. {
  125.    gTestController.maybe_fail(0);
  126.    object_count++;
  127. }
  128.  
  129. inline void simulate_destructor()
  130. {
  131.    object_count--;
  132. }
  133.  
  134. inline void TestController::TrackAllocations( bool track )
  135. {
  136.     track_allocations = track;
  137. }
  138.  
  139. inline bool TestController::TrackingEnabled()
  140. {
  141.     return track_allocations;
  142. }
  143.  
  144. inline void TestController::SetFailureCountdown(long count) {
  145.     Failure_threshold() = count;
  146.     possible_failure_count = 0;
  147. }
  148.  
  149. inline void TestController::CancelFailureCountdown() {
  150.     Failure_threshold() = kNotInExceptionTest;
  151. }
  152.  
  153. inline void TestController::BeginLeakDetection()
  154. {
  155.     alloc_count = 0;
  156.     object_count = 0;
  157.        ClearAllocationSet();
  158.        leak_detection_enabled = true;
  159. }
  160.  
  161. inline bool TestController::LeakDetectionEnabled()
  162. {
  163.     return leak_detection_enabled;
  164. }
  165.  
  166. inline void TestController::EndLeakDetection()
  167. {
  168.     leak_detection_enabled = false;
  169. }
  170.  
  171. inline void TestController::SetCurrentTestCategory(const char* str)
  172. {
  173.     current_test_category=str;
  174.     if (nc_verbose)
  175.         PrintTestName();
  176. }
  177.  
  178. inline void TestController::SetCurrentContainer(const char* str) {
  179.     current_container=str;
  180. }
  181.  
  182. inline void TestController::SetCurrentTestName(const char* str)
  183. {
  184.     current_test=str;
  185. }
  186.  
  187. inline void TestController::SetVerbose(bool val)
  188. {
  189.     nc_verbose=val;
  190. }
  191.  
  192. inline void TestController::TurnOffExceptions()
  193. {
  194.     never_fail = true;
  195. }
  196.  
  197. #endif // INCLUDED_MOTU_nc_alloc
  198.