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

Last change on this file since 2513 was 2513, checked in by ansari, 22 years ago

Programme test commander (tcmd.cc) complete avec partie test classes CExpression/RPNExpression - Reza 16 Mars 2004

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