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