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

Last change on this file since 3606 was 3582, checked in by cmv, 17 years ago

ajout unistd.h pour prototypt de sleep(), cmv 23/02/2009

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