1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: fbinops.m4,v 1.3 1997-12-17 12:16:40 schmidt Exp $ */ |
---|
3 | |
---|
4 | ftestSetNameOfGame( fbinops, `" |
---|
5 | Usage: fbinops [<options>] [<envSpec>] <f> <operator> <g> |
---|
6 | executes operator an canonical forms f, g. |
---|
7 | |
---|
8 | The following operators (with aliases) are recognized: |
---|
9 | `+', `-', `*' (= `mul'), `/', `%': return a canonicalform; |
---|
10 | `==', `!=', `<' (= `lt'), `>' (= `gt'): return an integer (i.e. boolean) |
---|
11 | "'`' ) |
---|
12 | |
---|
13 | //{{{ docu |
---|
14 | // |
---|
15 | // ftestAlgorithm.m4 - ftestAlgorithm test program. |
---|
16 | // |
---|
17 | // To create ftestAlgorithm.cc, run m4 using the ftest_util.m4 library in |
---|
18 | // the following way: |
---|
19 | // |
---|
20 | // m4 ftest_util.m4 ftestAlgorithm.m4 > ftestAlgorithm.cc |
---|
21 | // |
---|
22 | //}}} |
---|
23 | |
---|
24 | ftestPreprocInit(); |
---|
25 | |
---|
26 | ftestGlobalInit(); |
---|
27 | |
---|
28 | //{{{ typedef binOpCFT, binOpBoolT, binOpCFTestT |
---|
29 | //{{{ docu |
---|
30 | // |
---|
31 | // typedef binOpCFT, binOpBoolT, binOpCFTestT - |
---|
32 | // pointer to functions types. |
---|
33 | // |
---|
34 | // binOpCFT, binOpBoolT: pointers to binary operators getting two |
---|
35 | // CanonicalForms and returning a CanonicalForm or a bool, resp. |
---|
36 | // binOpCFTestT: pointers to test functions getting three |
---|
37 | // CanonicalForms and returning a ftestStatusT. |
---|
38 | // |
---|
39 | //}}} |
---|
40 | typedef CanonicalForm (* binOpCFT)( const CanonicalForm &, const CanonicalForm & ); |
---|
41 | typedef bool (* binOpBoolT)( const CanonicalForm &, const CanonicalForm & ); |
---|
42 | typedef ftestStatusT (* binOpCFTestT)( const CanonicalForm &, const CanonicalForm &, const CanonicalForm & ); |
---|
43 | //}}} |
---|
44 | |
---|
45 | //{{{ struct binOpCFSpecT, binOpBoolSpecT |
---|
46 | //{{{ docu |
---|
47 | // |
---|
48 | // struct binOpCFSpecT, struct binOpBoolSpecT - types describing a binary |
---|
49 | // operator. |
---|
50 | // |
---|
51 | // binOpCFSpecT describes binary operators returning a CanonicalForm. |
---|
52 | // binOpBoolSpecT describes binary operators returning a bool. |
---|
53 | // |
---|
54 | // op: pointer to operator |
---|
55 | // test: test function |
---|
56 | // operatorName: symbolic name, used as a key |
---|
57 | // operatorTag: tag to print result |
---|
58 | // |
---|
59 | //}}} |
---|
60 | struct binOpCFSpecT |
---|
61 | { |
---|
62 | binOpCFT op; |
---|
63 | binOpCFTestT test; |
---|
64 | const char * operatorName; |
---|
65 | const char * operatorTag; |
---|
66 | }; |
---|
67 | |
---|
68 | struct binOpBoolSpecT |
---|
69 | { |
---|
70 | binOpBoolT op; |
---|
71 | const char * operatorName; |
---|
72 | const char * operatorTag; |
---|
73 | }; |
---|
74 | //}}} |
---|
75 | |
---|
76 | //{{{ comparison functions, tests |
---|
77 | static inline bool |
---|
78 | ftestBoolEquiv ( bool a, bool b ) |
---|
79 | { |
---|
80 | return ( ( !a || b ) && ( !b || a ) ); |
---|
81 | } |
---|
82 | |
---|
83 | static inline bool |
---|
84 | ftestCheckImplementation ( const CanonicalForm & f, const CanonicalForm & g ) |
---|
85 | { |
---|
86 | return ( ftestBoolEquiv( f == g, !(f != g) ) |
---|
87 | && ftestBoolEquiv( f < g, g > f ) |
---|
88 | && ftestBoolEquiv( f != g, (f < g) || (f > g) ) ); |
---|
89 | } |
---|
90 | |
---|
91 | static inline bool |
---|
92 | ftestCheckTrichotomy ( const CanonicalForm & f, const CanonicalForm & g ) |
---|
93 | { |
---|
94 | if ( f == g ) { |
---|
95 | if ( f < g ) return false; |
---|
96 | if ( g < f ) return false; |
---|
97 | return true; |
---|
98 | } else if ( f < g ) { |
---|
99 | if ( f == g ) return false; |
---|
100 | if ( g < f ) return false; |
---|
101 | return true; |
---|
102 | } else if ( g < f ) { |
---|
103 | if ( f == g ) return false; |
---|
104 | if ( f < g ) return false; |
---|
105 | return true; |
---|
106 | } else |
---|
107 | return false; |
---|
108 | } |
---|
109 | |
---|
110 | static ftestStatusT |
---|
111 | ftestRelOpTest ( const CanonicalForm & f, const CanonicalForm & g ) |
---|
112 | { |
---|
113 | // check reflexivity |
---|
114 | if ( ! ( f == f ) ) { |
---|
115 | ftestError( CheckError, "reflexivity check (f) failed\n" ); |
---|
116 | return Failed; |
---|
117 | } |
---|
118 | // check reflexivity |
---|
119 | if ( ! ( g == g ) ) { |
---|
120 | ftestError( CheckError, "reflexivity check (g) failed\n" ); |
---|
121 | return Failed; |
---|
122 | } |
---|
123 | // check symmetry |
---|
124 | if ( ! ftestBoolEquiv( f == g, g == f ) ) { |
---|
125 | ftestError( CheckError, "symmetry check failed\n" ); |
---|
126 | return Failed; |
---|
127 | } |
---|
128 | // check implementation of operators |
---|
129 | if ( ! ftestCheckImplementation( f, g ) ) { |
---|
130 | ftestError( CheckError, "implementation check (f, g) failed\n" ); |
---|
131 | return Failed; |
---|
132 | } |
---|
133 | // check implementation of operators |
---|
134 | if ( ! ftestCheckImplementation( g, f ) ) { |
---|
135 | ftestError( CheckError, "implementation check (g, f) failed\n" ); |
---|
136 | return Failed; |
---|
137 | } |
---|
138 | // check trichotomy |
---|
139 | if ( ! ftestCheckTrichotomy( f, g ) ) { |
---|
140 | ftestError( CheckError, "trichotomy check (f, g) failed\n" ); |
---|
141 | return Failed; |
---|
142 | } |
---|
143 | // check trichotomy |
---|
144 | if ( ! ftestCheckTrichotomy( g, f ) ) { |
---|
145 | ftestError( CheckError, "trichotomy check (g, f) failed\n" ); |
---|
146 | return Failed; |
---|
147 | } |
---|
148 | |
---|
149 | return Passed; |
---|
150 | } |
---|
151 | //}}} |
---|
152 | |
---|
153 | //{{{ arithmetic functions, tests |
---|
154 | static ftestStatusT |
---|
155 | ftestArithTest( const CanonicalForm &, const CanonicalForm &, const CanonicalForm & ) |
---|
156 | { |
---|
157 | return UndefinedResult; |
---|
158 | } |
---|
159 | //}}} |
---|
160 | |
---|
161 | //{{{ binOpCFSpecArray, binOpBoolSpecArray |
---|
162 | //{{{ docu |
---|
163 | // |
---|
164 | // binOpCFSpec, binOpBoolSpec - arrays of operator descriptions. |
---|
165 | // |
---|
166 | //}}} |
---|
167 | binOpCFSpecT binOpCFSpecArray[] = |
---|
168 | { |
---|
169 | { &operator+, ftestArithTest, "+", "f+g" }, |
---|
170 | { &operator-, ftestArithTest, "-", "f-g" }, |
---|
171 | { &operator*, ftestArithTest, "*", "f*g" }, |
---|
172 | { &operator*, ftestArithTest, "mul", "f*g" }, |
---|
173 | { &operator/, ftestArithTest, "/", "f/g" }, |
---|
174 | { &operator%, ftestArithTest, "%", "f%g" }, |
---|
175 | { 0, 0, 0, 0 } |
---|
176 | }; |
---|
177 | |
---|
178 | binOpBoolSpecT binOpBoolSpecArray[] = |
---|
179 | { |
---|
180 | { &operator==, "==", "f==g" }, |
---|
181 | { &operator!=, "!=", "f!=g" }, |
---|
182 | { &operator>, ">", "f>g" }, |
---|
183 | { &operator>, "gt", "f>g" }, |
---|
184 | { &operator<, "<", "f<g" }, |
---|
185 | { &operator<, "lt", "f<g" }, |
---|
186 | { 0, 0, 0 } |
---|
187 | }; |
---|
188 | //}}} |
---|
189 | |
---|
190 | // |
---|
191 | // - main program. |
---|
192 | // |
---|
193 | int |
---|
194 | main ( int argc, char ** argv ) |
---|
195 | { |
---|
196 | // initialization |
---|
197 | ftestMainInit(); |
---|
198 | |
---|
199 | // declare input and output variables |
---|
200 | ftestOutVar( CanonicalForm, resultCF ); |
---|
201 | ftestOutVar( bool, resultBool ); |
---|
202 | ftestInVar( CanonicalForm, f ); |
---|
203 | ftestInVar( CanonicalForm, g ); |
---|
204 | |
---|
205 | // process argument list and set environment |
---|
206 | ftestGetOpts(); |
---|
207 | ftestGetEnv(); |
---|
208 | |
---|
209 | // read first operand |
---|
210 | ftestGetInVar( f ); |
---|
211 | |
---|
212 | // declarations to search operator |
---|
213 | const char * operatorName = 0; |
---|
214 | const char * operatorTag = 0; |
---|
215 | binOpCFT binOpCF = 0; |
---|
216 | binOpBoolT binOpBool = 0; |
---|
217 | binOpCFTestT binOpCFTest = 0; |
---|
218 | |
---|
219 | // get and search operator |
---|
220 | if ( argv[ optind ] ) { |
---|
221 | operatorName = ftestSkipBlancs( argv[ optind++ ] ); |
---|
222 | } else |
---|
223 | ftestError( CommandlineError, |
---|
224 | "expected operator specification at position %d in commandline\n", |
---|
225 | optind ); |
---|
226 | |
---|
227 | // search through binOpCFSpecArray |
---|
228 | int i = 0; |
---|
229 | while ( binOpCFSpecArray[i].operatorName ) { |
---|
230 | if ( strcmp( binOpCFSpecArray[i].operatorName, operatorName ) == 0 ) { |
---|
231 | binOpCF = binOpCFSpecArray[i].op; |
---|
232 | binOpCFTest = binOpCFSpecArray[i].test; |
---|
233 | operatorTag = binOpCFSpecArray[i].operatorTag; |
---|
234 | break; |
---|
235 | } |
---|
236 | i++; |
---|
237 | } |
---|
238 | |
---|
239 | // search through binOpBoolSpecArray |
---|
240 | i = 0; |
---|
241 | if ( ! binOpCF ) |
---|
242 | while ( binOpBoolSpecArray[i].operatorName ) { |
---|
243 | if ( strcmp( binOpBoolSpecArray[i].operatorName, operatorName ) == 0 ) { |
---|
244 | binOpBool = binOpBoolSpecArray[i].op; |
---|
245 | operatorTag = binOpBoolSpecArray[i].operatorTag; |
---|
246 | break; |
---|
247 | } |
---|
248 | i++; |
---|
249 | } |
---|
250 | |
---|
251 | // check whether operator has been found |
---|
252 | if ( binOpCF == 0 && binOpBool == 0 ) |
---|
253 | ftestError( CommandlineError, |
---|
254 | "unknown operator `%s'\n", operatorName ); |
---|
255 | |
---|
256 | // read second operand |
---|
257 | ftestGetInVar( g ); |
---|
258 | |
---|
259 | // do the test! |
---|
260 | if ( binOpCF ) { |
---|
261 | ftestRun( |
---|
262 | resultCF = binOpCF( f, g ); ); |
---|
263 | ftestCheck( |
---|
264 | binOpCFTest( f, g, resultCF ) ); |
---|
265 | } else { |
---|
266 | ftestRun( |
---|
267 | resultBool = binOpBool( f, g ); ); |
---|
268 | ftestCheck( |
---|
269 | ftestRelOpTest( f, g ) ); |
---|
270 | } |
---|
271 | |
---|
272 | // print results |
---|
273 | if ( binOpCF ) { |
---|
274 | ftestOutput( operatorTag, resultCF ); |
---|
275 | } else { |
---|
276 | ftestOutput( operatorTag, resultBool ); |
---|
277 | } |
---|
278 | |
---|
279 | // clean up |
---|
280 | ftestMainExit(); |
---|
281 | } |
---|