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

Last change on this file since 3578 was 3578, checked in by ansari, 17 years ago

Amelioration est classe Timer ds tcmd.cc , Reza 20/02/2009

File size: 13.7 KB
RevLine 
[2615]1#include "sopnamsp.h"
[2447]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"
[2513]10#include "cexpre.h"
11#include "rpneval.h"
[2447]12#include "commander.h"
13
[3276]14/* -------------- Programme test de SOPHYA tcmd.cc
15 Test des classes Timer , CExpressionEvaluator ,
16 RPNExpressionEvaluator , Commander ,
17 csh> tcmd commander
18 csh> tcmd cexptst
19 csh> tcmd rpntst
20 csh tcmd timer
21 ------ R. Ansari ---- 2003-2007 --------------- */
[2447]22
23class TCmdExecutor : public CmdExecutor {
24public:
25 TCmdExecutor(Commander& cmd);
26 virtual ~TCmdExecutor() { }
27
28 virtual int Execute(string& keyw,vector<string>& args, string& toks);
29};
30
31TCmdExecutor::TCmdExecutor(Commander& cmd)
32{
33string hgrp = "TCmdExecutor";
34string usage,kw;
35
36kw = "ls";
37usage = "ls: Execute /usr/bin/ls \n";
38cmd.RegisterCommand(kw, usage, this, hgrp);
39kw = "mv";
40usage = "mv: Execute /usr/bin/mv \n";
41cmd.RegisterCommand(kw, usage, this, hgrp);
42kw = "cp";
[2480]43usage = "cp: Execute /usr/bin/cp \n";
[2447]44cmd.RegisterCommand(kw, usage, this, hgrp);
45}
46
47int TCmdExecutor::Execute(string& kw, vector<string>& args, string& toks)
48{
49 if ((kw!="ls")&&(kw!="mv")&&(kw!="cp")) return 99;
50 string cmd;
51 if (kw == "ls") cmd = "/usr/bin/ls " ;
52 else if (kw == "mv") cmd = "/usr/bin/mv " ;
53 else if (kw == "cp") cmd = "/usr/bin/cp " ;
54 else cmd = "/usr/bin/echo " ;
[2480]55 for(int kk=0; kk<args.size(); kk++) { cmd += args[kk]; cmd += ' '; }
[2447]56 cout << "TCmdExecutor::Execute() : Executing " << cmd << endl;
57 return system(cmd.c_str());
58}
59
60int InputLoop(Commander & cmd);
[2513]61int tst_cexpreval(); // Test de CExpressionEvaluator
62int tst_rpneval(); // Test de RPNEvaluator
[3276]63int tst_timer(); // Test de Timer
[2447]64
65/* --Main-- */
66int main(int narg, char *arg[])
67{
68
[2513]69 if (narg < 2) {
[3276]70 cout << " Usage: tcmd S=commander/cexptst/rpntst/timer \n"
[2592]71 << " S= commander: Commader class test \n"
72 << " S= cexptst: CExpressionEvaluator class test \n"
[3276]73 << " S= rpntst: RPNExpressionEvaluator class test \n"
74 << " S= timer: Timer class test \n" << endl;
[2513]75 return 0;
76 }
77 string opt = arg[1];
[2447]78 SophyaInit();
79 InitTim();
[2513]80 int rc = 0;
[2592]81 cout << " ---- tcmd S= " << opt << " (commander/evaluator test) " << endl;
[2447]82 try {
[2513]83 if (opt == "commander") {
84 Commander cmd;
85 TCmdExecutor cmdex(cmd);
86 InputLoop(cmd);
87 }
88 else if (opt == "cexptst") rc = tst_cexpreval();
[2592]89 else if (opt == "rpntst") rc = tst_rpneval();
[3276]90 else if (opt == "timer") rc = tst_timer();
[2592]91 else {
92 cout << " tcmd/error: Unknown select option: " << opt << endl;
93 rc = 9;
94 }
95 }
[2447]96
97 catch (PThrowable & exc) {
98 cerr << " Catched Exception " << (string)typeid(exc).name()
99 << " - Msg= " << exc.Msg() << endl;
100 }
[2597]101 catch (std::exception & e) {
[2592]102 cerr << " exception catched : e.what()= " << e.what() << endl;
103 }
[2447]104 catch (...) {
105 cerr << " some other exception was caught ! " << endl;
106 }
107
108 PrtTim(" End of tcmd ");
[2592]109 PrtTim("--Fin tcmd ");
[2513]110 return(rc);
[2447]111}
112
113/* --Fonction-- */
114int InputLoop(Commander & cmd)
115{
[2513]116 cout << " ====================================================== " << endl;
117 cout << " ============== Test of Commander class ============ " << endl;
118 cout << " ====================================================== " << endl;
[2447]119 cout << " tcmd/InputLoop() : Type in your commands, \n"
120 << " end with a blanck line OR <Cntl>D " << endl;
121 int line = 0;
122 bool fg = true;
123 char buff[1024];
124 char * ret;
125 while (fg) {
126 printf("%d-%s ", line+1, cmd.GetCurrentPrompt().c_str());
127 fflush(stdout);
128 buff[0] = '\0';
129 ret = fgets(buff, 1024, stdin);
130 buff[1023] = '\0';
131 if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) {
[2480]132 buff[strlen(buff)-1] = '\0';
[2447]133 string cline = buff;
134 cmd.Interpret(cline);
135 line++;
136 }
137 else fg = false;
138 }
139 cout << " \n Total " << line << " lines from stdin -> Commander " << endl;
140 if (line > 0) return(0);
141 else return(1);
142}
143
[2513]144/* -- Fonction Test de CExpressionEvaluator -- */
145int tst_cexpreval()
146{
147 double res;
148 int nok=0;
149 int nerr=0;
150 int nerrparse=0;
151 int num = 0;
152 cout << " ====================================================== " << endl;
153 cout << " ============ Test of CExpressionEvaluator ========== " << endl;
154 cout << " ====================================================== " << endl;
155 try {
156 try {
157 num++;
[2592]158 CExpressionEvaluator cex("4.e-1");
159 cout << "CExpr=" << cex;
160 cout << " -> cex.Value() = " << cex.Value() << " =? " << ( res=4.e-1 ) << endl;
161 if (fabs(cex.Value()-res) > 1.e-9) {
162 cout << " ERREUR CALCUL " << cex << endl;
163 nerr++;
164 }
165 else nok++;
166 }
167 catch (CExprException& err) {
168 nerrparse++;
169 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
170 }
171 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
172 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
173
174 try {
175 num++;
[2513]176 CExpressionEvaluator cex("4*3+8");
177 cout << "CExpr=" << cex;
178 cout << " -> cex.Value() = " << cex.Value() << " =? 4*3+8 = " << ( res=4*3+8 ) << endl;
179 if (fabs(cex.Value()-res) > 1.e-9) {
180 cout << " ERREUR CALCUL " << cex << endl;
181 nerr++;
182 }
183 else nok++;
184 }
185 catch (CExprException& err) {
186 nerrparse++;
187 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
188 }
189 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
190 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
[2447]191
[2513]192 try {
193 num++;
194 CExpressionEvaluator cex("cos(0)+2");
195 cout << "CExpr=" << cex;
196 cout << " -> cex.Value() = " << cex.Value() << " =? cos(0)+2 = " << ( res=cos(0.)+2 ) << endl;
197 if (fabs(cex.Value()-res) > 1.e-9) {
198 cout << " ERREUR CALCUL " << cex << endl;
199 nerr++;
200 }
201 else nok++;
202 }
203 catch (CExprException& err) {
204 nerrparse++;
205 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
206 }
207 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
208 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
209
210
211 try {
212 num++;
213 CExpressionEvaluator cex("4+3*8");
214 cout << "CExpr=" << cex;
215 cout << " -> cex.Value() = " << cex.Value() << " =? 4+3*8 = " << ( res=4+3*8 ) << endl;
216 if (fabs(cex.Value()-res) > 1.e-9) {
217 cout << " ERREUR CALCUL " << cex << endl;
218 nerr++;
219 }
220 else nok++;
221 }
222 catch (CExprException& err) {
223 nerrparse++;
224 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
225 }
226 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
227 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
228
229
230 try {
231 num++;
232 CExpressionEvaluator cex("4+3*6*2");
233 cout << "CExpr=" << cex;
234 cout << " -> cex.Value() = " << cex.Value() << " =? 4+3*6*2 = " << ( res=4+3*6*2 ) << endl;
235 if (fabs(cex.Value()-res) > 1.e-9) {
236 cout << " ERREUR CALCUL " << cex << endl;
237 nerr++;
238 }
239 else nok++;
240 }
241
242 catch (CExprException& err) {
243 nerrparse++;
244 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
245 }
246 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
247 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
248
249 try {
250 num++;
251 CExpressionEvaluator cex("(12+3*6*cos(0))");
252 cout << "CExpr=" << cex;
253 cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0)) = "
254 << ( res=(12+3*6*cos(0.)) ) << endl;
255 if (fabs(cex.Value()-res) > 1.e-9) {
256 cout << " ERREUR CALCUL " << cex << endl;
257 nerr++;
258 }
259 else nok++;
260 }
261 catch (CExprException& err) {
262 nerrparse++;
263 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
264 }
265 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
266 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
267
268 try {
269 num++;
270 CExpressionEvaluator cex("(12+3*6*cos(0))-(5*pow(2,2))");
271 cout << "CExpr=" << cex;
272 cout << " -> cex.Value() = " << cex.Value() << " =? (12+3*6*cos(0))-(5*pow(2,2)) = "
273 << ( res=(12+3*6*cos(0.))-(5*pow(2.,2.)) ) << endl;
274 if (fabs(cex.Value()-res) > 1.e-9) {
275 cout << " ERREUR CALCUL " << cex << endl;
276 nerr++;
277 }
278 else nok++;
279 }
280 catch (CExprException& err) {
281 nerrparse++;
282 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
283 }
284 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
285 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
286
287 try {
288 num++;
289 CExpressionEvaluator cex("sin(Pi/6)+E");
290 cout << "CExpr=sin(Pi/6)+E " << cex;
291 cout << " -> cex.Value() = " << cex.Value() << " =? sin(M_PI/6)+M_E = "
292 << ( res=sin(M_PI/6)+M_E ) << endl;
293 if (fabs(cex.Value()-res) > 1.e-9) {
294 cout << " ERREUR CALCUL " << cex << endl;
295 nerr++;
296 }
297 else nok++;
298 }
299 catch (CExprException& err) {
300 nerrparse++;
301 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
302 }
303 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
304 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
305
306 // Expression avec erreur de syntaxe
[2592]307 cout << " >>>>> Expression avec erreur <<<<<< " << endl;
[2513]308 try {
309 num++;
310 CExpressionEvaluator cex("6**8");
311 cout << "CExpr=" << cex;
312 cout << " -> cex.Value() = " << cex.Value() << endl;
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 try {
322 num++;
323 CExpressionEvaluator cex("6*(8+4");
324 cout << "CExpr=" << cex;
325 cout << " -> cex.Value() = " << cex.Value() << endl;
326 }
327 catch (CExprException& err) {
328 nerrparse++;
329 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
330 }
331 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
332 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
333
334
335 try {
336 num++;
337 CExpressionEvaluator cex("6*sin(4,)+12");
338 cout << "CExpr=" << cex;
339 cout << " -> cex.Value() = " << cex.Value() << endl;
340 }
341 catch (CExprException& err) {
342 nerrparse++;
343 cout << "Exp[" << num << "] Exception: " << err.Msg() << endl;
344 }
345 cout << " [" << num << "] CExprBase::NbCreate() = " << CExprBase::NbCreate()
346 << "CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
347
348 }
349 catch (CExprException& err) {
350 cout << " Exception: " << err.Msg() << endl;
351 }
352
353 cout << "-------- CExprBase::NbCreate() = " << CExprBase::NbCreate() << " CExprBase::NbDelete()=" << CExprBase::NbDelete() << endl;
354 cout << "--- NExpr= " << num << " NOk= " << nok << " NErr=" << nerr << " NErrParse=" << nerrparse << endl;
[2592]355 if ( (nok == 8) && (nerr == 0) && (nerrparse == 3) ) return 0;
356 else return 99;
[2513]357}
358
359/* -- Fonction Test de CExpressionEvaluator -- */
360int tst_rpneval()
361{
362 int num = 0;
363 cout << " ====================================================== " << endl;
364 cout << " ============ Test of RPNExpressionEvaluator ========== " << endl;
365 cout << " ====================================================== " << endl;
366 try {
367 {
368 num++;
369 RPNExpressionEvaluator rpn("4 2 print + 3 * ");
370 cout << "4 2 + 3 * -> rpn.Value() = " << rpn.Value() << endl;
371 }
372 {
373 num++;
374 RPNExpressionEvaluator rpn("1 2 3 4 5 sum");
375 cout << "1 2 3 4 5 sum -> rpn.Value() = " << rpn.Value() << endl;
376 }
377 {
378 num++;
379 RPNExpressionEvaluator rpn("4 3 pow");
380 cout << "4 3 pow -> rpn.Value() = " << rpn.Value() << endl;
381 }
382 }
383 catch (RPNExprException& err) {
384 cout << "RPNExp[" << num << "] Exception: " << err.Msg() << endl;
385 }
386
387 return 0;
388}
[3276]389
390/* --Fonction-- */
391int tst_timer()
392{
393 TIMEF ;
394 cout << "**** tcmd.cc : Test class SOPHYA::Timer() **** " << endl;
395 Timer tm("tcmd::Timer", false);
[3578]396
[3276]397 int N = 10000000;
398 double * x = new double[N];
399 for(int k=0; k<N; k++) x[k] = k*0.0001;
400 for(int k=0; k<N; k++) x[k] = sin(x[k]);
401 for(int k=0; k<N; k++) x[k] = cos(x[k]);
402 tm.Split();
[3578]403 cout << " Step1: PARTIAL CPU(s)=" << tm.PartialCPUTime()
404 << " Elapsed (s)= " << tm.PartialElapsedTime()
405 << " Elapsed (ms)= " << tm.PartialElapsedTimems() << endl;
406 cout << " Step1: TOTAL CPU(s)=" << tm.TotalCPUTime()
407 << " Elapsed (s)= " << tm.TotalElapsedTime()
408 << " Elapsed (ms)= " << tm.TotalElapsedTimems() << endl;
[3276]409
410 for(int k=0; k<N; k++) x[k] *= 8.5;
411 for(int k=0; k<N; k++) x[k] = tan(x[k]);
412
413 tm.Split();
[3578]414 cout << " Step2: PARTIAL CPU(s)=" << tm.PartialCPUTime()
415 << " Elapsed (s)= " << tm.PartialElapsedTime()
416 << " Elapsed (ms)= " << tm.PartialElapsedTimems() << endl;
417 cout << " Step2: TOTAL CPU(s)=" << tm.TotalCPUTime()
418 << " Elapsed (s)= " << tm.TotalElapsedTime()
419 << " Elapsed (ms)= " << tm.TotalElapsedTimems() << endl;
[3276]420
421 for(int k=0; k<N; k++) x[k] = sin(x[k]);
422 for(int k=0; k<N; k++) x[k] = cos(x[k]);
423 for(int k=0; k<N; k++) x[k] *= 15.5;
424 for(int k=0; k<N; k++) x[k] = sin(x[k]);
425
426 tm.Split();
[3578]427 cout << " Step3: PARTIAL CPU(s)=" << tm.PartialCPUTime()
428 << " Elapsed (s)= " << tm.PartialElapsedTime()
429 << " Elapsed (ms)= " << tm.PartialElapsedTimems() << endl;
430 cout << " Step3: TOTAL CPU(s)=" << tm.TotalCPUTime()
431 << " Elapsed (s)= " << tm.TotalElapsedTime()
432 << " Elapsed (ms)= " << tm.TotalElapsedTimems() << endl;
[3276]433
[3578]434 tm.Split("CheckPrintFmt", true);
435 cout << " Waiting 70 sec (sleep(70)) ... " << endl;
436 sleep(100);
437 tm.Split("CheckPrintFmt-AfterWait", true);
438
439 cout << " Doing delete[] x, sleep(5) ... " << endl;
440 delete[] x;
441 sleep(5);
[3276]442}
443
Note: See TracBrowser for help on using the repository browser.