- Timestamp:
- Apr 27, 2012, 12:36:50 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaProg/Tests/tcmd.cc
r3619 r4064 18 18 RPNExpressionEvaluator , Commander , 19 19 csh> tcmd commander 20 csh> tcmd cexptst 21 csh> tcmd rpntst 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 ) 22 24 csh tcmd timer 23 ------ R. Ansari ---- 2003-2007 --------------- */ 24 25 ------ R. Ansari ---- 2003-2012 --------------- */ 26 27 size_t NLOOP = 1000; 28 int commander_inputloop(); // Test de la classe Commander 29 int tst_cexpreval(); // Test de CExpressionEvaluator 30 int tst_cexpreval_v(bool fgparse=false); // Test de CExpressionEvaluator avec variables 31 int tst_rpneval(); // Test de RPNEvaluator 32 int tst_timer(); // Test de Timer 33 34 /* --Main-- */ 35 int 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 25 86 class TCmdExecutor : public CmdExecutor { 26 87 public: … … 31 92 }; 32 93 94 /* --Methode-- */ 33 95 TCmdExecutor::TCmdExecutor(Commander& cmd) 34 96 { … … 47 109 } 48 110 111 /* --Methode-- */ 49 112 int TCmdExecutor::Execute(string& kw, vector<string>& args, string& toks) 50 113 { … … 60 123 } 61 124 62 int InputLoop(Commander & cmd);63 int tst_cexpreval(); // Test de CExpressionEvaluator64 int tst_rpneval(); // Test de RPNEvaluator65 int tst_timer(); // Test de Timer66 67 /* --Main-- */68 int main(int narg, char *arg[])69 {70 71 if (narg < 2) {72 cout << " Usage: tcmd S=commander/cexptst/rpntst/timer \n"73 << " S= commander: Commader class test \n"74 << " S= cexptst: CExpressionEvaluator class test \n"75 << " S= rpntst: RPNExpressionEvaluator class test \n"76 << " S= timer: Timer class test \n" << endl;77 return 0;78 }79 string opt = arg[1];80 SophyaInit();81 InitTim();82 int rc = 0;83 cout << " ---- tcmd S= " << opt << " (commander/evaluator test) " << endl;84 try {85 if (opt == "commander") {86 Commander cmd;87 TCmdExecutor cmdex(cmd);88 InputLoop(cmd);89 }90 else if (opt == "cexptst") rc = tst_cexpreval();91 else if (opt == "rpntst") rc = tst_rpneval();92 else if (opt == "timer") rc = tst_timer();93 else {94 cout << " tcmd/error: Unknown select option: " << opt << endl;95 rc = 9;96 }97 }98 99 catch (PThrowable & exc) {100 cerr << " Catched Exception " << (string)typeid(exc).name()101 << " - Msg= " << exc.Msg() << endl;102 }103 catch (std::exception & e) {104 cerr << " exception catched : e.what()= " << e.what() << endl;105 }106 catch (...) {107 cerr << " some other exception was caught ! " << endl;108 }109 110 PrtTim(" End of tcmd ");111 PrtTim("--Fin tcmd ");112 return(rc);113 }114 115 125 /* --Fonction-- */ 116 int InputLoop(Commander & cmd)126 int commander_inputloop() 117 127 { 118 128 cout << " ====================================================== " << endl; 119 129 cout << " ============== Test of Commander class ============ " << endl; 120 130 cout << " ====================================================== " << endl; 131 Commander cmd; 132 TCmdExecutor cmdex(cmd); 133 121 134 cout << " tcmd/InputLoop() : Type in your commands, \n" 122 135 << " end with a blanck line OR <Cntl>D " << endl; … … 161 174 cout << "CExpr=" << cex; 162 175 cout << " -> cex.Value() = " << cex.Value() << " =? " << ( res=4.e-1 ) << endl; 163 if (fabs(cex.Value()-res) > 1.e- 9) {176 if (fabs(cex.Value()-res) > 1.e-19) { 164 177 cout << " ERREUR CALCUL " << cex << endl; 165 178 nerr++; … … 179 192 cout << "CExpr=" << cex; 180 193 cout << " -> cex.Value() = " << cex.Value() << " =? 4*3+8 = " << ( res=4*3+8 ) << endl; 181 if (fabs(cex.Value()-res) > 1.e- 9) {194 if (fabs(cex.Value()-res) > 1.e-19) { 182 195 cout << " ERREUR CALCUL " << cex << endl; 183 196 nerr++; … … 197 210 cout << "CExpr=" << cex; 198 211 cout << " -> cex.Value() = " << cex.Value() << " =? cos(0)+2 = " << ( res=cos(0.)+2 ) << endl; 199 if (fabs(cex.Value()-res) > 1.e- 9) {212 if (fabs(cex.Value()-res) > 1.e-19) { 200 213 cout << " ERREUR CALCUL " << cex << endl; 201 214 nerr++; … … 216 229 cout << "CExpr=" << cex; 217 230 cout << " -> cex.Value() = " << cex.Value() << " =? 4+3*8 = " << ( res=4+3*8 ) << endl; 218 if (fabs(cex.Value()-res) > 1.e- 9) {231 if (fabs(cex.Value()-res) > 1.e-19) { 219 232 cout << " ERREUR CALCUL " << cex << endl; 220 233 nerr++; … … 235 248 cout << "CExpr=" << cex; 236 249 cout << " -> cex.Value() = " << cex.Value() << " =? 4+3*6*2 = " << ( res=4+3*6*2 ) << endl; 237 if (fabs(cex.Value()-res) > 1.e- 9) {250 if (fabs(cex.Value()-res) > 1.e-19) { 238 251 cout << " ERREUR CALCUL " << cex << endl; 239 252 nerr++; … … 255 268 cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0)) = " 256 269 << ( res=(12+3*6*cos(0.)) ) << endl; 257 if (fabs(cex.Value()-res) > 1.e- 9) {270 if (fabs(cex.Value()-res) > 1.e-19) { 258 271 cout << " ERREUR CALCUL " << cex << endl; 259 272 nerr++; … … 274 287 cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0))-(5*pow(2,2)) = " 275 288 << ( res=(12+3*6*cos(0.))-(5*pow(2.,2.)) ) << endl; 276 if (fabs(cex.Value()-res) > 1.e- 9) {289 if (fabs(cex.Value()-res) > 1.e-19) { 277 290 cout << " ERREUR CALCUL " << cex << endl; 278 291 nerr++; … … 293 306 cout << " -> cex.Value() = " << cex.Value() << " =? sin(M_PI/6)+M_E = " 294 307 << ( res=sin(M_PI/6)+M_E ) << endl; 295 if (fabs(cex.Value()-res) > 1.e- 9) {308 if (fabs(cex.Value()-res) > 1.e-19) { 296 309 cout << " ERREUR CALCUL " << cex << endl; 297 310 nerr++; … … 359 372 } 360 373 374 /* -- classe de test pour liste de variables a utiliser par CExpressionEvaluator -- */ 375 class CE_TestVarList : public CE_VarListInterface { 376 public: 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 393 protected: 394 std::map<std::string, size_t> nameids_; 395 double* pvalues_; 396 }; 397 398 361 399 /* -- Fonction Test de CExpressionEvaluator -- */ 400 int 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 -- */ 362 445 int tst_rpneval() 363 446 { 447 double res; 448 int nok=0; 449 int nerr=0; 364 450 int num = 0; 365 451 cout << " ====================================================== " << endl; … … 370 456 num++; 371 457 RPNExpressionEvaluator rpn("4 2 print + 3 * "); 372 cout << "4 2 + 3 * -> rpn.Value() = " << rpn.Value() << endl; 458 cout << "4 2 + 3 * -> rpn.Value() = " << rpn.Value() << " =? " << (res=(4+2)*3.) << endl; 459 if (fabs(rpn.Value()-res)>1.e-19) nerr++; 373 460 } 374 461 { 375 462 num++; 376 463 RPNExpressionEvaluator rpn("1 2 3 4 5 sum"); 377 cout << "1 2 3 4 5 sum -> rpn.Value() = " << rpn.Value() << endl; 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++; 378 466 } 379 467 { 380 468 num++; 381 469 RPNExpressionEvaluator rpn("4 3 pow"); 382 cout << "4 3 pow -> rpn.Value() = " << rpn.Value() << endl; 383 } 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 384 488 } 385 489 catch (RPNExprException& err) { … … 387 491 } 388 492 389 return 0; 493 cout << "--- NExpr= " << num << " NOk= " << nok << " NErr=" << nerr << endl; 494 495 if (nerr==0) return 0; 496 else return 99; 390 497 } 391 498
Note:
See TracChangeset
for help on using the changeset viewer.