Salome HOME
PR: what YACS means
[modules/yacs.git] / src / engine / Test / testPorts.cxx
1 #include "Node.hxx"
2 #include "Data.hxx"
3 #include "InGate.hxx"
4 #include "OutGate.hxx"
5 #include "InputPort.hxx"
6 #include "OutputPort.hxx"
7 //
8 #include <cppunit/extensions/HelperMacros.h>
9 #include <cppunit/ui/text/TestRunner.h>
10 #include <sstream>
11 //
12
13 using namespace YACS::ENGINE;
14
15 class PortsTest : public CppUnit::TestFixture
16 {
17   CPPUNIT_TEST_SUITE( PortsTest );
18   CPPUNIT_TEST( testControlFlow );
19   CPPUNIT_TEST( testDataFlow1 );
20   CPPUNIT_TEST_EXCEPTION( testDataFlowTypeCheck, YACS::ENGINE::ConversionException);
21   CPPUNIT_TEST_SUITE_END();
22 public: 
23   void tearDown();
24   void testControlFlow();
25   void testDataFlow1();
26   void testDataFlowTypeCheck();
27 };
28
29 void PortsTest::tearDown()
30 {
31 }
32
33 void PortsTest::testControlFlow()
34 {
35   Node *n1=0,*n2=0,*n3=0,*n4=0;
36   OutGate *o1=new OutGate(n1);
37   OutGate *o2=new OutGate(n2);
38   OutGate *o3=new OutGate(n3);
39   OutGate *o4=new OutGate(n4);
40   InGate *i1=new InGate(n1);
41   InGate *i2=new InGate(n2);
42   InGate *i3=new InGate(n3);
43   InGate *i4=new InGate(n4);
44   // Simuling edition
45   o1->edAddInGate(i2); o1->edAddInGate(i3);
46   o2->edAddInGate(i2); o2->edAddInGate(i4);
47   o3->edAddInGate(i2); o3->edAddInGate(i4); o3->edAddInGate(i3);
48   o4->edAddInGate(i2); o4->edAddInGate(i4);
49   // Simuling execution
50   CPPUNIT_ASSERT( i1->exIsReady() ); CPPUNIT_ASSERT( !i2->exIsReady() ); CPPUNIT_ASSERT( !i3->exIsReady() ); CPPUNIT_ASSERT( !i4->exIsReady() );
51   o1->exNotifyDone();
52   CPPUNIT_ASSERT( i1->exIsReady() ); CPPUNIT_ASSERT( !i2->exIsReady() ); CPPUNIT_ASSERT( !i3->exIsReady() ); CPPUNIT_ASSERT( !i4->exIsReady() );
53   o2->exNotifyDone();
54   CPPUNIT_ASSERT( i1->exIsReady() ); CPPUNIT_ASSERT( !i2->exIsReady() ); CPPUNIT_ASSERT( !i3->exIsReady() ); CPPUNIT_ASSERT( !i4->exIsReady() );
55   o3->exNotifyDone();
56   CPPUNIT_ASSERT( i1->exIsReady() ); CPPUNIT_ASSERT( !i2->exIsReady() ); CPPUNIT_ASSERT( i3->exIsReady() ); CPPUNIT_ASSERT( !i4->exIsReady() );
57   o4->exNotifyDone();
58   CPPUNIT_ASSERT( i1->exIsReady() ); CPPUNIT_ASSERT( i2->exIsReady() ); CPPUNIT_ASSERT( i3->exIsReady() ); CPPUNIT_ASSERT( i4->exIsReady() );
59   //
60   delete o1; delete o2; delete o3; delete o4;
61   delete i1; delete i2; delete i3; delete i4;
62 }
63
64 void PortsTest::testDataFlow1()
65 {
66   Node *n1=0,*n2=0,*n3=0,*n4=0;
67   OutputPort *o1=new OutputPort("o1",n1,YACS::Double);
68   OutputPort *o3=new OutputPort("o3",n3,YACS::Int);
69   InputPort *i1=new InputPort("i1",n1,YACS::Double);
70   InputPort *i2=new InputPort("i2",n2,YACS::Int);
71   InputPort *i4=new InputPort("i4",n4,YACS::Bool);
72   // Simuling edition
73   o1->edAddInputPort(i1); o1->edAddInputPort(i2);
74   o3->edAddInputPort(i1); o3->edAddInputPort(i2); o3->edAddInputPort(i4);
75   CPPUNIT_ASSERT( o1->foGet().edGetRepresentation() == "41_NULL" ); CPPUNIT_ASSERT( o3->foGet().edGetRepresentation() == "42_NULL" );
76   CPPUNIT_ASSERT( i1->exGet().edGetRepresentation() == "41_NULL" ); CPPUNIT_ASSERT( i2->exGet().edGetRepresentation() == "42_NULL" ); CPPUNIT_ASSERT( i4->exGet().edGetRepresentation() == "45_NULL" );
77   // Simuling execution
78   o1->exPut(3.14);
79   CPPUNIT_ASSERT( i1->exGet().edGetRepresentation() == "41_3.14" ); CPPUNIT_ASSERT( i2->exGet().edGetRepresentation() == "42_3" ); CPPUNIT_ASSERT( i4->exGet().edGetRepresentation() == "45_NULL" );
80   o3->exPut(325);
81   CPPUNIT_ASSERT( i1->exGet().edGetRepresentation() == "41_325" ); CPPUNIT_ASSERT( i2->exGet().edGetRepresentation() == "42_325" ); CPPUNIT_ASSERT( i4->exGet().edGetRepresentation() == "45_1" );
82   o1->exPut(-2.78);
83   CPPUNIT_ASSERT( i1->exGet().edGetRepresentation() == "41_-2.78" ); CPPUNIT_ASSERT( i2->exGet().edGetRepresentation() == "42_-2" ); CPPUNIT_ASSERT( i4->exGet().edGetRepresentation() == "45_1" );
84   //
85   delete o1; delete o3;
86   delete i1; delete i2; delete i4;
87 }
88
89 void PortsTest::testDataFlowTypeCheck()
90 {
91   Node *n1=0;
92   OutputPort o1("o1",n1,YACS::Double);
93   InputPort i1("i1",n1,YACS::Bool);
94   o1.edAddInputPort(&i1);//Should throw exception
95 }
96
97 int main()
98 {
99   CppUnit::TextUi::TestRunner runner;
100   runner.addTest( PortsTest::suite() );
101   runner.run();
102   return 0;
103 }