source: git/factory/ftest/ftest_util.m4 @ 341696

spielwiese
Last change on this file since 341696 was 341696, checked in by Hans Schönemann <hannes@…>, 14 years ago
Adding Id property to all files git-svn-id: file:///usr/local/Singular/svn/trunk@12231 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.9 KB
Line 
1dnl $Id$
2dnl
3dnl ftest_util.m4 - m4 macros used by the factory test environment.
4dnl
5dnl "External" macro start with prefix `ftest', "internal" macros
6dnl with prefix `_'.
7dnl
8dnl Almost all macros insert `#line'-preprocessor directives into
9dnl code to aid debugging.
10dnl
11dnl Note: Be carefull where to place the ';'!
12dnl
13dnl do not output anything of this library
14divert(-1)
15
16#
17# - internal macros.
18#
19
20#
21# _stripTWS() - strip trailing white space from $1.
22#
23define(`_stripTWS', `dnl
24patsubst(`$1', `[       ]*$')')
25
26#
27# _qstripTWS() - strip trailing white space from $1, return
28#   quoted result.
29#
30define(`_qstripTWS', `dnl
31ifelse(
32  translit(`$1', `      '), `', ,
33  `patsubst(`$1', `\(.*[^       ]\)[    ]*$', ``\1'')')')
34
35
36#
37# - external macros.
38#
39
40#
41# ftestSetNameOfGame() - set name of game.
42#
43# $1: name of algorithm
44# $2: usage of algorithm
45#
46# These are stored in the macros `ftestAlgorithm' and
47# `ftestUsage', resp.  Leaves notice on creator of this file.
48#
49define(`ftestSetNameOfGame', `dnl
50define(`ftestAlgorithm',``$1'')dnl
51define(`ftestUsage',``$2'')dnl
52`/* This file was automatically generated by m4 using the ftest_util.m4 library */'')
53
54#
55# ftestPreprocInit() - initial preprocessor directives.
56#
57# In addition, change m4's comment character.  Change it in
58# this place (and not earlier) because we want to replace
59# `ftestAlgorithm' in the file-docu of the generated file.
60#
61define(`ftestPreprocInit', `dnl
62changecom(`//')dnl
63`#line' __line__ "__file__"
64`#include <unistd.h>
65
66#define TIMING
67#include <timing.h>
68
69#include <factory.h>
70
71#include "ftest_util.h"
72#include "ftest_io.h"'
73dnl')
74
75#
76# ftestGlobalInit() - global initialization.
77#
78define(`ftestGlobalInit', `dnl
79`#line' __line__ "__file__"
80`TIMING_DEFINE_PRINT( ftestTimer )'')
81
82#
83# ftestMainInit() - initialization in main().
84#
85# Set the name of the game, check for missing arguments (in
86# this case print the usage and exit), and catche signals.
87#
88define(`ftestMainInit', `dnl
89`#line' __line__ "__file__"
90    `int optind = 0;
91    ftestStatusT check = UndefinedResult;
92
93    ftestSetName( argv[0], "'ftestAlgorithm`", 'ftestUsage`);
94
95    if ( argc == 1 ) {
96        ftestUsagePrint();
97        exit( 0 );
98    }
99
100    ftestSignalCatch();
101    #line' __line__ "__file__"
102dnl')
103
104#
105# ftestMainExit() - clean up in main().
106#
107define(`ftestMainExit', `dnl
108`#line' __line__ "__file__"
109    `return check'')
110
111#
112# ftestDefaultInitializer() - return type dependent default
113#   initializer.
114#
115# $1: type
116#
117define(`ftestDefaultInitializer', `dnl
118ifelse(
119  `$1', `int', `` = 0'',
120  `$1', `bool', `` = false'',
121  `')')
122
123#
124# ftestOutVar() - declare output variable.
125#
126# $1: type of output variable
127# $2: name of output variable
128#
129# Stores type of variable in macro _ftestOutType_<name>.  Does
130# some extra magic for internal data types to avoid warnings on
131# uninitialized variables.
132#
133define(`ftestOutVar', `dnl
134define(`_ftestOutType_'_qstripTWS(`$2'), `$1')dnl
135`$1 '_qstripTWS(`$2')ftestDefaultInitializer(`$1')')
136
137#
138# ftestInVar() - declare input variable.
139#
140# $1: type of input variable
141# $2: name of input variable
142#
143# Stores type of variable in macro _ftestInType_<name>.
144# Furthermore, declares a variable ftestArgGiven<name> for later
145# checks whether this variable has been set from commandline or
146# not.
147# Does some extra magic for internal data types to avoid warnings
148# on uninitialized variables.
149#
150define(`ftestInVar', `dnl
151define(`_ftestInType_'_qstripTWS(`$2'), `$1')dnl
152`$1 '_qstripTWS(`$2')ftestDefaultInitializer(`$1')`;
153    bool ftestArgGiven$2= false'')
154
155#
156# ftestGetOpts() - read options.
157#
158define(`ftestGetOpts', `dnl
159`#line' __line__ "__file__"
160    `ftestGetOpts( argc, argv, optind )'')
161
162#
163# ftestGetEnv() - read environment.
164#
165# And print it directly after reading it.
166#
167define(`ftestGetEnv', `dnl
168`#line' __line__ "__file__"
169    `ftestGetEnv( argc, argv, optind );
170
171    ftestPrintEnv();
172
173'dnl')
174
175#
176# ftestGetInVar() - read variable from command line.
177#
178# $1: name of input variable
179# $2: default for optional command line arguments
180# $3: search for optional argument with this tag
181#
182# Before reading the argument, check whether it really exists.
183# If so, call the appropriate function to convert the string into
184# the type of the variable you are reading.  If there are not any
185# more arguments, and there is no default specified, print an
186# error.  If there is a default value, use it instead.
187#
188# If optional third argument is given, call
189# `ftestSearchTaggedArg()' to check whether there is a tagged
190# optional argument `<tag> = <value>' where `<tag>' is a
191# substring of `$3'.  Use `<value>' as value for the input
192# variable in this case, `$2' otherwise.
193#
194# In any case, save the fact whether the argument was given or
195# not in the variable ftestArgGiven<name>.
196#
197define(`ftestGetInVar', `dnl
198ifelse(
199  `$#', `1',
200  ``if ( argv[ optind ] ) {
201        ftestArgGiven$1= true;
202        ftestReadString( argv[ optind++ ], $1);
203    } else
204        ftestError( CommandlineError,
205                    "expected '_stripTWS(`_ftestInType_$1')` at position %d in commandline\n",
206                    optind );'',
207  `$#', `2',
208  ``if ( argv[ optind ] ) {
209        ftestArgGiven$1= true;
210        ftestReadString( argv[ optind++ ], $1 );
211    } else
212        $1 = '_qstripTWS(`$2')`;'',
213  ``if ( ftestSearchTaggedArg( argc, argv, optind, $3) ) {
214        ftestArgGiven$1 = true;
215        ftestReadString( argv[ optind++ ], $1 );
216    } else
217        $1 = '_qstripTWS(`$2')`;'')
218    `#line' __line__ "__file__"
219
220dnl')
221
222#
223# ftestArgGiven() - check whether an argument was given.
224#
225# $1: name of input variable
226#
227define(`ftestArgGiven', `dnl
228`ftestArgGiven'_qstripTWS(`$1')')
229
230#
231# ftestRun() - run test.
232#
233# $1: code to execute
234#
235# Do not forget to terminate the code to execute with a
236# semicolon!
237#
238define(`ftestRun', `dnl
239`#line' __line__ "__file__"
240    `// check for superfluous command line arguments
241    if ( argv[ optind ] )
242        ftestError( CommandlineError, "superfluous argument `%s' at position %d",
243                    argv[ optind ], optind );
244
245    // save random generator seed now since the algorithm
246    // most likely is going to change it
247    ftestWriteSeed();
248
249    if ( ftestAlarm )
250        alarm( ftestAlarm );            // set alarm
251    TIMING_START(ftestTimer);
252    while ( ftestCircle > 0 ) {
253        $1
254        ftestCircle--;
255    };
256    TIMING_END(ftestTimer);
257    if ( ftestAlarm )                   // reset alarm
258        alarm( 0 )'')
259
260#
261# ftestCheck() - run check.
262#
263# $1: check function (with parameters) to call
264#
265define(`ftestCheck', `dnl
266`#line' __line__ "__file__"
267    `if ( ftestCheckFlag )
268        check = '_qstripTWS(`$1')')
269
270#
271# ftestOuput() - print results.
272#
273# Expands to code to print timer and check.  Then, for each
274# argument pair `resultName, result', expands to print this
275# pair.
276#
277
278# internal auxiliary function
279define(`_ftestOutput', `dnl
280ifelse(
281  `$#', `0', ,
282  `$#', `1', ,
283``;
284    ftestPrintResult( $1, '_qstripTWS(`$2')` )'_ftestOutput(shift(shift($@)))')')
285
286define(`ftestOutput', `dnl
287`#line' __line__ "__file__"
288    `ftestPrintTimer( timing_ftestTimer_time );
289    ftestPrintCheck( check )'_ftestOutput($@)')
290
291dnl switch on output again
292divert`'dnl
Note: See TracBrowser for help on using the repository browser.