source: Sophya/trunk/SophyaProg/Tests/tcmd.cc@ 4080

Last change on this file since 4080 was 4064, checked in by ansari, 13 years ago

amelioration du programme test tcmd de Commander/CExpressionEvaluator, RPNExpressionEvaluator et test de la fonctionalite d decodage nom de variables par CExpressionEvaluator, Reza 27/04/2012

File size: 17.6 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <math.h>
7#include <iostream>
8#include <fstream>
9#include <unistd.h>
10#include "sophyainit.h"
11#include "timing.h"
12#include "cexpre.h"
13#include "rpneval.h"
14#include "commander.h"
15
16/* -------------- Programme test de SOPHYA tcmd.cc
17 Test des classes Timer , CExpressionEvaluator ,
18 RPNExpressionEvaluator , Commander ,
19 csh> tcmd commander
20 csh> tcmd cexptst ( check Rc=0 )
21 csh> tcmd cexptv or tcmd cexptv 5000 ( check Rc=0 )
22 csh> tcmd cexptvp or tcmd cexptvp 5000 ( check Rc=0 )
23 csh> tcmd rpntst ( check Rc=0 )
24 csh tcmd timer
25 ------ R. Ansari ---- 2003-2012 --------------- */
26
27size_t NLOOP = 1000;
28int commander_inputloop(); // Test de la classe Commander
29int tst_cexpreval(); // Test de CExpressionEvaluator
30int tst_cexpreval_v(bool fgparse=false); // Test de CExpressionEvaluator avec variables
31int tst_rpneval(); // Test de RPNEvaluator
32int tst_timer(); // Test de Timer
33
34/* --Main-- */
35int main(int narg, char *arg[])
36{
37
38 if (narg < 2) {
39 cout << " Usage: tcmd S=commander/cexptst/rpntst/timer [NLOOP , def=1000] \n"
40 << " S= commander: Commader class test \n"
41 << " S= cexptst: CExpressionEvaluator class test \n"
42 << " S= cexptv : CExpressionEvaluator class test with variables\n"
43 << " S= cexptvp : CExpressionEvaluator class test with variables, parse each time \n"
44 << " S= rpntst: RPNExpressionEvaluator class test \n"
45 << " S= timer: Timer class test \n" << endl;
46 return 0;
47 }
48 string opt = arg[1];
49 if (narg>2) NLOOP=atoi(arg[2]);
50 SophyaInit();
51 InitTim();
52 int rc = 0;
53 cout << " ---- tcmd S= " << opt << " (commander/evaluator test) " << endl;
54 try {
55 if (opt == "commander") commander_inputloop();
56 else if (opt == "cexptst") rc = tst_cexpreval();
57 else if (opt == "cexptv") rc = tst_cexpreval_v();
58 else if (opt == "cexptvp") rc = tst_cexpreval_v(true);
59 else if (opt == "rpntst") rc = tst_rpneval();
60 else if (opt == "timer") rc = tst_timer();
61 else {
62 cout << " tcmd/error: Unknown select option: " << opt << endl;
63 rc = 9;
64 }
65 }
66
67 catch (PThrowable & exc) {
68 cerr << " Catched Exception " << (string)typeid(exc).name()
69 << " - Msg= " << exc.Msg() << endl;
70 }
71 catch (std::exception & e) {
72 cerr << " exception catched : e.what()= " << e.what() << endl;
73 }
74 catch (...) {
75 cerr << " some other exception was caught ! " << endl;
76 }
77
78 cout << " =========== End of tcmd " << opt << " -> RC= " << rc << " ===========" << endl;
79 PrtTim(" End of tcmd ");
80 PrtTim("--Fin tcmd ");
81 return(rc);
82}
83
84// ----- Test of Commander class ------
85// ------ Test Executor class
86class TCmdExecutor : public CmdExecutor {
87public:
88 TCmdExecutor(Commander& cmd);
89 virtual ~TCmdExecutor() { }
90
91 virtual int Execute(string& keyw,vector<string>& args, string& toks);
92};
93
94/* --Methode-- */
95TCmdExecutor::TCmdExecutor(Commander& cmd)
96{
97string hgrp = "TCmdExecutor";
98string usage,kw;
99
100kw = "ls";
101usage = "ls: Execute /usr/bin/ls \n";
102cmd.RegisterCommand(kw, usage, this, hgrp);
103kw = "mv";
104usage = "mv: Execute /usr/bin/mv \n";
105cmd.RegisterCommand(kw, usage, this, hgrp);
106kw = "cp";
107usage = "cp: Execute /usr/bin/cp \n";
108cmd.RegisterCommand(kw, usage, this, hgrp);
109}
110
111/* --Methode-- */
112int TCmdExecutor::Execute(string& kw, vector<string>& args, string& toks)
113{
114 if ((kw!="ls")&&(kw!="mv")&&(kw!="cp")) return 99;
115 string cmd;
116 if (kw == "ls") cmd = "/usr/bin/ls " ;
117 else if (kw == "mv") cmd = "/usr/bin/mv " ;
118 else if (kw == "cp") cmd = "/usr/bin/cp " ;
119 else cmd = "/usr/bin/echo " ;
120 for(int kk=0; kk<args.size(); kk++) { cmd += args[kk]; cmd += ' '; }
121 cout << "TCmdExecutor::Execute() : Executing " << cmd << endl;
122 return system(cmd.c_str());
123}
124
125/* --Fonction-- */
126int commander_inputloop()
127{
128 cout << " ====================================================== " << endl;
129 cout << " ============== Test of Commander class ============ " << endl;
130 cout << " ====================================================== " << endl;
131 Commander cmd;
132 TCmdExecutor cmdex(cmd);
133
134 cout << " tcmd/InputLoop() : Type in your commands, \n"
135 << " end with a blanck line OR <Cntl>D " << endl;
136 int line = 0;
137 bool fg = true;
138 char buff[1024];
139 char * ret;
140 while (fg) {
141 printf("%d-%s ", line+1, cmd.GetCurrentPrompt().c_str());
142 fflush(stdout);
143 buff[0] = '\0';
144 ret = fgets(buff, 1024, stdin);
145 buff[1023] = '\0';
146 if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) {
147 buff[strlen(buff)-1] = '\0';
148 string cline = buff;
149 cmd.Interpret(cline);
150 line++;
151 }
152 else fg = false;
153 }
154 cout << " \n Total " << line << " lines from stdin -> Commander " << endl;
155 if (line > 0) return(0);
156 else return(1);
157}
158
159/* -- Fonction Test de CExpressionEvaluator -- */
160int tst_cexpreval()
161{
162 double res;
163 int nok=0;
164 int nerr=0;
165 int nerrparse=0;
166 int num = 0;
167 cout << " ====================================================== " << endl;
168 cout << " ============ Test of CExpressionEvaluator ========== " << endl;
169 cout << " ====================================================== " << endl;
170 try {
171 try {
172 num++;
173 CExpressionEvaluator cex("4.e-1");
174 cout << "CExpr=" << cex;
175 cout << " -> cex.Value() = " << cex.Value() << " =? " << ( res=4.e-1 ) << endl;
176 if (fabs(cex.Value()-res) > 1.e-19) {
177 cout << " ERREUR CALCUL " << cex << endl;
178 nerr++;
179 }
180 else nok++;
181 }
182 catch (CExprException& err) {
183 nerrparse++;
184 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
185 }
186 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
187 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
188
189 try {
190 num++;
191 CExpressionEvaluator cex("4*3+8");
192 cout << "CExpr=" << cex;
193 cout << " -> cex.Value() = " << cex.Value() << " =? 4*3+8 = " << ( res=4*3+8 ) << endl;
194 if (fabs(cex.Value()-res) > 1.e-19) {
195 cout << " ERREUR CALCUL " << cex << endl;
196 nerr++;
197 }
198 else nok++;
199 }
200 catch (CExprException& err) {
201 nerrparse++;
202 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
203 }
204 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
205 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
206
207 try {
208 num++;
209 CExpressionEvaluator cex("cos(0)+2");
210 cout << "CExpr=" << cex;
211 cout << " -> cex.Value() = " << cex.Value() << " =? cos(0)+2 = " << ( res=cos(0.)+2 ) << endl;
212 if (fabs(cex.Value()-res) > 1.e-19) {
213 cout << " ERREUR CALCUL " << cex << endl;
214 nerr++;
215 }
216 else nok++;
217 }
218 catch (CExprException& err) {
219 nerrparse++;
220 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
221 }
222 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
223 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
224
225
226 try {
227 num++;
228 CExpressionEvaluator cex("4+3*8");
229 cout << "CExpr=" << cex;
230 cout << " -> cex.Value() = " << cex.Value() << " =? 4+3*8 = " << ( res=4+3*8 ) << endl;
231 if (fabs(cex.Value()-res) > 1.e-19) {
232 cout << " ERREUR CALCUL " << cex << endl;
233 nerr++;
234 }
235 else nok++;
236 }
237 catch (CExprException& err) {
238 nerrparse++;
239 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
240 }
241 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
242 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
243
244
245 try {
246 num++;
247 CExpressionEvaluator cex("4+3*6*2");
248 cout << "CExpr=" << cex;
249 cout << " -> cex.Value() = " << cex.Value() << " =? 4+3*6*2 = " << ( res=4+3*6*2 ) << endl;
250 if (fabs(cex.Value()-res) > 1.e-19) {
251 cout << " ERREUR CALCUL " << cex << endl;
252 nerr++;
253 }
254 else nok++;
255 }
256
257 catch (CExprException& err) {
258 nerrparse++;
259 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
260 }
261 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
262 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
263
264 try {
265 num++;
266 CExpressionEvaluator cex("(12+3*6*cos(0))");
267 cout << "CExpr=" << cex;
268 cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0)) = "
269 << ( res=(12+3*6*cos(0.)) ) << endl;
270 if (fabs(cex.Value()-res) > 1.e-19) {
271 cout << " ERREUR CALCUL " << cex << endl;
272 nerr++;
273 }
274 else nok++;
275 }
276 catch (CExprException& err) {
277 nerrparse++;
278 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
279 }
280 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
281 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
282
283 try {
284 num++;
285 CExpressionEvaluator cex("(12+3*6*cos(0))-(5*pow(2,2))");
286 cout << "CExpr=" << cex;
287 cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0))-(5*pow(2,2)) = "
288 << ( res=(12+3*6*cos(0.))-(5*pow(2.,2.)) ) << endl;
289 if (fabs(cex.Value()-res) > 1.e-19) {
290 cout << " ERREUR CALCUL " << cex << endl;
291 nerr++;
292 }
293 else nok++;
294 }
295 catch (CExprException& err) {
296 nerrparse++;
297 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
298 }
299 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
300 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
301
302 try {
303 num++;
304 CExpressionEvaluator cex("sin(Pi/6)+E");
305 cout << "CExpr=sin(Pi/6)+E " << cex;
306 cout << " -> cex.Value() = " << cex.Value() << " =? sin(M_PI/6)+M_E = "
307 << ( res=sin(M_PI/6)+M_E ) << endl;
308 if (fabs(cex.Value()-res) > 1.e-19) {
309 cout << " ERREUR CALCUL " << cex << endl;
310 nerr++;
311 }
312 else nok++;
313 }
314 catch (CExprException& err) {
315 nerrparse++;
316 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
317 }
318 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
319 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
320
321 // Expression avec erreur de syntaxe
322 cout << " >>>>> Expression avec erreur <<<<<< " << endl;
323 try {
324 num++;
325 CExpressionEvaluator cex("6**8");
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 try {
337 num++;
338 CExpressionEvaluator cex("6*(8+4");
339 cout << "CExpr=" << cex;
340 cout << " -> cex.Value() = " << cex.Value() << endl;
341 }
342 catch (CExprException& err) {
343 nerrparse++;
344 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
345 }
346 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
347 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
348
349
350 try {
351 num++;
352 CExpressionEvaluator cex("6*sin(4,)+12");
353 cout << "CExpr=" << cex;
354 cout << " -> cex.Value() = " << cex.Value() << endl;
355 }
356 catch (CExprException& err) {
357 nerrparse++;
358 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
359 }
360 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
361 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
362
363 }
364 catch (CExprException& err) {
365 cout << " Exception: " << err.Msg() << endl;
366 }
367
368 cout << "-------- CExprBase::NbCreate() = " << CExprBase::NbCreate() << " CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
369 cout << "--- NExpr= " << num << " NOk= " << nok << " NErr=" << nerr << " NErrParse=" << nerrparse << endl;
370 if ( (nok == 8) && (nerr == 0) && (nerrparse == 3) ) return 0;
371 else return 99;
372}
373
374/* -- classe de test pour liste de variables a utiliser par CExpressionEvaluator -- */
375class CE_TestVarList : public CE_VarListInterface {
376public:
377 CE_TestVarList(std::vector<std::string> const& names)
378 {
379 for(size_t i=0; i<names.size(); i++) nameids_[names[i] ] = i;
380 pvalues_=new double[names.size()];
381 }
382 ~CE_TestVarList() { delete[] pvalues_; }
383
384 inline double& operator[] (size_t n) { return pvalues_[n]; }
385 inline double* getPointer() { return pvalues_; }
386 double* GetVarPointer(std::string const& name)
387 {
388 std::map<std::string, size_t>::iterator it = nameids_.find(name);
389 if (it==nameids_.end()) return NULL;
390 else return pvalues_+(*it).second;
391 }
392
393protected:
394 std::map<std::string, size_t> nameids_;
395 double* pvalues_;
396};
397
398
399/* -- Fonction Test de CExpressionEvaluator -- */
400int tst_cexpreval_v(bool fgparse)
401{
402 double res;
403 int nok=0;
404 int nerr=0;
405 int nerrparse=0;
406 int num = 0;
407 cout << " ============================================================ " << endl;
408 cout << " ======= Test of CExpressionEvaluator with variables ====== " << endl;
409 if (!fgparse) cout << " ======= (parse once - Loop[evaluate] ) NLoop=" << NLOOP << " ======= " << endl;
410 else cout << " ======= (Loop[parse & evaluate] ) NLoop=" << NLOOP << " ======= " << endl;
411 cout << " ============================================================ " << endl;
412 Timer tm("tcmd::CExpVar");
413
414 try {
415 vector<string> names;
416 names.push_back("x");
417 names.push_back("y");
418 names.push_back("z");
419 CE_TestVarList vars(names);
420 CExpressionEvaluator cex("sin(0.7*x)+sqrt(y*z)",&vars);
421 double x,y,z;
422 for(size_t i=0;i<NLOOP; i++) {
423 x=vars[0]=(double)i*7.56; y=vars[1]=vars[0]+986.23; z=vars[2]=(double)((i+1)*(i+1))*sqrt(0.7*i+3.);
424 double res=sin(0.7*x)+sqrt(y*z);
425 if (fgparse) {
426 CExpressionEvaluator cexp("sin(0.7*x)+sqrt(y*z)",&vars);
427 if (fabs(cexp.Value()-res) > 1.e-19) nerr++;
428 }
429 else {
430 if (fabs(cex.Value()-res) > 1.e-19) nerr++;
431 }
432 num++;
433 }
434 }
435 catch (CExprException& err) {
436 cout << " Exception: " << err.Msg() << endl;
437 }
438 cout << "--- NExpr= " << num << " NErr=" << nerr << endl;
439 if (nerr == 0) return 0;
440 else return 99;
441
442}
443
444/* -- Fonction Test de RPNExpressionEvaluator -- */
445int tst_rpneval()
446{
447 double res;
448 int nok=0;
449 int nerr=0;
450 int num = 0;
451 cout << " ====================================================== " << endl;
452 cout << " ============ Test of RPNExpressionEvaluator ========== " << endl;
453 cout << " ====================================================== " << endl;
454 try {
455 {
456 num++;
457 RPNExpressionEvaluator rpn("4 2 print + 3 * ");
458 cout << "4 2 + 3 * -> rpn.Value() = " << rpn.Value() << " =? " << (res=(4+2)*3.) << endl;
459 if (fabs(rpn.Value()-res)>1.e-19) nerr++;
460 }
461 {
462 num++;
463 RPNExpressionEvaluator rpn("1 2 3 4 5 sum");
464 cout << "1 2 3 4 5 sum -> rpn.Value() = " << rpn.Value() << " =? " << (res=(1+2+3+4+5.)) << endl;
465 if (fabs(rpn.Value()-res)>1.e-19) nerr++;
466 }
467 {
468 num++;
469 RPNExpressionEvaluator rpn("4 3 pow");
470 cout << "4 3 pow -> rpn.Value() = " << rpn.Value() << " =? " << (res=4.*4.*4.) << endl;
471 if (fabs(rpn.Value()-res)>1.e-19) nerr++;
472 }
473 {
474 num++;
475 RPNExpressionEvaluator rpn("pi 0.7 * cos 5. log10 /");
476 cout << "pi 0.7 * cos 5. log10 / -> rpn.Value() = " << rpn.Value()
477 << " =? " << (res=cos(0.7*M_PI)/log10(5.)) << endl;
478 if (fabs(rpn.Value()-res)>1.e-19) nerr++;
479 }
480 {
481 num++;
482 RPNExpressionEvaluator rpn("9.5 e * log 34.5 7 - sqrt +");
483 cout << "9.5 e * log 34.5 7 - sqrt + -> rpn.Value() = " << rpn.Value()
484 << " =? " << (res=log(9.5*M_E)+sqrt(34.5-7)) << endl;
485 if (fabs(rpn.Value()-res)>1.e-19) nerr++;
486 }
487
488 }
489 catch (RPNExprException& err) {
490 cout << "RPNExp[" << num << "] Exception: " << err.Msg() << endl;
491 }
492
493 cout << "--- NExpr= " << num << " NOk= " << nok << " NErr=" << nerr << endl;
494
495 if (nerr==0) return 0;
496 else return 99;
497}
498
499/* --Fonction-- */
500int tst_timer()
501{
502 TIMEF ;
503 cout << "**** tcmd.cc : Test class SOPHYA::Timer() **** " << endl;
504 Timer tm("tcmd::Timer", false);
505
506 int N = 10000000;
507 double * x = new double[N];
508 for(int k=0; k<N; k++) x[k] = k*0.0001;
509 for(int k=0; k<N; k++) x[k] = sin(x[k]);
510 for(int k=0; k<N; k++) x[k] = cos(x[k]);
511 tm.Split();
512 cout << " Step1: PARTIAL CPU(s)=" << tm.PartialCPUTime()
513 << " Elapsed (s)= " << tm.PartialElapsedTime()
514 << " Elapsed (ms)= " << tm.PartialElapsedTimems() << endl;
515 cout << " Step1: TOTAL CPU(s)=" << tm.TotalCPUTime()
516 << " Elapsed (s)= " << tm.TotalElapsedTime()
517 << " Elapsed (ms)= " << tm.TotalElapsedTimems() << endl;
518
519 for(int k=0; k<N; k++) x[k] *= 8.5;
520 for(int k=0; k<N; k++) x[k] = tan(x[k]);
521
522 tm.Split();
523 cout << " Step2: PARTIAL CPU(s)=" << tm.PartialCPUTime()
524 << " Elapsed (s)= " << tm.PartialElapsedTime()
525 << " Elapsed (ms)= " << tm.PartialElapsedTimems() << endl;
526 cout << " Step2: TOTAL CPU(s)=" << tm.TotalCPUTime()
527 << " Elapsed (s)= " << tm.TotalElapsedTime()
528 << " Elapsed (ms)= " << tm.TotalElapsedTimems() << endl;
529
530 for(int k=0; k<N; k++) x[k] = sin(x[k]);
531 for(int k=0; k<N; k++) x[k] = cos(x[k]);
532 for(int k=0; k<N; k++) x[k] *= 15.5;
533 for(int k=0; k<N; k++) x[k] = sin(x[k]);
534
535 tm.Split();
536 cout << " Step3: PARTIAL CPU(s)=" << tm.PartialCPUTime()
537 << " Elapsed (s)= " << tm.PartialElapsedTime()
538 << " Elapsed (ms)= " << tm.PartialElapsedTimems() << endl;
539 cout << " Step3: TOTAL CPU(s)=" << tm.TotalCPUTime()
540 << " Elapsed (s)= " << tm.TotalElapsedTime()
541 << " Elapsed (ms)= " << tm.TotalElapsedTimems() << endl;
542
543 tm.Split("CheckPrintFmt", true);
544 cout << " Waiting 70 sec (sleep(70)) ... " << endl;
545 sleep(100);
546 tm.Split("CheckPrintFmt-AfterWait", true);
547
548 cout << " Doing delete[] x, sleep(5) ... " << endl;
549 delete[] x;
550 sleep(5);
551}
552
Note: See TracBrowser for help on using the repository browser.