Changeset 02c9d9 in git


Ignore:
Timestamp:
Oct 2, 1997, 1:09:20 PM (27 years ago)
Author:
Jens Schmidt <schmidt@…>
Branches:
(u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'c7af8613769b29c741d6c338945669719f1fc4f8')
Children:
d37f27288dce392575ad1129adb7540432ce098f
Parents:
a09c41577667052c82234ad7730f0c65ddeefda2
Message:
	* ftest_util.cc (ftestWriteSeed): new function.  Declaration
	  added.
	* ftest_util.cc(ftestSeedFile): new static variable
	  (ftestParseRandom): reads seed from file
	* ftest_util.cc (ftestError): handles error `FileError' now

	* ftest_util.cc (ftestParseSwitches): bug fix.  Explicitly
	  switches off switch if specifies with `-'
	  (ftestParseEnv): cosmetic changes
	  (ftestParseVars): check on error removed
	  (ftestEnv): declared static

	* ftest_util.cc (struct ftestEnvT): new member `seedSet'
	  (ftestParseRandom): sets `seedSet' if a definite seed has
	  been set
	  (ftestGetEnv): initialization for `seedSet' added
	  (ftestPrintEnv, ftestSetRandom): handles `seedSet' more neatly

	* ftest_util.cc (ftestSetSeed, ftestSetRandom): function
	  ftestSetSeed() renamed to ftestSetRandom().  All callers
	  changed.
	  (ftestParseSeed, ftestParseRandom): function ftestParseSeed()
	  renamed to ftestParseRandom().  All callers changed.

	* ftest_util.cc (ftestMainExit): function removed.  Declaration,
	  too.

	* ftest_util.cc (ftestMainExit): new function.  Declaration added.


git-svn-id: file:///usr/local/Singular/svn/trunk@778 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • factory/ftest/ftest_util.cc

    ra09c415 r02c9d9  
    11/* emacs edit mode for this file is -*- C++ -*- */
    2 /* $Id: ftest_util.cc,v 1.6 1997-10-01 12:24:44 schmidt Exp $ */
     2/* $Id: ftest_util.cc,v 1.7 1997-10-02 11:09:20 schmidt Exp $ */
    33
    44//{{{ docu
     
    1414#include <string.h>
    1515#include <ctype.h>
     16#include <errno.h>
     17#include <iostream.h>
     18
    1619#include <GetOpt.h>
    1720
     
    5760{
    5861    int seed;
     62    bool seedSet;
    5963    int characteristic;
    6064    int extDegree;
     
    106110//   variable is quite spurious, but I keep it for future use.
    107111// ftestSwitchNames: symbolic names of switches
    108 //
     112// ftestSeedFile: file where to read/to which to print the random
     113//   generator seed.  Set by ftestParseRandom(), read by
     114//   ftestWriteSeed().
    109115//
    110116//}}}
     
    118124const char * ftestUsage = 0;
    119125
    120 ftestEnvT ftestEnv;
     126static ftestEnvT ftestEnv;
    121127
    122128const char * ftestSwitchNames[] =
     
    131137    "USE_SPARSEMOD"
    132138};
     139
     140static char * ftestSeedFile = 0;
    133141//}}}
    134142
     
    202210//}}}
    203211
    204 //{{{ static const char * ftestParseSeed ( const char * tokenString )
    205 //{{{ docu
    206 //
    207 // ftestParseSeed() - parse seed specification and set factory's
     212//{{{ static const char * ftestParseRandom ( const char * tokenString )
     213//{{{ docu
     214//
     215// ftestParseRandom() - parse random specification and set factory's
    208216//   random generator seed.
    209217//
     
    213221//}}}
    214222static const char *
    215 ftestParseSeed ( const char * tokenString )
    216 {
    217     const char * tokenCursor;
    218 
    219     tokenString++;                      // strtol will skip blancs
    220     int seed = (int)strtol( tokenString, (char **)&tokenCursor, 0 );
    221     if ( tokenCursor == tokenString )
    222         ftestError( EnvSyntaxError,
    223                     "number expected in seed specification `%s'\n",
    224                     tokenString );
     223ftestParseRandom ( const char * tokenString )
     224{
     225    int seed = 0;
     226    bool seedSet = false;
     227
     228    do {
     229        const char * tokenCursor;
     230
     231        // we skip either `@' or `,'
     232        tokenString = ftestSkipBlancs( tokenString+1 );
     233        if ( isdigit( *tokenString ) ) {
     234            // read seed specification from command line
     235            seed = (int)strtol( tokenString, (char **)&tokenCursor, 0 );
     236            if ( tokenCursor == tokenString )
     237                ftestError( EnvSyntaxError,
     238                            "bad seed specification `%s'\n",
     239                            tokenString );
     240            seedSet = true;
     241        } else if ( isalpha( *tokenString ) ) {
     242            // read seed specification from file
     243            tokenCursor = strpbrk( tokenString, ",/" );
     244            if ( ! tokenCursor )
     245                tokenCursor = strchr( tokenString, '\0' );
     246
     247            // store file name
     248            int nameLen = tokenCursor-tokenString;
     249            ftestSeedFile = new char[nameLen+1];
     250            strncpy( ftestSeedFile, tokenString, nameLen );
     251            ftestSeedFile[nameLen] = '\0';
     252
     253            // open file, read seed, close file
     254            FILE * seedFile = fopen( ftestSeedFile, "r" );
     255            if ( seedFile ) {
     256                // if file exists, read seed
     257                if ( fscanf( seedFile, "%d", &seed ) != 1 )
     258                    ftestError( FileError,
     259                                "garbled seed in seed file `%s'\n", ftestSeedFile );
     260                fclose( seedFile );
     261
     262                seedSet = true;
     263            } else
     264                if ( errno != ENOENT )
     265                    ftestError( FileError,
     266                                "failed to open `%s' for reading: %s\n", ftestSeedFile,
     267                                strerror( errno ) );
     268                else if ( ! (ftestEnv.seedSet || seedSet) ) {
     269                    // if file does not exist, set seed to some
     270                    // default value if it has not been set before
     271                    seed = 0;
     272                    seedSet = true;
     273                }
     274        } else
     275            ftestError( EnvSyntaxError,
     276                        "bad random specification `%s'\n", tokenString );
     277
     278        tokenString = ftestSkipBlancs( tokenCursor );
     279    } while ( *tokenString == ',' );
    225280
    226281    // set seed
    227     ftestEnv.seed = seed;
    228     factoryseed( seed );
    229 
    230     return tokenCursor;
     282    ftestEnv.seedSet = seedSet;
     283    if ( seedSet ) {
     284        ftestEnv.seed = seed;
     285        factoryseed( seed );
     286    }
     287
     288    return tokenString;
    231289}
    232290//}}}
     
    304362    bool switchSetting;
    305363
    306     while ( *tokenString == '+' || *tokenString == '-') {
     364    while ( *tokenString == '+' || *tokenString == '-' ) {
    307365        const char * tokenCursor;
    308366        if ( *tokenString == '+' )
     
    331389        if ( switchSetting )
    332390            On( switchNumber );
     391        else
     392            Off( switchNumber );
    333393
    334394        tokenString = ftestSkipBlancs( tokenCursor );
     
    382442            mipoString[mipoLen] = '\0';
    383443            mipo = ftestGetCanonicalForm( mipoString );
     444            delete [] mipoString;
    384445
    385446            tokenString = ftestSkipBlancs( tokenCursor );
    386             delete [] mipoString;
    387447        }
    388 
    389448        if ( *tokenString == ',' )
    390449            // skip optional separator
     
    412471    }
    413472
    414     if ( *tokenString && *tokenString != '/' )
    415         ftestError( EnvSyntaxError,
    416                     "extra characters in variable specification `%s'\n",
    417                     tokenString );
    418 
    419473    return tokenString;
    420474}
     
    428482//
    429483// The results are also stored in global variable `ftestEnv'.
    430 // Returns pointer behind last parsed character in tokenString.
     484//
     485// The parser is quite simple.  As long as `tokenString' starts
     486// with a `/', he looks up the next character to determine which
     487// token follows and parses it.  The functions used to do this
     488// should leave `tokenString' behind the last character it
     489// succeeded to read.
    431490//
    432491//}}}
     
    434493ftestParseEnv ( const char * tokenString )
    435494{
     495    // read list of environment specifications
    436496    tokenString = ftestSkipBlancs( tokenString );
    437497    while ( *tokenString == '/' ) {
    438 
    439498        tokenString = ftestSkipBlancs( tokenString+1 );
    440499
    441500        if ( *tokenString == '@' ) {
    442             // seed specification
    443             tokenString = ftestParseSeed( tokenString );
     501            // random specification
     502            tokenString = ftestParseRandom( tokenString );
    444503        } else if ( isdigit( *tokenString ) ) {
    445504            // specification of characteristics
     
    458517    if ( *tokenString )
    459518        ftestError( EnvSyntaxError,
    460                     "extra characters in environment specification `%s'\n",
     519                    "extra characters in environment specification list `%s'\n",
    461520                    tokenString );
    462521}
     
    559618    case EnvSyntaxError:
    560619    case CanFormSpecError:
     620    case FileError:
    561621        // ftestUsage();
    562622        break;
     
    710770    // initialize environment
    711771    ftestEnv.seed = 0;
     772    ftestEnv.seedSet = false;
    712773    ftestEnv.characteristic = 0;
    713774    ftestEnv.extDegree = 0;
     
    720781    ftestParseEnv( envString );
    721782    delete [] envString;
     783}
     784//}}}
     785
     786//{{{ void ftestWriteSeed ()
     787//{{{ docu
     788//
     789// ftestWriteSeed() - write current seed to seed file.
     790//
     791//}}}
     792void
     793ftestWriteSeed ()
     794{
     795    if ( ftestSeedFile ) {
     796        FILE * seedFile = fopen( ftestSeedFile, "w" );
     797        if ( ! seedFile )
     798            ftestError( FileError,
     799                        "failed to open `%s' for writing: %s\n", ftestSeedFile,
     800                        strerror( errno ) );
     801        fprintf( seedFile, "%d\n", factoryrandom() );
     802        fclose( seedFile );
     803    }
    722804}
    723805//}}}
     
    806888        printf( "\n" );
    807889
    808         // factory version and other information
     890        // number of repetitions
    809891        ftestPrint( "circle: %d\n", "%d\n", ftestCircle );
    810         ftestPrint( "seed  : %d\n", "%d\n", ftestEnv.seed );
    811 
     892
     893        // random generator seed
     894        if ( ftestEnv.seedSet )
     895            ftestPrint( "seed  : %d\n", "%d\n", ftestEnv.seed );
     896        else
     897            ftestPrint( "seed  : <not set>\n", "<not set>\n" );
     898
     899        // factory version
    812900        const char * version;
    813901        version = strchr( factoryVersion, '=' )+2;
     
    850938//}}}
    851939
    852 //{{{ static void ftestSetSeed ()
    853 //{{{ docu
    854 //
    855 // ftestSetSeed() - set random generator seed from `ftestEnv'.
     940//{{{ static void ftestSetRandom ()
     941//{{{ docu
     942//
     943// ftestSetRandom() - set random generator seed from `ftestEnv'.
    856944//
    857945// This function is quite spurious, but I keep it for future
     
    860948//}}}
    861949static void
    862 ftestSetSeed ()
    863 {
    864     factoryseed( ftestEnv.seed );
     950ftestSetRandom ()
     951{
     952    if ( ftestEnv.seedSet )
     953        factoryseed( ftestEnv.seed );
    865954}
    866955//}}}
     
    9571046ftestSetEnv ()
    9581047{
    959     ftestSetSeed();
     1048    ftestSetRandom();
    9601049    ftestSetSwitches();
    9611050    ftestSetChar();
Note: See TracChangeset for help on using the changeset viewer.