1 | #include "machdefs.h"
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <math.h>
|
---|
5 | #include <iostream>
|
---|
6 | #include <fstream>
|
---|
7 | #include "sophyainit.h"
|
---|
8 | #include "timing.h"
|
---|
9 | #include "cexpre.h"
|
---|
10 | #include "rpneval.h"
|
---|
11 | #include "commander.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | class TCmdExecutor : public CmdExecutor {
|
---|
15 | public:
|
---|
16 | TCmdExecutor(Commander& cmd);
|
---|
17 | virtual ~TCmdExecutor() { }
|
---|
18 |
|
---|
19 | virtual int Execute(string& keyw,vector<string>& args, string& toks);
|
---|
20 | };
|
---|
21 |
|
---|
22 | TCmdExecutor::TCmdExecutor(Commander& cmd)
|
---|
23 | {
|
---|
24 | string hgrp = "TCmdExecutor";
|
---|
25 | string usage,kw;
|
---|
26 |
|
---|
27 | kw = "ls";
|
---|
28 | usage = "ls: Execute /usr/bin/ls \n";
|
---|
29 | cmd.RegisterCommand(kw, usage, this, hgrp);
|
---|
30 | kw = "mv";
|
---|
31 | usage = "mv: Execute /usr/bin/mv \n";
|
---|
32 | cmd.RegisterCommand(kw, usage, this, hgrp);
|
---|
33 | kw = "cp";
|
---|
34 | usage = "cp: Execute /usr/bin/cp \n";
|
---|
35 | cmd.RegisterCommand(kw, usage, this, hgrp);
|
---|
36 | }
|
---|
37 |
|
---|
38 | int TCmdExecutor::Execute(string& kw, vector<string>& args, string& toks)
|
---|
39 | {
|
---|
40 | if ((kw!="ls")&&(kw!="mv")&&(kw!="cp")) return 99;
|
---|
41 | string cmd;
|
---|
42 | if (kw == "ls") cmd = "/usr/bin/ls " ;
|
---|
43 | else if (kw == "mv") cmd = "/usr/bin/mv " ;
|
---|
44 | else if (kw == "cp") cmd = "/usr/bin/cp " ;
|
---|
45 | else cmd = "/usr/bin/echo " ;
|
---|
46 | for(int kk=0; kk<args.size(); kk++) { cmd += args[kk]; cmd += ' '; }
|
---|
47 | cout << "TCmdExecutor::Execute() : Executing " << cmd << endl;
|
---|
48 | return system(cmd.c_str());
|
---|
49 | }
|
---|
50 |
|
---|
51 | int InputLoop(Commander & cmd);
|
---|
52 | int tst_cexpreval(); // Test de CExpressionEvaluator
|
---|
53 | int tst_rpneval(); // Test de RPNEvaluator
|
---|
54 |
|
---|
55 | /* --Main-- */
|
---|
56 | int main(int narg, char *arg[])
|
---|
57 | {
|
---|
58 |
|
---|
59 | if (narg < 2) {
|
---|
60 | cout << " Usage: tcmd S=commander/cexptst/rpntst \n"
|
---|
61 | << " S= commander: Commader class test \n"
|
---|
62 | << " S= cexptst: CExpressionEvaluator class test \n"
|
---|
63 | << " S= rpntst: RPNExpressionEvaluator class test \n" << endl;
|
---|
64 | return 0;
|
---|
65 | }
|
---|
66 | string opt = arg[1];
|
---|
67 | SophyaInit();
|
---|
68 | InitTim();
|
---|
69 | int rc = 0;
|
---|
70 | cout << " ---- tcmd S= " << opt << " (commander/evaluator test) " << endl;
|
---|
71 | try {
|
---|
72 | if (opt == "commander") {
|
---|
73 | Commander cmd;
|
---|
74 | TCmdExecutor cmdex(cmd);
|
---|
75 | InputLoop(cmd);
|
---|
76 | }
|
---|
77 | else if (opt == "cexptst") rc = tst_cexpreval();
|
---|
78 | else if (opt == "rpntst") rc = tst_rpneval();
|
---|
79 | else {
|
---|
80 | cout << " tcmd/error: Unknown select option: " << opt << endl;
|
---|
81 | rc = 9;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | catch (PThrowable & exc) {
|
---|
86 | cerr << " Catched Exception " << (string)typeid(exc).name()
|
---|
87 | << " - Msg= " << exc.Msg() << endl;
|
---|
88 | }
|
---|
89 | catch (exception & e) {
|
---|
90 | cerr << " exception catched : e.what()= " << e.what() << endl;
|
---|
91 | }
|
---|
92 | catch (...) {
|
---|
93 | cerr << " some other exception was caught ! " << endl;
|
---|
94 | }
|
---|
95 |
|
---|
96 | PrtTim(" End of tcmd ");
|
---|
97 | PrtTim("--Fin tcmd ");
|
---|
98 | return(rc);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* --Fonction-- */
|
---|
102 | int InputLoop(Commander & cmd)
|
---|
103 | {
|
---|
104 | cout << " ====================================================== " << endl;
|
---|
105 | cout << " ============== Test of Commander class ============ " << endl;
|
---|
106 | cout << " ====================================================== " << endl;
|
---|
107 | cout << " tcmd/InputLoop() : Type in your commands, \n"
|
---|
108 | << " end with a blanck line OR <Cntl>D " << endl;
|
---|
109 | int line = 0;
|
---|
110 | bool fg = true;
|
---|
111 | char buff[1024];
|
---|
112 | char * ret;
|
---|
113 | while (fg) {
|
---|
114 | printf("%d-%s ", line+1, cmd.GetCurrentPrompt().c_str());
|
---|
115 | fflush(stdout);
|
---|
116 | buff[0] = '\0';
|
---|
117 | ret = fgets(buff, 1024, stdin);
|
---|
118 | buff[1023] = '\0';
|
---|
119 | if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) {
|
---|
120 | buff[strlen(buff)-1] = '\0';
|
---|
121 | string cline = buff;
|
---|
122 | cmd.Interpret(cline);
|
---|
123 | line++;
|
---|
124 | }
|
---|
125 | else fg = false;
|
---|
126 | }
|
---|
127 | cout << " \n Total " << line << " lines from stdin -> Commander " << endl;
|
---|
128 | if (line > 0) return(0);
|
---|
129 | else return(1);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* -- Fonction Test de CExpressionEvaluator -- */
|
---|
133 | int tst_cexpreval()
|
---|
134 | {
|
---|
135 | double res;
|
---|
136 | int nok=0;
|
---|
137 | int nerr=0;
|
---|
138 | int nerrparse=0;
|
---|
139 | int num = 0;
|
---|
140 | cout << " ====================================================== " << endl;
|
---|
141 | cout << " ============ Test of CExpressionEvaluator ========== " << endl;
|
---|
142 | cout << " ====================================================== " << endl;
|
---|
143 | try {
|
---|
144 | try {
|
---|
145 | num++;
|
---|
146 | CExpressionEvaluator cex("4.e-1");
|
---|
147 | cout << "CExpr=" << cex;
|
---|
148 | cout << " -> cex.Value() = " << cex.Value() << " =? " << ( res=4.e-1 ) << endl;
|
---|
149 | if (fabs(cex.Value()-res) > 1.e-9) {
|
---|
150 | cout << " ERREUR CALCUL " << cex << endl;
|
---|
151 | nerr++;
|
---|
152 | }
|
---|
153 | else nok++;
|
---|
154 | }
|
---|
155 | catch (CExprException& err) {
|
---|
156 | nerrparse++;
|
---|
157 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
158 | }
|
---|
159 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
160 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
161 |
|
---|
162 | try {
|
---|
163 | num++;
|
---|
164 | CExpressionEvaluator cex("4*3+8");
|
---|
165 | cout << "CExpr=" << cex;
|
---|
166 | cout << " -> cex.Value() = " << cex.Value() << " =? 4*3+8 = " << ( res=4*3+8 ) << endl;
|
---|
167 | if (fabs(cex.Value()-res) > 1.e-9) {
|
---|
168 | cout << " ERREUR CALCUL " << cex << endl;
|
---|
169 | nerr++;
|
---|
170 | }
|
---|
171 | else nok++;
|
---|
172 | }
|
---|
173 | catch (CExprException& err) {
|
---|
174 | nerrparse++;
|
---|
175 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
176 | }
|
---|
177 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
178 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
179 |
|
---|
180 | try {
|
---|
181 | num++;
|
---|
182 | CExpressionEvaluator cex("cos(0)+2");
|
---|
183 | cout << "CExpr=" << cex;
|
---|
184 | cout << " -> cex.Value() = " << cex.Value() << " =? cos(0)+2 = " << ( res=cos(0.)+2 ) << endl;
|
---|
185 | if (fabs(cex.Value()-res) > 1.e-9) {
|
---|
186 | cout << " ERREUR CALCUL " << cex << endl;
|
---|
187 | nerr++;
|
---|
188 | }
|
---|
189 | else nok++;
|
---|
190 | }
|
---|
191 | catch (CExprException& err) {
|
---|
192 | nerrparse++;
|
---|
193 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
194 | }
|
---|
195 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
196 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
197 |
|
---|
198 |
|
---|
199 | try {
|
---|
200 | num++;
|
---|
201 | CExpressionEvaluator cex("4+3*8");
|
---|
202 | cout << "CExpr=" << cex;
|
---|
203 | cout << " -> cex.Value() = " << cex.Value() << " =? 4+3*8 = " << ( res=4+3*8 ) << endl;
|
---|
204 | if (fabs(cex.Value()-res) > 1.e-9) {
|
---|
205 | cout << " ERREUR CALCUL " << cex << endl;
|
---|
206 | nerr++;
|
---|
207 | }
|
---|
208 | else nok++;
|
---|
209 | }
|
---|
210 | catch (CExprException& err) {
|
---|
211 | nerrparse++;
|
---|
212 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
213 | }
|
---|
214 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
215 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
216 |
|
---|
217 |
|
---|
218 | try {
|
---|
219 | num++;
|
---|
220 | CExpressionEvaluator cex("4+3*6*2");
|
---|
221 | cout << "CExpr=" << cex;
|
---|
222 | cout << " -> cex.Value() = " << cex.Value() << " =? 4+3*6*2 = " << ( res=4+3*6*2 ) << endl;
|
---|
223 | if (fabs(cex.Value()-res) > 1.e-9) {
|
---|
224 | cout << " ERREUR CALCUL " << cex << endl;
|
---|
225 | nerr++;
|
---|
226 | }
|
---|
227 | else nok++;
|
---|
228 | }
|
---|
229 |
|
---|
230 | catch (CExprException& err) {
|
---|
231 | nerrparse++;
|
---|
232 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
233 | }
|
---|
234 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
235 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
236 |
|
---|
237 | try {
|
---|
238 | num++;
|
---|
239 | CExpressionEvaluator cex("(12+3*6*cos(0))");
|
---|
240 | cout << "CExpr=" << cex;
|
---|
241 | cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0)) = "
|
---|
242 | << ( res=(12+3*6*cos(0.)) ) << endl;
|
---|
243 | if (fabs(cex.Value()-res) > 1.e-9) {
|
---|
244 | cout << " ERREUR CALCUL " << cex << endl;
|
---|
245 | nerr++;
|
---|
246 | }
|
---|
247 | else nok++;
|
---|
248 | }
|
---|
249 | catch (CExprException& err) {
|
---|
250 | nerrparse++;
|
---|
251 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
252 | }
|
---|
253 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
254 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
255 |
|
---|
256 | try {
|
---|
257 | num++;
|
---|
258 | CExpressionEvaluator cex("(12+3*6*cos(0))-(5*pow(2,2))");
|
---|
259 | cout << "CExpr=" << cex;
|
---|
260 | cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0))-(5*pow(2,2)) = "
|
---|
261 | << ( res=(12+3*6*cos(0.))-(5*pow(2.,2.)) ) << endl;
|
---|
262 | if (fabs(cex.Value()-res) > 1.e-9) {
|
---|
263 | cout << " ERREUR CALCUL " << cex << endl;
|
---|
264 | nerr++;
|
---|
265 | }
|
---|
266 | else nok++;
|
---|
267 | }
|
---|
268 | catch (CExprException& err) {
|
---|
269 | nerrparse++;
|
---|
270 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
271 | }
|
---|
272 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
273 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
274 |
|
---|
275 | try {
|
---|
276 | num++;
|
---|
277 | CExpressionEvaluator cex("sin(Pi/6)+E");
|
---|
278 | cout << "CExpr=sin(Pi/6)+E " << cex;
|
---|
279 | cout << " -> cex.Value() = " << cex.Value() << " =? sin(M_PI/6)+M_E = "
|
---|
280 | << ( res=sin(M_PI/6)+M_E ) << endl;
|
---|
281 | if (fabs(cex.Value()-res) > 1.e-9) {
|
---|
282 | cout << " ERREUR CALCUL " << cex << endl;
|
---|
283 | nerr++;
|
---|
284 | }
|
---|
285 | else nok++;
|
---|
286 | }
|
---|
287 | catch (CExprException& err) {
|
---|
288 | nerrparse++;
|
---|
289 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
290 | }
|
---|
291 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
292 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
293 |
|
---|
294 | // Expression avec erreur de syntaxe
|
---|
295 | cout << " >>>>> Expression avec erreur <<<<<< " << endl;
|
---|
296 | try {
|
---|
297 | num++;
|
---|
298 | CExpressionEvaluator cex("6**8");
|
---|
299 | cout << "CExpr=" << cex;
|
---|
300 | cout << " -> cex.Value() = " << cex.Value() << endl;
|
---|
301 | }
|
---|
302 | catch (CExprException& err) {
|
---|
303 | nerrparse++;
|
---|
304 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
305 | }
|
---|
306 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
307 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
308 |
|
---|
309 | try {
|
---|
310 | num++;
|
---|
311 | CExpressionEvaluator cex("6*(8+4");
|
---|
312 | cout << "CExpr=" << cex;
|
---|
313 | cout << " -> cex.Value() = " << cex.Value() << endl;
|
---|
314 | }
|
---|
315 | catch (CExprException& err) {
|
---|
316 | nerrparse++;
|
---|
317 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
318 | }
|
---|
319 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
320 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
321 |
|
---|
322 |
|
---|
323 | try {
|
---|
324 | num++;
|
---|
325 | CExpressionEvaluator cex("6*sin(4,)+12");
|
---|
326 | cout << "CExpr=" << cex;
|
---|
327 | cout << " -> cex.Value() = " << cex.Value() << endl;
|
---|
328 | }
|
---|
329 | catch (CExprException& err) {
|
---|
330 | nerrparse++;
|
---|
331 | cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
332 | }
|
---|
333 | cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
|
---|
334 | << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
335 |
|
---|
336 | }
|
---|
337 | catch (CExprException& err) {
|
---|
338 | cout << " Exception: " << err.Msg() << endl;
|
---|
339 | }
|
---|
340 |
|
---|
341 | cout << "-------- CExprBase::NbCreate() = " << CExprBase::NbCreate() << " CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
|
---|
342 | cout << "--- NExpr= " << num << " NOk= " << nok << " NErr=" << nerr << " NErrParse=" << nerrparse << endl;
|
---|
343 | if ( (nok == 8) && (nerr == 0) && (nerrparse == 3) ) return 0;
|
---|
344 | else return 99;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /* -- Fonction Test de CExpressionEvaluator -- */
|
---|
348 | int tst_rpneval()
|
---|
349 | {
|
---|
350 | int num = 0;
|
---|
351 | cout << " ====================================================== " << endl;
|
---|
352 | cout << " ============ Test of RPNExpressionEvaluator ========== " << endl;
|
---|
353 | cout << " ====================================================== " << endl;
|
---|
354 | try {
|
---|
355 | {
|
---|
356 | num++;
|
---|
357 | RPNExpressionEvaluator rpn("4 2 print + 3 * ");
|
---|
358 | cout << "4 2 + 3 * -> rpn.Value() = " << rpn.Value() << endl;
|
---|
359 | }
|
---|
360 | {
|
---|
361 | num++;
|
---|
362 | RPNExpressionEvaluator rpn("1 2 3 4 5 sum");
|
---|
363 | cout << "1 2 3 4 5 sum -> rpn.Value() = " << rpn.Value() << endl;
|
---|
364 | }
|
---|
365 | {
|
---|
366 | num++;
|
---|
367 | RPNExpressionEvaluator rpn("4 3 pow");
|
---|
368 | cout << "4 3 pow -> rpn.Value() = " << rpn.Value() << endl;
|
---|
369 | }
|
---|
370 | }
|
---|
371 | catch (RPNExprException& err) {
|
---|
372 | cout << "RPNExp[" << num << "] Exception: " << err.Msg() << endl;
|
---|
373 | }
|
---|
374 |
|
---|
375 | return 0;
|
---|
376 | }
|
---|