pd_alg_opt_test.cpp

Go to the documentation of this file.
00001 /** @file pd_alg_opt_test.cpp  Unit Tests */
00002 
00003 
00004 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes)
00005 
00006    Copyright (C) 2013  Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess
00007 
00008 */
00009 #include "pd_alg_opt_test.h"
00010 
00011 namespace faudes {
00012 
00013 /* *****************
00014  * TestRemoveUselessTransitionsNumberOfTransitions
00015  * *****************/
00016 void TestRemoveUselessTransitionsNumberOfTransitions(){
00017   std::string name = "RemoveUselessTransitions Number of Transitions";
00018   TestStart(name);
00019   
00020   PushdownGenerator pd = TestGenerator21();
00021   
00022   PushdownGenerator rPd = RemoveUselessTransitions(pd);
00023   
00024   Idx s1 = pd.StateIndex("s1");
00025   Idx s2 = pd.StateIndex("s2");
00026   Idx square = pd.StackSymbolIndex("square");
00027   Idx dot = pd.StackSymbolIndex("dot");
00028   Idx lambda = pd.StackSymbolIndex(FAUDES_PD_LAMBDA);
00029   Idx a = pd.EventIndex("a");
00030   Idx b = pd.EventIndex("b");
00031   
00032   try{
00033     
00034     TransSet::Iterator transit;
00035     PopPushSet::const_iterator ppit;
00036     
00037     //number of transitions must be 4
00038     if(rPd.TransRelSize() != 4){
00039       std::stringstream errstr;
00040       errstr << "Number of transitions was expected to be 4, but was " << rPd.TransRelSize() << "." << std::endl;
00041       throw Exception(name, errstr.str(), 1003);
00042     }
00043     
00044     //test transition going out from s1
00045     for(transit = rPd.TransRelBegin(s1); transit != rPd.TransRelEnd(s1); transit++){
00046       
00047       //transition with event a must pop square
00048       if(transit->Ev == a){
00049         for(ppit = rPd.PopPushBegin(*transit); ppit != rPd.PopPushEnd(*transit); ppit++){
00050           
00051           //transition must pop square
00052           if(ppit->first.front() != square){
00053             std::stringstream errstr;
00054             errstr << "Transition originating at state s1 was expected to pop square, but popped " << rPd.StackSymbolName(ppit->first.front()) << "." << std::endl;
00055             throw Exception(name, errstr.str(), 1003);
00056           }
00057         }
00058       }
00059       
00060       //transition with event b must pop square
00061       if(transit->Ev == b){
00062         for(ppit = rPd.PopPushBegin(*transit); ppit != rPd.PopPushEnd(*transit); ppit++){
00063           
00064           //transition must pop square
00065           if(ppit->first.front() != square){
00066             std::stringstream errstr;
00067             errstr << "Transition originating at state s1 was expected to pop square, but popped " << rPd.StackSymbolName(ppit->first.front()) << "." << std::endl;
00068             throw Exception(name, errstr.str(), 1003);
00069           }
00070         }
00071       }
00072     }
00073     
00074     //test transitions going out from s2
00075     for(transit = rPd.TransRelBegin(s2); transit != rPd.TransRelEnd(s2); transit++){
00076       
00077       //transition with event a must pop dot
00078       if(transit->Ev == a){
00079         for(ppit = rPd.PopPushBegin(*transit); ppit != rPd.PopPushEnd(*transit); ppit++){
00080           
00081           //transition must pop dot
00082           if(ppit->first.front() != dot){
00083             std::stringstream errstr;
00084             errstr << "Transition originating at state s2 with event a was expected to pop dot, but popped " << rPd.StackSymbolName(ppit->first.front()) << "." << std::endl;
00085             throw Exception(name, errstr.str(), 1003);
00086           }
00087         }
00088       }
00089       //transition with event b must pop lambda
00090       if(transit->Ev == b){
00091         for(ppit = rPd.PopPushBegin(*transit); ppit != rPd.PopPushEnd(*transit); ppit++){
00092           
00093           //transition must pop lambda
00094           if(ppit->first.front() != lambda){
00095             std::stringstream errstr;
00096             errstr << "Transition originating at state s2 with event b was expected to pop lambda, but popped " << rPd.StackSymbolName(ppit->first.front()) << "." << std::endl;
00097             throw Exception(name, errstr.str(), 1003);
00098           }
00099         }
00100       }
00101     }
00102   }
00103   catch(Exception e){
00104   }
00105   
00106   TestEnd(name);
00107 }
00108 
00109 /* *****************
00110  * TestGetPossibleStackTopsFindCorrectOnes
00111  * *****************/
00112 void TestGetPossibleStackTopsFindCorrectOnes(){
00113   
00114   std::string name = "TestGetPossibleStackTops Find Correct Ones Number";
00115   TestStart(name);
00116   
00117   PushdownGenerator pd = TestGenerator23();
00118   
00119   Idx dot = pd.StackSymbolIndex("dot");
00120   Idx heart= pd.StackSymbolIndex("heart");
00121   Idx diamond = pd.StackSymbolIndex("diamond");
00122   Idx square = pd.StackSymbolIndex("square");
00123   
00124   Idx s1 = pd.StateIndex("s1");
00125   Idx s2 = pd.StateIndex("s2");
00126   Idx s3 = pd.StateIndex("s3");
00127   
00128   StackSymbolSet::Iterator ssit;
00129   
00130   StackSymbolSet topS1 = GetPossibleStackTops(pd, StateSet(), s1);
00131   StackSymbolSet topS2 = GetPossibleStackTops(pd, StateSet(), s2);
00132   StackSymbolSet topS3 = GetPossibleStackTops(pd, StateSet(), s3);
00133   
00134   try{ 
00135     
00136     TransSet::Iterator transit;
00137     PopPushSet::const_iterator ppit;
00138     
00139     //test state 1, must contain square and diamond
00140     if(topS1.Size() != 2){
00141       std::stringstream errstr;
00142       errstr << "Number of possible stack top symbols of s1 was expected to be 2, but was " << topS1.Size() << "." << std::endl;
00143       throw Exception(name, errstr.str(), 1003);
00144     }
00145     if(!topS1.Exists(diamond)){
00146       std::stringstream errstr;
00147       errstr << "Stack symbol diamond was expected to be a possible stack top symbol of s1, but was not." << std::endl;
00148       throw Exception(name, errstr.str(), 1003);
00149     }
00150     if(!topS1.Exists(square)){
00151       std::stringstream errstr;
00152       errstr << "Stack symbol diamond was expected to be a possible stack top symbol of s1, but was not." << std::endl;
00153       throw Exception(name, errstr.str(), 1003);
00154     }
00155     //test state 2, must contain all symbols
00156     if(topS2.Size() != 4){
00157       std::stringstream errstr;
00158       errstr << "Number of possible stack top symbols of s2 was expected to be 4, but was " << topS2.Size() << "." << std::endl;
00159       throw Exception(name, errstr.str(), 1003);
00160     }
00161     //test state 2, must contain all symbols
00162     if(topS3.Size() != 4){
00163       std::stringstream errstr;
00164       errstr << "Number of possible stack top symbols of s3 was expected to be 4, but was " << topS3.Size() << "." << std::endl;
00165       throw Exception(name, errstr.str(), 1003);
00166     }
00167     
00168 
00169   }
00170   catch(Exception e){
00171   }
00172   
00173   TestEnd(name);
00174 }
00175   
00176 /* *****************
00177  * TestRemoveUselessTransitions
00178  * *****************/
00179 void TestRemoveUselessTransitions(){
00180   TestRemoveUselessTransitionsNumberOfTransitions();
00181 }
00182 
00183 /* *****************
00184  * TestGetPossibleStackTops
00185  * *****************/
00186 void TestGetPossibleStackTops(){
00187   TestGetPossibleStackTopsFindCorrectOnes();
00188 }
00189 
00190 } // namespace faudes
00191 

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen