pd_alg_first_test.cpp

Go to the documentation of this file.
00001 /** @file pd_alg_first_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_first_test.h"
00010 
00011 namespace faudes {
00012  
00013   
00014 /* *****************
00015  * TestPostClPostfixes
00016  * *****************/  
00017 void TestPostClPostfixes(){
00018   std::string name = "PostCl Postfixes";
00019   TestStart(name);
00020   
00021   //just to populate events and stack symbols
00022   TestGenerator1();
00023   
00024   GrammarSymbolVector word;
00025   GrammarSymbolWordSet wordSet;
00026 
00027   //create terminals (a) and (lambda)
00028   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00029   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00030   //create nonterminals (1, [dot]) and (2, [square], 1)
00031   std::vector<Idx> v;
00032   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00033   Nonterminal* nt1dot = new Nonterminal(1,v);
00034   v.clear();
00035   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00036   Nonterminal* nt2square1 = new Nonterminal(2,v,1);
00037   //create word ((2, [square], 1)a(1, [dot]))
00038   GrammarSymbolPtr ptrnt2square1(nt2square1);
00039   word.push_back(ptrnt2square1);
00040   GrammarSymbolPtr ptrta(ta);
00041   word.push_back(ptrta);
00042   GrammarSymbolPtr ptrnt1dot(nt1dot);
00043   word.push_back(ptrnt1dot);
00044   
00045   wordSet = PostCl(word);
00046   
00047   try{
00048     
00049     //the set must contain::
00050     GrammarSymbolWordSet expectedWords;
00051     GrammarSymbolWordSet::const_iterator wordit;
00052 
00053     //(2, [square], 1)a(1, [dot])
00054     GrammarSymbolVector v2square1a1dot;
00055     v2square1a1dot.push_back(ptrnt2square1);
00056     v2square1a1dot.push_back(ptrta);
00057     v2square1a1dot.push_back(ptrnt1dot);    
00058     expectedWords.insert(v2square1a1dot);
00059     //a(1, [dot])
00060     GrammarSymbolVector va1dot;
00061     va1dot.push_back(ptrta);
00062     va1dot.push_back(ptrnt1dot);
00063     expectedWords.insert(va1dot);
00064     //(1, [dot])
00065     GrammarSymbolVector v1dot;
00066     v1dot.push_back(ptrnt1dot);
00067     expectedWords.insert(v1dot);
00068     //lambda
00069     GrammarSymbolVector v1lambda;
00070     GrammarSymbolPtr ptrtlambda(tlambda);
00071     v1lambda.push_back(ptrtlambda);
00072     expectedWords.insert(v1lambda);
00073     
00074     //the set must contain 4 items
00075     if(wordSet.size() != expectedWords.size()){
00076       std::stringstream errstr;
00077       errstr << "Word set size was " << wordSet.size() << ", but " << expectedWords.size() << " was expected." << std::endl;
00078       throw Exception(name, errstr.str(), 1003);
00079     }
00080     
00081     //check for specific items
00082     for(wordit = expectedWords.begin(); wordit != expectedWords.end(); wordit++){
00083       if(!ContainsWord(wordSet, *wordit)){
00084         std::stringstream errstr;
00085         errstr << "Word set did not contain the expected word ";
00086         
00087         GrammarSymbolVector::const_iterator gsit;
00088         for(gsit = wordit->begin(); gsit != wordit->end(); gsit++){
00089           errstr << (*gsit)->Str();
00090         }
00091         errstr << std::endl;
00092         throw Exception(name, errstr.str(), 1003);
00093       }
00094     }
00095   }
00096   catch (Exception e){ 
00097   }
00098   
00099   TestEnd(name);
00100   
00101 }
00102 
00103 /* *****************
00104  * TestFdsWords
00105  * *****************/
00106 void TestFdsWords(){
00107   std::string name = "Fds Words";
00108   TestStart(name);
00109   
00110   //just to populate events and stack symbols
00111   TestGenerator1();
00112   
00113   Grammar gr = TestGrammar4();
00114   
00115   GrammarSymbolWordSet wordSet = Fds(gr);
00116   
00117   try{
00118     
00119     //create terminals (a) and (lambda)
00120     Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00121     GrammarSymbolPtr ptrta(ta);
00122     Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00123     GrammarSymbolPtr ptrtlambda(tlambda);
00124     //create nonterminals (1, [dot]), (1, [dot], 1) and (2,square)
00125     std::vector<Idx> v;
00126     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00127     Nonterminal* nt1dot = new Nonterminal(1,v);
00128     GrammarSymbolPtr ptrnt1dot(nt1dot);
00129     v.clear();
00130     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00131     Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
00132     GrammarSymbolPtr ptrnt1dot1(nt1dot1);
00133     v.clear();
00134     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00135     Nonterminal* nt2square = new Nonterminal(2,v);
00136     GrammarSymbolPtr ptrnt2square(nt2square);
00137     
00138     //the set must contain::
00139     GrammarSymbolWordSet expectedWords;
00140     GrammarSymbolWordSet::const_iterator wordit;
00141     
00142     //(1, [dot])
00143     GrammarSymbolVector v1dot;
00144     v1dot.push_back(ptrnt1dot);
00145     expectedWords.insert(v1dot);
00146     //a(1, [dot])
00147     GrammarSymbolVector va1dot;
00148     va1dot.push_back(ptrta);
00149     va1dot.push_back(ptrnt1dot);
00150     expectedWords.insert(va1dot);
00151     //lambda
00152     GrammarSymbolVector v1lambda;
00153     v1lambda.push_back(ptrtlambda);
00154     expectedWords.insert(v1lambda);
00155     //(2, [square])
00156     GrammarSymbolVector v2square;
00157     v2square.push_back(ptrnt2square);
00158     expectedWords.insert(v2square);
00159     //a(2, [square])
00160     GrammarSymbolVector va2square;
00161     va2square.push_back(ptrta);
00162     va2square.push_back(ptrnt2square);
00163     expectedWords.insert(va2square);
00164     //(1, [dot] ,1)a(2, [square])
00165     GrammarSymbolVector v1dot1a2square;
00166     v1dot1a2square.push_back(ptrnt1dot1);
00167     v1dot1a2square.push_back(ptrta);
00168     v1dot1a2square.push_back(ptrnt2square);
00169     expectedWords.insert(v1dot1a2square);
00170     
00171     
00172     //the set must contain 6 items
00173     if(wordSet.size() != expectedWords.size()){
00174       std::stringstream errstr;
00175       errstr << "Word set size was " << wordSet.size() << ", but " << expectedWords.size() << " was expected." << std::endl;
00176       throw Exception(name, errstr.str(), 1003);
00177     }
00178     
00179     //check for specific items
00180     for(wordit = expectedWords.begin(); wordit != expectedWords.end(); wordit++){
00181       if(!ContainsWord(wordSet, *wordit)){
00182         std::stringstream errstr;
00183         errstr << "Word set did not contain the expected word ";
00184         
00185         GrammarSymbolVector::const_iterator gsit;
00186         for(gsit = wordit->begin(); gsit != wordit->end(); gsit++){
00187           errstr << (*gsit)->Str();
00188         }
00189         errstr << std::endl;
00190         throw Exception(name, errstr.str(), 1003);
00191       }
00192     }
00193     
00194   }
00195   catch (Exception e){ 
00196   }
00197   
00198   TestEnd(name);
00199 }
00200 
00201 /* *****************
00202  * TestFirstLMap
00203  * *****************/
00204 void TestFirstLMap(){
00205   std::string name = "FirstL Map";
00206   TestStart(name);
00207   
00208   //just to populate events and stack symbols
00209   TestGenerator1();
00210   
00211   Grammar gr = TestGrammar5();
00212   
00213   GrammarSymbolWordMap f = FirstL(gr,GrammarSymbolWordMap());
00214   
00215   /* 
00216    * the expected map is as follows:
00217    * 
00218    * lambda -> lambda
00219    * b -> b
00220    * (1, dot) -> a
00221    * (1, square) -> b, lambda
00222    * (1, dot, 1) -> a
00223    * a(1, square) -> a
00224    * (1, dot, 1)a(1, square) -> a
00225    * (2, square) -> a, b
00226    * (1, square)(1, dot) -> a, b
00227    */
00228   try{
00229     
00230     //create terminals (a), (b) and (lambda)
00231     Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00232     GrammarSymbolPtr ptrta(ta);
00233     Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00234     GrammarSymbolPtr ptrtb(tb);
00235     Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00236     GrammarSymbolPtr ptrtlambda(tlambda);
00237     //create nonterminals (1, [dot]), (1, [dot], 1), (1, [square]) and (2,[square])
00238     std::vector<Idx> v;
00239     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00240     Nonterminal* nt1dot = new Nonterminal(1,v);
00241     GrammarSymbolPtr ptrnt1dot(nt1dot);
00242     v.clear();
00243     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00244     Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
00245     GrammarSymbolPtr ptrnt1dot1(nt1dot1);
00246     v.clear();
00247     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00248     Nonterminal* nt1square = new Nonterminal(1,v);
00249     GrammarSymbolPtr ptrnt1square(nt1square);
00250     v.clear();
00251     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00252     Nonterminal* nt2square = new Nonterminal(2,v);
00253     GrammarSymbolPtr ptrnt2square(nt2square);
00254     
00255     //test size of result set and result set contents for each word
00256     //lambda
00257     GrammarSymbolVector gsv;
00258     gsv.push_back(ptrtlambda);
00259     if(f.find(gsv)->second.size() != 1){
00260       std::stringstream errstr;
00261       errstr << "Word set of word lambda was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00262       throw Exception(name, errstr.str(), 1003);
00263     }
00264     if(f.find(gsv)->second.find(*tlambda) == f.find(gsv)->second.end()){
00265       std::stringstream errstr;
00266       errstr << "Word set of word lambda did not contain the expected word lambda." << std::endl;
00267       throw Exception(name, errstr.str(), 1003);
00268     }
00269     
00270     //b
00271     gsv.clear();
00272     gsv.push_back(ptrtb);
00273     if(f.find(gsv)->second.size() != 1){
00274       std::stringstream errstr;
00275       errstr << "Word set of word b was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00276       throw Exception(name, errstr.str(), 1003);
00277     }
00278     if(f.find(gsv)->second.find(*tb) == f.find(gsv)->second.end()){
00279       std::stringstream errstr;
00280       errstr << "Word set of word b did not contain the expected word b." << std::endl;
00281       throw Exception(name, errstr.str(), 1003);
00282     }
00283     
00284     //(1, dot)
00285     gsv.clear();
00286     gsv.push_back(ptrnt1dot);
00287     if(f.find(gsv)->second.size() != 1){
00288       std::stringstream errstr;
00289       errstr << "Word set of word (1, dot) was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00290       throw Exception(name, errstr.str(), 1003);
00291     }
00292     if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
00293       std::stringstream errstr;
00294       errstr << "Word set of word (1, dot) did not contain the expected word a." << std::endl;
00295       throw Exception(name, errstr.str(), 1003);
00296     }
00297     
00298     //(1, square)
00299     gsv.clear();
00300     gsv.push_back(ptrnt1square);
00301     if(f.find(gsv)->second.size() != 2){
00302       std::stringstream errstr;
00303       errstr << "Word set of word (1, square) was expected to be of size 2, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00304       throw Exception(name, errstr.str(), 1003);
00305     }
00306     if(f.find(gsv)->second.find(*tb) == f.find(gsv)->second.end()){
00307       std::stringstream errstr;
00308       errstr << "Word set of word (1, square) did not contain the expected word b." << std::endl;
00309       throw Exception(name, errstr.str(), 1003);
00310     }
00311     if(f.find(gsv)->second.find(*tlambda) == f.find(gsv)->second.end()){
00312       std::stringstream errstr;
00313       errstr << "Word set of word (1, square) did not contain the expected word lambda." << std::endl;
00314       throw Exception(name, errstr.str(), 1003);
00315     }
00316     
00317     //(1, dot, 1)
00318     gsv.clear();
00319     gsv.push_back(ptrnt1dot1);
00320     if(f.find(gsv)->second.size() != 1){
00321       std::stringstream errstr;
00322       errstr << "Word set of word (1, dot, 1) was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00323       throw Exception(name, errstr.str(), 1003);
00324     }
00325     if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
00326       std::stringstream errstr;
00327       errstr << "Word set of word (1, dot, 1) did not contain the expected word a." << std::endl;
00328       throw Exception(name, errstr.str(), 1003);
00329     }
00330     
00331     //a(1, square)
00332     gsv.clear();
00333     gsv.push_back(ptrta);
00334     gsv.push_back(ptrnt1square);
00335     if(f.find(gsv)->second.size() != 1){
00336       std::stringstream errstr;
00337       errstr << "Word set of word a(1, square) was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00338       throw Exception(name, errstr.str(), 1003);
00339     }
00340     if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
00341       std::stringstream errstr;
00342       errstr << "Word set of word a(1, square) did not contain the expected word a." << std::endl;
00343       throw Exception(name, errstr.str(), 1003);
00344     }
00345     
00346     //(1, dot, 1)a(1, square)
00347     gsv.clear();
00348     gsv.push_back(ptrnt1dot1);
00349     gsv.push_back(ptrta);
00350     gsv.push_back(ptrnt1square);
00351     if(f.find(gsv)->second.size() != 1){
00352       std::stringstream errstr;
00353       errstr << "Word set of word (1, dot, 1)a(1, square) was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00354       throw Exception(name, errstr.str(), 1003);
00355     }
00356     if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
00357       std::stringstream errstr;
00358       errstr << "Word set of word (1, dot, 1)a(1, square) did not contain the expected word a." << std::endl;
00359       throw Exception(name, errstr.str(), 1003);
00360     }
00361     
00362     //(2, square)
00363     gsv.clear();
00364     gsv.push_back(ptrnt2square);
00365     if(f.find(gsv)->second.size() != 2){
00366       std::stringstream errstr;
00367       errstr << "Word set of word (2, square) was expected to be of size 2, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00368       throw Exception(name, errstr.str(), 1003);
00369     }
00370     if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
00371       std::stringstream errstr;
00372       errstr << "Word set of word (2, square) did not contain the expected word a." << std::endl;
00373       throw Exception(name, errstr.str(), 1003);
00374     }
00375     if(f.find(gsv)->second.find(*tb) == f.find(gsv)->second.end()){
00376       std::stringstream errstr;
00377       errstr << "Word set of word (2, square) did not contain the expected word b." << std::endl;
00378       throw Exception(name, errstr.str(), 1003);
00379     }
00380     
00381     //(1, square)(1, dot)
00382     gsv.clear();
00383     gsv.push_back(ptrnt1square);
00384     gsv.push_back(ptrnt1dot);
00385     if(f.find(gsv)->second.size() != 2){
00386       std::stringstream errstr;
00387       errstr << "Word set of word (1, square)(1, dot) was expected to be of size 2, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
00388       throw Exception(name, errstr.str(), 1003);
00389     }
00390     if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
00391       std::stringstream errstr;
00392       errstr << "Word set of word (1, square)(1, dot) did not contain the expected word a." << std::endl;
00393       throw Exception(name, errstr.str(), 1003);
00394     }
00395     if(f.find(gsv)->second.find(*tb) == f.find(gsv)->second.end()){
00396       std::stringstream errstr;
00397       errstr << "Word set of word (1, square)(1, dot) did not contain the expected word b." << std::endl;
00398       throw Exception(name, errstr.str(), 1003);
00399     }
00400   }
00401   catch (Exception e){ 
00402   }
00403   
00404   TestEnd(name);
00405 }
00406 
00407 /* *****************
00408  * TestFirstASelectedWords
00409  * *****************/
00410 void TestFirstASelectedWords(){
00411   std::string name = "FirstA Selected Words";
00412   TestStart(name);
00413   
00414   //just to populate events and stack symbols
00415   TestGenerator1();
00416   
00417   Grammar gr = TestGrammar5();
00418   
00419   //create terminals (a), (b) and (lambda)
00420   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00421   GrammarSymbolPtr ptrta(ta);
00422   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00423   GrammarSymbolPtr ptrtb(tb);
00424   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00425   GrammarSymbolPtr ptrtlambda(tlambda);
00426   //create nonterminals (1, [dot]) (1, [square])
00427   std::vector<Idx> v;
00428   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00429   Nonterminal* nt1dot = new Nonterminal(1,v);
00430   GrammarSymbolPtr ptrnt1dot(nt1dot);
00431   v.clear();
00432   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00433   Nonterminal* nt1square = new Nonterminal(1,v);
00434   GrammarSymbolPtr ptrnt1square(nt1square);    
00435   
00436   //test for lambda -> lambda
00437   GrammarSymbolVector word;
00438   word.push_back(ptrtlambda);  
00439   std::set<Terminal> firstSet = FirstA(gr, word);
00440   try{
00441 
00442     if(firstSet.size() != 1){
00443       std::stringstream errstr;
00444       errstr << "Word set of word lambda was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
00445       throw Exception(name, errstr.str(), 1003);
00446     }
00447     if(firstSet.find(*tlambda) == firstSet.end()){
00448       std::stringstream errstr;
00449       errstr << "Word set of word lambda did not contain the expected word lambda." << std::endl;
00450       throw Exception(name, errstr.str(), 1003);
00451     }
00452   }
00453   catch (Exception e){ 
00454   }
00455   
00456   //test for (1, square) -> b, lambda
00457   word.clear();
00458   word.push_back(ptrnt1square);  
00459    firstSet = FirstA(gr, word);
00460   try{
00461 
00462     if(firstSet.size() != 2){
00463       std::stringstream errstr;
00464       errstr << "Word set of word (1, square) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
00465       throw Exception(name, errstr.str(), 1003);
00466     }
00467     if(firstSet.find(*tb) == firstSet.end()){
00468       std::stringstream errstr;
00469       errstr << "Word set of word (1, square) did not contain the expected word b." << std::endl;
00470       throw Exception(name, errstr.str(), 1003);
00471     }
00472     if(firstSet.find(*tlambda) == firstSet.end()){
00473       std::stringstream errstr;
00474       errstr << "Word set of word (1, square) did not contain the expected word lambda." << std::endl;
00475       throw Exception(name, errstr.str(), 1003);
00476     }
00477   }
00478   catch (Exception e){ 
00479   }
00480   
00481   //test for (1, square)(1, dot) -> a, b
00482   word.clear();
00483   word.push_back(ptrnt1square);
00484   word.push_back(ptrnt1dot);  
00485    firstSet = FirstA(gr, word);
00486   try{
00487 
00488     if(firstSet.size() != 2){
00489       std::stringstream errstr;
00490       errstr << "Word set of word (1, square)(1, dot) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
00491       throw Exception(name, errstr.str(), 1003);
00492     }
00493     if(firstSet.find(*ta) == firstSet.end()){
00494       std::stringstream errstr;
00495       errstr << "Word set of word (1, square)(1, dot) did not contain the expected word a." << std::endl;
00496       throw Exception(name, errstr.str(), 1003);
00497     }
00498     if(firstSet.find(*tb) == firstSet.end()){
00499       std::stringstream errstr;
00500       errstr << "Word set of word (1, square)(1, dot) did not contain the expected word b." << std::endl;
00501       throw Exception(name, errstr.str(), 1003);
00502     }
00503   }
00504   catch (Exception e){ 
00505   }
00506   
00507   TestEnd(name);
00508 }
00509 
00510 /* *****************
00511  * TestFirstSelectedWords
00512  * *****************/
00513 void TestFirstSelectedWords(){
00514   std::string name = "First Selected Words";
00515   TestStart(name);
00516   
00517   //just to populate events and stack symbols
00518   TestGenerator1();
00519   
00520   Grammar gr = TestGrammar5();
00521   
00522   //create terminals (a), (b) and (lambda)
00523   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00524   GrammarSymbolPtr ptrta(ta);
00525   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00526   GrammarSymbolPtr ptrtb(tb);
00527   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00528   GrammarSymbolPtr ptrtlambda(tlambda);
00529   //create nonterminals (1, [dot]), (1, [square]) and (2, [square])
00530   std::vector<Idx> v;
00531   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00532   Nonterminal* nt1dot = new Nonterminal(1,v);
00533   GrammarSymbolPtr ptrnt1dot(nt1dot);
00534   v.clear();
00535   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00536   Nonterminal* nt1square = new Nonterminal(1,v);
00537   GrammarSymbolPtr ptrnt1square(nt1square);    
00538   v.clear();
00539   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00540   Nonterminal* nt2square = new Nonterminal(2,v);
00541   GrammarSymbolPtr ptrnt2square(nt2square);    
00542   
00543   //test for lambda -> lambda
00544   GrammarSymbolVector word;
00545   word.push_back(ptrtlambda);  
00546   std::set<Terminal> firstSet = First(gr, word);
00547   try{
00548 
00549     if(firstSet.size() != 1){
00550       std::stringstream errstr;
00551       errstr << "Word set of word lambda was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
00552       throw Exception(name, errstr.str(), 1003);
00553     }
00554     if(firstSet.find(*tlambda) == firstSet.end()){
00555       std::stringstream errstr;
00556       errstr << "Word set of word lambda did not contain the expected word lambda." << std::endl;
00557       throw Exception(name, errstr.str(), 1003);
00558     }
00559   }
00560   catch (Exception e){ 
00561   }
00562   
00563   //test for (2, square)(1, dot) -> a, b
00564   word.clear();
00565   word.push_back(ptrnt2square);
00566   word.push_back(ptrnt1dot);
00567   firstSet = First(gr, word);
00568   try{
00569 
00570     if(firstSet.size() != 2){
00571       std::stringstream errstr;
00572       errstr << "Word set of word (2, square)(1, dot) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
00573       throw Exception(name, errstr.str(), 1003);
00574     }
00575     if(firstSet.find(*tb) == firstSet.end()){
00576       std::stringstream errstr;
00577       errstr << "Word set of word (2, square)(1, dot) did not contain the expected word b." << std::endl;
00578       throw Exception(name, errstr.str(), 1003);
00579     }
00580     if(firstSet.find(*ta) == firstSet.end()){
00581       std::stringstream errstr;
00582       errstr << "Word set of word (2, square)(1, dot) did not contain the expected word a." << std::endl;
00583       throw Exception(name, errstr.str(), 1003);
00584     }
00585   }
00586   catch (Exception e){ 
00587   }
00588   
00589   //test for (1, square)(1, dot) -> a, b
00590   word.clear();
00591   word.push_back(ptrnt1square);
00592   word.push_back(ptrnt1dot);  
00593    firstSet = First(gr, word);
00594   try{
00595 
00596     if(firstSet.size() != 2){
00597       std::stringstream errstr;
00598       errstr << "Word set of word (1, square)(1, dot) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
00599       throw Exception(name, errstr.str(), 1003);
00600     }
00601     if(firstSet.find(*ta) == firstSet.end()){
00602       std::stringstream errstr;
00603       errstr << "Word set of word (1, square)(1, dot) did not contain the expected word a." << std::endl;
00604       throw Exception(name, errstr.str(), 1003);
00605     }
00606     if(firstSet.find(*tb) == firstSet.end()){
00607       std::stringstream errstr;
00608       errstr << "Word set of word (1, square)(1, dot) did not contain the expected word b." << std::endl;
00609       throw Exception(name, errstr.str(), 1003);
00610     }
00611   }
00612   catch (Exception e){ 
00613   }
00614   
00615   TestEnd(name);
00616 }
00617 
00618 /* *****************
00619  * TestFirstRedConsistentWord
00620  * *****************/
00621 void TestFirstRedConsistentWord(){
00622   std::string name = "FirstRed Consistent Word";
00623   TestStart(name);
00624   
00625   //just to populate events and stack symbols
00626   TestGenerator1();
00627   
00628   Grammar gr = TestGrammar5();
00629   
00630   //create terminals (b), lambda
00631   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00632   GrammarSymbolPtr ptrtb(tb);
00633   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00634   GrammarSymbolPtr ptrtlambda(tlambda);
00635   //create nonterminal (1, [square])
00636   std::vector<Idx> v;
00637   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00638   Nonterminal* nt1square = new Nonterminal(1,v);
00639   GrammarSymbolPtr ptrnt1square(nt1square);    
00640   
00641   
00642   //test for (1, square) -> b, lambda
00643   GrammarSymbolVector word;
00644   word.push_back(ptrnt1square);
00645   std::set<Terminal> firstSet = FirstRed(gr, word);
00646   try{
00647 
00648     if(firstSet.size() != 2){
00649       std::stringstream errstr;
00650       errstr << "Word set of word (1, square) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
00651       throw Exception(name, errstr.str(), 1003);
00652     }
00653     if(firstSet.find(*tb) == firstSet.end()){
00654       std::stringstream errstr;
00655       errstr << "Word set of word (1, square) did not contain the expected word b." << std::endl;
00656       throw Exception(name, errstr.str(), 1003);
00657     }
00658     if(firstSet.find(*tlambda) == firstSet.end()){
00659       std::stringstream errstr;
00660       errstr << "Word set of word (1, square) did not contain the expected word lambda." << std::endl;
00661       throw Exception(name, errstr.str(), 1003);
00662     }
00663   }
00664   catch (Exception e){ 
00665   }
00666   
00667   TestEnd(name);
00668 }
00669 
00670 /* *****************
00671  * TestFirstRedInconsistentWord
00672  * *****************/
00673 void TestFirstRedInconsistentWord(){
00674   std::string name = "FirstRed Inconsistent Word";
00675   TestStart(name);
00676   
00677   //just to populate events and stack symbols
00678   TestGenerator1();
00679   
00680   Grammar gr = TestGrammar5();
00681   
00682   //create terminal (a)
00683   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00684   GrammarSymbolPtr ptrta(ta);
00685 
00686   //create nonterminal (1, [dot], 2)
00687   std::vector<Idx> v;
00688   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00689   Nonterminal* nt1dot2 = new Nonterminal(1,v,2);
00690   GrammarSymbolPtr ptrnt1dot2(nt1dot2);   
00691 
00692   
00693   //test for (1, dot, 2) -> empty
00694   GrammarSymbolVector word;
00695   word.push_back(ptrnt1dot2);  
00696   std::set<Terminal> firstSet = FirstRed(gr, word);
00697   try{
00698 
00699     if(!firstSet.empty()){
00700       std::stringstream errstr;
00701       errstr << "Word set of word (1, dot, 2) was expected to be empty, but was of size " << firstSet.size() << "." << std::endl;
00702       throw Exception(name, errstr.str(), 1003);
00703     }
00704   }
00705   catch (Exception e){ 
00706   }
00707   
00708   TestEnd(name);
00709   
00710 }
00711 
00712 /* *****************
00713  * TestFilter1MixedGrammarSymbols
00714  * *****************/
00715 void TestFilter1MixedGrammarSymbols(){
00716   
00717   std::string name = "Filter1 Mixed Grammar Symbols";
00718   TestStart(name);
00719   
00720   Grammar gr1 = TestGrammar1();
00721   
00722   GrammarSymbolVector word;
00723   std::set<Terminal>::const_iterator tit;
00724   std::vector<TerminalPtr> tpv;
00725   std::vector<TerminalPtr>::iterator tpvit;
00726   
00727   //build word to filter
00728   //contains two nonterminal and all terminals
00729   Terminal* t;
00730   Nonterminal* nt = new Nonterminal(*gr1.Nonterminals().begin());
00731   GrammarSymbolPtr ntPtr(nt);
00732   word.push_back(ntPtr);
00733   word.push_back(ntPtr);
00734 
00735   for(tit = gr1.Terminals().begin(); tit != gr1.Terminals().end(); tit++){
00736     //create new terminal
00737     t = new Terminal(*tit);
00738     TerminalPtr tPtr(t);
00739     //push terminal into word
00740     word.push_back(tPtr);
00741   }
00742   
00743   NonterminalPtr rPtr = Filter1(gr1.Nonterminals(),word);
00744   
00745   try{
00746     //resulting nonterminal needs to be the same the nonterminal in the word 
00747     if(*rPtr != *ntPtr){
00748       std::stringstream errstr;
00749       errstr << "result nonterminal is " << rPtr->Str() << ", but " << ntPtr->Str() << " was expected" << std::endl;
00750       throw Exception(name, errstr.str(), 1003);
00751     }
00752   }
00753    catch(Exception e){
00754   }
00755   
00756   TestEnd(name);
00757 }
00758 
00759 /* *****************
00760  * TestFilter1Nothing
00761  * *****************/
00762 void TestFilter1Nothing(){
00763   std::string name = "Filter1 Nothing";
00764   TestStart(name);
00765   
00766   std::set<Nonterminal> nt;
00767   GrammarSymbolVector gs;
00768   
00769   NonterminalPtr rPtr = Filter1(nt,gs);
00770   
00771   try{
00772     //resulting pointer has to be NULL
00773     if(rPtr != NULL){
00774       std::stringstream errstr;
00775       errstr << "size of result set is no NULL" << std::endl;
00776       throw Exception(name, errstr.str(), 1003);
00777     }
00778   }
00779    catch(Exception e){
00780   }
00781   
00782   TestEnd(name);
00783 }
00784 
00785 
00786 /* *****************
00787  * TestFirstAllTerminalsOnly
00788  * *****************/
00789 void TestFirstAllTerminalsOnly(){
00790   std::string name = "FirstAll Terminals Only";
00791   TestStart(name);
00792   
00793   //just to populate events and stack symbols
00794   TestGenerator1();
00795   
00796   Grammar gr = TestGrammar6();
00797   
00798   //create terminals (a), (b)
00799   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00800   GrammarSymbolPtr ptrta(ta);
00801   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00802   GrammarSymbolPtr ptrtb(tb);
00803   
00804   //test for ab -> a
00805   GrammarSymbolVector word;
00806   word.push_back(ptrta);  
00807   word.push_back(ptrtb);  
00808   std::set<Terminal> firstSet = FirstAll(gr, word);
00809   try{
00810 
00811     if(firstSet.size() != 1){
00812       std::stringstream errstr;
00813       errstr << "Word set of word ab was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
00814       throw Exception(name, errstr.str(), 1003);
00815     }
00816     if(firstSet.find(*ta) == firstSet.end()){
00817       std::stringstream errstr;
00818       errstr << "Word set of word ab did not contain the expected word a." << std::endl;
00819       throw Exception(name, errstr.str(), 1003);
00820     }
00821   }
00822   catch (Exception e){ 
00823   }
00824   
00825   TestEnd(name);
00826 }
00827 
00828 /* *****************
00829  * TestFirstAllNonterminalsIrreducibleWord
00830  * *****************/
00831 void TestFirstAllNonterminalsIrreducibleWord(){
00832   std::string name = "FirstAll Nonterminals Irreducible Word";
00833   TestStart(name);
00834   
00835   //just to populate events and stack symbols
00836   TestGenerator1();
00837   
00838   Grammar gr = TestGrammar6();
00839   
00840   //create nonterminal (1, [square], 1)
00841   std::vector<Idx> v;
00842   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00843   Nonterminal* nt1square1 = new Nonterminal(1,v,1);
00844   GrammarSymbolPtr ptrnt1square1(nt1square1);
00845   
00846   //test for (1, square, 1) -> empty
00847   GrammarSymbolVector word;
00848   word.push_back(ptrnt1square1);  
00849   std::set<Terminal> firstSet = FirstAll(gr, word);
00850   try{
00851 
00852     if(!firstSet.empty()){
00853       std::stringstream errstr;
00854       errstr << "Word set of word (1, square, 1) was expected to be empty, but was of size " << firstSet.size() << "." << std::endl;
00855       throw Exception(name, errstr.str(), 1003);
00856     }
00857   }
00858   catch (Exception e){ 
00859   }
00860   
00861   TestEnd(name);
00862   
00863 }
00864 
00865 /* *****************
00866  * TestFirstAllNonterminalsReducibleWord
00867  * *****************/
00868 void TestFirstAllNonterminalsReducibleWord(){
00869   std::string name = "FirstAll Nonterminals Reducible Word";
00870   TestStart(name);
00871   
00872   //just to populate events and stack symbols
00873   TestGenerator1();
00874   
00875   Grammar gr = TestGrammar6();
00876   
00877   //create terminal (a)
00878   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00879   GrammarSymbolPtr ptrta(ta);
00880   
00881   //create nonterminals (1, [dot], 1) and (1, [dot])
00882   std::vector<Idx> v;
00883   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00884   Nonterminal* nt1dot = new Nonterminal(1,v);
00885   GrammarSymbolPtr ptrnt1dot(nt1dot);
00886   v.clear();
00887   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00888   Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
00889   GrammarSymbolPtr ptrnt1dot1(nt1dot1);
00890   
00891   //test for (1, [dot])(1, [dot], 1) -> a
00892   GrammarSymbolVector word;
00893   word.push_back(ptrnt1dot);  
00894   word.push_back(ptrnt1dot1);  
00895   std::set<Terminal> firstSet = FirstAll(gr, word);
00896   try{
00897 
00898     if(firstSet.size() != 1){
00899       std::stringstream errstr;
00900       errstr << "Word set of word (1, [dot])(1, [dot], 1) was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
00901       throw Exception(name, errstr.str(), 1003);
00902     }
00903     if(firstSet.find(*ta) == firstSet.end()){
00904       std::stringstream errstr;
00905       errstr << "Word set of word (1, [dot])(1, [dot], 1) did not contain the expected word a." << std::endl;
00906       throw Exception(name, errstr.str(), 1003);
00907     }
00908   }
00909   catch (Exception e){ 
00910   }
00911   
00912   TestEnd(name);
00913 }
00914 
00915 /* *****************
00916  * TestFirstLeq1ZeroEmpty
00917  * *****************/
00918 void TestFirstLeq1ZeroEmpty(){
00919   std::string name = "FirstLeq1 Zero Empty";
00920   TestStart(name);
00921   
00922   //just to populate events and stack symbols
00923   TestGenerator1();
00924   
00925   Grammar gr = TestGrammar6();
00926   
00927   //create nonterminals (1, [square], 1)
00928   std::vector<Idx> v;
00929   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00930   Nonterminal* nt1square1 = new Nonterminal(1,v,1);
00931   GrammarSymbolPtr ptrnt1square1(nt1square1);
00932 
00933   //test for 0, (1, [square], 1) -> empty
00934   GrammarSymbolVector word;
00935   word.push_back(ptrnt1square1);  
00936   std::set<Terminal> firstSet = FirstLeq1(gr, 0, word);
00937   try{
00938 
00939     if(!firstSet.empty()){
00940       std::stringstream errstr;
00941       errstr << "Word set of k = 0 and word (1, [square], 1) was expected to be empty, but was of size " << firstSet.size() << "." << std::endl;
00942       throw Exception(name, errstr.str(), 1003);
00943     }
00944   }
00945   catch (Exception e){ 
00946   }
00947   
00948   TestEnd(name);
00949 }
00950 
00951 /* *****************
00952  * TestFirstLeq1ZeroNonempty
00953  * *****************/
00954 void TestFirstLeq1ZeroNonempty(){
00955   std::string name = "FirstLeq1 Zero Nonempty";
00956   TestStart(name);
00957   
00958   //just to populate events and stack symbols
00959   TestGenerator1();
00960   
00961   Grammar gr = TestGrammar6();
00962   
00963   //create terminal lambda
00964   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00965   GrammarSymbolPtr ptrtlambda(tlambda);
00966   
00967   //create nonterminals (1, [dot])
00968   std::vector<Idx> v;
00969   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00970   Nonterminal* nt1dot = new Nonterminal(1,v);
00971   GrammarSymbolPtr ptrnt1dot(nt1dot);
00972   
00973   //test for 0, (1, [dot]) -> lambda
00974   GrammarSymbolVector word;
00975   word.push_back(ptrnt1dot);  
00976   std::set<Terminal> firstSet = FirstLeq1(gr, 0, word);
00977   try{
00978 
00979     if(firstSet.size() != 1){
00980       std::stringstream errstr;
00981       errstr << "Word set of k = 0 and word (1, [dot]) was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
00982       throw Exception(name, errstr.str(), 1003);
00983     }
00984     if(firstSet.find(*tlambda) == firstSet.end()){
00985       std::stringstream errstr;
00986       errstr << "Word set of k = 0 and word (1, [dot]) did not contain the expected word lambda." << std::endl;
00987       throw Exception(name, errstr.str(), 1003);
00988     }
00989   }
00990   catch (Exception e){ 
00991   }
00992   
00993   TestEnd(name);
00994 }
00995 
00996 /* *****************
00997  * TestFirstLeq1OneNonempty
00998  * *****************/
00999 void TestFirstLeq1OneNonempty(){
01000   std::string name = "FirstLeq1 One Nonempty";
01001   TestStart(name);
01002   
01003   //just to populate events and stack symbols
01004   TestGenerator1();
01005   
01006   Grammar gr = TestGrammar6();
01007   
01008   //create terminal a
01009   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
01010   GrammarSymbolPtr ptrta(ta);
01011   
01012   //create nonterminals (1, [dot])
01013   std::vector<Idx> v;
01014   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
01015   Nonterminal* nt1dot = new Nonterminal(1,v);
01016   GrammarSymbolPtr ptrnt1dot(nt1dot);
01017   
01018   //test for 1, (1, [dot]) -> a
01019   GrammarSymbolVector word;
01020   word.push_back(ptrnt1dot);  
01021   std::set<Terminal> firstSet = FirstLeq1(gr, 1, word);
01022   try{
01023 
01024     if(firstSet.size() != 1){
01025       std::stringstream errstr;
01026       errstr << "Word set of k = 1 and word (1, [dot]) was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
01027       throw Exception(name, errstr.str(), 1003);
01028     }
01029     if(firstSet.find(*ta) == firstSet.end()){
01030       std::stringstream errstr;
01031       errstr << "Word set of k = 1 and word (1, [dot]) did not contain the expected word lambda." << std::endl;
01032       throw Exception(name, errstr.str(), 1003);
01033     }
01034   }
01035   catch (Exception e){ 
01036   }
01037   
01038   TestEnd(name);
01039 }
01040 
01041 /* *****************
01042  * TestFirstLeq1TwoNonempty
01043  * *****************/
01044 void TestFirstLeq1TwoNonempty(){
01045   std::string name = "FirstLeq1 Two Nonempty";
01046   TestStart(name);
01047   
01048   //just to populate events and stack symbols
01049   TestGenerator1();
01050   
01051   Grammar gr = TestGrammar6();
01052   
01053   //create nonterminals (1, [dot])
01054   std::vector<Idx> v;
01055   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
01056   Nonterminal* nt1dot = new Nonterminal(1,v);
01057   GrammarSymbolPtr ptrnt1dot(nt1dot);
01058 
01059   //test for 2, (1, [dot]) -> empty
01060   GrammarSymbolVector word;
01061   word.push_back(ptrnt1dot);  
01062   std::set<Terminal> firstSet = FirstLeq1(gr, 2, word);
01063   try{
01064 
01065    if(!firstSet.empty()){
01066       std::stringstream errstr;
01067       errstr << "Word set of k = 2 and word (1, [dot]) was expected to be empty, but was of size " << firstSet.size() << "." << std::endl;
01068       throw Exception(name, errstr.str(), 1003);
01069     }
01070   }
01071   catch (Exception e){ 
01072   }
01073   
01074   TestEnd(name);
01075 }
01076 
01077 /* *****************
01078  * TestPostCl
01079  * *****************/
01080 void TestPostCl(){
01081   TestPostClPostfixes();  
01082 }
01083 
01084 /* *****************
01085  * TestFds
01086  * *****************/
01087 void TestFds(){
01088   TestFdsWords();  
01089 }
01090 
01091 /* *****************
01092  * TestFirstA
01093  * *****************/
01094 void TestFirstA(){
01095   TestFirstASelectedWords();
01096 }
01097 
01098 /* *****************
01099  * TestFirstL
01100  * *****************/
01101 void TestFirstL(){
01102   TestFirstLMap();
01103 }
01104 
01105 /* *****************
01106  * TestFirst
01107  * *****************/
01108 void TestFirst(){
01109   TestFirstSelectedWords();
01110 }
01111 
01112 
01113 /* *****************
01114  * TestFirstRed
01115  * *****************/
01116 void TestFirstRed(){
01117   TestFirstRedConsistentWord();
01118   TestFirstRedInconsistentWord();
01119 }
01120 
01121 /* *****************
01122  * TestFilter1
01123  * *****************/
01124 void TestFilter1(){
01125   TestFilter1MixedGrammarSymbols();
01126   TestFilter1Nothing();
01127 }
01128 
01129 /* *****************
01130  * TestFirstAll
01131  * *****************/
01132 void TestFirstAll(){
01133   TestFirstAllTerminalsOnly();
01134   TestFirstAllNonterminalsReducibleWord();
01135   TestFirstAllNonterminalsIrreducibleWord();
01136 }
01137 
01138 /* *****************
01139  * TestFirstLeq1
01140  * *****************/
01141 void TestFirstLeq1(){
01142   TestFirstLeq1ZeroEmpty();
01143   TestFirstLeq1ZeroNonempty();
01144   TestFirstLeq1OneNonempty();
01145   TestFirstLeq1TwoNonempty();
01146 }
01147 
01148 } // namespace faudes
01149 

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