Salome HOME
PR: merge from BR_DATACONV_PR tag "mergeto_trunk_25oct06"
[modules/yacs.git] / src / engine / Test / engineTest.cxx
1
2 // --- include from engine first, to avoid redifinition warning _POSIX_C_SOURCE
3
4 #include "Bloc.hxx"
5 #include "ElementaryNode.hxx"
6 #include "Loop.hxx"
7 #include "Switch.hxx"
8 #include "Runtime.hxx"
9
10 #include "engineTest.hxx"
11
12 #include <iostream>
13 #include <sstream>
14 #include <string>
15 #include <list>
16 #include <vector>
17
18 using namespace YACS::ENGINE;
19 using namespace YACS;
20 using namespace std;
21
22 #define _DEVDEBUG_
23 #ifdef _DEVDEBUG_
24 #define MYDEBTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
25 #define DEBTRACE(msg) {MYDEBTRACE; std::cerr<<msg<<std::endl<<std::flush;}
26 #else
27 #define MYDEBTRACE
28 #define DEBTRACE(msg)
29 #endif
30
31 map<string, Node*> EngineTest::_nodeMap; 
32 map<string, ComposedNode*> EngineTest::_compoMap;
33
34 // --- init typecodes
35
36 TypeCode *EngineTest::_tc_bool   = new TypeCode(Bool);
37 TypeCode *EngineTest::_tc_int    = new TypeCode(Int);
38 TypeCode *EngineTest::_tc_double = new TypeCode(Double);
39     
40
41 void EngineTest::setUp()
42 {
43 }
44
45 void EngineTest::tearDown()
46 {
47 }
48
49 void EngineTest::checkGetRuntime()
50 {
51   CPPUNIT_ASSERT_THROW(Runtime *myrun = getRuntime(), YACS::Exception); 
52   Runtime::setRuntime();
53   Runtime *myrun1 = getRuntime();
54   CPPUNIT_ASSERT(myrun1);
55   Runtime *myrun2 = getRuntime();
56   CPPUNIT_ASSERT_EQUAL(myrun1, myrun2);
57   }
58
59 void EngineTest::checkInGateOutGate()
60 {
61   string nodeName = "Node1";
62   ElementaryNode* node1 = new TestElemNode(nodeName);
63   _nodeMap[nodeName] = node1;
64
65   nodeName = "Node2";
66   ElementaryNode* node2 = new TestElemNode(nodeName);
67   _nodeMap[nodeName] = node2;
68
69   DEBTRACE(" --- check InGate OK after node creation" );
70   {
71     InGate *node1Ingate = node1->getInGate();
72     CPPUNIT_ASSERT(node1Ingate);
73     string name=node1Ingate->getNameOfTypeOfCurrentInstance();
74     string expected="InGate";
75     CPPUNIT_ASSERT_EQUAL(name,expected);
76   }
77
78   DEBTRACE(" --- check OutGate OK after node creation" );
79   {
80     OutGate *node1Outgate = node1->getOutGate();
81     CPPUNIT_ASSERT(node1Outgate);
82     string name=node1Outgate->getNameOfTypeOfCurrentInstance();
83     string expected="OutGate";
84     CPPUNIT_ASSERT_EQUAL(name,expected);
85   }
86 }
87
88 void EngineTest::checkNodePortNumber()
89 {
90   ElementaryNode* node1 = (ElementaryNode*) _nodeMap["Node1"];
91
92   DEBTRACE(" --- check number of ports = 0 after node creation" );
93   CPPUNIT_ASSERT(node1->getNumberOfInputPorts() == 0);
94   CPPUNIT_ASSERT(node1->getNumberOfOutputPorts() == 0);
95
96   InputPort  *in1 = node1->edAddInputPort("ib1",_tc_bool);
97   InputPort  *in2 = node1->edAddInputPort("ii1",_tc_int);
98   InputPort  *in3 = node1->edAddInputPort("ii2",_tc_int);
99   InputPort  *in4 = node1->edAddInputPort("id1",_tc_double);
100
101   OutputPort *ou1 = node1->edAddOutputPort("ob1",_tc_bool);
102   OutputPort *ou2 = node1->edAddOutputPort("oi1",_tc_int);
103   OutputPort *ou3 = node1->edAddOutputPort("od1",_tc_double);
104
105   DEBTRACE(" --- check number of ports after ports creation" );
106 //   DEBTRACE("     node1->getNumberOfInputPorts(): "
107 //         << node1->getNumberOfInputPorts());
108 //   DEBTRACE("     node1->getNumberOfOutputPorts(): "
109 //         << node1->getNumberOfOutputPorts());
110   CPPUNIT_ASSERT(node1->getNumberOfInputPorts() == 4);
111   CPPUNIT_ASSERT(node1->getNumberOfOutputPorts() == 3);
112 }
113
114 void EngineTest::checkPortTypeName()
115 {
116   ElementaryNode* node1 = (ElementaryNode*) _nodeMap["Node1"];
117
118   DEBTRACE(" --- check InputPort name OK" );
119   {
120     string name=node1->getInputPort("ib1")->NAME;
121     string expected="InputPort";
122     CPPUNIT_ASSERT_EQUAL(name,expected);
123   }
124
125   DEBTRACE(" --- check OutputPort name OK" );
126   {
127     string name=node1->getOutputPort("ob1")->NAME;
128     string expected="OutputPort";
129     CPPUNIT_ASSERT_EQUAL(name,expected);
130   }
131 }
132
133 void EngineTest::checkDuplicatePortName()
134 {
135   ElementaryNode* node1 = (ElementaryNode*) _nodeMap["Node1"];
136   DEBTRACE(" --- check duplicated name throws exception" );
137   CPPUNIT_ASSERT_THROW(InputPort *in5=node1->edAddInputPort("ii2",_tc_int),
138                        YACS::Exception); 
139 }
140
141 void EngineTest::checkRemovePort()
142 {
143   ElementaryNode* node1 = (ElementaryNode*) _nodeMap["Node1"];
144   ElementaryNode* node2 = (ElementaryNode*) _nodeMap["Node2"];
145
146     DEBTRACE(" --- check remove port" );
147   {
148     node1->edRemovePort(node1->getInputPort("ib1"));
149     CPPUNIT_ASSERT(node1->getNumberOfInputPorts() == 3);
150     CPPUNIT_ASSERT(node1->getNumberOfOutputPorts() == 3);
151   }
152
153   DEBTRACE(" --- check remove wrong port throws exception" )
154   {
155     CPPUNIT_ASSERT_THROW(node1->edRemovePort(node1->getInputPort("ib1")),
156                          YACS::Exception);
157   }
158 }
159
160 void EngineTest::checkAddNodesToBloc()
161 {
162   DEBTRACE(" --- delete node; // ====== NOT OK : done by bloc" );
163
164   for (int i=0; i<10; i++)
165     {
166       ostringstream ss;
167       ss << "Node_" << i;
168       string s = ss.str();
169       ElementaryNode* node = new TestElemNode(s);
170       _nodeMap[s] = node;
171       InputPort  *i1 = node->edAddInputPort("ib1",_tc_bool);
172       InputPort  *i2 = node->edAddInputPort("ii1",_tc_int);
173       InputPort  *i3 = node->edAddInputPort("ii2",_tc_int);
174       InputPort  *i4 = node->edAddInputPort("id1",_tc_double);
175       OutputPort *o1 = node->edAddOutputPort("ob1",_tc_bool);
176       OutputPort *o2 = node->edAddOutputPort("oi1",_tc_int);
177       OutputPort *o3 = node->edAddOutputPort("od1",_tc_double);
178     }
179
180   DEBTRACE(" --- create bloc, add two nodes, check constituants" );
181
182   Bloc* bloc1 = new Bloc("bloc1");
183   _nodeMap["bloc1"] = bloc1;
184   _compoMap["bloc1"] = bloc1;
185   bloc1->edAddChild(_nodeMap["Node_1"]);
186   bloc1->edAddChild(_nodeMap["Node_2"]);
187   {
188     set<ElementaryNode *> setelem = bloc1->getRecursiveConstituents();
189     CPPUNIT_ASSERT(setelem.size() == 2);
190   }
191 }
192
193 void EngineTest::checkAddingTwiceSameNodeInSameBloc()
194 {
195   DEBTRACE(" --- add the same node two times does nothing: return false" );
196
197   CPPUNIT_ASSERT(! ((Bloc*)_compoMap["bloc1"])->edAddChild(_nodeMap["Node_1"]));
198 }
199
200 void EngineTest::checkAddingTwiceSameNodeInTwoBlocs()
201 {
202   DEBTRACE(" --- add a node already used elsewhere raises exception" );
203
204   Bloc* bloc2 = new Bloc("bloc2");
205   _nodeMap["bloc2"] = bloc2;
206   _compoMap["bloc2"] = bloc2;
207   bloc2->edAddChild(_nodeMap["Node_3"]);
208
209   CPPUNIT_ASSERT_THROW(bloc2->edAddChild(_nodeMap["Node_1"]),
210                        YACS::Exception);
211 }
212
213 void EngineTest::checkRecursiveBlocs_NumberOfNodes()
214 {
215   Bloc *bloc1 = (Bloc*)_compoMap["bloc1"];
216   Bloc *bloc2 = (Bloc*)_compoMap["bloc2"];
217
218   DEBTRACE(" --- recursive blocs, check constituants" );
219
220   Bloc* bloc3 = new Bloc("bloc3");
221   _nodeMap["bloc3"] = bloc3;
222   _compoMap["bloc3"] = bloc3;
223   bloc3->edAddChild((bloc1));  // 2 elementary nodes 
224   bloc3->edAddChild((bloc2));  // 1 elementary node
225   bloc3->edAddChild(_nodeMap["Node_4"]);   // 1 elementary node
226   {
227     set<ElementaryNode *> setelem = bloc3->getRecursiveConstituents();
228     CPPUNIT_ASSERT(setelem.size() == 4);
229     for (set<ElementaryNode*>::iterator it=setelem.begin(); it!=setelem.end(); it++)
230       {
231         DEBTRACE("     elem name = " << (*it)->getName());
232       }
233   }
234 }
235
236 void EngineTest::checkRecursiveBlocs_NumberOfPorts()
237 {
238   Bloc *bloc3 = (Bloc*)_compoMap["bloc3"];
239
240   DEBTRACE(" --- recursive blocs, check input & output ports // COMPLETER" );
241   CPPUNIT_ASSERT(bloc3->getNumberOfInputPorts() == 4*4);
242   DEBTRACE("     number of input ports: " << bloc3->getNumberOfInputPorts());
243   DEBTRACE("     number of output ports: " << bloc3->getNumberOfOutputPorts());
244   {
245     set<InputPort *> inset = bloc3->getSetOfInputPort();
246     set<OutputPort *> outset = bloc3->getSetOfOutputPort();
247     for (set<InputPort *>::iterator it=inset.begin(); it!=inset.end(); it++)
248       {
249         DEBTRACE("     input port name = " << bloc3->getInputPortName(*it));
250       }
251     for (set<OutputPort *>::iterator it=outset.begin(); it!=outset.end(); it++)
252       {
253         DEBTRACE("     output port name = " << (*it)->getName());
254       }
255   }
256 }
257
258 void EngineTest::checkPortNameInBloc()
259 {
260
261   DEBTRACE(" --- recursive blocs, check port names" );
262   
263   InputPort *inport = _nodeMap["Node_1"]->getInputPort("id1");
264   CPPUNIT_ASSERT(_nodeMap["bloc3"]->getInputPortName(inport) == "bloc1.Node_1.id1");
265 }
266
267 void EngineTest::checkGetNameOfPortNotInBloc()
268 {
269   InputPort *inport = _nodeMap["Node_5"]->getInputPort("id1");
270   CPPUNIT_ASSERT_THROW(string name = _nodeMap["bloc3"]->getInputPortName(inport),
271                        YACS::Exception);
272 }
273
274 void EngineTest::RecursiveBlocs_multipleRecursion()
275 {
276   {
277     Bloc* bloc = new Bloc("bloc4");
278     _nodeMap["bloc4"] = bloc;
279     _compoMap["bloc4"] = bloc;
280     bloc->edAddChild(_nodeMap["Node_5"]);
281     bloc->edAddChild(_nodeMap["Node_6"]);
282   }
283
284   {
285     Bloc* bloc = new Bloc("bloc5");
286     _nodeMap["bloc5"] = bloc;
287     _compoMap["bloc5"] = bloc;
288     bloc->edAddChild(_nodeMap["Node_7"]);
289     bloc->edAddChild(_nodeMap["Node_8"]);
290   }
291
292   {
293     Bloc* bloc = new Bloc("bloc6");
294     _nodeMap["bloc6"] = bloc;
295     _compoMap["bloc6"] = bloc;
296     bloc->edAddChild(_nodeMap["bloc4"]);
297     bloc->edAddChild(_nodeMap["bloc5"]);
298   }
299
300   {   
301     Bloc* bloc = new Bloc("bloc7");
302     _nodeMap["bloc7"] = bloc;
303     _compoMap["bloc7"] = bloc;
304     bloc->edAddChild(_nodeMap["bloc3"]);
305     bloc->edAddChild(_nodeMap["bloc6"]);
306   }
307
308   {   
309     Bloc* bloc = new Bloc("graphe");
310     _nodeMap["graphe"] = bloc;
311     _compoMap["graphe"] = bloc;
312     bloc->edAddChild(_nodeMap["bloc7"]);
313     bloc->edAddChild(_nodeMap["Node_9"]);
314   }
315
316   {
317     set<ElementaryNode *> setelem = _nodeMap["graphe"]->getRecursiveConstituents();
318     CPPUNIT_ASSERT(setelem.size() == 9);
319     for (set<ElementaryNode*>::iterator it=setelem.begin(); it!=setelem.end(); it++)
320       {
321         DEBTRACE("     elem name = " << (*it)->getName());
322       }
323   }
324
325   {
326     set<InputPort *> inset = _nodeMap["bloc7"]->getSetOfInputPort();
327     set<OutputPort *> outset = _nodeMap["bloc7"]->getSetOfOutputPort();
328     for (set<InputPort *>::iterator it=inset.begin(); it!=inset.end(); it++)
329       {
330         DEBTRACE("     input port name for bloc7  = " << _nodeMap["bloc7"]->getInputPortName(*it));
331         DEBTRACE("     input port name for graphe = " << _nodeMap["graphe"]->getInputPortName(*it));
332       }
333     for (set<OutputPort *>::iterator it=outset.begin(); it!=outset.end(); it++)
334       {
335         DEBTRACE("     output port name = " << (*it)->getName());
336       }
337   }
338 }