home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / test / eh / LeakCheck.h < prev    next >
C/C++ Source or Header  |  2000-12-07  |  7KB  |  220 lines

  1. /*
  2.  * Copyright (c) 1997
  3.  * Mark of the Unicorn, Inc.
  4.  *
  5.  * Permission to use, copy, modify, distribute and sell this software
  6.  * and its documentation for any purpose is hereby granted without fee,
  7.  * provided that the above copyright notice appear in all copies and
  8.  * that both that copyright notice and this permission notice appear
  9.  * in supporting documentation.  Mark of the Unicorn makes no
  10.  * representations about the suitability of this software for any
  11.  * purpose.  It is provided "as is" without express or implied warranty.
  12.  */
  13. /***********************************************************************************
  14.     LeakCheck.h
  15.     
  16.         SUMMARY: A suite of template functions for verifying the behavior of
  17.             operations in the presence of exceptions. Requires that the operations
  18.             be written so that each operation that could cause an exception causes
  19.             simulate_possible_failure() to be called (see "nc_alloc.h").
  20.         
  21. ***********************************************************************************/
  22. #if !INCLUDED_MOTU_LeakCheck
  23. #define INCLUDED_MOTU_LeakCheck 1
  24.  
  25. # include "Prefix.h"
  26.  
  27. # include "nc_alloc.h"
  28.  
  29. # if defined (EH_NEW_HEADERS)
  30. #  include <cstdio>
  31. #  include <cassert>
  32. #  include <iterator>
  33. # else
  34. #  include <stdio.h>
  35. #  include <assert.h>
  36. #  include <iterator.h>
  37. # endif
  38.  
  39. # if defined (EH_NEW_IOSTREAMS)
  40. #  include <iostream>
  41. # else
  42. #  include <iostream.h>
  43. # endif
  44.  
  45. EH_BEGIN_NAMESPACE
  46.  
  47. template <class T1, class T2>
  48. inline ostream& operator << ( 
  49. ostream& s, 
  50. const pair <T1, T2>& p) {
  51.     return s<<'['<<p.first<<":"<<p.second<<']';
  52. }
  53. EH_END_NAMESPACE
  54.  
  55. /*===================================================================================
  56.     CheckInvariant
  57.  
  58.     EFFECTS:  Generalized function to check an invariant on a container. Specialize
  59.         this for particular containers if such a check is available.
  60. ====================================================================================*/
  61. template <class C>
  62. void CheckInvariant(const C&)
  63. {
  64. }
  65.  
  66. /*===================================================================================
  67.     WeakCheck
  68.  
  69.     EFFECTS: Given a value and an operation, repeatedly applies the operation to a
  70.         copy of the value triggering the nth possible exception, where n increments
  71.         with each repetition until no exception is thrown or max_iters is reached.
  72.         Reports any detected memory leaks and checks any invariant defined for the
  73.         value type whether the operation succeeds or fails.
  74. ====================================================================================*/
  75. template <class Value, class Operation>
  76. void WeakCheck( const Value& v, const Operation& op, long max_iters = 2000000 )
  77. {
  78.     bool succeeded = false;
  79.     bool failed = false;
  80.     gTestController.SetCurrentTestCategory("weak");
  81.     for ( long count = 0; !succeeded && !failed && count < max_iters; count++ )
  82.     {
  83.         gTestController.BeginLeakDetection();
  84.         {
  85.             Value dup = v;
  86. # ifndef EH_NO_EXCEPTIONS
  87.             try {
  88. # endif
  89.                 gTestController.SetFailureCountdown(count);
  90.                 op( dup );
  91.                 succeeded = true;
  92. # ifndef EH_NO_EXCEPTIONS
  93.             }
  94.             catch(...) {}    // Just try again.
  95. # endif
  96.             gTestController.CancelFailureCountdown();
  97.             CheckInvariant(dup);
  98.         }
  99.         failed = gTestController.ReportLeaked();
  100.         EH_ASSERT( !failed );
  101.         
  102.         if ( succeeded )
  103.             gTestController.ReportSuccess(count);
  104.     }
  105.     EH_ASSERT( succeeded || failed );    // Make sure the count hasn't gone over
  106. }
  107.  
  108. /*===================================================================================
  109.     ConstCheck
  110.  
  111.     EFFECTS:  Similar to WeakCheck (above), but for operations which may not modify
  112.         their arguments. The operation is performed on the value itself, and no
  113.         invariant checking is performed. Leak checking still occurs.
  114. ====================================================================================*/
  115. template <class Value, class Operation>
  116. void ConstCheck( const Value& v, const Operation& op, long max_iters = 2000000 )
  117. {
  118.     bool succeeded = false;
  119.     bool failed = false;
  120.     gTestController.SetCurrentTestCategory("const");
  121.     for ( long count = 0; !succeeded && !failed && count < max_iters; count++ )
  122.     {
  123.         gTestController.BeginLeakDetection();
  124.         {
  125. # ifndef EH_NO_EXCEPTIONS
  126.             try {
  127. # endif
  128.                 gTestController.SetFailureCountdown(count);
  129.                 op( v );
  130.                 succeeded = true;
  131. # ifndef EH_NO_EXCEPTIONS
  132.             }
  133.             catch(...) {}    // Just try again.
  134. # endif
  135.             gTestController.CancelFailureCountdown();
  136.         }
  137.         failed = gTestController.ReportLeaked();
  138.         EH_ASSERT( !failed );
  139.  
  140.         if ( succeeded )
  141.             gTestController.ReportSuccess(count);
  142.     }
  143.     EH_ASSERT( succeeded || failed );    // Make sure the count hasn't gone over
  144. }
  145.  
  146. /*===================================================================================
  147.     StrongCheck
  148.  
  149.     EFFECTS:  Similar to WeakCheck (above), but additionally checks a component of
  150.         the "strong guarantee": if the operation fails due to an exception, the
  151.         value being operated on must be unchanged, as checked with operator==().
  152.         
  153.     CAVEATS: Note that this does not check everything required for the strong
  154.         guarantee, which says that if an exception is thrown, the operation has no
  155.         effects. Do do that we would have to check that no there were no side-effects
  156.         on objects which are not part of v (e.g. iterator validity must be preserved).
  157.         
  158. ====================================================================================*/
  159. template <class Value, class Operation>
  160. void StrongCheck( const Value& v, const Operation& op, long max_iters = 2000000 )
  161. {
  162.     bool succeeded = false;
  163.     bool failed = false;
  164.     gTestController.SetCurrentTestCategory("strong");
  165.     for ( long count = 0; !succeeded && !failed && count < max_iters; count++ )
  166.     {
  167.         gTestController.BeginLeakDetection();
  168.  
  169.         {
  170.             Value dup = v;
  171.             {
  172. # ifndef EH_NO_EXCEPTIONS
  173.             try
  174. # endif
  175.             {
  176.                 gTestController.SetFailureCountdown(count);
  177.                 op( dup );
  178.                 succeeded = true;
  179.                 gTestController.CancelFailureCountdown();
  180.             }
  181. # ifndef EH_NO_EXCEPTIONS
  182.             catch(...)
  183.             {
  184.                 gTestController.CancelFailureCountdown();
  185.                 bool unchanged = dup == v;
  186.                 EH_ASSERT( unchanged );
  187.                 
  188.                 if ( !unchanged )
  189.                 {
  190. #if 0
  191.                     typedef typename Value::value_type value_type;
  192.                     EH_STD::ostream_iterator<value_type> o(EH_STD::cerr, " ");
  193.                     EH_STD::cerr<<"EH test FAILED:\nStrong guaranee failed !\n";
  194.                     EH_STD::copy(dup.begin(), dup.end(), o);
  195.                     EH_STD::cerr<<"\nOriginal is:\n";
  196.                     EH_STD::copy(v.begin(), v.end(), o);
  197.                     EH_STD::cerr<<EH_STD::endl;
  198. #endif
  199.                     failed = true;
  200.                 }
  201.             }    // Just try again.
  202. # endif
  203.             CheckInvariant(v);
  204.         }
  205.  
  206.         }
  207.  
  208.     bool leaked = gTestController.ReportLeaked();
  209.     EH_ASSERT( !leaked );
  210.     if ( leaked )
  211.       failed = true;
  212.     
  213.         if ( succeeded )
  214.             gTestController.ReportSuccess(count);
  215.     }
  216.     EH_ASSERT( succeeded || failed );    // Make sure the count hasn't gone over
  217. }
  218.  
  219. #endif // INCLUDED_MOTU_LeakCheck
  220.