genericopenlibs/cppstdlib/stl/test/eh/nc_alloc.h
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     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  * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     9  *
       
    10  * Copyright (c) 1997
       
    11  * Mark of the Unicorn, Inc.
       
    12  *
       
    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.
       
    20 
       
    21 ***********************************************************************************/
       
    22 #if !INCLUDED_MOTU_nc_alloc
       
    23 #define INCLUDED_MOTU_nc_alloc 1
       
    24 
       
    25 #include "Prefix.h"
       
    26 
       
    27 #if defined (EH_NEW_HEADERS)
       
    28 #  include <utility>
       
    29 #else
       
    30 #  include <pair.h>
       
    31 #endif
       
    32 
       
    33 extern long alloc_count;
       
    34 extern long object_count;
       
    35 
       
    36 //struct 
       
    37 class TestController {
       
    38 public:
       
    39   // Report that the current test has succeeded.
       
    40   static void ReportSuccess(int);
       
    41 
       
    42   //
       
    43   // Leak detection
       
    44   //
       
    45 
       
    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();
       
    51 
       
    52   // Call this to begin a new leak-detection cycle. Resets all
       
    53   // allocation counts, etc.
       
    54   static void BeginLeakDetection();
       
    55 
       
    56   // Returns true iff leak detection is currently in effect
       
    57   static bool LeakDetectionEnabled();
       
    58 
       
    59   // Ends leak detection and reports any resource leaks.
       
    60   // Returns true if any occurred.
       
    61   static bool ReportLeaked();
       
    62 
       
    63   //
       
    64   // Exception-safety
       
    65   //
       
    66 
       
    67   // Don't test for exception-safety
       
    68   static void TurnOffExceptions();
       
    69 
       
    70   // Set operator new to fail on the nth invocation
       
    71   static void SetFailureCountdown( long n );
       
    72 
       
    73   // Set operator new to never fail.
       
    74   static void CancelFailureCountdown();
       
    75 
       
    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);
       
    79 
       
    80   //
       
    81   // Managing verbose feedback.
       
    82   //
       
    83 
       
    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 );
       
    87 
       
    88   // Call to set the name of the container being tested.
       
    89   static void SetCurrentContainer( const char* str );
       
    90 
       
    91   // Sets the name of the current test.
       
    92   static void SetCurrentTestName(const char* str);
       
    93 
       
    94   // Turn verbose reporting on or off.
       
    95   static void SetVerbose(bool val);
       
    96 
       
    97 private:
       
    98   enum { kNotInExceptionTest = -1 };
       
    99 
       
   100   static void ClearAllocationSet();
       
   101   static void EndLeakDetection();
       
   102   static void PrintTestName( bool err=false );
       
   103 
       
   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;
       
   113 };
       
   114 
       
   115 extern TestController gTestController;
       
   116 
       
   117 
       
   118 //
       
   119 // inline implementations
       
   120 //
       
   121 inline void simulate_possible_failure() {
       
   122   gTestController.maybe_fail(0);
       
   123 }
       
   124 
       
   125 inline void simulate_constructor() {
       
   126   gTestController.maybe_fail(0);
       
   127   ++object_count;
       
   128 }
       
   129 
       
   130 inline void simulate_destructor() {
       
   131   --object_count;
       
   132 }
       
   133 #if 1  //enable/disable inline functions
       
   134 inline void TestController::TrackAllocations(bool track) {
       
   135   track_allocations = track;
       
   136 }
       
   137 
       
   138 inline bool TestController::TrackingEnabled() {
       
   139   return track_allocations;
       
   140 }
       
   141 
       
   142  inline void TestController::SetFailureCountdown(long count) {
       
   143   Failure_threshold() = count;
       
   144   possible_failure_count = 0;
       
   145 }
       
   146 
       
   147 inline void TestController::CancelFailureCountdown() {
       
   148   Failure_threshold() = kNotInExceptionTest;
       
   149 }
       
   150 
       
   151 inline void TestController::BeginLeakDetection() {
       
   152   alloc_count = 0;
       
   153   object_count = 0;
       
   154   ClearAllocationSet();
       
   155   leak_detection_enabled = true;
       
   156 }
       
   157 
       
   158 inline bool TestController::LeakDetectionEnabled() {
       
   159   return leak_detection_enabled;
       
   160 }
       
   161 
       
   162 inline void TestController::EndLeakDetection() {
       
   163   leak_detection_enabled = false;
       
   164 }
       
   165 
       
   166 inline void TestController::SetCurrentTestCategory(const char* str) {
       
   167   current_test_category = str;
       
   168   if (nc_verbose)
       
   169     PrintTestName();
       
   170 }
       
   171 
       
   172 inline void TestController::SetCurrentContainer(const char* str) {
       
   173   current_container=str;
       
   174 }
       
   175 
       
   176 inline void TestController::SetCurrentTestName(const char* str) {
       
   177   current_test = str;
       
   178 }
       
   179 
       
   180 inline void TestController::SetVerbose(bool val) {
       
   181   nc_verbose = val;
       
   182 }
       
   183 
       
   184 inline void TestController::TurnOffExceptions() {
       
   185   never_fail = true;
       
   186 }
       
   187 #endif
       
   188 #endif // INCLUDED_MOTU_nc_alloc