00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !INCLUDED_MOTU_LeakCheck
00023 #define INCLUDED_MOTU_LeakCheck 1
00024
00025 # include "Prefix.h"
00026
00027 # include "nc_alloc.h"
00028
00029 # if defined (EH_NEW_HEADERS)
00030 # include <cstdio>
00031 # include <cassert>
00032 # include <iterator>
00033 # else
00034 # include <stdio.h>
00035 # include <assert.h>
00036 # include <iterator.h>
00037 # endif
00038
00039 # if defined (EH_NEW_IOSTREAMS)
00040 # include <iostream>
00041 # else
00042 # include <iostream.h>
00043 # endif
00044
00045 EH_BEGIN_NAMESPACE
00046
00047 template <class T1, class T2>
00048 inline ostream& operator << (
00049 ostream& s,
00050 const pair <T1, T2>& p) {
00051 return s<<'['<<p.first<<":"<<p.second<<']';
00052 }
00053 EH_END_NAMESPACE
00054
00055
00056
00057
00058
00059
00060
00061 template <class C>
00062 void CheckInvariant(const C&)
00063 {
00064 }
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 template <class Value, class Operation>
00076 void WeakCheck( const Value& v, const Operation& op, long max_iters = 2000000 )
00077 {
00078 bool succeeded = false;
00079 bool failed = false;
00080 gTestController.SetCurrentTestCategory("weak");
00081 for ( long count = 0; !succeeded && !failed && count < max_iters; count++ )
00082 {
00083 gTestController.BeginLeakDetection();
00084 {
00085 Value dup = v;
00086 # ifndef EH_NO_EXCEPTIONS
00087 try {
00088 # endif
00089 gTestController.SetFailureCountdown(count);
00090 op( dup );
00091 succeeded = true;
00092 # ifndef EH_NO_EXCEPTIONS
00093 }
00094 catch(...) {}
00095 # endif
00096 gTestController.CancelFailureCountdown();
00097 CheckInvariant(dup);
00098 }
00099 failed = gTestController.ReportLeaked();
00100 EH_ASSERT( !failed );
00101
00102 if ( succeeded )
00103 gTestController.ReportSuccess(count);
00104 }
00105 EH_ASSERT( succeeded || failed );
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115 template <class Value, class Operation>
00116 void ConstCheck( const Value& v, const Operation& op, long max_iters = 2000000 )
00117 {
00118 bool succeeded = false;
00119 bool failed = false;
00120 gTestController.SetCurrentTestCategory("const");
00121 for ( long count = 0; !succeeded && !failed && count < max_iters; count++ )
00122 {
00123 gTestController.BeginLeakDetection();
00124 {
00125 # ifndef EH_NO_EXCEPTIONS
00126 try {
00127 # endif
00128 gTestController.SetFailureCountdown(count);
00129 op( v );
00130 succeeded = true;
00131 # ifndef EH_NO_EXCEPTIONS
00132 }
00133 catch(...) {}
00134 # endif
00135 gTestController.CancelFailureCountdown();
00136 }
00137 failed = gTestController.ReportLeaked();
00138 EH_ASSERT( !failed );
00139
00140 if ( succeeded )
00141 gTestController.ReportSuccess(count);
00142 }
00143 EH_ASSERT( succeeded || failed );
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 template <class Value, class Operation>
00160 void StrongCheck( const Value& v, const Operation& op, long max_iters = 2000000 )
00161 {
00162 bool succeeded = false;
00163 bool failed = false;
00164 gTestController.SetCurrentTestCategory("strong");
00165 for ( long count = 0; !succeeded && !failed && count < max_iters; count++ )
00166 {
00167 gTestController.BeginLeakDetection();
00168
00169 {
00170 Value dup = v;
00171 {
00172 # ifndef EH_NO_EXCEPTIONS
00173 try
00174 # endif
00175 {
00176 gTestController.SetFailureCountdown(count);
00177 op( dup );
00178 succeeded = true;
00179 gTestController.CancelFailureCountdown();
00180 }
00181 # ifndef EH_NO_EXCEPTIONS
00182 catch(...)
00183 {
00184 gTestController.CancelFailureCountdown();
00185 bool unchanged = dup == v;
00186 EH_ASSERT( unchanged );
00187
00188 if ( !unchanged )
00189 {
00190 #if 0
00191 typedef typename Value::value_type value_type;
00192 EH_STD::ostream_iterator<value_type> o(EH_STD::cerr, " ");
00193 EH_STD::cerr<<"EH test FAILED:\nStrong guaranee failed !\n";
00194 EH_STD::copy(dup.begin(), dup.end(), o);
00195 EH_STD::cerr<<"\nOriginal is:\n";
00196 EH_STD::copy(v.begin(), v.end(), o);
00197 EH_STD::cerr<<EH_STD::endl;
00198 #endif
00199 failed = true;
00200 }
00201 }
00202 # endif
00203 CheckInvariant(v);
00204 }
00205
00206 }
00207
00208 bool leaked = gTestController.ReportLeaked();
00209 EH_ASSERT( !leaked );
00210 if ( leaked )
00211 failed = true;
00212
00213 if ( succeeded )
00214 gTestController.ReportSuccess(count);
00215 }
00216 EH_ASSERT( succeeded || failed );
00217 }
00218
00219 #endif // INCLUDED_MOTU_LeakCheck