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

Last change on this file since 3570 was 3276, checked in by ansari, 18 years ago

Codage test specifique classeTimer ds tcmd.cc , Reza 05/07/2007

File size: 13.1 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 "sophyainit.h"
9#include "timing.h"
10#include "cexpre.h"
11#include "rpneval.h"
12#include "commander.h"
13
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 --------------- */
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";
43usage = "cp: Execute /usr/bin/cp \n";
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 " ;
55 for(int kk=0; kk<args.size(); kk++) { cmd += args[kk]; cmd += ' '; }
56 cout << "TCmdExecutor::Execute() : Executing " << cmd << endl;
57 return system(cmd.c_str());
58}
59
60int InputLoop(Commander & cmd);
61int tst_cexpreval(); // Test de CExpressionEvaluator
62int tst_rpneval(); // Test de RPNEvaluator
63int tst_timer(); // Test de Timer
64
65/* --Main-- */
66int main(int narg, char *arg[])
67{
68
69 if (narg < 2) {
70 cout << " Usage: tcmd S=commander/cexptst/rpntst/timer \n"
71 << " S= commander: Commader class test \n"
72 << " S= cexptst: CExpressionEvaluator class test \n"
73 << " S= rpntst: RPNExpressionEvaluator class test \n"
74 << " S= timer: Timer class test \n" << endl;
75 return 0;
76 }
77 string opt = arg[1];
78 SophyaInit();
79 InitTim();
80 int rc = 0;
81 cout << " ---- tcmd S= " << opt << " (commander/evaluator test) " << endl;
82 try {
83 if (opt == "commander") {
84 Commander cmd;
85 TCmdExecutor cmdex(cmd);
86 InputLoop(cmd);
87 }
88 else if (opt == "cexptst") rc = tst_cexpreval();
89 else if (opt == "rpntst") rc = tst_rpneval();
90 else if (opt == "timer") rc = tst_timer();
91 else {
92 cout << " tcmd/error: Unknown select option: " << opt << endl;
93 rc = 9;
94 }
95 }
96
97 catch (PThrowable & exc) {
98 cerr << " Catched Exception " << (string)typeid(exc).name()
99 << " - Msg= " << exc.Msg() << endl;
100 }
101 catch (std::exception & e) {
102 cerr << " exception catched : e.what()= " << e.what() << endl;
103 }
104 catch (...) {
105 cerr << " some other exception was caught ! " << endl;
106 }
107
108 PrtTim(" End of tcmd ");
109 PrtTim("--Fin tcmd ");
110 return(rc);
111}
112
113/* --Fonction-- */
114int InputLoop(Commander & cmd)
115{
116 cout << " ====================================================== " << endl;
117 cout << " ============== Test of Commander class ============ " << endl;
118 cout << " ====================================================== " << endl;
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')) ) {
132 buff[strlen(buff)-1] = '\0';
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
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++;
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++;
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;
191
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
307 cout << " >>>>> Expression avec erreur <<<<<< " << endl;
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;
355 if ( (nok == 8) && (nerr == 0) && (nerrparse == 3) ) return 0;
356 else return 99;
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}
389
390/* --Fonction-- */
391int tst_timer()
392{
393 TIMEF ;
394 cout << "**** tcmd.cc : Test class SOPHYA::Timer() **** " << endl;
395 Timer tm("tcmd::Timer", false);
396 int N = 10000000;
397 double * x = new double[N];
398 for(int k=0; k<N; k++) x[k] = k*0.0001;
399 for(int k=0; k<N; k++) x[k] = sin(x[k]);
400 for(int k=0; k<N; k++) x[k] = cos(x[k]);
401 tm.Split();
402 cout << " Step1: PARTIAL CPU=" << tm.PartialCPUTime()
403 << " Elapsed= " << tm.PartialElapsedTime() << endl;
404 cout << " Step1: TOTAL CPU=" << tm.TotalCPUTime()
405 << " Elapsed= " << tm.TotalElapsedTime() << endl;
406
407 for(int k=0; k<N; k++) x[k] *= 8.5;
408 for(int k=0; k<N; k++) x[k] = tan(x[k]);
409
410 tm.Split();
411 cout << " Step2: PARTIAL CPU=" << tm.PartialCPUTime()
412 << " Elapsed= " << tm.PartialElapsedTime() << endl;
413 cout << " Step2: TOTAL CPU=" << tm.TotalCPUTime()
414 << " Elapsed= " << tm.TotalElapsedTime() << endl;
415
416 for(int k=0; k<N; k++) x[k] = sin(x[k]);
417 for(int k=0; k<N; k++) x[k] = cos(x[k]);
418 for(int k=0; k<N; k++) x[k] *= 15.5;
419 for(int k=0; k<N; k++) x[k] = sin(x[k]);
420
421 tm.Split();
422 cout << " Step3: PARTIAL CPU=" << tm.PartialCPUTime()
423 << " Elapsed= " << tm.PartialElapsedTime() << endl;
424 cout << " Step3: TOTAL CPU=" << tm.TotalCPUTime()
425 << " Elapsed= " << tm.TotalElapsedTime() << endl;
426
427
428 delete x;
429}
430
Note: See TracBrowser for help on using the repository browser.