1 /***********************************************************************************
4 SUMMARY: An "faux-singleton" object to encapsulate a hodgepodge of state and
5 functionality relating to the test suite. Probably should be broken
9 * Mark of the Unicorn, Inc.
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.
19 ***********************************************************************************/
20 #if !INCLUDED_MOTU_nc_alloc
21 #define INCLUDED_MOTU_nc_alloc 1
25 #if defined (EH_NEW_HEADERS)
31 extern long alloc_count
;
32 extern long object_count
;
34 struct TestController
{
35 // Report that the current test has succeeded.
36 static void ReportSuccess(int);
42 // Turn the recording of the addresses of individual allocated
43 // blocks on or off. If not called, allocations will only be
44 // counted, but deallocations won't be checked for validity.
45 static void TrackAllocations( bool );
46 static bool TrackingEnabled();
48 // Call this to begin a new leak-detection cycle. Resets all
49 // allocation counts, etc.
50 static void BeginLeakDetection();
52 // Returns true iff leak detection is currently in effect
53 static bool LeakDetectionEnabled();
55 // Ends leak detection and reports any resource leaks.
56 // Returns true if any occurred.
57 static bool ReportLeaked();
63 // Don't test for exception-safety
64 static void TurnOffExceptions();
66 // Set operator new to fail on the nth invocation
67 static void SetFailureCountdown( long n
);
69 // Set operator new to never fail.
70 static void CancelFailureCountdown();
72 // Throws an exception if the count has been reached. Call this
73 // before every operation that might fail in the real world.
74 static void maybe_fail(long);
77 // Managing verbose feedback.
80 // Call to begin a strong, weak, or const test. If verbose
81 // reporting is enabled, prints the test category.
82 static void SetCurrentTestCategory( const char* str
);
84 // Call to set the name of the container being tested.
85 static void SetCurrentContainer( const char* str
);
87 // Sets the name of the current test.
88 static void SetCurrentTestName(const char* str
);
90 // Turn verbose reporting on or off.
91 static void SetVerbose(bool val
);
94 enum { kNotInExceptionTest
= -1 };
96 static void ClearAllocationSet();
97 static void EndLeakDetection();
98 static void PrintTestName( bool err
=false );
100 static long& Failure_threshold();
101 static long possible_failure_count
;
102 static const char* current_test
;
103 static const char* current_test_category
;
104 static const char* current_container
;
105 static bool nc_verbose
;
106 static bool never_fail
;
107 static bool track_allocations
;
108 static bool leak_detection_enabled
;
111 extern TestController gTestController
;
114 // inline implementations
117 inline void simulate_possible_failure() {
118 gTestController
.maybe_fail(0);
121 inline void simulate_constructor() {
122 gTestController
.maybe_fail(0);
126 inline void simulate_destructor() {
130 inline void TestController::TrackAllocations(bool track
) {
131 track_allocations
= track
;
134 inline bool TestController::TrackingEnabled() {
135 return track_allocations
;
138 inline void TestController::SetFailureCountdown(long count
) {
139 Failure_threshold() = count
;
140 possible_failure_count
= 0;
143 inline void TestController::CancelFailureCountdown() {
144 Failure_threshold() = kNotInExceptionTest
;
147 inline void TestController::BeginLeakDetection() {
150 ClearAllocationSet();
151 leak_detection_enabled
= true;
154 inline bool TestController::LeakDetectionEnabled() {
155 return leak_detection_enabled
;
158 inline void TestController::EndLeakDetection() {
159 leak_detection_enabled
= false;
162 inline void TestController::SetCurrentTestCategory(const char* str
) {
163 current_test_category
= str
;
168 inline void TestController::SetCurrentContainer(const char* str
) {
169 current_container
=str
;
172 inline void TestController::SetCurrentTestName(const char* str
) {
176 inline void TestController::SetVerbose(bool val
) {
180 inline void TestController::TurnOffExceptions() {
184 #endif // INCLUDED_MOTU_nc_alloc