00001
00002
00003
00004 #include "cddefines.h"
00005 #include "parser.h"
00006
00007 #ifdef _MSC_VER
00008
00009 # pragma warning( disable : 4700 )
00010
00011 # pragma warning( disable : 4756 )
00012
00013 # pragma warning( disable : 4127 )
00014 #endif
00015
00016 #ifdef __INTEL_COMPILER
00017 # pragma warning( disable : 592 )
00018 #endif
00019
00020
00021 const int ARR_SIZE = 10;
00022
00023
00024 static double ar2[ARR_SIZE];
00025
00026
00027
00028 #if defined(_MSC_VER) || defined(__ICC)
00029 #pragma optimize("", off)
00030 #elif defined(__PGI)
00031 #pragma global opt=0
00032 #elif defined(__HP_aCC)
00033 #pragma OPT_LEVEL 0
00034 #endif
00035
00036
00037 void ParseCrashDo(Parser &p)
00038 {
00039 double ar1, br1;
00040 bool lgCrash = false;
00041
00042 DEBUG_ENTRY( "ParseCrashDo()" );
00043
00044
00045 if( p.nMatch("ZERO") )
00046 {
00047 fprintf(ioQQQ," I will now div by 0 to get crash. Hold on.\n");
00048 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00049 fflush(ioQQQ);
00050 ar1 = 1. / ZeroNum;
00051 fprintf(ioQQQ," I am still alive - something is wrong, result is %e\n",
00052 ar1);
00053 lgCrash = true;
00054 }
00055
00056
00057 else if( p.nMatch("UNDE") )
00058 {
00059 if( p.nMatch("STAT") )
00060 {
00061 fprintf(ioQQQ," Now I will now use an undefined static variable. Hold on.\n");
00062 fprintf(ioQQQ," This should never fail since the compiler should have automatically initialized it to zero.\n");
00063 fflush(ioQQQ);
00064
00065 ar2[0] *= 1e-10;
00066
00067
00068 fprintf(ioQQQ," I am still alive, this is the expected result. The "
00069 "result of the multiplication of undefined by 1e-10 is "
00070 "%e\n", ar2[0] );
00071 fflush(ioQQQ);
00072 }
00073 else if( p.nMatch("STAC") || p.nMatch("AUTO") )
00074 {
00075 double A_variable_which_SHOULD_be_used_uninitialized;
00076 fprintf(ioQQQ," Now I will now use an undefined variable off the stack. Hold on.\n");
00077 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00078 fflush(ioQQQ);
00079
00080 A_variable_which_SHOULD_be_used_uninitialized *= 1e-10f;
00081
00082
00083 fprintf(ioQQQ," I am still alive - something is wrong, the result of the multiplication of undefined by 1e-10 is %e\n", A_variable_which_SHOULD_be_used_uninitialized );
00084 fflush(ioQQQ);
00085 }
00086 else
00087 {
00088 double *aa = (double*)MALLOC(3*sizeof(double));
00089 fprintf(ioQQQ," I will now use an undefined variable off the heap obtained with malloc. Hold on.\n");
00090
00091 if( MyIsnan( aa[1] ) )
00092 fprintf(ioQQQ," The malloc'ed memory was set to NaN.\n" );
00093 else
00094 fprintf(ioQQQ," The malloc'ed memory was NOT initialized by MyMalloc.\n" );
00095 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00096 fflush(ioQQQ);
00097
00098 aa[1] *= 1e-10;
00099
00100 fprintf(ioQQQ," I am still alive - something is wrong, the result of the multiplication of undefined by 1e-10 is %e\n", aa[1] );
00101 fflush(ioQQQ);
00102 free( aa );
00103 }
00104 lgCrash = true;
00105 }
00106
00107
00108 else if( p.nMatch("OVER") && p.nMatch("LONG") )
00109 {
00110 long lng;
00111 fprintf(ioQQQ," I will now make long overflow to get crash. Hold on.\n");
00112 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00113 fflush(ioQQQ);
00114 lng = (long)(LONG_MAX*sqrt(1e6));
00115 fprintf(ioQQQ," I am still alive - something is wrong, the result was %li\n",
00116 lng);
00117 lgCrash = true;
00118 }
00119
00120
00121 else if( p.nMatch("OVER") )
00122 {
00123 ar1 = 1e-20;
00124 fprintf(ioQQQ," I will now make floating point overflow to get crash. Hold on.\n");
00125 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00126 fflush(ioQQQ);
00127 br1 = DBL_MAX / ar1;
00128 fprintf(ioQQQ," I am still alive - something is wrong, the result was %e\n",
00129 br1);
00130 lgCrash = true;
00131 }
00132
00133
00134 else if( p.nMatch("ASSE") )
00135 {
00136 fprintf(ioQQQ," I will now assert that a false statement is true to get a crash.\n\n");
00137 fprintf(ioQQQ," The correct behavior is for the statement \"PROBLEM DISASTER An assert has been thrown, this is bad\" to be printed, followed by lots more scary looking messages.\n\n");
00138 fprintf(ioQQQ," If the next line says \"I am still alive - the assert macro is not working ....\" then there are problems.\n\n");
00139 fflush(ioQQQ);
00140 ASSERT( DBL_MAX < ZeroNum );
00141 fprintf(ioQQQ," I am still alive - the assert macro is not working in this executable.\n");
00142 lgCrash = true;
00143 }
00144
00145
00146 else if( p.nMatch(" NAN") )
00147 {
00148 ar1 = 0.;
00149 fprintf(ioQQQ," I will now make invalid operation (div 0 by 0) to get crash. Hold on.\n");
00150 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00151 fflush(ioQQQ);
00152 br1 = ar1 / ZeroNum;
00153 fprintf(ioQQQ," I am still alive - something is wrong, the result was %e\n",
00154 br1);
00155 lgCrash = true;
00156 }
00157
00158
00159 else if( p.nMatch("SETN") && p.nMatch("FLOA") )
00160 {
00161 sys_float f;
00162 fprintf(ioQQQ," I will now initialize a float to a signaling NaN. This should never crash!\n");
00163 set_NaN(f);
00164 fprintf(ioQQQ," Initialization finished. I will now perform an operation on this variable. Hold on.\n");
00165 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00166 fflush(ioQQQ);
00167 f *= 2.f;
00168 fprintf(ioQQQ," I am still alive - something is wrong, the result was %e\n",
00169 f);
00170 lgCrash = true;
00171 }
00172
00173
00174 else if( p.nMatch("SETN") )
00175 {
00176 double d;
00177 fprintf(ioQQQ," I will now initialize a double to a signaling NaN. This should never crash!\n");
00178 set_NaN(d);
00179 fprintf(ioQQQ," Initialization finished. I will now perform an operation on this variable. Hold on.\n");
00180 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong ....\" then there are problems.\n");
00181 fflush(ioQQQ);
00182 d *= 2.;
00183 fprintf(ioQQQ," I am still alive - something is wrong, the result was %e\n",
00184 d);
00185 lgCrash = true;
00186 }
00187
00188
00189
00190 else if( p.nMatch("BOUN") )
00191 {
00192 double x;
00193
00194
00195 x = p.FFmtRead();
00196 if( p.lgEOL() && p.nMatch(" LOW" ) )
00197 x = -2.;
00198 if( p.lgEOL() && p.nMatch("HIGH" ) )
00199 x = 2.;
00200
00201
00202
00203 long int i = ( x >= 0. ) ? (long)(x+0.5) + ARR_SIZE : (long)(x-0.5);
00204
00205
00206 if( p.nMatch("STAT") )
00207 {
00208 fprintf(ioQQQ," I will now access static array element ar2[%ld]. Hold on.\n", i );
00209 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00210 fflush(ioQQQ);
00211 ar2[i] = 1e-10;
00212
00213 fprintf(ioQQQ," I am still alive - something is wrong\n" );
00214 fflush(ioQQQ);
00215 }
00216 else if( p.nMatch("STAC") || p.nMatch("AUTO") )
00217 {
00218 double a[ARR_SIZE];
00219 fprintf(ioQQQ," I will now access automatic array element a[%ld]. Hold on.\n", i );
00220 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00221 fflush(ioQQQ);
00222 a[i] = 1e-10;
00223
00224 fprintf(ioQQQ," I am still alive - something is wrong, return value was %.2e\n", a[i] );
00225 fflush(ioQQQ);
00226 }
00227 else if( p.nMatch("HEAP") )
00228 {
00229 int *ibound;
00230 ibound = ((int *)MALLOC( ARR_SIZE*sizeof(int) ));
00231 fprintf(ioQQQ," I will now access malloced heap array element ibound[%ld]. Hold on.\n", i );
00232 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00233 fflush(ioQQQ);
00234 ibound[i] = 1;
00235 fprintf(ioQQQ," I am still alive - something is wrong, return value is %i\n" , ibound[i] );
00236 fflush(ioQQQ);
00237 free(ibound);
00238 }
00239 else if( p.nMatch("MULT") )
00240 {
00241
00242
00243 multi_arr<double,2> b;
00244 b.reserve(3);
00245 for( int j=0; j < 3; j++ )
00246 b.reserve(j,ARR_SIZE+j);
00247 b.alloc();
00248 if( p.nMatch("ITER") )
00249 {
00250 fprintf(ioQQQ," I will now access multi_arr array element *b.ptr(0,%ld). Hold on.\n", i );
00251 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n\n");
00252 fflush(ioQQQ);
00253 md2i p = b.ptr(0,i);
00254 *p = 2.;
00255 fprintf(ioQQQ," I am still alive - something is wrong, return value is %g\n", *p );
00256 fflush(ioQQQ);
00257 }
00258 else
00259 {
00260 fprintf(ioQQQ," I will now access multi_arr array element b[0][%ld]. Hold on.\n", i );
00261 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n\n");
00262 fflush(ioQQQ);
00263 b[0][i] = 2.;
00264 fprintf(ioQQQ," I am still alive - something is wrong, return value is %g\n" , b[0][i] );
00265 fflush(ioQQQ);
00266 }
00267 b.clear();
00268 }
00269 else
00270 {
00271 fprintf(ioQQQ," The CRASH BOUNDS command has four different tests. One must be specified\n" );
00272 fprintf(ioQQQ," The HEAP option tests a malloc/'d array - this tests valgrind or purify.\n");
00273 fprintf(ioQQQ," The STATIC option tests a static declared array, and the STACK or AUTO option tests an automatic array - these test pgcc.\n");
00274 fprintf(ioQQQ," The MULTI option tests if bounds checking is enabled in the multi_arr class (i.e., if the preprocessor macro BOUNDS_CHECK has been set).\n" );
00275 fprintf(ioQQQ," All have a number as an optional argument, the array element to be accessed.\n");
00276 fflush(ioQQQ);
00277 }
00278 lgCrash = true;
00279 }
00280
00281
00282 else if( p.nMatch("ISNA") )
00283 {
00284 if( p.nMatch("FLOA") )
00285 {
00286 sys_float ff;
00287 fprintf(ioQQQ," I will now set a float to SNaN. This should never crash!\n" );
00288 set_NaN( ff );
00289 fprintf(ioQQQ," I will now test this variable with the isnan function\n" );
00290 fprintf(ioQQQ," The correct behavior is for the statement \"PROBLEM DISASTER An assert has been thrown, this is bad\" to be printed, followed by lots more scary looking messages.\n\n");
00291 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00292 ASSERT( !isnan( ff ) );
00293 fprintf(ioQQQ," I am still alive - something is wrong, value is %e\n", ff );
00294 }
00295 else
00296 {
00297 double dd;
00298 fprintf(ioQQQ," I will now set a double to SNaN. This should never crash!\n" );
00299 set_NaN( dd );
00300 fprintf(ioQQQ," I will now test this variable with the isnan function\n" );
00301 fprintf(ioQQQ," The correct behavior is for the statement \"PROBLEM DISASTER An assert has been thrown, this is bad\" to be printed, followed by lots more scary looking messages.\n\n");
00302 fprintf(ioQQQ," If the next line says \"I am still alive - something is wrong\" then there are problems.\n");
00303 ASSERT( !isnan( dd ) );
00304 fprintf(ioQQQ," I am still alive - something is wrong, value is %e\n", dd );
00305 }
00306 }
00307
00308
00309 else if( p.nMatch("EXCE") )
00310 {
00311 fprintf(ioQQQ," I will now throw a C++ exception of type out_of_range()\n" );
00312 fprintf(ioQQQ," The correct behavior is for the statement \"DISASTER - An out_of_range exception was caught, what() = Cloudy Test. Bailing out...\" to be printed.\n\n");
00313 fprintf(ioQQQ," If you get any other message, the exception was not caught correctly.\n\n");
00314 throw out_of_range( "Cloudy Test" );
00315 fprintf(ioQQQ," If you see this statement, the exception did not terminate the program.\n" );
00316 }
00317
00318 else
00319 {
00320 fprintf(ioQQQ,
00321 "Crash option not found - valid options are ZERO, UNDEfined,"
00322 " OVERflow, ASSErt, _NAN, SETNan, BOUNds, ISNAn, and EXCEption.\nSorry.\n");
00323 lgCrash = true;
00324 }
00325
00326 if( lgCrash )
00327 {
00328 cdEXIT(EXIT_FAILURE);
00329 }
00330 }