Changeset 4064 in Sophya for trunk/SophyaProg


Ignore:
Timestamp:
Apr 27, 2012, 12:36:50 AM (13 years ago)
Author:
ansari
Message:

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:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaProg/Tests/tcmd.cc

    r3619 r4064  
    1818   RPNExpressionEvaluator , Commander  ,
    1919   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 )
    2224   csh  tcmd timer
    23    ------ R. Ansari ---- 2003-2007    --------------- */
    24 
     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
    2586class TCmdExecutor : public CmdExecutor {
    2687public:
     
    3192};
    3293
     94/* --Methode-- */
    3395TCmdExecutor::TCmdExecutor(Commander& cmd)
    3496{
     
    47109}
    48110
     111/* --Methode-- */
    49112int TCmdExecutor::Execute(string& kw, vector<string>& args, string& toks)
    50113{
     
    60123}
    61124
    62 int  InputLoop(Commander & cmd);
    63 int tst_cexpreval();  // Test de CExpressionEvaluator
    64 int tst_rpneval();    // Test de RPNEvaluator
    65 int tst_timer();      // Test de Timer
    66 
    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 
    115125/* --Fonction-- */
    116 int  InputLoop(Commander & cmd)
     126int  commander_inputloop()
    117127{
    118128  cout << " ====================================================== " << endl;
    119129  cout << " ==============   Test of Commander class  ============ " << endl;
    120130  cout << " ====================================================== " << endl;
     131  Commander cmd;
     132  TCmdExecutor cmdex(cmd);
     133
    121134  cout << " tcmd/InputLoop() : Type in your commands, \n"
    122135       << "     end with a blanck line OR <Cntl>D " << endl;
     
    161174      cout << "CExpr=" << cex;
    162175      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) {
    164177        cout << " ERREUR CALCUL " << cex << endl;
    165178        nerr++;
     
    179192      cout << "CExpr=" << cex;
    180193      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) {
    182195        cout << " ERREUR CALCUL " << cex << endl;
    183196        nerr++;
     
    197210      cout << "CExpr=" << cex;
    198211      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) {
    200213        cout << " ERREUR CALCUL " << cex << endl;
    201214        nerr++;
     
    216229      cout << "CExpr=" << cex;
    217230      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) {
    219232        cout << " ERREUR CALCUL " << cex << endl;
    220233        nerr++;
     
    235248      cout << "CExpr=" << cex;
    236249      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) {
    238251        cout << " ERREUR CALCUL " << cex << endl;
    239252        nerr++;
     
    255268      cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0))  = "
    256269           << ( 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) {
    258271        cout << " ERREUR CALCUL " << cex << endl;
    259272        nerr++;
     
    274287      cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0))-(5*pow(2,2))  = "
    275288           << ( 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) {
    277290        cout << " ERREUR CALCUL " << cex << endl;
    278291        nerr++;
     
    293306      cout << " -> cex.Value() = " << cex.Value() << " =? sin(M_PI/6)+M_E  = "
    294307           << ( 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) {
    296309        cout << " ERREUR CALCUL " << cex << endl;
    297310        nerr++;
     
    359372}
    360373
     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
    361399/* -- 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 -- */
    362445int tst_rpneval()
    363446{
     447  double res;
     448  int nok=0;
     449  int nerr=0;
    364450  int num = 0;
    365451  cout << " ====================================================== " << endl;
     
    370456      num++;
    371457      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++;
    373460    }
    374461    {
    375462      num++;
    376463      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++;
    378466    }
    379467    {
    380468      num++;
    381469      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
    384488  }
    385489  catch (RPNExprException& err) {
     
    387491  }
    388492
    389   return 0;
     493  cout << "--- NExpr= " << num << " NOk= " <<  nok << " NErr=" << nerr << endl;
     494
     495  if (nerr==0) return 0;
     496  else return 99;
    390497}
    391498
Note: See TracChangeset for help on using the changeset viewer.