IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 1360 for trunk/psLib/test


Ignore:
Timestamp:
Jul 30, 2004, 4:28:29 PM (22 years ago)
Author:
desonia
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/test/psTest.c

    r1193 r1360  
    2727#define HEADER_BOTTOM "\\**********************************************************************************/\n\n"
    2828
    29 bool p_runTestSuite(FILE *fp, const char* testPointFile, const char* packageName,
    30                     testDescription tests[], int argc, char * const argv[])
     29bool p_runTestSuite( FILE *fp, const char* testPointFile, const char* packageName,
     30                     testDescription tests[], int argc, char * const argv[] )
    3131{
    3232    bool success = true;
     
    3737    int n;
    3838    extern char *optarg;
    39 
    40     if (argc > 0) {
    41         while ( (c=getopt(argc,argv,"lhn:dt:")) != -1) {
    42             switch (c) {
    43             case 'h':
    44                 printf("Usage: %s [-l] [-d] [-h] [-n=Testpoint#] [-t=TestpointName]\n"
    45                        "    where:\n"
    46                        "           -l  : lists the testpoints contained in this test driver executable\n"
    47                        "           -d  : turns on debugger-friendly mode (no forking, aborts/signals are not handled)\n"
    48                        "           -h  : prints this help\n"
    49                        "           -n  : specifies a particular testpoint by number to run\n"
    50                        "           -t  : specifies a particular testpoint by name to run\n"
    51                        "    (if no -l, -n or -t options are given, all testpoints are run)\n",
    52                        argv[0]);
    53                 runAll = false;
    54                 break;
    55             case 'd':
    56                 useFork = false;
    57                 break;
    58             case 'l':
    59                 printf("Test Driver:  %s\n",testPointFile);
    60                 printf("Package Name: %s\n",packageName);
    61                 printf("Testpoints:\n");
    62                 runAll = false;
    63                 for (int index=0; tests[index].fcn != NULL; index++) {
    64                     printf("    %6d - %s \n",tests[index].testPointNumber,
    65                            tests[index].testPointName);
    66                 }
    67                 printf("\n");
    68                 break;
    69             case 't':
    70                 runAll = false;
    71                 for (int index=0; tests[index].fcn != NULL; index++) {
    72                     if (strcmp(optarg,tests[index].testPointName) == 0) {
    73                         success = p_runTest(fp,
    74                                             testPointFile,
    75                                             packageName,
    76                                             tests[index].testPointName,
    77                                             tests[index].fcn,
    78                                             tests[index].expectedReturn,
    79                                             useFork) && success;
    80                     }
    81                 }
    82                 break;
    83             case 'n':
    84                 runAll = false;
    85                 if (sscanf(optarg,"%i",&n) != 1) {
    86                     psError(__func__,"Failed to parse the testpoint number (%s).",
    87                             optarg);
    88                     break;
    89                 }
    90                 found = false;
    91                 for (int index=0; tests[index].fcn != NULL; index++) {
    92                     if (n==tests[index].testPointNumber) {
    93                         found = true;
    94                         success = p_runTest(fp,
    95                                             testPointFile,
    96                                             packageName,
    97                                             tests[index].testPointName,
    98                                             tests[index].fcn,
    99                                             tests[index].expectedReturn,
    100                                             useFork) && success;
    101                     }
    102                 }
    103                 if (! found) {
    104                     psError(__func__,"The specified testpoint number (%d) doesn't exist in this test driver.",
    105                             n);
    106                     break;
    107                 }
    108                 break;
    109             case '?':
    110                 psError(__func__,"Option %s is not recognized and is ignored.",optarg);
    111                 break;
    112             }
    113         }
    114     }
    115 
    116     if (runAll) {
    117         for (int index=0; tests[index].fcn != NULL; index++) {
    118             if (! tests[index].isDuplicateEntry) {
    119                 success = p_runTest(fp,
    120                                     testPointFile,
    121                                     packageName,
    122                                     tests[index].testPointName,
    123                                     tests[index].fcn,
    124                                     tests[index].expectedReturn,
    125                                     useFork) && success;
    126             }
    127         }
    128     }
     39   
     40    if ( argc > 0 ) {
     41            while ( ( c = getopt( argc, argv, "lhn:dt:" ) ) != -1 ) {
     42                    switch ( c ) {
     43                            case 'h':
     44                            printf( "Usage: %s [-l] [-d] [-h] [-n=Testpoint#] [-t=TestpointName]\n"
     45                                    "    where:\n"
     46                                    "           -l  : lists the testpoints contained in this test driver executable\n"
     47                                    "           -d  : turns on debugger-friendly mode (no forking, aborts/signals are not handled)\n"
     48                                    "           -h  : prints this help\n"
     49                                    "           -n  : specifies a particular testpoint by number to run\n"
     50                                    "           -t  : specifies a particular testpoint by name to run\n"
     51                                    "    (if no -l, -n or -t options are given, all testpoints are run)\n",
     52                                    argv[ 0 ] );
     53                            runAll = false;
     54                            break;
     55                            case 'd':
     56                            useFork = false;
     57                            break;
     58                            case 'l':
     59                            printf( "Test Driver:  %s\n", testPointFile );
     60                            printf( "Package Name: %s\n", packageName );
     61                            printf( "Testpoints:\n" );
     62                            runAll = false;
     63                            for ( int index = 0; tests[ index ].fcn != NULL; index++ ) {
     64                                    printf( "    %6d - %s \n", tests[ index ].testPointNumber,
     65                                            tests[ index ].testPointName );
     66                                }
     67                            printf( "\n" );
     68                            break;
     69                            case 't':
     70                            runAll = false;
     71                            for ( int index = 0; tests[ index ].fcn != NULL; index++ ) {
     72                                    if ( strcmp( optarg, tests[ index ].testPointName ) == 0 ) {
     73                                            success = p_runTest( fp,
     74                                                                 testPointFile,
     75                                                                 packageName,
     76                                                                 tests[ index ].testPointName,
     77                                                                 tests[ index ].fcn,
     78                                                                 tests[ index ].expectedReturn,
     79                                                                 useFork ) && success;
     80                                        }
     81                                }
     82                            break;
     83                            case 'n':
     84                            runAll = false;
     85                            if ( sscanf( optarg, "%i", &n ) != 1 ) {
     86                                    psError( __func__, "Failed to parse the testpoint number (%s).",
     87                                             optarg );
     88                                    break;
     89                                }
     90                            found = false;
     91                            for ( int index = 0; tests[ index ].fcn != NULL; index++ ) {
     92                                    if ( n == tests[ index ].testPointNumber ) {
     93                                            found = true;
     94                                            success = p_runTest( fp,
     95                                                                 testPointFile,
     96                                                                 packageName,
     97                                                                 tests[ index ].testPointName,
     98                                                                 tests[ index ].fcn,
     99                                                                 tests[ index ].expectedReturn,
     100                                                                 useFork ) && success;
     101                                        }
     102                                }
     103                            if ( ! found ) {
     104                                    psError( __func__, "The specified testpoint number (%d) doesn't exist in this test driver.",
     105                                             n );
     106                                    break;
     107                                }
     108                            break;
     109                            case '?':
     110                            psError( __func__, "Option %s is not recognized and is ignored.", optarg );
     111                            break;
     112                        }
     113                }
     114        }
     115       
     116    if ( runAll ) {
     117            for ( int index = 0; tests[ index ].fcn != NULL; index++ ) {
     118                    if ( ! tests[ index ].isDuplicateEntry ) {
     119                            success = p_runTest( fp,
     120                                                 testPointFile,
     121                                                 packageName,
     122                                                 tests[ index ].testPointName,
     123                                                 tests[ index ].fcn,
     124                                                 tests[ index ].expectedReturn,
     125                                                 useFork ) && success;
     126                        }
     127                }
     128        }
     129       
     130    if ( ! success ) {
     131            psError( testPointFile, "One or more tests failed" );
     132        }
     133       
    129134    return success;
    130135}
    131136
    132 bool p_runTest(FILE *fp, const char* testPointFile, const char* packageName, const char* testPointName,
    133                testFcn fcn, int expectedReturn, bool useFork)
     137bool p_runTest( FILE *fp, const char* testPointFile, const char* packageName, const char* testPointName,
     138                testFcn fcn, int expectedReturn, bool useFork )
    134139{
    135140    int childReturn = 0;
    136141    pid_t child;
    137 
    138     p_printPositiveTestHeader(fp,testPointFile,packageName,testPointName);
    139 
    140     if (useFork) {
    141         child = fork();
    142         if (child == 0) {                   // I am the child process, run the test
     142   
     143    p_printPositiveTestHeader( fp, testPointFile, packageName, testPointName );
     144   
     145    if ( useFork ) {
     146            child = fork();
     147            if ( child == 0 ) {                   // I am the child process, run the test
     148                    int currentId = psMemGetId();
     149                    int retVal = fcn();
     150                    if ( retVal == 0 ) { // only bother checking memory if test executed to end.
     151                            if ( psMemCheckLeaks( currentId, NULL, stderr ) != 0 ) {
     152                                    psError( __func__, "Memory Leaks Detected" );
     153                                    retVal = 64;
     154                                }
     155                            psMemCheckCorruption( 1 );
     156                        }
     157                    exit( retVal );
     158                } else if ( child < 0 ) {
     159                    fprintf( fp, "Couldn't fork a process to run a negative test (%s|%s)",
     160                             packageName, testPointName );
     161                    abort();
     162                }
     163               
     164            waitpid( child, &childReturn, 0 );
     165            if ( WIFSIGNALED( childReturn ) ) {
     166                    childReturn = -WTERMSIG( childReturn );
     167                } else {
     168                    childReturn = WEXITSTATUS( childReturn );
     169                }
     170        } else {
    143171            int currentId = psMemGetId();
    144             int retVal = fcn();
    145             if (retVal == 0) { // only bother checking memory if test executed to end.
    146                 if (psMemCheckLeaks(currentId,NULL,stderr) != 0) {
    147                     psError(__func__,"Memory Leaks Detected");
    148                     retVal = 64;
    149                 }
    150                 psMemCheckCorruption(1);
    151             }
    152             exit(retVal);
    153         } else if (child < 0) {
    154             fprintf(fp,"Couldn't fork a process to run a negative test (%s|%s)",
    155                     packageName, testPointName);
    156             abort();
    157         }
    158 
    159         waitpid(child,&childReturn,0);
    160         if (WIFSIGNALED(childReturn)) {
    161             childReturn =  -WTERMSIG(childReturn);
     172            childReturn = fcn();
     173            if ( childReturn == 0 ) { // only bother checking memory if test executed to end.
     174                    if ( psMemCheckLeaks( currentId, NULL, stderr ) != 0 ) {
     175                            psError( __func__, "Memory Leaks Detected" );
     176                            childReturn = 64;
     177                        }
     178                    psMemCheckCorruption( 1 );
     179                }
     180        }
     181       
     182       
     183    if ( childReturn != expectedReturn ) {
     184            fprintf( fp, "Return value mismatch: expected %d, got %d",
     185                     expectedReturn, childReturn );
     186        }
     187       
     188    p_printFooter( fp, testPointFile, packageName, testPointName,
     189                   ( childReturn == expectedReturn ) );
     190                   
     191    return ( childReturn == expectedReturn );
     192}
     193
     194void p_printPositiveTestHeader( FILE *fp,
     195                                const char* testPointFile,
     196                                const char* packageName,
     197                                const char* testPointName )
     198{
     199    char TP[ 80 ];
     200   
     201    snprintf( TP, 80, "%s{%s}", packageName, testPointName );
     202   
     203    fprintf( fp, HEADER_TOP );
     204    fprintf( fp, HEADER_LINE_STRING, "TestFile", testPointFile );
     205    fprintf( fp, HEADER_LINE_STRING, "TestPoint", TP );
     206    fprintf( fp, HEADER_LINE_STRING, "TestType", "Positive" );
     207    fprintf( fp, HEADER_BOTTOM );
     208}
     209
     210void p_printNegativeTestHeader( FILE *fp,
     211                                const char* testPointFile,
     212                                const char* packageName,
     213                                const char* testPointName,
     214                                const char* expectedError,
     215                                int exitValue )
     216{
     217    char TP[ 80 ];
     218   
     219    snprintf( TP, 80, "%s{%s}", packageName, testPointName );
     220   
     221    fprintf( fp, HEADER_TOP );
     222    fprintf( fp, HEADER_LINE_STRING, "TestFile", testPointFile );
     223    fprintf( fp, HEADER_LINE_STRING, "TestPoint", TP );
     224    fprintf( fp, HEADER_LINE_STRING, "TestType", "Negative" );
     225    fprintf( fp, HEADER_LINE_STRING, "ExpectedErrorText", expectedError );
     226    fprintf( fp, HEADER_LINE_INT, "ExpectedStatusValue", exitValue );
     227    fprintf( fp, HEADER_BOTTOM );
     228}
     229
     230
     231void p_printFooter( FILE *fp,
     232                    const char* testPointFile,
     233                    const char* packageName,
     234                    const char* testPointName,
     235                    bool success )
     236{
     237    if ( success ) {
     238            fprintf( fp, "\n---> TESTPOINT PASSED (%s{%s} | %s)\n\n", packageName, testPointName, testPointFile );
    162239        } else {
    163             childReturn = WEXITSTATUS(childReturn);
    164         }
    165     } else {
    166         int currentId = psMemGetId();
    167         childReturn = fcn();
    168         if (childReturn == 0) { // only bother checking memory if test executed to end.
    169             if (psMemCheckLeaks(currentId,NULL,stderr) != 0) {
    170                 psError(__func__,"Memory Leaks Detected");
    171                 childReturn = 64;
    172             }
    173             psMemCheckCorruption(1);
    174         }
    175     }
    176 
    177 
    178     if (childReturn != expectedReturn) {
    179         fprintf(fp,"Return value mismatch: expected %d, got %d",
    180                 expectedReturn,childReturn);
    181     }
    182 
    183     p_printFooter(fp,testPointFile,packageName,testPointName,
    184                   (childReturn==expectedReturn));
    185 
    186     return (childReturn==expectedReturn);
    187 }
    188 
    189 void p_printPositiveTestHeader(FILE *fp,
    190                                const char* testPointFile,
    191                                const char* packageName,
    192                                const char* testPointName)
    193 {
    194     char TP[80];
    195 
    196     snprintf(TP,80,"%s{%s}",packageName,testPointName);
    197 
    198     fprintf(fp, HEADER_TOP);
    199     fprintf(fp, HEADER_LINE_STRING, "TestFile", testPointFile);
    200     fprintf(fp, HEADER_LINE_STRING, "TestPoint", TP);
    201     fprintf(fp, HEADER_LINE_STRING, "TestType","Positive");
    202     fprintf(fp, HEADER_BOTTOM);
    203 }
    204 
    205 void p_printNegativeTestHeader(FILE *fp,
    206                                const char* testPointFile,
    207                                const char* packageName,
    208                                const char* testPointName,
    209                                const char* expectedError,
    210                                int exitValue)
    211 {
    212     char TP[80];
    213 
    214     snprintf(TP,80,"%s{%s}",packageName,testPointName);
    215 
    216     fprintf(fp, HEADER_TOP);
    217     fprintf(fp, HEADER_LINE_STRING, "TestFile", testPointFile);
    218     fprintf(fp, HEADER_LINE_STRING, "TestPoint", TP);
    219     fprintf(fp, HEADER_LINE_STRING,"TestType","Negative");
    220     fprintf(fp, HEADER_LINE_STRING,"ExpectedErrorText",expectedError);
    221     fprintf(fp, HEADER_LINE_INT,"ExpectedStatusValue",exitValue);
    222     fprintf(fp, HEADER_BOTTOM);
    223 }
    224 
    225 
    226 void p_printFooter(FILE *fp,
    227                    const char* testPointFile,
    228                    const char* packageName,
    229                    const char* testPointName,
    230                    bool success)
    231 {
    232     if (success) {
    233         fprintf(fp, "\n---> TESTPOINT PASSED (%s{%s} | %s)\n\n", packageName,testPointName, testPointFile);
    234     } else {
    235         fprintf(fp, "\n---> TESTPOINT FAILED (%s{%s} | %s)\n\n", packageName,testPointName,testPointFile);
    236     }
    237 }
     240            fprintf( fp, "\n---> TESTPOINT FAILED (%s{%s} | %s)\n\n", packageName, testPointName, testPointFile );
     241        }
     242}
Note: See TracChangeset for help on using the changeset viewer.