1 | dnl $Id: ftest_util.m4,v 1.17 1998-04-06 11:09:14 schmidt Exp $ |
---|
2 | dnl |
---|
3 | dnl ftest_util.m4 - m4 macros used by the factory test environment. |
---|
4 | dnl |
---|
5 | dnl "External" macro start with prefix `ftest', "internal" macros |
---|
6 | dnl with prefix `_'. |
---|
7 | dnl |
---|
8 | dnl Almost all macros insert `#line'-preprocessor directives into |
---|
9 | dnl code to aid debugging. |
---|
10 | dnl |
---|
11 | dnl Note: Be carefull where to place the ';'! |
---|
12 | dnl |
---|
13 | dnl do not output anything of this library |
---|
14 | divert(-1) |
---|
15 | |
---|
16 | # |
---|
17 | # - internal macros. |
---|
18 | # |
---|
19 | |
---|
20 | # |
---|
21 | # _stripTWS() - strip trailing white space from $1. |
---|
22 | # |
---|
23 | define(`_stripTWS', `dnl |
---|
24 | patsubst(`$1', `[ ]*$')') |
---|
25 | |
---|
26 | # |
---|
27 | # _qstripTWS() - strip trailing white space from $1, return |
---|
28 | # quoted result. |
---|
29 | # |
---|
30 | define(`_qstripTWS', `dnl |
---|
31 | ifelse( |
---|
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 | # |
---|
49 | define(`ftestSetNameOfGame', `dnl |
---|
50 | define(`ftestAlgorithm',``$1'')dnl |
---|
51 | define(`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 | # |
---|
61 | define(`ftestPreprocInit', `dnl |
---|
62 | changecom(`//')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"' |
---|
73 | dnl') |
---|
74 | |
---|
75 | # |
---|
76 | # ftestGlobalInit() - global initialization. |
---|
77 | # |
---|
78 | define(`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 | # |
---|
88 | define(`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__" |
---|
102 | dnl') |
---|
103 | |
---|
104 | # |
---|
105 | # ftestMainExit() - clean up in main(). |
---|
106 | # |
---|
107 | define(`ftestMainExit', `dnl |
---|
108 | `#line' __line__ "__file__" |
---|
109 | `return check'') |
---|
110 | |
---|
111 | # |
---|
112 | # ftestDefaultInitializer() - return type dependent default |
---|
113 | # initializer. |
---|
114 | # |
---|
115 | # $1: type |
---|
116 | # |
---|
117 | define(`ftestDefaultInitializer', `dnl |
---|
118 | ifelse( |
---|
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 | # |
---|
133 | define(`ftestOutVar', `dnl |
---|
134 | define(`_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 | # |
---|
150 | define(`ftestInVar', `dnl |
---|
151 | define(`_ftestInType_'_qstripTWS(`$2'), `$1')dnl |
---|
152 | `$1 '_qstripTWS(`$2')ftestDefaultInitializer(`$1')`; |
---|
153 | bool ftestArgGiven$2= false'') |
---|
154 | |
---|
155 | # |
---|
156 | # ftestGetOpts() - read options. |
---|
157 | # |
---|
158 | define(`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 | # |
---|
167 | define(`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 | # |
---|
197 | define(`ftestGetInVar', `dnl |
---|
198 | ifelse( |
---|
199 | `$#', `1', |
---|
200 | ``if ( argv[ optind ] ) { |
---|
201 | ftestArgGiven$1= true; |
---|
202 | ftestRead( 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 | ftestRead( argv[ optind++ ], $1 ); |
---|
211 | } else |
---|
212 | $1 = '_qstripTWS(`$2')`;'', |
---|
213 | ``if ( ftestSearchTaggedArg( argc, argv, optind, $3) ) { |
---|
214 | ftestArgGiven$1 = true; |
---|
215 | ftestRead( argv[ optind++ ], $1 ); |
---|
216 | } else |
---|
217 | $1 = '_qstripTWS(`$2')`;'') |
---|
218 | `#line' __line__ "__file__" |
---|
219 | |
---|
220 | dnl') |
---|
221 | |
---|
222 | # |
---|
223 | # ftestArgGiven() - check whether an argument was given. |
---|
224 | # |
---|
225 | # $1: name of input variable |
---|
226 | # |
---|
227 | define(`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 | # |
---|
238 | define(`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 | # |
---|
265 | define(`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 |
---|
279 | define(`_ftestOutput', `dnl |
---|
280 | ifelse( |
---|
281 | `$#', `0', , |
---|
282 | `$#', `1', , |
---|
283 | ``; |
---|
284 | ftestPrintResult( $1, '_qstripTWS(`$2')` )'_ftestOutput(shift(shift($@)))')') |
---|
285 | |
---|
286 | define(`ftestOutput', `dnl |
---|
287 | `#line' __line__ "__file__" |
---|
288 | `ftestPrintTimer( timing_ftestTimer_time ); |
---|
289 | ftestPrintCheck( check )'_ftestOutput($@)') |
---|
290 | |
---|
291 | dnl switch on output again |
---|
292 | divert`'dnl |
---|