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