1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: ftest_util.cc,v 1.12 1997-11-21 10:40:16 schmidt Exp $ */ |
---|
3 | |
---|
4 | //{{{ docu |
---|
5 | // |
---|
6 | // ftest_util.cc - some utilities for the factory test environment. |
---|
7 | // |
---|
8 | //}}} |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdarg.h> |
---|
12 | #include <stdlib.h> |
---|
13 | #include <signal.h> |
---|
14 | #include <string.h> |
---|
15 | #include <ctype.h> |
---|
16 | #include <errno.h> |
---|
17 | #include <unistd.h> |
---|
18 | #include <iostream.h> |
---|
19 | |
---|
20 | // we include timing.h for calculation of HZ only |
---|
21 | #define TIMING |
---|
22 | #include <timing.h> |
---|
23 | |
---|
24 | #include <factory.h> |
---|
25 | |
---|
26 | #include "ftest_util.h" |
---|
27 | #include "ftest_io.h" |
---|
28 | |
---|
29 | //{{{ const int CFSwitchesMax |
---|
30 | //{{{ docu |
---|
31 | // |
---|
32 | // const CFSwitchesMax - maximum number of switches. |
---|
33 | // |
---|
34 | //}}} |
---|
35 | const int CFSwitchesMax = 10; |
---|
36 | //}}} |
---|
37 | |
---|
38 | //{{{ struct varSpecT |
---|
39 | struct varSpecT |
---|
40 | { |
---|
41 | char variable; |
---|
42 | CanonicalForm mipo; |
---|
43 | varSpecT * next; |
---|
44 | }; |
---|
45 | //}}} |
---|
46 | |
---|
47 | //{{{ struct ftestEnvT |
---|
48 | //{{{ docu |
---|
49 | // |
---|
50 | // struct ftestEnvT - factory environment. |
---|
51 | // |
---|
52 | //}}} |
---|
53 | struct ftestEnvT |
---|
54 | { |
---|
55 | int seed; |
---|
56 | bool seedSet; |
---|
57 | int characteristic; |
---|
58 | int extDegree; |
---|
59 | char extGen; |
---|
60 | varSpecT * varSpec; |
---|
61 | bool switches[CFSwitchesMax]; |
---|
62 | }; |
---|
63 | //}}} |
---|
64 | |
---|
65 | //{{{ external variables |
---|
66 | //{{{ docu |
---|
67 | // |
---|
68 | // - external variables. |
---|
69 | // |
---|
70 | // ftestCircle: set by ftestGetOpts() from commandline, read by |
---|
71 | // main(). Number of test circles. |
---|
72 | // ftestAlarm: set by ftestGetOpts() from acommandline, read by |
---|
73 | // main(). |
---|
74 | // ftestPrintFlag: set by ftestParseOutputType() from |
---|
75 | // commandline, read by ftestPrintResult(). True iff there was |
---|
76 | // some output type specifier on commandline. |
---|
77 | // ftestPrintResultFlag: set by ftestParseOutputType() from |
---|
78 | // commandline, read by output routines. Whether to print result |
---|
79 | // or not. |
---|
80 | // |
---|
81 | //}}} |
---|
82 | int ftestCircle = 1; |
---|
83 | int ftestAlarm = 0; |
---|
84 | |
---|
85 | int ftestPrintFlag = 0; |
---|
86 | int ftestPrintResultFlag = 0; |
---|
87 | //}}} |
---|
88 | |
---|
89 | //{{{ static variables |
---|
90 | //{{{ docu |
---|
91 | // |
---|
92 | // - static variables. |
---|
93 | // |
---|
94 | // ftestPrint*Flag: set by ftestParseOutputType() from |
---|
95 | // commandline, read by output routines. Things to print/not to |
---|
96 | // print. Note that we need `ftestPrintFlag' and |
---|
97 | // `ftestPrintResultFlag' to be external. |
---|
98 | // ftestExecName, ftestAlgorithmName: set by ftestSetName(), read |
---|
99 | // by output routines. Name of executable and algorithm. |
---|
100 | // ftestUsage: set by ftestSetName(), read by ftestUsagePrint(). |
---|
101 | // Algorithm-specific usage information. |
---|
102 | // ftestEnv: factory environment specification. Set by |
---|
103 | // ftestGetEnv(), read by ftestPrintEnv() function. This |
---|
104 | // variable is quite spurious, but I keep it for future use. |
---|
105 | // ftestSwitchNames: symbolic names of switches |
---|
106 | // ftestSeedFile: file where to read/to which to print the random |
---|
107 | // generator seed. Set by ftestParseRandom(), read by |
---|
108 | // ftestWriteSeed(). |
---|
109 | // |
---|
110 | //}}} |
---|
111 | static int ftestPrintTimingFlag = 0; |
---|
112 | static int ftestPrintCheckFlag = 0; |
---|
113 | static int ftestPrintEnvFlag = 0; |
---|
114 | static int ftestPrintShortFlag = 0; |
---|
115 | |
---|
116 | const char * ftestExecName = 0; |
---|
117 | const char * ftestAlgorithmName = 0; |
---|
118 | const char * ftestUsage = 0; |
---|
119 | |
---|
120 | static ftestEnvT ftestEnv; |
---|
121 | |
---|
122 | const char * ftestSwitchNames[] = |
---|
123 | { |
---|
124 | "RATIONAL", |
---|
125 | "QUOTIENT", |
---|
126 | "SYMMETRIC_FF", |
---|
127 | "BERLEKAMP", |
---|
128 | "FAC_USE_BIG_PRIMES", |
---|
129 | "FAC_QUADRATICLIFT", |
---|
130 | "USE_EZGCD", |
---|
131 | "USE_SPARSEMOD" |
---|
132 | }; |
---|
133 | |
---|
134 | static char * ftestSeedFile = 0; |
---|
135 | //}}} |
---|
136 | |
---|
137 | // |
---|
138 | // - static functions. |
---|
139 | // |
---|
140 | |
---|
141 | //{{{ static const char * ftestSubStr( const char * subString, const char * string ) |
---|
142 | //{{{ docu |
---|
143 | // |
---|
144 | // ftestSubStr() - check whether subString is a substring of string. |
---|
145 | // |
---|
146 | // If so, return index behind subString in string, otherwise |
---|
147 | // string. |
---|
148 | // |
---|
149 | //}}} |
---|
150 | static const char * |
---|
151 | ftestSubStr( const char * subString, const char * string ) |
---|
152 | { |
---|
153 | const char * stringStart = string; |
---|
154 | |
---|
155 | while ( *subString && *subString == *string ) { |
---|
156 | subString++; string++; |
---|
157 | } |
---|
158 | |
---|
159 | if ( *subString ) |
---|
160 | return stringStart; |
---|
161 | else |
---|
162 | return string; |
---|
163 | } |
---|
164 | //}}} |
---|
165 | |
---|
166 | //{{{ static char * ftestConcatEnv ( char ** argv, int & optind ) |
---|
167 | //{{{ docu |
---|
168 | // |
---|
169 | // ftestConcatEnv() - concatenate all environment information |
---|
170 | // together and return the result. |
---|
171 | // |
---|
172 | // The new index into the commandline is returned in optind. |
---|
173 | // |
---|
174 | //}}} |
---|
175 | static char * |
---|
176 | ftestConcatEnv ( char ** argv, int & optind ) |
---|
177 | { |
---|
178 | // first get length |
---|
179 | int i = optind; |
---|
180 | int len = 0; |
---|
181 | const char * envString = getenv( "FTEST_ENV" ); |
---|
182 | if ( envString ) |
---|
183 | len = strlen( envString ); |
---|
184 | |
---|
185 | while ( (argv[i] != 0) && (*ftestSkipBlancs( argv[i] ) == '/') ) { |
---|
186 | len += strlen( argv[i] ); |
---|
187 | i++; |
---|
188 | } |
---|
189 | |
---|
190 | char * fEnvString = new char[len+1]; |
---|
191 | |
---|
192 | // now build string |
---|
193 | fEnvString[0] = '\0'; |
---|
194 | if ( envString ) |
---|
195 | strcat( fEnvString, envString ); |
---|
196 | |
---|
197 | while ( optind < i ) { |
---|
198 | strcat( fEnvString, argv[optind] ); |
---|
199 | optind++; |
---|
200 | } |
---|
201 | |
---|
202 | return fEnvString; |
---|
203 | } |
---|
204 | //}}} |
---|
205 | |
---|
206 | //{{{ static const char * ftestParseRandom ( const char * tokenString ) |
---|
207 | //{{{ docu |
---|
208 | // |
---|
209 | // ftestParseRandom() - parse random specification and set factory's |
---|
210 | // random generator seed. |
---|
211 | // |
---|
212 | // The results are also stored in global variable `ftestEnv'. |
---|
213 | // Returns pointer behind last parsed character in tokenString. |
---|
214 | // |
---|
215 | //}}} |
---|
216 | static const char * |
---|
217 | ftestParseRandom ( const char * tokenString ) |
---|
218 | { |
---|
219 | int seed = 0; |
---|
220 | bool seedSet = false; |
---|
221 | |
---|
222 | do { |
---|
223 | const char * tokenCursor; |
---|
224 | |
---|
225 | // we skip either `@' or `,' |
---|
226 | tokenString = ftestSkipBlancs( tokenString+1 ); |
---|
227 | if ( isdigit( *tokenString ) ) { |
---|
228 | // read seed specification from command line |
---|
229 | seed = (int)strtol( tokenString, (char **)&tokenCursor, 0 ); |
---|
230 | if ( tokenCursor == tokenString ) |
---|
231 | ftestError( EnvSyntaxError, |
---|
232 | "bad seed specification `%s'\n", |
---|
233 | tokenString ); |
---|
234 | seedSet = true; |
---|
235 | } else if ( isalpha( *tokenString ) ) { |
---|
236 | // read seed specification from file |
---|
237 | tokenCursor = strpbrk( tokenString, ",/" ); |
---|
238 | if ( ! tokenCursor ) |
---|
239 | tokenCursor = strchr( tokenString, '\0' ); |
---|
240 | |
---|
241 | // store file name |
---|
242 | int nameLen = tokenCursor-tokenString; |
---|
243 | ftestSeedFile = new char[nameLen+1]; |
---|
244 | strncpy( ftestSeedFile, tokenString, nameLen ); |
---|
245 | ftestSeedFile[nameLen] = '\0'; |
---|
246 | |
---|
247 | // open file, read seed, close file |
---|
248 | FILE * seedFile = fopen( ftestSeedFile, "r" ); |
---|
249 | if ( seedFile ) { |
---|
250 | // if file exists, read seed |
---|
251 | if ( fscanf( seedFile, "%d", &seed ) != 1 ) |
---|
252 | ftestError( FileError, |
---|
253 | "garbled seed in seed file `%s'\n", ftestSeedFile ); |
---|
254 | fclose( seedFile ); |
---|
255 | |
---|
256 | seedSet = true; |
---|
257 | } else |
---|
258 | if ( errno != ENOENT ) |
---|
259 | ftestError( FileError, |
---|
260 | "failed to open `%s' for reading: %s\n", ftestSeedFile, |
---|
261 | strerror( errno ) ); |
---|
262 | else if ( ! (ftestEnv.seedSet || seedSet) ) { |
---|
263 | // if file does not exist, set seed to some |
---|
264 | // default value if it has not been set before |
---|
265 | seed = 0; |
---|
266 | seedSet = true; |
---|
267 | } |
---|
268 | } else |
---|
269 | ftestError( EnvSyntaxError, |
---|
270 | "bad random specification `%s'\n", tokenString ); |
---|
271 | |
---|
272 | tokenString = ftestSkipBlancs( tokenCursor ); |
---|
273 | } while ( *tokenString == ',' ); |
---|
274 | |
---|
275 | // set seed |
---|
276 | ftestEnv.seedSet = seedSet; |
---|
277 | if ( seedSet ) { |
---|
278 | ftestEnv.seed = seed; |
---|
279 | factoryseed( seed ); |
---|
280 | } |
---|
281 | |
---|
282 | return tokenString; |
---|
283 | } |
---|
284 | //}}} |
---|
285 | |
---|
286 | //{{{ static const char * ftestParseChar ( const char * tokenString ) |
---|
287 | //{{{ docu |
---|
288 | // |
---|
289 | // ftestParseChar() - parse characteristic specification and set factory's |
---|
290 | // characteristic. |
---|
291 | // |
---|
292 | // The results are also stored in global variable `ftestEnv'. |
---|
293 | // Returns pointer behind last parsed character in tokenString. |
---|
294 | // |
---|
295 | //}}} |
---|
296 | static const char * |
---|
297 | ftestParseChar ( const char * tokenString ) |
---|
298 | { |
---|
299 | const char * tokenCursor; |
---|
300 | int characteristic = 0; |
---|
301 | int extDegree = 0; |
---|
302 | char extGen = 'Z'; |
---|
303 | |
---|
304 | characteristic = (int)strtol( tokenString, (char **)&tokenCursor, 0 ); |
---|
305 | tokenString = ftestSkipBlancs( tokenCursor ); |
---|
306 | |
---|
307 | // look for exponent |
---|
308 | if ( *tokenString == '^' ) { |
---|
309 | tokenString++; |
---|
310 | extDegree = (int)strtol( tokenString, (char **)&tokenCursor, 0 ); |
---|
311 | if ( tokenCursor == tokenString ) |
---|
312 | ftestError( EnvSyntaxError, |
---|
313 | "bad exponent in char specification `%s'\n", |
---|
314 | tokenString ); |
---|
315 | tokenString = ftestSkipBlancs( tokenCursor ); |
---|
316 | // look for generator |
---|
317 | if ( *tokenString == ',' ) { |
---|
318 | tokenString = ftestSkipBlancs( tokenString+1 ); |
---|
319 | if ( isalpha( *tokenString ) ) { |
---|
320 | extGen = *tokenString; |
---|
321 | tokenString++; |
---|
322 | } else |
---|
323 | ftestError( EnvSyntaxError, |
---|
324 | "bad generator in char specification `%s'\n", |
---|
325 | tokenString ); |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | // set characteristic |
---|
330 | ftestEnv.characteristic = characteristic; |
---|
331 | if ( extDegree ) { |
---|
332 | ftestEnv.extDegree = extDegree; |
---|
333 | ftestEnv.extGen = extGen; |
---|
334 | setCharacteristic( characteristic, extDegree, extGen ); |
---|
335 | } else |
---|
336 | setCharacteristic( characteristic ); |
---|
337 | |
---|
338 | return tokenString; |
---|
339 | } |
---|
340 | //}}} |
---|
341 | |
---|
342 | //{{{ static const char * ftestParseSwitches ( const char * tokenString ) |
---|
343 | //{{{ docu |
---|
344 | // |
---|
345 | // ftestParseSwitches() - parse switch specification and set factory's |
---|
346 | // switches. |
---|
347 | // |
---|
348 | // The results are also stored in global variable `ftestEnv'. |
---|
349 | // Returns pointer behind last parsed character in tokenString. |
---|
350 | // |
---|
351 | //}}} |
---|
352 | static const char * |
---|
353 | ftestParseSwitches ( const char * tokenString ) |
---|
354 | { |
---|
355 | unsigned int switchNumber; |
---|
356 | bool switchSetting; |
---|
357 | |
---|
358 | while ( *tokenString == '+' || *tokenString == '-' ) { |
---|
359 | const char * tokenCursor; |
---|
360 | if ( *tokenString == '+' ) |
---|
361 | switchSetting = true; |
---|
362 | else |
---|
363 | switchSetting = false; |
---|
364 | tokenString = ftestSkipBlancs( tokenString+1 ); |
---|
365 | |
---|
366 | // skip optional leading "SW_" |
---|
367 | tokenString = ftestSubStr( "SW_", tokenString ); |
---|
368 | |
---|
369 | // now search name of switch |
---|
370 | for ( switchNumber = 0; |
---|
371 | switchNumber < sizeof( ftestSwitchNames ) / sizeof( char* ); |
---|
372 | switchNumber++ ) |
---|
373 | if ( (tokenCursor = ftestSubStr( ftestSwitchNames[switchNumber], tokenString )) |
---|
374 | != tokenString ) |
---|
375 | break; |
---|
376 | |
---|
377 | if ( tokenCursor == tokenString ) |
---|
378 | ftestError( EnvSyntaxError, |
---|
379 | "unknown switch `%s'\n", tokenString ); |
---|
380 | |
---|
381 | // set switch |
---|
382 | ftestEnv.switches[switchNumber] = switchSetting; |
---|
383 | if ( switchSetting ) |
---|
384 | On( switchNumber ); |
---|
385 | else |
---|
386 | Off( switchNumber ); |
---|
387 | |
---|
388 | tokenString = ftestSkipBlancs( tokenCursor ); |
---|
389 | } |
---|
390 | |
---|
391 | return tokenString; |
---|
392 | } |
---|
393 | //}}} |
---|
394 | |
---|
395 | //{{{ static const char * ftestParseVars ( const char * tokenString ) |
---|
396 | //{{{ docu |
---|
397 | // |
---|
398 | // ftestParseVars() - parse variable specification and set factory's |
---|
399 | // variable. |
---|
400 | // |
---|
401 | // The results are also stored in global variable `ftestEnv'. |
---|
402 | // Returns pointer behind last parsed character in tokenString. |
---|
403 | // |
---|
404 | //}}} |
---|
405 | static const char * |
---|
406 | ftestParseVars ( const char * tokenString ) |
---|
407 | { |
---|
408 | char variable; |
---|
409 | CanonicalForm mipo = 0; |
---|
410 | Variable * dummy; |
---|
411 | |
---|
412 | // we append at end of list |
---|
413 | static varSpecT * endOfVarSpec = 0; |
---|
414 | varSpecT * varSpec; |
---|
415 | |
---|
416 | while ( isalpha( *tokenString ) ) { |
---|
417 | variable = *tokenString; |
---|
418 | mipo = 0; |
---|
419 | |
---|
420 | tokenString = ftestSkipBlancs( tokenString+1 ); |
---|
421 | |
---|
422 | // look for mipo |
---|
423 | if ( *tokenString == '=' ) { |
---|
424 | const char * tokenCursor; |
---|
425 | tokenString = ftestSkipBlancs( tokenString+1 ); |
---|
426 | |
---|
427 | // search for end of mipo |
---|
428 | tokenCursor = strpbrk( tokenString, ",/" ); |
---|
429 | if ( ! tokenCursor ) |
---|
430 | tokenCursor = strchr( tokenString, '\0' ); |
---|
431 | |
---|
432 | // copy mipo to new string and read it |
---|
433 | int mipoLen = tokenCursor-tokenString; |
---|
434 | char * mipoString = new char[mipoLen+1]; |
---|
435 | strncpy( mipoString, tokenString, mipoLen ); |
---|
436 | mipoString[mipoLen] = '\0'; |
---|
437 | mipo = ftestGetCanonicalForm( mipoString ); |
---|
438 | delete [] mipoString; |
---|
439 | |
---|
440 | tokenString = ftestSkipBlancs( tokenCursor ); |
---|
441 | } |
---|
442 | if ( *tokenString == ',' ) |
---|
443 | // skip optional separator |
---|
444 | tokenString = ftestSkipBlancs( tokenString+1 ); |
---|
445 | |
---|
446 | // store information in ftestEnv |
---|
447 | varSpec = new varSpecT; |
---|
448 | varSpec->variable = variable; |
---|
449 | varSpec->mipo = mipo; |
---|
450 | varSpec->next = 0; |
---|
451 | if ( ftestEnv.varSpec ) |
---|
452 | endOfVarSpec->next = varSpec; |
---|
453 | else |
---|
454 | ftestEnv.varSpec = varSpec; |
---|
455 | endOfVarSpec = varSpec; |
---|
456 | |
---|
457 | // set variable |
---|
458 | if ( mipo.isZero() ) |
---|
459 | // polynomial variable |
---|
460 | dummy = new Variable( variable ); |
---|
461 | else |
---|
462 | dummy = new Variable( rootOf( mipo, variable ) ); |
---|
463 | // we only need the global information |
---|
464 | delete dummy; |
---|
465 | } |
---|
466 | |
---|
467 | return tokenString; |
---|
468 | } |
---|
469 | //}}} |
---|
470 | |
---|
471 | //{{{ static void ftestParseEnv ( const char * tokenString ) |
---|
472 | //{{{ docu |
---|
473 | // |
---|
474 | // ftestParseEnv() - parse environment specification and set factory's |
---|
475 | // environment. |
---|
476 | // |
---|
477 | // The results are also stored in global variable `ftestEnv'. |
---|
478 | // |
---|
479 | // The parser is quite simple. As long as `tokenString' starts |
---|
480 | // with a `/', he looks up the next character to determine which |
---|
481 | // token follows and parses it. The functions used to do this |
---|
482 | // should leave `tokenString' behind the last character it |
---|
483 | // succeeded to read. |
---|
484 | // |
---|
485 | //}}} |
---|
486 | static void |
---|
487 | ftestParseEnv ( const char * tokenString ) |
---|
488 | { |
---|
489 | // read list of environment specifications |
---|
490 | tokenString = ftestSkipBlancs( tokenString ); |
---|
491 | while ( *tokenString == '/' ) { |
---|
492 | tokenString = ftestSkipBlancs( tokenString+1 ); |
---|
493 | |
---|
494 | if ( *tokenString == '@' ) { |
---|
495 | // random specification |
---|
496 | tokenString = ftestParseRandom( tokenString ); |
---|
497 | } else if ( isdigit( *tokenString ) ) { |
---|
498 | // specification of characteristics |
---|
499 | tokenString = ftestParseChar( tokenString ); |
---|
500 | } else if ( *tokenString == '+' || *tokenString == '-' ) { |
---|
501 | // specification of switches |
---|
502 | tokenString = ftestParseSwitches( tokenString ); |
---|
503 | } else if ( isalpha( *tokenString ) ) { |
---|
504 | // specification of variables |
---|
505 | tokenString = ftestParseVars( tokenString ); |
---|
506 | } |
---|
507 | tokenString = ftestSkipBlancs( tokenString ); |
---|
508 | } |
---|
509 | |
---|
510 | // check if we reached end |
---|
511 | if ( *tokenString ) |
---|
512 | ftestError( EnvSyntaxError, |
---|
513 | "extra characters in environment specification list `%s'\n", |
---|
514 | tokenString ); |
---|
515 | } |
---|
516 | //}}} |
---|
517 | |
---|
518 | //{{{ static void ftestParseOutputType ( const char * outputType ) |
---|
519 | //{{{ docu |
---|
520 | // |
---|
521 | // ftestParseOutputType() - parse output type and set ftestPrint*Flags. |
---|
522 | // |
---|
523 | //}}} |
---|
524 | static void |
---|
525 | ftestParseOutputType ( const char * outputType ) |
---|
526 | { |
---|
527 | ftestPrintFlag = 1; |
---|
528 | while ( *outputType ) { |
---|
529 | switch ( *outputType ) { |
---|
530 | case 'r': ftestPrintResultFlag = 1; break; |
---|
531 | case 't': ftestPrintTimingFlag = 1; break; |
---|
532 | case 'c': ftestPrintCheckFlag = 1; break; |
---|
533 | case 'e': ftestPrintEnvFlag = 1; break; |
---|
534 | case 's': ftestPrintShortFlag = 1; break; |
---|
535 | case 'a': |
---|
536 | ftestPrintResultFlag = 1; |
---|
537 | ftestPrintCheckFlag = 1; |
---|
538 | ftestPrintTimingFlag = 1; |
---|
539 | ftestPrintEnvFlag = 1; break; |
---|
540 | default: ftestError( CommandlineError, "unknown output type specifier `%c'\n", *outputType ); |
---|
541 | } |
---|
542 | outputType++; |
---|
543 | } |
---|
544 | } |
---|
545 | //}}} |
---|
546 | |
---|
547 | //{{{ static void ftestSignalHandler ( int signal ), static void ftestAlarmlHandler ( int ) |
---|
548 | //{{{ docu |
---|
549 | // |
---|
550 | // ftestSignalHandler(), ftestAlarmHandler() - signal handlers. |
---|
551 | // |
---|
552 | // Blocks new signals, flushes `cout', and calls `ftestError()'. |
---|
553 | // |
---|
554 | //}}} |
---|
555 | static void |
---|
556 | ftestSignalHandler ( int signal ) |
---|
557 | { |
---|
558 | ftestSignalCatch( true ); |
---|
559 | cout.flush(); |
---|
560 | ftestError( (ftestErrorT)((int)SignalError + signal ), |
---|
561 | "received signal %d\n", signal ); |
---|
562 | } |
---|
563 | static void |
---|
564 | ftestAlarmHandler ( int ) |
---|
565 | { |
---|
566 | ftestSignalCatch( true ); |
---|
567 | cout.flush(); |
---|
568 | ftestError( TimeoutError, "timeout after %d secs\n", ftestAlarm ); |
---|
569 | } |
---|
570 | //}}} |
---|
571 | |
---|
572 | // |
---|
573 | // - external functions. |
---|
574 | // |
---|
575 | |
---|
576 | //{{{ const char * ftestSkipBlancs ( const char * string ) |
---|
577 | //{{{ docu |
---|
578 | // |
---|
579 | // ftestSkipBlancs() - skip all leading blancs in string. |
---|
580 | // |
---|
581 | // Return new position. |
---|
582 | // |
---|
583 | //}}} |
---|
584 | const char * |
---|
585 | ftestSkipBlancs ( const char * string ) |
---|
586 | { |
---|
587 | while ( *string && isspace( *string ) ) |
---|
588 | string++; |
---|
589 | return string; |
---|
590 | } |
---|
591 | //}}} |
---|
592 | |
---|
593 | //{{{ void ftestError ( const ftestErrorT errno, const char * format ... ) |
---|
594 | //{{{ docu |
---|
595 | // |
---|
596 | // ftestError() - main error handler. |
---|
597 | // |
---|
598 | // Prints error message consisting of formatString and following |
---|
599 | // arguments, some additional information, and exits with errno. |
---|
600 | // In case of an `CheckError', does not exit but returns to the |
---|
601 | // caller. |
---|
602 | // |
---|
603 | //}}} |
---|
604 | void |
---|
605 | ftestError ( const ftestErrorT errno, const char * format ... ) |
---|
606 | { |
---|
607 | // print error message |
---|
608 | if ( format ) { |
---|
609 | if ( errno != CheckError ) |
---|
610 | fprintf( stderr, "%s: ", ftestExecName ); |
---|
611 | else |
---|
612 | fprintf( stderr, "%s(CheckError): ", ftestExecName ); |
---|
613 | va_list ap; |
---|
614 | va_start( ap, format ); |
---|
615 | vfprintf( stderr, format, ap ); |
---|
616 | va_end( ap ); |
---|
617 | } |
---|
618 | |
---|
619 | switch ( errno ) { |
---|
620 | case CommandlineError: |
---|
621 | case EnvSyntaxError: |
---|
622 | case CanFormSpecError: |
---|
623 | ftestUsagePrint(); |
---|
624 | break; |
---|
625 | case FileError: |
---|
626 | break; |
---|
627 | case CheckError: |
---|
628 | return; |
---|
629 | case TimeoutError: |
---|
630 | if ( ftestPrintTimingFlag ) |
---|
631 | ftestPrint( "time : > %.2f\n", "> %.2f\n", (float)ftestAlarm ); |
---|
632 | if ( ftestPrintCheckFlag ) |
---|
633 | ftestPrint( "check : TmeOut\n", "TmeOut\n" ); |
---|
634 | break; |
---|
635 | default: |
---|
636 | if ( ftestPrintTimingFlag ) |
---|
637 | ftestPrint( "time : -0.00\n", "-0.00\n" ); |
---|
638 | if ( ftestPrintCheckFlag ) |
---|
639 | ftestPrint( "check : Sig.%0.2d\n", "Sig.%0.2d\n", (int)errno-(int)SignalError ); |
---|
640 | } |
---|
641 | exit( errno ); |
---|
642 | } |
---|
643 | //}}} |
---|
644 | |
---|
645 | //{{{ void ftestUsagePrint ( const char * additionalUsage ) |
---|
646 | //{{{ docu |
---|
647 | // |
---|
648 | // ftestUsagePrint() - print usage message to stderr. |
---|
649 | // |
---|
650 | // We use the static variable `ftestUsage' to do so. If |
---|
651 | // `additionalUsage' is non-zero, we print it after |
---|
652 | // `ftestUsage', otherwise some short reference where to find |
---|
653 | // more information. |
---|
654 | // |
---|
655 | //}}} |
---|
656 | void |
---|
657 | ftestUsagePrint ( const char * additionalUsage ) |
---|
658 | { |
---|
659 | cerr << ftestUsage << flush; |
---|
660 | if ( ! additionalUsage ) |
---|
661 | cerr << " |
---|
662 | For a descriptions of the features common to all programs of the |
---|
663 | Factory Test Environment, call `feval -?' (exactly in this way). |
---|
664 | " << endl; |
---|
665 | else |
---|
666 | cerr << additionalUsage << endl; |
---|
667 | } |
---|
668 | //}}} |
---|
669 | |
---|
670 | //{{{ void ftestPrint ( const char * longFormat, const char * shortFormat ... ) |
---|
671 | //{{{ docu |
---|
672 | // |
---|
673 | // ftestPrint() - print according to `ftestPrintShortFlag'. |
---|
674 | // |
---|
675 | // If variable ftestPrintShortFlag is set, use shortFormat as |
---|
676 | // format, otherwise longFormat. |
---|
677 | // |
---|
678 | //}}} |
---|
679 | void |
---|
680 | ftestPrint ( const char * longFormat, const char * shortFormat ... ) |
---|
681 | { |
---|
682 | va_list ap; |
---|
683 | va_start( ap, shortFormat ); |
---|
684 | |
---|
685 | if ( ! ftestPrintShortFlag && longFormat ) |
---|
686 | vprintf( longFormat, ap ); |
---|
687 | else if ( ftestPrintShortFlag && shortFormat ) |
---|
688 | vprintf( shortFormat, ap ); |
---|
689 | |
---|
690 | va_end( ap ); |
---|
691 | } |
---|
692 | //}}} |
---|
693 | |
---|
694 | //{{{ void ftestSignalCatch ( bool block ) |
---|
695 | //{{{ docu |
---|
696 | // |
---|
697 | // ftestSignalCatch() - set signal and alarm handlers. |
---|
698 | // |
---|
699 | // If block is true block the signals instead. |
---|
700 | // |
---|
701 | //}}} |
---|
702 | void |
---|
703 | ftestSignalCatch ( bool block ) |
---|
704 | { |
---|
705 | void (*signalHandler)( int ); |
---|
706 | void (*alarmHandler)( int ); |
---|
707 | |
---|
708 | if ( block ) { |
---|
709 | signalHandler = 0; |
---|
710 | alarmHandler = 0; |
---|
711 | } else { |
---|
712 | signalHandler = ftestSignalHandler; |
---|
713 | alarmHandler = ftestAlarmHandler; |
---|
714 | } |
---|
715 | |
---|
716 | #ifdef SIGHUP |
---|
717 | signal( SIGHUP, signalHandler ); |
---|
718 | #endif |
---|
719 | #ifdef SIGINT |
---|
720 | signal( SIGINT, signalHandler ); |
---|
721 | #endif |
---|
722 | #ifdef SIGQUIT |
---|
723 | signal( SIGQUIT, signalHandler ); |
---|
724 | #endif |
---|
725 | #ifdef SIGILL |
---|
726 | signal( SIGILL, signalHandler ); |
---|
727 | #endif |
---|
728 | #ifdef SIGIOT |
---|
729 | signal( SIGIOT, signalHandler ); |
---|
730 | #endif |
---|
731 | #ifdef SIGFPE |
---|
732 | signal( SIGFPE, signalHandler ); |
---|
733 | #endif |
---|
734 | #ifdef SIGBUS |
---|
735 | signal( SIGBUS, signalHandler ); |
---|
736 | #endif |
---|
737 | #ifdef SIGSEGV |
---|
738 | signal( SIGSEGV, signalHandler ); |
---|
739 | #endif |
---|
740 | #ifdef SIGTERM |
---|
741 | signal( SIGTERM, signalHandler ); |
---|
742 | #endif |
---|
743 | |
---|
744 | // alarm handler |
---|
745 | signal( SIGALRM, alarmHandler ); |
---|
746 | } |
---|
747 | //}}} |
---|
748 | |
---|
749 | //{{{ void ftestSetName ( const char * execName, const char * algorithmName, const char * usage ) |
---|
750 | //{{{ docu |
---|
751 | // |
---|
752 | // ftestSetName() - set name of executable and algorithm. |
---|
753 | // |
---|
754 | //}}} |
---|
755 | void |
---|
756 | ftestSetName ( const char * execName, const char * algorithmName, const char * usage ) |
---|
757 | { |
---|
758 | ftestExecName = execName; |
---|
759 | ftestAlgorithmName = algorithmName; |
---|
760 | ftestUsage = usage; |
---|
761 | } |
---|
762 | //}}} |
---|
763 | |
---|
764 | //{{{ void ftestGetOpts ( const int argc, char ** argv, int & optind ) |
---|
765 | //{{{ docu |
---|
766 | // |
---|
767 | // ftestGetOpts() - read options from commandline. |
---|
768 | // |
---|
769 | // Returns new index into commandline in `optind'. |
---|
770 | // |
---|
771 | //}}} |
---|
772 | void |
---|
773 | ftestGetOpts ( const int argc, char ** argv, int & optind ) |
---|
774 | { |
---|
775 | // parse command line |
---|
776 | int optionChar; |
---|
777 | const char * outputType = 0; |
---|
778 | const char * envString = 0; |
---|
779 | |
---|
780 | // read from environment first |
---|
781 | envString = getenv( "FTEST_ALARM" ); |
---|
782 | if ( envString ) |
---|
783 | ftestAlarm = (int)strtol( envString, 0, 0 ); |
---|
784 | envString = getenv( "FTEST_CIRCLE" ); |
---|
785 | if ( envString ) |
---|
786 | ftestCircle = (int)strtol( envString, 0, 0 ); |
---|
787 | |
---|
788 | // parse options |
---|
789 | while ( (optionChar = getopt( argc, argv, "a:o:c:" )) != -1 ) { |
---|
790 | switch ( optionChar ) { |
---|
791 | case 'a': ftestAlarm = (int)strtol( optarg, 0, 0 ); break; |
---|
792 | case 'c': ftestCircle = (int)strtol( optarg, 0, 0 ); break; |
---|
793 | case 'o': outputType = optarg; break; |
---|
794 | default: ftestError( CommandlineError, 0 ); |
---|
795 | } |
---|
796 | } |
---|
797 | optind = ::optind; |
---|
798 | |
---|
799 | if ( outputType ) |
---|
800 | ftestParseOutputType( outputType ); |
---|
801 | } |
---|
802 | //}}} |
---|
803 | |
---|
804 | //{{{ void ftestGetEnv ( const int, char ** argv, int & optind ) |
---|
805 | //{{{ docu |
---|
806 | // |
---|
807 | // ftestGetEnv() - read factory environment specs, set environment. |
---|
808 | // |
---|
809 | // The new index into the commandline is returned in optind. |
---|
810 | // The results are stored in global variable `ftestEnv'. |
---|
811 | // |
---|
812 | //}}} |
---|
813 | void |
---|
814 | ftestGetEnv ( const int, char ** argv, int & optind ) |
---|
815 | { |
---|
816 | // initialize environment |
---|
817 | ftestEnv.seed = 0; |
---|
818 | ftestEnv.seedSet = false; |
---|
819 | ftestEnv.characteristic = 0; |
---|
820 | ftestEnv.extDegree = 0; |
---|
821 | ftestEnv.extGen = 'Z'; |
---|
822 | ftestEnv.varSpec = 0; |
---|
823 | for ( int i = 0; i < CFSwitchesMax; i++ ) |
---|
824 | ftestEnv.switches[i] = false; |
---|
825 | |
---|
826 | char * envString = ftestConcatEnv( argv, optind ); |
---|
827 | ftestParseEnv( envString ); |
---|
828 | delete [] envString; |
---|
829 | } |
---|
830 | //}}} |
---|
831 | |
---|
832 | //{{{ void ftestWriteSeed () |
---|
833 | //{{{ docu |
---|
834 | // |
---|
835 | // ftestWriteSeed() - write current seed to seed file. |
---|
836 | // |
---|
837 | //}}} |
---|
838 | void |
---|
839 | ftestWriteSeed () |
---|
840 | { |
---|
841 | if ( ftestSeedFile ) { |
---|
842 | FILE * seedFile = fopen( ftestSeedFile, "w" ); |
---|
843 | if ( ! seedFile ) |
---|
844 | ftestError( FileError, |
---|
845 | "failed to open `%s' for writing: %s\n", ftestSeedFile, |
---|
846 | strerror( errno ) ); |
---|
847 | fprintf( seedFile, "%d\n", factoryrandom( 0 ) ); |
---|
848 | fclose( seedFile ); |
---|
849 | } |
---|
850 | } |
---|
851 | //}}} |
---|
852 | |
---|
853 | //{{{ void ftestPrintTimer ( const long timer ) |
---|
854 | //{{{ docu |
---|
855 | // |
---|
856 | // ftestPrintTimer() - print value of timer. |
---|
857 | // |
---|
858 | //}}} |
---|
859 | void |
---|
860 | ftestPrintTimer ( const long timer ) |
---|
861 | { |
---|
862 | if ( ftestPrintTimingFlag ) |
---|
863 | ftestPrint( "time : %.2f\n", "%.2f\n", (float)timer / HZ ); |
---|
864 | } |
---|
865 | //}}} |
---|
866 | |
---|
867 | //{{{ void ftestPrintCheck ( const ftestSatusT check ) |
---|
868 | //{{{ docu |
---|
869 | // |
---|
870 | // ftestPrintCheck() - print status of checks. |
---|
871 | // |
---|
872 | //}}} |
---|
873 | void |
---|
874 | ftestPrintCheck ( const ftestStatusT check ) |
---|
875 | { |
---|
876 | if ( ftestPrintCheckFlag ) { |
---|
877 | char * checkStr = 0; |
---|
878 | |
---|
879 | switch (check) { |
---|
880 | case Passed: checkStr = "Passed"; break; |
---|
881 | case Failed: checkStr = "Failed"; break; |
---|
882 | case UndefinedResult: checkStr = "Undef."; break; |
---|
883 | default: checkStr = "Error!"; |
---|
884 | } |
---|
885 | |
---|
886 | ftestPrint( "check : %s\n", "%s\n", checkStr ); |
---|
887 | } |
---|
888 | } |
---|
889 | //}}} |
---|
890 | |
---|
891 | //{{{ void ftestPrintEnv () |
---|
892 | //{{{ docu |
---|
893 | // |
---|
894 | // ftestPrintEnv() - print current factory environment. |
---|
895 | // |
---|
896 | // The environment ist read from `ftestEnv'. |
---|
897 | // |
---|
898 | //}}} |
---|
899 | void |
---|
900 | ftestPrintEnv () |
---|
901 | { |
---|
902 | if ( ftestPrintEnvFlag ) { |
---|
903 | // characteristic |
---|
904 | if ( ftestEnv.extDegree ) |
---|
905 | ftestPrint( "char : %d^%d(%c)\n", |
---|
906 | "%d^%d(%c)\n", |
---|
907 | ftestEnv.characteristic, ftestEnv.extDegree, ftestEnv.extGen ); |
---|
908 | else |
---|
909 | ftestPrint( "char : %d\n", "%d\n", ftestEnv.characteristic ); |
---|
910 | |
---|
911 | // switches |
---|
912 | bool switchSet = false; |
---|
913 | ftestPrint( "switch: ", (char *)0 ); |
---|
914 | for ( int i = 0; i < CFSwitchesMax; i++ ) |
---|
915 | if ( ftestEnv.switches[i] ) { |
---|
916 | switchSet = true; |
---|
917 | printf( "%s ", ftestSwitchNames[i] ); |
---|
918 | } |
---|
919 | if ( ! switchSet ) |
---|
920 | printf( "(not set)\n" ); |
---|
921 | else |
---|
922 | printf( "\n" ); |
---|
923 | |
---|
924 | // variables |
---|
925 | varSpecT * varSpec = ftestEnv.varSpec; |
---|
926 | ftestPrint( "vars : ", (char *)0 ); |
---|
927 | if ( ! varSpec ) |
---|
928 | printf( "(not specified)" ); |
---|
929 | else |
---|
930 | while ( varSpec ) { |
---|
931 | if ( varSpec->mipo.isZero() ) |
---|
932 | printf( "%c(%d) ", |
---|
933 | varSpec->variable, |
---|
934 | Variable( varSpec->variable ).level() ); |
---|
935 | else { |
---|
936 | printf( "%c(%d)", |
---|
937 | varSpec->variable, |
---|
938 | Variable( varSpec->variable ).level() ); |
---|
939 | cout << "(" << varSpec->mipo << ") "; |
---|
940 | } |
---|
941 | varSpec = varSpec->next; |
---|
942 | } |
---|
943 | printf( "\n" ); |
---|
944 | |
---|
945 | // number of repetitions |
---|
946 | ftestPrint( "circle: %d\n", "%d\n", ftestCircle ); |
---|
947 | |
---|
948 | // random generator seed |
---|
949 | if ( ftestEnv.seedSet ) |
---|
950 | ftestPrint( "seed : %d\n", "%d\n", ftestEnv.seed ); |
---|
951 | else |
---|
952 | ftestPrint( "seed : (not set)\n", "(not set)\n" ); |
---|
953 | |
---|
954 | // factory version |
---|
955 | const char * version; |
---|
956 | version = strchr( factoryVersion, '=' )+2; |
---|
957 | ftestPrint( "vers : %s\n", "%s\n", version ); |
---|
958 | } |
---|
959 | } |
---|
960 | //}}} |
---|
961 | |
---|
962 | // |
---|
963 | // - garbage. |
---|
964 | // |
---|
965 | |
---|
966 | #if 0 |
---|
967 | //{{{ static size_t ftestStrLen( const char * string ) |
---|
968 | //{{{ docu |
---|
969 | // |
---|
970 | // ftestStrLen() - return length of string, 0 for zero pointer. |
---|
971 | // |
---|
972 | //}}} |
---|
973 | static size_t |
---|
974 | ftestStrLen( const char * string ) |
---|
975 | { |
---|
976 | return string ? strlen( string ) : 0; |
---|
977 | } |
---|
978 | //}}} |
---|
979 | |
---|
980 | //{{{ static char * ftestStrCat (char * to, const char * from) |
---|
981 | //{{{ docu |
---|
982 | // |
---|
983 | // ftestStrCat() - concatenate (maybe empty) from and to. |
---|
984 | // |
---|
985 | //}}} |
---|
986 | static char * |
---|
987 | ftestStrCat (char * to, const char * from) |
---|
988 | { |
---|
989 | if ( from ) |
---|
990 | strcat( to, from ); |
---|
991 | return to; |
---|
992 | } |
---|
993 | //}}} |
---|
994 | |
---|
995 | //{{{ static void ftestSetRandom () |
---|
996 | //{{{ docu |
---|
997 | // |
---|
998 | // ftestSetRandom() - set random generator seed from `ftestEnv'. |
---|
999 | // |
---|
1000 | // This function is quite spurious, but I keep it for future |
---|
1001 | // use. |
---|
1002 | // |
---|
1003 | //}}} |
---|
1004 | static void |
---|
1005 | ftestSetRandom () |
---|
1006 | { |
---|
1007 | if ( ftestEnv.seedSet ) |
---|
1008 | factoryseed( ftestEnv.seed ); |
---|
1009 | } |
---|
1010 | //}}} |
---|
1011 | |
---|
1012 | //{{{ static void ftestSetChar ( bool reset = false ) |
---|
1013 | //{{{ docu |
---|
1014 | // |
---|
1015 | // ftestSetChar() - set characteristic from `ftestEnv'. |
---|
1016 | // |
---|
1017 | // If reset is true, reset characteristic to default. |
---|
1018 | // |
---|
1019 | // This function is quite spurious, but I keep it for future |
---|
1020 | // use. |
---|
1021 | // |
---|
1022 | //}}} |
---|
1023 | static void |
---|
1024 | ftestSetChar ( bool reset = false ) |
---|
1025 | { |
---|
1026 | if ( reset ) |
---|
1027 | setCharacteristic( 0 ); |
---|
1028 | else |
---|
1029 | if ( ftestEnv.extDegree ) |
---|
1030 | setCharacteristic( ftestEnv.characteristic, |
---|
1031 | ftestEnv.extDegree, |
---|
1032 | ftestEnv.extGen ); |
---|
1033 | else |
---|
1034 | setCharacteristic( ftestEnv.characteristic ); |
---|
1035 | } |
---|
1036 | //}}} |
---|
1037 | |
---|
1038 | //{{{ static void ftestSetSwitches ( bool reset = false ) |
---|
1039 | //{{{ docu |
---|
1040 | // |
---|
1041 | // ftestSetSwitches() - set switches from `ftestEnv'. |
---|
1042 | // |
---|
1043 | // If reset is true, reset switches to default. |
---|
1044 | // |
---|
1045 | // This function is quite spurious, but I keep it for future |
---|
1046 | // use. |
---|
1047 | // |
---|
1048 | //}}} |
---|
1049 | static void |
---|
1050 | ftestSetSwitches ( bool reset = false ) |
---|
1051 | { |
---|
1052 | int i; |
---|
1053 | |
---|
1054 | if ( reset ) |
---|
1055 | for ( i = 0; i < CFSwitchesMax; i ++ ) |
---|
1056 | Off( i ); |
---|
1057 | else |
---|
1058 | for ( i = 0; i < CFSwitchesMax; i ++ ) |
---|
1059 | if ( ftestEnv.switches[i] ) |
---|
1060 | On( i ); |
---|
1061 | } |
---|
1062 | //}}} |
---|
1063 | |
---|
1064 | //{{{ static void ftestSetVars () |
---|
1065 | //{{{ docu |
---|
1066 | // |
---|
1067 | // ftestSetVars() - set variables from `ftestEnv'. |
---|
1068 | // |
---|
1069 | // This function is quite spurious, but I keep it for future |
---|
1070 | // use. |
---|
1071 | // |
---|
1072 | //}}} |
---|
1073 | static void |
---|
1074 | ftestSetVars () |
---|
1075 | { |
---|
1076 | varSpecT * varSpec = ftestEnv.varSpec; |
---|
1077 | Variable * dummy; |
---|
1078 | |
---|
1079 | while ( varSpec ) { |
---|
1080 | if ( varSpec->mipo.isZero() ) |
---|
1081 | // polynomial variable |
---|
1082 | dummy = new Variable( varSpec->variable ); |
---|
1083 | else |
---|
1084 | dummy = new Variable( rootOf( varSpec->mipo, varSpec->variable ) ); |
---|
1085 | |
---|
1086 | varSpec = varSpec->next; |
---|
1087 | } |
---|
1088 | } |
---|
1089 | //}}} |
---|
1090 | |
---|
1091 | //{{{ void ftestSetEnv () |
---|
1092 | //{{{ docu |
---|
1093 | // |
---|
1094 | // ftestSetEnv() - set environment from `ftestEnv'. |
---|
1095 | // |
---|
1096 | // This function is quite spurious, but I keep it for future |
---|
1097 | // use. |
---|
1098 | // |
---|
1099 | //}}} |
---|
1100 | void |
---|
1101 | ftestSetEnv () |
---|
1102 | { |
---|
1103 | ftestSetRandom(); |
---|
1104 | ftestSetSwitches(); |
---|
1105 | ftestSetChar(); |
---|
1106 | ftestSetVars(); |
---|
1107 | } |
---|
1108 | //}}} |
---|
1109 | #endif |
---|