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

Last change on this file since 3721 was 3619, checked in by cmv, 16 years ago

add various #include<> for g++ 4.3 (jaunty 9.04), cmv 05/05/2009

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