nc_alloc.h

00001 /***********************************************************************************
00002         TestController.h
00003         
00004                 SUMMARY: An "faux-singleton" object to encapsulate a hodgepodge of state and
00005                         functionality relating to the test suite. Probably should be broken
00006                         into smaller pieces.
00007 
00008  * Copyright (c) 1997
00009  * Mark of the Unicorn, Inc.
00010  *
00011  * Permission to use, copy, modify, distribute and sell this software
00012  * and its documentation for any purpose is hereby granted without fee,
00013  * provided that the above copyright notice appear in all copies and
00014  * that both that copyright notice and this permission notice appear
00015  * in supporting documentation.  Mark of the Unicorn makes no
00016  * representations about the suitability of this software for any
00017  * purpose.  It is provided "as is" without express or implied warranty.
00018                 
00019 ***********************************************************************************/
00020 #if !INCLUDED_MOTU_nc_alloc
00021 #define INCLUDED_MOTU_nc_alloc 1
00022 
00023 #include "Prefix.h"
00024 
00025 # if defined (EH_NEW_HEADERS)
00026 #  include <utility>
00027 # else
00028 #  include <pair.h>
00029 # endif
00030 
00031 extern long alloc_count;
00032 extern long object_count;
00033 
00034 struct TestController
00035 {
00036     // Report that the current test has succeeded.
00037     static void ReportSuccess(int);
00038 
00039         //
00040         // Leak detection
00041         //
00042         
00043         // Turn the recording of the addresses of individual allocated
00044         // blocks on or off. If not called, allocations will only be
00045         // counted, but deallocations won't be checked for validity.
00046         static void TrackAllocations( bool );
00047         static bool TrackingEnabled();
00048         
00049         // Call this to begin a new leak-detection cycle. Resets all
00050         // allocation counts, etc.
00051     static void BeginLeakDetection();
00052         
00053         // Returns true iff leak detection is currently in effect
00054         static bool LeakDetectionEnabled();
00055         
00056         // Ends leak detection and reports any resource leaks.
00057         // Returns true if any occurred.
00058     static bool ReportLeaked();
00059 
00060         //
00061         // Exception-safety
00062         //
00063         
00064         // Don't test for exception-safety
00065     static void TurnOffExceptions();
00066     
00067         // Set operator new to fail on the nth invocation
00068     static void SetFailureCountdown( long n );
00069         
00070         // Set operator new to never fail.
00071     static void CancelFailureCountdown();
00072 
00073         // Throws an exception if the count has been reached. Call this
00074         // before every operation that might fail in the real world.
00075     static void maybe_fail(long);
00076 
00077         //
00078         // Managing verbose feedback.
00079         //
00080 
00081         // Call to begin a strong, weak, or const test. If verbose
00082         // reporting is enabled, prints the test category.
00083     static void SetCurrentTestCategory( const char* str );
00084 
00085         // Call to set the name of the container being tested.
00086     static void SetCurrentContainer( const char* str );
00087 
00088         // Sets the name of the current test.
00089     static void SetCurrentTestName(const char* str);
00090 
00091     // Turn verbose reporting on or off.
00092     static void SetVerbose(bool val);
00093 
00094 private:
00095         enum { kNotInExceptionTest = -1 };
00096         
00097         static void ClearAllocationSet();
00098     static void EndLeakDetection();
00099         static void PrintTestName( bool err=false );
00100         
00101     static long& Failure_threshold();
00102         static long possible_failure_count;
00103     static const char* current_test;
00104     static const char* current_test_category;
00105     static const char* current_container;
00106     static bool  nc_verbose;
00107     static bool  never_fail;
00108     static bool track_allocations;
00109     static bool leak_detection_enabled;
00110 };
00111 
00112 extern TestController gTestController;
00113 
00114 //
00115 // inline implementations
00116 //
00117 
00118 inline void simulate_possible_failure()
00119 {
00120    gTestController.maybe_fail(0);
00121 }
00122 
00123 inline void simulate_constructor()
00124 {
00125    gTestController.maybe_fail(0);
00126    object_count++;
00127 }
00128 
00129 inline void simulate_destructor()
00130 {
00131    object_count--;
00132 }
00133 
00134 inline void TestController::TrackAllocations( bool track )
00135 {
00136         track_allocations = track;
00137 }
00138 
00139 inline bool TestController::TrackingEnabled()
00140 {
00141         return track_allocations;
00142 }
00143 
00144 inline void TestController::SetFailureCountdown(long count) {
00145     Failure_threshold() = count;
00146         possible_failure_count = 0;
00147 }
00148 
00149 inline void TestController::CancelFailureCountdown() {
00150     Failure_threshold() = kNotInExceptionTest;
00151 }
00152 
00153 inline void TestController::BeginLeakDetection()
00154 {
00155     alloc_count = 0;
00156     object_count = 0;
00157         ClearAllocationSet();
00158         leak_detection_enabled = true;
00159 }
00160 
00161 inline bool TestController::LeakDetectionEnabled()
00162 {
00163         return leak_detection_enabled;
00164 }
00165 
00166 inline void TestController::EndLeakDetection()
00167 {
00168         leak_detection_enabled = false;
00169 }
00170 
00171 inline void TestController::SetCurrentTestCategory(const char* str)
00172 {
00173     current_test_category=str;
00174     if (nc_verbose)
00175         PrintTestName();
00176 }
00177 
00178 inline void TestController::SetCurrentContainer(const char* str) {
00179     current_container=str;
00180 }
00181 
00182 inline void TestController::SetCurrentTestName(const char* str)
00183 {
00184     current_test=str;
00185 }
00186 
00187 inline void TestController::SetVerbose(bool val)
00188 {
00189         nc_verbose=val;
00190 }
00191 
00192 inline void TestController::TurnOffExceptions()
00193 {
00194         never_fail = true;
00195 }
00196 
00197 #endif // INCLUDED_MOTU_nc_alloc

Generated on Mon Jun 5 10:20:48 2006 for Intelligence.kdevelop by  doxygen 1.4.6