Salome HOME
PR: first version from Antony GEAY, with directory restructuration
[modules/yacs.git] / src / engine / Test / testBloc.cxx
1 #ifndef __TESTBLOC_HXX__
2 #define __TESTBLOC_HXX__
3
4 #include "Bloc.hxx"
5 #include "Data.hxx"
6 #include "ToyNode.hxx"
7 #include "Executor.hxx"
8 #include "Exception.hxx"
9 #include "InputPort.hxx"
10 #include "OutputPort.hxx"
11 //
12 #include <cppunit/extensions/HelperMacros.h>
13 #include <cppunit/ui/text/TestRunner.h>
14 #include <sstream>
15 #include <string.h>
16 //
17
18 #define DBL_PRECISION_COMPARE 1e-15
19
20 using namespace YACS::ENGINE;
21
22 class BlocTest : public CppUnit::TestFixture
23 {
24   CPPUNIT_TEST_SUITE( BlocTest );
25   CPPUNIT_TEST( test1 );
26   CPPUNIT_TEST( test2 );
27   CPPUNIT_TEST( test3 );
28   CPPUNIT_TEST_SUITE_END();
29 public: 
30   void tearDown();
31   void test1();
32   void test2();
33   void test3();
34 };
35
36 void BlocTest::tearDown()
37 {
38 }
39
40 //Simplest test to test basic mechanisms like Data, initialisation of ports.
41 void BlocTest::test1()
42 {
43   ToyNode *n1=new ToyNode("T1");
44   InputPort *i8=n1->edAddInputPort("o");
45   InputPort *i9=n1->edAddInputPort("p");
46   Bloc *graph=new Bloc("toto");
47   graph->edAddChild(n1);
48   OutputPort *o1=n1->edAddOutputPort("b");
49   Data v1(5.67);
50   i9->edInit(v1);
51   v1=2.78;
52   i8->edInit(v1);
53   Executor exe;
54   exe.RunW(graph);
55   CPPUNIT_ASSERT_DOUBLES_EQUAL( o1->foGet().exGet<double>(),8.45,DBL_PRECISION_COMPARE );
56   OutputPort *o2=n1->edAddOutputPort("c");
57   exe.RunW(graph);
58   CPPUNIT_ASSERT_DOUBLES_EQUAL( o1->foGet().exGet<double>(),4.225,DBL_PRECISION_COMPARE );
59   delete graph;
60 }
61
62 //Only one level(hierarchy) simple graph. Tests DF and CF links.
63 void BlocTest::test2()
64 {
65   ToyNode *n1=new ToyNode("T1");
66   ToyNode *n2=new ToyNode("T2",50);
67   ToyNode *n3=new ToyNode("T3",20);
68   ToyNode *n4=new ToyNode("T4");
69   Bloc *graph=new Bloc("Global");
70   graph->edAddChild(n1); graph->edAddChild(n2); graph->edAddChild(n3); graph->edAddChild(n4);
71   InputPort *i1_a=n1->edAddInputPort("a"); InputPort *i1_b=n1->edAddInputPort("b"); OutputPort *o1_j=n1->edAddOutputPort("j"); OutputPort *o1_k=n1->edAddOutputPort("k");
72   InputPort *i2_c=n2->edAddInputPort("c"); InputPort *i2_d=n2->edAddInputPort("d"); OutputPort *o2_f=n2->edAddOutputPort("f");
73   InputPort *i3_e=n3->edAddInputPort("e"); OutputPort *o3_g=n3->edAddOutputPort("g"); OutputPort *o3_h=n3->edAddOutputPort("h");
74   InputPort *i4_l=n4->edAddInputPort("l"); InputPort *i4_m=n4->edAddInputPort("m"); OutputPort *o4_i=n4->edAddOutputPort("i");
75   //Retrieving gates
76   InGate *iN1=n1->getInGate(); InGate *iN2=n2->getInGate(); InGate *iN3=n3->getInGate(); InGate *iN4=n4->getInGate();
77   OutGate *oN1=n1->getOutGate(); OutGate *oN2=n2->getOutGate(); OutGate *oN3=n3->getOutGate(); OutGate *oN4=n4->getOutGate();
78   Data v1(1.2);
79   i1_a->edInit(v1);
80   v1=3.4;
81   i1_b->edInit(v1);
82   v1=7;
83   i2_d->edInit(v1);
84   //DF                                                                      //CF
85   graph->edAddLink(o1_j,i2_c);                                              graph->edAddLink(oN1,iN2);
86   graph->edAddLink(o1_k,i3_e);                                              graph->edAddLink(oN1,iN3);
87   graph->edAddLink(o2_f,i4_l);                                              graph->edAddLink(oN2,iN4);
88   graph->edAddLink(o3_g,i4_m);                                              graph->edAddLink(oN3,iN4);
89   Executor exe;
90   exe.RunW(graph);
91   CPPUNIT_ASSERT_DOUBLES_EQUAL( o4_i->foGet().exGet<double>(),10.45,DBL_PRECISION_COMPARE );
92   n2->setTime(0); n3->setTime(0);
93   exe.RunW(graph);
94   CPPUNIT_ASSERT_DOUBLES_EQUAL( o4_i->foGet().exGet<double>(),10.45,DBL_PRECISION_COMPARE );
95   bool testOfCycleSucceed=false;
96   try
97     { graph->edAddLink(oN4,iN1); }
98   catch(YACS::Exception &e)
99     { if(strcmp(e.what(),"Cycle has been detected")==0)
100       testOfCycleSucceed=true; }
101   CPPUNIT_ASSERT(testOfCycleSucceed);
102   delete graph;
103 }
104
105 //test multi level graphs
106 void BlocTest::test3()
107 {
108   Bloc *toto=new Bloc("toto");
109   Bloc *tata=new Bloc("tata");
110   Bloc *titi=new Bloc("titi");
111   ToyNode *n2=new ToyNode("T2"); ToyNode *n3=new ToyNode("T3"); ToyNode *n4=new ToyNode("T4"); ToyNode *n5=new ToyNode("T5"); ToyNode *n6=new ToyNode("T6");
112   ToyNode *n7=new ToyNode("T7",20); ToyNode *n8=new ToyNode("T8");
113   toto->edAddChild(titi); titi->edAddChild(tata); titi->edAddChild(n3); toto->edAddChild(n2); tata->edAddChild(n4); tata->edAddChild(n5); tata->edAddChild(n6);
114   titi->edAddChild(n7); titi->edAddChild(n8);
115   toto->edAddCFLink(n2,titi); toto->edAddCFLink(n3,tata); toto->edAddCFLink(n5,n4); titi->edAddCFLink(n7,n8); titi->edAddCFLink(tata,n8);
116   //
117   InputPort *i2_a=n2->edAddInputPort("a"); OutputPort *o2_a=n2->edAddOutputPort("a"); OutputPort *o2_b=n2->edAddOutputPort("b");
118   InputPort *i3_a=n3->edAddInputPort("a"); OutputPort *o3_a=n3->edAddOutputPort("a"); OutputPort *o3_b=n3->edAddOutputPort("b");
119   InputPort *i4_a=n4->edAddInputPort("a"); InputPort *i4_b=n4->edAddInputPort("b"); OutputPort *o4_a=n4->edAddOutputPort("a"); OutputPort *o4_b=n4->edAddOutputPort("b");
120   InputPort *i5_a=n5->edAddInputPort("a"); InputPort *i5_b=n5->edAddInputPort("b"); OutputPort *o5_a=n5->edAddOutputPort("a");
121   InputPort *i6_a=n6->edAddInputPort("a"); InputPort *i6_b=n6->edAddInputPort("b"); OutputPort *o6_a=n6->edAddOutputPort("a");
122   InputPort *i7_a=n7->edAddInputPort("a"); OutputPort *o7_a=n7->edAddOutputPort("a");
123   InputPort *i8_a=n8->edAddInputPort("a"); InputPort *i8_b=n8->edAddInputPort("b"); OutputPort *o8_a=n8->edAddOutputPort("a");
124   //
125   toto->edAddLink(o2_a,i3_a);  toto->edAddLink(o3_a,i5_a); toto->edAddLink(o2_b,i5_b); toto->edAddLink(o2_a,i4_b);
126   toto->edAddLink(o3_b,i6_a); toto->edAddLink(o6_a,i8_a); toto->edAddLink(o7_a,i8_b);
127   //
128   Data v1(1.2);
129   i2_a->edInit(v1);
130   v1=7;
131   i6_b->edInit(v1);
132   i4_a->edInit(v1);
133   v1=3;
134   i7_a->edInit(v1);
135   CPPUNIT_ASSERT( toto->getNumberOfCFLinks()== 1 ); CPPUNIT_ASSERT( titi->getNumberOfCFLinks()== 3 ); CPPUNIT_ASSERT( tata->getNumberOfCFLinks()== 1 );
136   Executor exe;
137   exe.RunW(toto);
138   CPPUNIT_ASSERT_DOUBLES_EQUAL( o6_a->foGet().exGet<double>(),7.3,DBL_PRECISION_COMPARE );
139   CPPUNIT_ASSERT_DOUBLES_EQUAL( o4_a->foGet().exGet<double>(),3.8,DBL_PRECISION_COMPARE );
140   CPPUNIT_ASSERT_DOUBLES_EQUAL( o8_a->foGet().exGet<double>(),10.3,DBL_PRECISION_COMPARE );
141   delete toto;
142 }
143
144 int main()
145 {
146   CppUnit::TextUi::TestRunner runner;
147   runner.addTest( BlocTest::suite() );
148   runner.run();
149   return 0;
150 }
151
152
153 #endif