Update contrib.
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
8 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
11 * Mark of the Unicorn, Inc.
13 * Permission to use, copy, modify, distribute and sell this software
14 * and its documentation for any purpose is hereby granted without fee,
15 * provided that the above copyright notice appear in all copies and
16 * that both that copyright notice and this permission notice appear
17 * in supporting documentation. Mark of the Unicorn makes no
18 * representations about the suitability of this software for any
19 * purpose. It is provided "as is" without express or implied warranty.
21 ***********************************************************************************/
22 #if !INCLUDED_MOTU_nc_alloc
23 #define INCLUDED_MOTU_nc_alloc 1
27 #if defined (EH_NEW_HEADERS)
33 extern long alloc_count;
34 extern long object_count;
37 class TestController {
39 // Report that the current test has succeeded.
40 static void ReportSuccess(int);
46 // Turn the recording of the addresses of individual allocated
47 // blocks on or off. If not called, allocations will only be
48 // counted, but deallocations won't be checked for validity.
49 static void TrackAllocations( bool );
50 static bool TrackingEnabled();
52 // Call this to begin a new leak-detection cycle. Resets all
53 // allocation counts, etc.
54 static void BeginLeakDetection();
56 // Returns true iff leak detection is currently in effect
57 static bool LeakDetectionEnabled();
59 // Ends leak detection and reports any resource leaks.
60 // Returns true if any occurred.
61 static bool ReportLeaked();
67 // Don't test for exception-safety
68 static void TurnOffExceptions();
70 // Set operator new to fail on the nth invocation
71 static void SetFailureCountdown( long n );
73 // Set operator new to never fail.
74 static void CancelFailureCountdown();
76 // Throws an exception if the count has been reached. Call this
77 // before every operation that might fail in the real world.
78 static void maybe_fail(long);
81 // Managing verbose feedback.
84 // Call to begin a strong, weak, or const test. If verbose
85 // reporting is enabled, prints the test category.
86 static void SetCurrentTestCategory( const char* str );
88 // Call to set the name of the container being tested.
89 static void SetCurrentContainer( const char* str );
91 // Sets the name of the current test.
92 static void SetCurrentTestName(const char* str);
94 // Turn verbose reporting on or off.
95 static void SetVerbose(bool val);
98 enum { kNotInExceptionTest = -1 };
100 static void ClearAllocationSet();
101 static void EndLeakDetection();
102 static void PrintTestName( bool err=false );
104 static long& Failure_threshold();
105 static long possible_failure_count;
106 static const char* current_test;
107 static const char* current_test_category;
108 static const char* current_container;
109 static bool nc_verbose;
110 static bool never_fail;
111 static bool track_allocations;
112 static bool leak_detection_enabled;
115 extern TestController gTestController;
119 // inline implementations
121 inline void simulate_possible_failure() {
122 gTestController.maybe_fail(0);
125 inline void simulate_constructor() {
126 gTestController.maybe_fail(0);
130 inline void simulate_destructor() {
133 #if 1 //enable/disable inline functions
134 inline void TestController::TrackAllocations(bool track) {
135 track_allocations = track;
138 inline bool TestController::TrackingEnabled() {
139 return track_allocations;
142 inline void TestController::SetFailureCountdown(long count) {
143 Failure_threshold() = count;
144 possible_failure_count = 0;
147 inline void TestController::CancelFailureCountdown() {
148 Failure_threshold() = kNotInExceptionTest;
151 inline void TestController::BeginLeakDetection() {
154 ClearAllocationSet();
155 leak_detection_enabled = true;
158 inline bool TestController::LeakDetectionEnabled() {
159 return leak_detection_enabled;
162 inline void TestController::EndLeakDetection() {
163 leak_detection_enabled = false;
166 inline void TestController::SetCurrentTestCategory(const char* str) {
167 current_test_category = str;
172 inline void TestController::SetCurrentContainer(const char* str) {
173 current_container=str;
176 inline void TestController::SetCurrentTestName(const char* str) {
180 inline void TestController::SetVerbose(bool val) {
184 inline void TestController::TurnOffExceptions() {
188 #endif // INCLUDED_MOTU_nc_alloc