pd_alg_lrm_test.cpp

Go to the documentation of this file.
00001 /** @file pd_alg_lrm_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_lrm_test.h"
00010 
00011 namespace faudes {
00012 
00013   /* *****************
00014  * TestDesc11Terminal
00015  * *****************/
00016 void TestDesc11Terminal(){
00017   std::string name = "Desc11 Terminal";
00018   TestStart(name);
00019   
00020   //just to populate events and stack symbols
00021   TestGenerator1();
00022   
00023   Grammar gr = TestGrammar5();
00024   
00025   //create terminals
00026   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00027   GrammarSymbolPtr ptrta(ta);
00028   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00029   GrammarSymbolPtr ptrtlambda(tlambda);
00030   
00031   //create nonterminals (1, [dot])
00032   std::vector<Idx> v;
00033   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00034   Nonterminal* nt1dot = new Nonterminal(1,v);
00035   GrammarSymbolPtr ptrnt1dot(nt1dot);
00036   
00037   //test for ((1, [dot]) -> lambda . a, lambda)
00038   //    ((1, [dot]) -> lambda . a, lambda)
00039   GrammarSymbolVector beforeDot, afterDot;
00040   beforeDot.push_back(ptrtlambda);
00041   afterDot.push_back(ptrta);
00042   Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tlambda);
00043   
00044   std::set<Lr1Configuration> descSet = Desc11(gr, 1, config);
00045   try{
00046 
00047     if(descSet.size() != 1){
00048       std::stringstream errstr;
00049       errstr << "descendant set of (" << config.Str() << ") was expected to be of size 1, but was of size " << descSet.size() << "." << std::endl;
00050       throw Exception(name, errstr.str(), 1003);
00051     }
00052     
00053     if(descSet.find(config) == descSet.end()){
00054       std::stringstream errstr;
00055       errstr << "descendant set of  (" << config.Str() << ") was expected to contain the configuration (" << config.Str() << "), but the configuration was not found." << std::endl;
00056       throw Exception(name, errstr.str(), 1003);
00057     }
00058   }
00059   catch (Exception e){ 
00060   }
00061   
00062   TestEnd(name);
00063 }
00064 
00065 /* *****************
00066  * TestDesc11Nonterminal
00067  * *****************/
00068 void TestDesc11Nonterminal(){
00069   std::string name = "Desc11 Nonterminal";
00070   TestStart(name);
00071   
00072   //just to populate events and stack symbols
00073   TestGenerator1();
00074   
00075   Grammar gr = TestGrammar5();
00076   
00077   //create terminals
00078   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00079   GrammarSymbolPtr ptrta(ta);
00080   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00081   GrammarSymbolPtr ptrtb(tb);
00082   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00083   GrammarSymbolPtr ptrtlambda(tlambda);
00084   
00085   //create nonterminals (1, [dot])
00086   std::vector<Idx> v;
00087   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00088   Nonterminal* nt1dot = new Nonterminal(1,v);
00089   GrammarSymbolPtr ptrnt1dot(nt1dot);
00090   v.clear();
00091   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00092   Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
00093   GrammarSymbolPtr ptrnt1dot1(nt1dot1);
00094   v.clear();
00095   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00096   Nonterminal* nt1square = new Nonterminal(1,v);
00097   GrammarSymbolPtr ptrnt1square(nt1square);
00098   
00099   //test for ((1, [dot]) -> lambda . (1, [dot], 1), b
00100   //    ((1, [dot]) -> lambda . (1, [dot], 1), b
00101   //    (1, [dot], 1) -> lambda . (1, [dot]), b
00102   //    (1, [dot], 1) -> lambda . a(1, [square]), b
00103   GrammarSymbolVector beforeDot, afterDot;
00104   beforeDot.push_back(ptrtlambda);
00105   afterDot.push_back(ptrnt1dot1);
00106   Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tb);
00107   
00108   beforeDot.clear();
00109   afterDot.clear();
00110   beforeDot.push_back(ptrtlambda);
00111   afterDot.push_back(ptrnt1dot);
00112   Lr1Configuration descConfig1(*nt1dot1, beforeDot, afterDot, *tb);
00113   
00114   beforeDot.clear();
00115   afterDot.clear();
00116   beforeDot.push_back(ptrtlambda);
00117   afterDot.push_back(ptrta);
00118   afterDot.push_back(ptrnt1square);
00119   Lr1Configuration descConfig2(*nt1dot1, beforeDot, afterDot, *tb);
00120   
00121   std::set<Lr1Configuration> descSet = Desc11(gr, 1, config);
00122   try{
00123 
00124     if(descSet.size() != 3){
00125       std::stringstream errstr;
00126       errstr << "descendant set of (" << config.Str() << ") was expected to be of size 3, but was of size " << descSet.size() << "." << std::endl;
00127       throw Exception(name, errstr.str(), 1003);
00128     }
00129     
00130     if(descSet.find(config) == descSet.end()){
00131       std::stringstream errstr;
00132       errstr << "descendant set of  (" << config.Str() << ") was expected to contain the configuration (" << config.Str() << "), but the configuration was not found." << std::endl;
00133       throw Exception(name, errstr.str(), 1003);
00134     }
00135     
00136     if(descSet.find(descConfig1) == descSet.end()){
00137       std::stringstream errstr;
00138       errstr << "descendant set of  (" << config.Str() << ") was expected to contain the configuration (" << descConfig1.Str() << "), but the configuration was not found." << std::endl;
00139       throw Exception(name, errstr.str(), 1003);
00140     }
00141     
00142     if(descSet.find(descConfig2) == descSet.end()){
00143       std::stringstream errstr;
00144       errstr << "descendant set of  (" << config.Str() << ") was expected to contain the configuration (" << descConfig2.Str() << "), but the configuration was not found." << std::endl;
00145       throw Exception(name, errstr.str(), 1003);
00146     }
00147   }
00148   catch (Exception e){ 
00149   }
00150   
00151   TestEnd(name);
00152 }
00153 
00154 /* *****************
00155  * TestDescSelectedConfigs
00156  * *****************/
00157 void TestDescSelectedConfigs(){
00158     std::string name = "Desc SelectedConfigs";
00159   TestStart(name);
00160   
00161   //just to populate events and stack symbols
00162   TestGenerator1();
00163   
00164   Grammar gr = TestGrammar5();
00165   
00166   //create terminals
00167   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00168   GrammarSymbolPtr ptrta(ta);
00169   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00170   GrammarSymbolPtr ptrtb(tb);
00171   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00172   GrammarSymbolPtr ptrtlambda(tlambda);
00173   
00174   //create nonterminals
00175   std::vector<Idx> v;
00176   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00177   Nonterminal* nt1dot = new Nonterminal(1,v);
00178   GrammarSymbolPtr ptrnt1dot(nt1dot);
00179   v.clear();
00180   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00181   Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
00182   GrammarSymbolPtr ptrnt1dot1(nt1dot1);
00183   v.clear();
00184   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00185   Nonterminal* nt1square = new Nonterminal(1,v);
00186   GrammarSymbolPtr ptrnt1square(nt1square);
00187   
00188   //test for (1, [dot]) -> lambda . (1, [dot], 1)a(1, [square]), b
00189   //and (1, [dot]) -> a . a, a
00190   //and (1, [dot]) -> lambda . lambda  , b
00191   //    (1, [dot], 1) -> lambda . (1, [dot]), a
00192   //    (1, [dot], 1) -> lambda . a(1, [square]), a
00193   //    ((1, [dot]) -> lambda . (1, [dot], 1)a(1, [dot]), a
00194   GrammarSymbolVector beforeDot, afterDot;
00195   beforeDot.push_back(ptrtlambda);
00196   afterDot.push_back(ptrnt1dot1);
00197   afterDot.push_back(ptrta);
00198   afterDot.push_back(ptrnt1square);
00199   Lr1Configuration config1(*nt1dot, beforeDot, afterDot, *tb);
00200   
00201   beforeDot.clear();
00202   afterDot.clear();
00203   beforeDot.push_back(ptrta);
00204   afterDot.push_back(ptrta);
00205   Lr1Configuration config2(*nt1dot, beforeDot, afterDot, *ta);
00206   
00207   beforeDot.clear();
00208   afterDot.clear();
00209   beforeDot.push_back(ptrtlambda);
00210   afterDot.push_back(ptrtlambda);
00211   Lr1Configuration config3(*nt1dot, beforeDot, afterDot, *tb);
00212   
00213   beforeDot.clear();
00214   afterDot.clear();
00215   beforeDot.push_back(ptrtlambda);
00216   afterDot.push_back(ptrnt1dot);
00217   Lr1Configuration descConfig1(*nt1dot1, beforeDot, afterDot, *ta);
00218   
00219   beforeDot.clear();
00220   afterDot.clear();
00221   beforeDot.push_back(ptrtlambda);
00222   afterDot.push_back(ptrta);
00223   afterDot.push_back(ptrnt1square);
00224   Lr1Configuration descConfig2(*nt1dot1, beforeDot, afterDot, *ta);
00225   
00226   beforeDot.clear();
00227   afterDot.clear();
00228   beforeDot.push_back(ptrtlambda);
00229   afterDot.push_back(ptrnt1dot1);
00230   afterDot.push_back(ptrta);
00231   afterDot.push_back(ptrnt1square);
00232   Lr1Configuration descConfig3(*nt1dot, beforeDot, afterDot, *ta);
00233   
00234   
00235   std::set<Lr1Configuration> configs;
00236   configs.insert(config1);
00237   configs.insert(config2);
00238   configs.insert(config3);
00239   ;
00240   std::set<Lr1Configuration> descSet = Desc(gr, 1, configs);
00241 
00242   try{
00243 
00244     if(descSet.size() != 6){
00245       std::stringstream errstr;
00246       errstr << "descendant set was expected to be of size 5, but was of size " << descSet.size() << "." << std::endl;
00247       throw Exception(name, errstr.str(), 1003);
00248     }
00249     
00250     if(descSet.find(config1) == descSet.end()){
00251       std::stringstream errstr;
00252       errstr << "descendant set was expected to contain the configuration (" << config1.Str() << "), but the configuration was not found." << std::endl;
00253       throw Exception(name, errstr.str(), 1003);
00254     }
00255     
00256     if(descSet.find(config2) == descSet.end()){
00257       std::stringstream errstr;
00258       errstr << "descendant set was expected to contain the configuration (" << config2.Str() << "), but the configuration was not found." << std::endl;
00259       throw Exception(name, errstr.str(), 1003);
00260     }
00261     
00262     if(descSet.find(config3) == descSet.end()){
00263       std::stringstream errstr;
00264       errstr << "descendant set was expected to contain the configuration (" << config3.Str() << "), but the configuration was not found." << std::endl;
00265       throw Exception(name, errstr.str(), 1003);
00266     }
00267     
00268     if(descSet.find(descConfig1) == descSet.end()){
00269       std::stringstream errstr;
00270       errstr << "descendant set was expected to contain the configuration (" << descConfig1.Str() << "), but the configuration was not found." << std::endl;
00271       throw Exception(name, errstr.str(), 1003);
00272     }
00273     
00274     if(descSet.find(descConfig2) == descSet.end()){
00275       std::stringstream errstr;
00276       errstr << "descendant set was expected to contain the configuration (" << descConfig2.Str() << "), but the configuration was not found." << std::endl;
00277       throw Exception(name, errstr.str(), 1003);
00278     }
00279     
00280     if(descSet.find(descConfig3) == descSet.end()){
00281       std::stringstream errstr;
00282       errstr << "descendant set was expected to contain the configuration (" << descConfig3.Str() << "), but the configuration was not found." << std::endl;
00283       throw Exception(name, errstr.str(), 1003);
00284     }
00285   }
00286   catch (Exception e){ 
00287   }
00288   
00289   TestEnd(name);
00290 }
00291 
00292 /* *****************
00293  * TestPassesXNonterminal
00294  * *****************/
00295 void TestPassesXNonterminal(){
00296   std::string name = "PassesX Nonterminal";
00297   TestStart(name);
00298   
00299   //just to populate events and stack symbols
00300   TestGenerator1();
00301   
00302   Grammar gr = TestGrammar5();
00303   
00304   //create terminals
00305   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00306   GrammarSymbolPtr ptrta(ta);
00307   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00308   GrammarSymbolPtr ptrtb(tb);
00309   
00310   //create nonterminals
00311   std::vector<Idx> v;
00312   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00313   Nonterminal* nt1dot = new Nonterminal(1,v);
00314   GrammarSymbolPtr ptrnt1dot(nt1dot);
00315   
00316   v.clear();
00317   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00318   Nonterminal* nt1square = new Nonterminal(1,v);
00319   GrammarSymbolPtr ptrnt1square(nt1square);
00320   
00321   //test for ((1, [dot]) -> a . (1, [square])b, b)
00322   //    ((1, [dot]) -> a(1, [square]) . b, b)
00323   GrammarSymbolVector beforeDot, afterDot;
00324   beforeDot.push_back(ptrta);
00325   afterDot.push_back(ptrnt1square);
00326   afterDot.push_back(ptrtb);
00327   Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tb);
00328   
00329   beforeDot.clear();
00330   afterDot.clear();
00331   beforeDot.push_back(ptrta);
00332   beforeDot.push_back(ptrnt1square);
00333   afterDot.push_back(ptrtb);
00334   Lr1Configuration expectedConfig(*nt1dot, beforeDot, afterDot, *tb);
00335   
00336   std::set<Lr1Configuration> shiftSet = PassesX(config, ptrnt1square);
00337   try{
00338 
00339     if(shiftSet.size() != 1){
00340       std::stringstream errstr;
00341       errstr << "shift set of (" << config.Str() << ") to be shifted over symbol " << ptrnt1square->Str() << " was expected to be of size 1, but was of size " << shiftSet.size() << "." << std::endl;
00342       throw Exception(name, errstr.str(), 1003);
00343     }
00344     
00345     if(shiftSet.find(expectedConfig) == shiftSet.end()){
00346       std::stringstream errstr;
00347       errstr << "Shift of the configuration (" << config.Str() << ") over the symbol " << ptrnt1square->Str() << " was expected to result in the configuration (" << expectedConfig.Str() << "), but resulted in " << shiftSet.begin()->Str() << "." << std::endl;
00348       throw Exception(name, errstr.str(), 1003);
00349     }
00350   }
00351   catch (Exception e){ 
00352   }
00353   
00354   TestEnd(name);
00355 }
00356 
00357 /* *****************
00358  * TestPassesXTerminal
00359  * *****************/
00360 void TestPassesXTerminal(){
00361   std::string name = "PassesX Terminal";
00362   TestStart(name);
00363   
00364   //just to populate events and stack symbols
00365   TestGenerator1();
00366   
00367   Grammar gr = TestGrammar5();
00368   
00369   //create terminals
00370   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00371   GrammarSymbolPtr ptrta(ta);
00372   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00373   GrammarSymbolPtr ptrtb(tb);
00374   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00375   GrammarSymbolPtr ptrtlambda(tlambda);
00376   
00377   //create nonterminals
00378   std::vector<Idx> v;
00379   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00380   Nonterminal* nt1dot = new Nonterminal(1,v);
00381   GrammarSymbolPtr ptrnt1dot(nt1dot);
00382   
00383   //test for ((1, [dot]) -> lambda . a, b)
00384   //    ((1, [dot]) -> a . lambda , b)
00385   GrammarSymbolVector beforeDot, afterDot;
00386   beforeDot.push_back(ptrtlambda);
00387   afterDot.push_back(ptrta);
00388   Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tb);
00389   
00390   beforeDot.clear();
00391   afterDot.clear();
00392   beforeDot.push_back(ptrta);
00393   afterDot.push_back(ptrtlambda);
00394   Lr1Configuration expectedConfig(*nt1dot, beforeDot, afterDot, *tb);
00395   
00396   std::set<Lr1Configuration> shiftSet = PassesX(config, ptrta);
00397   try{
00398 
00399     if(shiftSet.size() != 1){
00400       std::stringstream errstr;
00401       errstr << "shift set of (" << config.Str() << ") to be shifted over symbol " << ptrta->Str() << " was expected to be of size 1, but was of size " << shiftSet.size() << "." << std::endl;
00402       throw Exception(name, errstr.str(), 1003);
00403     }
00404     
00405     if(shiftSet.find(expectedConfig) == shiftSet.end()){
00406       std::stringstream errstr;
00407       errstr << "Shift of the configuration (" << config.Str() << ") over the symbol " << ptrta->Str() << " was expected to result in the configuration (" << expectedConfig.Str() << "), but resulted in " << shiftSet.begin()->Str() << "." << std::endl;
00408       throw Exception(name, errstr.str(), 1003);
00409     }
00410   }
00411   catch (Exception e){ 
00412   }
00413   
00414   TestEnd(name);
00415 }
00416 
00417 /* *****************
00418  * TestPassesXNoShift
00419  * *****************/
00420 void TestPassesXNoShift(){
00421   std::string name = "PassesX No Shift";
00422   TestStart(name);
00423   
00424   //just to populate events and stack symbols
00425   TestGenerator1();
00426   
00427   Grammar gr = TestGrammar5();
00428   
00429   //create terminals
00430   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00431   GrammarSymbolPtr ptrta(ta);
00432   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00433   GrammarSymbolPtr ptrtb(tb);
00434   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00435   GrammarSymbolPtr ptrtlambda(tlambda);
00436   
00437   //create nonterminals
00438   std::vector<Idx> v;
00439   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00440   Nonterminal* nt1dot = new Nonterminal(1,v);
00441   GrammarSymbolPtr ptrnt1dot(nt1dot);
00442   
00443   v.clear();
00444   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00445   Nonterminal* nt1square = new Nonterminal(1,v);
00446   GrammarSymbolPtr ptrnt1square(nt1square);
00447   
00448   //test for ((1, [dot]) -> a . lambda , b)
00449   //    empty
00450   GrammarSymbolVector beforeDot, afterDot;
00451   beforeDot.push_back(ptrta);
00452   afterDot.push_back(ptrtlambda);
00453   Lr1Configuration config1(*nt1dot, beforeDot, afterDot, *tb);
00454   
00455   std::set<Lr1Configuration> shiftSet = PassesX(config1, ptrnt1square);
00456   try{
00457 
00458     if(!shiftSet.empty()){
00459       std::stringstream errstr;
00460       errstr << "shift set of (" << config1.Str() << ") to be shifted over symbol " << ptrnt1square->Str() << " was expected to be empty, but was of size " << shiftSet.size() << "." << std::endl;
00461       throw Exception(name, errstr.str(), 1003);
00462     }
00463   }
00464   catch (Exception e){ 
00465   }
00466   
00467   //test for ((1, [dot]) -> a . (1, [dot]), b)
00468   //    empty
00469   beforeDot.clear();
00470   afterDot.clear();
00471   beforeDot.push_back(ptrta);
00472   afterDot.push_back(ptrnt1dot);
00473   Lr1Configuration config2(*nt1dot, beforeDot, afterDot, *tb);
00474   
00475   shiftSet = PassesX(config2, ptrnt1square);
00476   try{
00477 
00478     if(!shiftSet.empty()){
00479       std::stringstream errstr;
00480       errstr << "shift set of (" << config2.Str() << ") to be shifted over symbol " << ptrnt1square->Str() << " was expected to be empty, but was of size " << shiftSet.size() << "." << std::endl;
00481       throw Exception(name, errstr.str(), 1003);
00482     }
00483   }
00484   catch (Exception e){ 
00485   }
00486   
00487   TestEnd(name);
00488 }
00489 
00490 /* *****************
00491  * TestLrm1FindOne
00492  * *****************/
00493 void TestLrm1FindOne(){
00494   std::string name = "Lrm1 Find One";
00495   TestStart(name);
00496   
00497   Grammar gr = TestGrammar7();
00498   
00499   //create terminals
00500   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00501   GrammarSymbolPtr ptrta(ta);
00502   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00503   GrammarSymbolPtr ptrtb(tb);
00504   Terminal* tdollar = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("$"));
00505   GrammarSymbolPtr ptrtdollar(tdollar);
00506   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00507   GrammarSymbolPtr ptrtlambda(tlambda);
00508   
00509   //create nonterminals
00510   std::vector<Idx> v;
00511   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00512   Nonterminal* nt1dot = new Nonterminal(1,v);
00513   GrammarSymbolPtr ptrnt1dot(nt1dot);
00514   
00515   v.clear();
00516   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00517   Nonterminal* nt1square = new Nonterminal(1,v);
00518   GrammarSymbolPtr ptrnt1square(nt1square);
00519   
00520   //test for ((1, [dot]) -> lambda . $(1, [square])$, lambda)
00521   // $: ((1, [dot]) -> $ . (1, [square])$, lambda)
00522   //    ((1, [square]) -> lambda . a(1, [square]), $)
00523   //    ((1, [square]) -> lambda . b, $)
00524   GrammarSymbolVector beforeDot, afterDot;
00525   beforeDot.push_back(ptrtlambda);
00526   afterDot.push_back(ptrtdollar);
00527   afterDot.push_back(ptrnt1square);
00528   afterDot.push_back(ptrtdollar);
00529   Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tlambda);
00530   
00531   std::set<Lr1Configuration> expectedConfigs;
00532   
00533   beforeDot.clear();
00534   afterDot.clear();
00535   beforeDot.push_back(ptrtdollar);
00536   afterDot.push_back(ptrnt1square);
00537   afterDot.push_back(ptrtdollar);
00538   expectedConfigs.insert(Lr1Configuration(*nt1dot, beforeDot, afterDot, *tlambda));
00539   
00540   beforeDot.clear();
00541   afterDot.clear();
00542   beforeDot.push_back(ptrtlambda);
00543   afterDot.push_back(ptrta);
00544   afterDot.push_back(ptrnt1square);
00545   expectedConfigs.insert(Lr1Configuration(*nt1square, beforeDot, afterDot, *tdollar));
00546   
00547   beforeDot.clear();
00548   afterDot.clear();
00549   beforeDot.push_back(ptrtlambda);
00550   afterDot.push_back(ptrtb);
00551   expectedConfigs.insert(Lr1Configuration(*nt1square, beforeDot, afterDot, *tdollar));
00552   
00553   std::set<Lr1Configuration> configSet;
00554   configSet.insert(config);
00555   
00556   Lr1ConfigurationSetSet configSetSet;
00557   configSetSet.insert(configSet);
00558   
00559   LrmTransitionMap transitionMap = Lrm1(gr, 1, configSetSet);
00560   try{
00561     
00562     //size of the transition map must be one
00563     if(transitionMap.size() != 1){
00564       std::stringstream errstr;
00565       errstr << "number of successor config sets of config set (" << ConfigSetToStr(configSet) << ") was expected to be 1, but there were " << transitionMap.size() << "." << std::endl;
00566       throw Exception(name, errstr.str(), 1003);
00567     }
00568     
00569     //the symbol must be dollar
00570     if(*transitionMap.begin()->first.second != *ptrtdollar){
00571       std::stringstream errstr;
00572       errstr << "event to transition out of config set (" << ConfigSetToStr(configSet) << ") was expected $, but was not found." << std::endl;
00573       throw Exception(name, errstr.str(), 1003);
00574     }
00575     
00576     //the size of the successor set must be three
00577     if(transitionMap.begin()->second.size() != 3){
00578       std::stringstream errstr;
00579       errstr << "config set \n" << ConfigSetToStr(configSet) << "\n has the successor config set \n" << ConfigSetToStr(transitionMap.begin()->second) << ",\n which was expected to be of size 3, but was of size " << transitionMap.begin()->second.size() << "." << std::endl;
00580       throw Exception(name, errstr.str(), 1003);
00581     }
00582     
00583     std::set<Lr1Configuration>::const_iterator configit;
00584     //the three expected configs must be in the successor set
00585     for(configit = expectedConfigs.begin(); configit != expectedConfigs.end(); configit++){
00586       if(transitionMap.begin()->second.find(*configit) == transitionMap.begin()->second.end()){
00587         std::stringstream errstr;
00588         errstr << "config set \n" << ConfigSetToStr(configSet) << "\n has the successor config set \n" << ConfigSetToStr(transitionMap.begin()->second) << ",\n which was expected to contain configuration " << configit->Str() << ", but was not found." << std::endl;
00589         throw Exception(name, errstr.str(), 1003);
00590       }
00591     }
00592   }
00593   catch (Exception e){ 
00594   }
00595 
00596   TestEnd(name);
00597 }
00598 
00599 /* *****************
00600  * TestLrm1FindThree
00601  * *****************/
00602 void TestLrm1FindThree(){
00603   std::string name = "Lrm1 Find Three";
00604   TestStart(name);
00605   
00606   Grammar gr = TestGrammar7();
00607   
00608   //create terminals
00609   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00610   GrammarSymbolPtr ptrta(ta);
00611   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00612   GrammarSymbolPtr ptrtb(tb);
00613   Terminal* tdollar = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("$"));
00614   GrammarSymbolPtr ptrtdollar(tdollar);
00615   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00616   GrammarSymbolPtr ptrtlambda(tlambda);
00617   
00618   //create nonterminals
00619   std::vector<Idx> v;
00620   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00621   Nonterminal* nt1dot = new Nonterminal(1,v);
00622   GrammarSymbolPtr ptrnt1dot(nt1dot);
00623   
00624   v.clear();
00625   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00626   Nonterminal* nt1square = new Nonterminal(1,v);
00627   GrammarSymbolPtr ptrnt1square(nt1square);
00628   
00629   //test for ((1, [dot]) -> $ lambda . (1, [square])$, lambda)
00630   //         ((1, [square]) -> lambda . a(1, [square]), $)
00631   //         ((1, [square]) -> lambda . b, $)
00632   // (1, [square]):
00633   //    (1, [dot]) -> $(1, [square]) . $, lambda)
00634   // a:
00635   //    ((1, [square]) -> a . (1, [square]), $)
00636   //    ((1, [square]) -> lambda . a(1, [square]), $)
00637   //    ((1, [square]) -> lambda . b, $)
00638   // b:
00639   //    (1, [square]) -> b . lambda, $
00640   GrammarSymbolVector beforeDot, afterDot;
00641   beforeDot.push_back(ptrtlambda);
00642   afterDot.push_back(ptrtdollar);
00643   afterDot.push_back(ptrnt1square);
00644   afterDot.push_back(ptrtdollar);
00645   Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tlambda);
00646   
00647   std::set<Lr1Configuration> configSet;
00648   
00649   beforeDot.clear();
00650   afterDot.clear();
00651   beforeDot.push_back(ptrtdollar);
00652   afterDot.push_back(ptrnt1square);
00653   afterDot.push_back(ptrtdollar);
00654   configSet.insert(Lr1Configuration(*nt1dot, beforeDot, afterDot, *tlambda));
00655   
00656   beforeDot.clear();
00657   afterDot.clear();
00658   beforeDot.push_back(ptrtlambda);
00659   afterDot.push_back(ptrta);
00660   afterDot.push_back(ptrnt1square);
00661   configSet.insert(Lr1Configuration(*nt1square, beforeDot, afterDot, *tdollar));
00662   
00663   beforeDot.clear();
00664   afterDot.clear();
00665   beforeDot.push_back(ptrtlambda);
00666   afterDot.push_back(ptrtb);
00667   configSet.insert(Lr1Configuration(*nt1square, beforeDot, afterDot, *tdollar));
00668   
00669   Lr1ConfigurationSetSet configSetSet;
00670   configSetSet.insert(configSet);
00671   
00672   LrmTransitionMap transitionMap = Lrm1(gr, 1, configSetSet);
00673   
00674   //std::cout << TransitionMapToStr(transitionMap) << std::endl;
00675   try{
00676     
00677     //size of the transition map must be three
00678     if(transitionMap.size() != 3){
00679       std::stringstream errstr;
00680       errstr << "number of successor config sets of config set (" << ConfigSetToStr(configSet) << ") was expected to be 3, but there were " << transitionMap.size() << "." << std::endl;
00681       throw Exception(name, errstr.str(), 1003);
00682     }
00683   }
00684   catch (Exception e){ 
00685   }
00686 
00687   TestEnd(name);
00688 }
00689 
00690 /* *****************
00691  * TestLrmLoopAnB
00692  * *****************/
00693 void TestLrmLoopAnB(){
00694   std::string name = "LrmLoop a^n b";
00695   TestStart(name);
00696   
00697   Grammar gr = TestGrammar7();
00698   
00699   //create terminals
00700   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00701   GrammarSymbolPtr ptrta(ta);
00702   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00703   GrammarSymbolPtr ptrtb(tb);
00704   Terminal* tdollar = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("$"));
00705   GrammarSymbolPtr ptrtdollar(tdollar);
00706   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00707   GrammarSymbolPtr ptrtlambda(tlambda);
00708   
00709   //create nonterminals
00710   std::vector<Idx> v;
00711   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00712   Nonterminal* nt1dot = new Nonterminal(1,v);
00713   GrammarSymbolPtr ptrnt1dot(nt1dot);
00714   
00715   v.clear();
00716   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00717   Nonterminal* nt1square = new Nonterminal(1,v);
00718   GrammarSymbolPtr ptrnt1square(nt1square);
00719   
00720   //test with start state ((1, [dot]) -> lambda . $(1, [square])$, lambda)
00721   GrammarSymbolVector beforeDot, afterDot;
00722   beforeDot.push_back(ptrtlambda);
00723   afterDot.push_back(ptrtdollar);
00724   afterDot.push_back(ptrnt1square);
00725   afterDot.push_back(ptrtdollar);
00726   Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tlambda);
00727   
00728   std::set<Lr1Configuration> configSet;
00729   configSet.insert(config);
00730   
00731   Lr1ConfigurationSetSet configSetSet;
00732   configSetSet.insert(configSet);
00733   
00734   std::pair<LrmTransitionMap,Lr1ConfigurationSetSet> transitionStates = LrmLoop(gr, 1, LrmTransitionMap(), Lr1ConfigurationSetSet(), configSetSet);
00735   
00736   LrmTransitionMap transitionMap = transitionStates.first;
00737   Lr1ConfigurationSetSet states = transitionStates.second;
00738   try{
00739     
00740     //size of the transition map must be eight
00741     if(transitionMap.size() != 8){
00742       std::stringstream errstr;
00743       errstr << "number of transitions was expected to be 8, but was " << transitionMap.size() << "." << std::endl;
00744       throw Exception(name, errstr.str(), 1003);
00745     }
00746     
00747     //the number of states must be seven
00748     if(states.size() != 7){
00749       std::stringstream errstr;
00750       errstr << "number of states was expected to be 7, but there were " << states.size() << "." << std::endl;
00751       throw Exception(name, errstr.str(), 1003);
00752     }  
00753   }
00754   catch (Exception e){ 
00755   }
00756   TestEnd(name);
00757 }
00758 
00759 /* *****************
00760  * TestDescInitialFindTwo
00761  * *****************/
00762 void TestDescInitialFindTwo(){
00763   std::string name = "DescInitial Find Two";
00764   TestStart(name);
00765   
00766   Grammar gr = TestGrammar3();
00767   
00768   //create terminals
00769   Terminal* ta = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00770   GrammarSymbolPtr ptrta(ta);
00771   Terminal* tb = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("b"));
00772   GrammarSymbolPtr ptrtb(tb);
00773   Terminal* tlambda = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index(FAUDES_PD_LAMBDA));
00774   GrammarSymbolPtr ptrtlambda(tlambda);
00775 
00776   //create nonterminals
00777   std::vector<Idx> v;
00778   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00779   Nonterminal* nt1dot = new Nonterminal(1,v);
00780   GrammarSymbolPtr ptrnt1dot(nt1dot);
00781   
00782   v.clear();
00783   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
00784   Nonterminal* nt2square = new Nonterminal(2,v);
00785   GrammarSymbolPtr ptrnt2square(nt2square);
00786   
00787   //create expected configurations
00788   GrammarSymbolVector beforeDot, afterDot;
00789   beforeDot.push_back(ptrtlambda);
00790   afterDot.push_back(ptrta);
00791   afterDot.push_back(ptrnt1dot);
00792   Lr1Configuration config1(*nt1dot, beforeDot, afterDot, *tlambda);
00793   
00794   beforeDot.clear();
00795   afterDot.clear();
00796   beforeDot.push_back(ptrtlambda);
00797   afterDot.push_back(ptrtb);
00798   afterDot.push_back(ptrnt2square);
00799   Lr1Configuration config2(*nt1dot, beforeDot, afterDot, *tlambda);
00800   
00801   std::set<Lr1Configuration> expectedConfigSet;
00802   expectedConfigSet.insert(config1);
00803   expectedConfigSet.insert(config2);
00804   
00805   std::set<Lr1Configuration> configSet = DescInitial(gr);
00806   
00807   try{
00808     
00809     //size of the config set must be two
00810     if(configSet.size() != expectedConfigSet.size()){
00811       std::stringstream errstr;
00812       errstr << "number of configurations was expected to be " << expectedConfigSet.size() << ", but was " << configSet.size() << "." << std::endl;
00813       throw Exception(name, errstr.str(), 1003);
00814     }
00815     
00816     std::set<Lr1Configuration>::const_iterator configit;
00817     //the expected configurations must be in the config set
00818     for(configit = expectedConfigSet.begin(); configit != expectedConfigSet.end(); configit++){
00819       if(configSet.find(*configit) == configSet.end()){
00820         std::stringstream errstr;
00821         errstr << "configurations " << configit->Str() << " was expected to be in the initial configuration set, but was not found" << std::endl;
00822         throw Exception(name, errstr.str(), 1003);
00823       }  
00824     }
00825   }
00826   catch (Exception e){ 
00827   }
00828   TestEnd(name);
00829 }
00830 
00831 /* *****************
00832  * TestLrmGr1
00833  * *****************/
00834 void TestLrmGr1(){
00835   std::string name = "Lrm Gr1";
00836   TestStart(name);
00837   
00838   Grammar gr = TestGrammar7();
00839   
00840   GotoGenerator gotoGen = Lrm(gr, 1);
00841   
00842   try{
00843     
00844     //number of transitions must be eight
00845     if(gotoGen.TransRelSize() != 8){
00846       std::stringstream errstr;
00847       errstr << "number of transitions was expected to be 8, but was " << gotoGen.TransRelSize() << "." << std::endl;
00848       throw Exception(name, errstr.str(), 1003);
00849     }
00850     
00851     //the number of states must be seven
00852     if(gotoGen.Size()!= 7){
00853       std::stringstream errstr;
00854       errstr << "number of states was expected to be 7, but there were " << gotoGen.Size() << "." << std::endl;
00855       throw Exception(name, errstr.str(), 1003);
00856     }  
00857   }
00858   catch (Exception e){ 
00859   }
00860   TestEnd(name);
00861 }
00862 
00863 /* *****************
00864  * TestLrmGr2
00865  * *****************/
00866 void TestLrmGr2(){
00867   //TODO dont know what to expect as a result
00868 //   std::string name = "Lrm Gr2";
00869 //   TestStart(name);
00870 //   
00871 //   Grammar gr = TestGrammar8();
00872 //   
00873 //   GotoMachine gotoMachine = Lrm(gr, 1);
00874 //   
00875 //   LrmTransitionMap transitions = gotoMachine.Transitions();
00876 //   Lr1ConfigurationSetSet states = gotoMachine.States();
00877 //   
00878 //   std::cout << "transitions: " << transitions.size() << ", states: " << states.size() << std::endl;
00879 //   
00880 //   std::cout << ConfigSetSetToStr(states) << std::endl;
00881 //   std::cout << TransitionMapToStr(transitions) << std::endl;
00882 //   
00883 //   try{
00884 //     
00885 //     //size of the transition map must be 31
00886 //     if(transitions.size() != 31){
00887 //       std::stringstream errstr;
00888 //       errstr << "number of transitions was expected to be 31, but was " << transitions.size() << "." << std::endl;
00889 //       throw Exception(name, errstr.str(), 1003);
00890 //     }
00891 //     
00892 //     //the number of states must be 25
00893 //     if(states.size() != 25){
00894 //       std::stringstream errstr;
00895 //       errstr << "number of states was expected to be 25, but there were " << states.size() << "." << std::endl;
00896 //       throw Exception(name, errstr.str(), 1003);
00897 //     }  
00898 //   }
00899 //   catch (Exception e){ 
00900 //   }
00901 //   TestEnd(name);
00902 }
00903 
00904 /* *****************
00905  * TestAugSuccess
00906  * *****************/
00907 void TestAugSuccess(){
00908   std::string name = "Aug Success";
00909   TestStart(name);
00910   
00911   Grammar gr = TestGrammar6();
00912   
00913   //create terminals
00914   PushdownGenerator gen;
00915   gen.InsEvent("$");
00916   Terminal* tdollar = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("$"));
00917   GrammarSymbolPtr ptrtdollar(tdollar);
00918 
00919   //create nonterminals
00920   std::vector<Idx> v;
00921   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00922   Nonterminal* nt1dot = new Nonterminal(1,v);
00923   GrammarSymbolPtr ptrnt1dot(nt1dot);
00924   v.clear();
00925   v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00926   Nonterminal* nt0dot = new Nonterminal(0,v);
00927   GrammarSymbolPtr ptrnt0dot(nt0dot);
00928   
00929   //create grammar production
00930   GrammarSymbolVector rhs;
00931   rhs.push_back(ptrtdollar);
00932   rhs.push_back(ptrnt1dot);
00933   rhs.push_back(ptrtdollar);
00934   GrammarProduction gp(*nt0dot,rhs);
00935   
00936   Grammar rGr = Aug(gr, *nt0dot, *tdollar);
00937   
00938   try{
00939     
00940     //the must be one grammar production more in the new grammar
00941     if(gr.GrammarProductions().size() + 1 != rGr.GrammarProductions().size()){
00942       std::stringstream errstr;
00943       errstr << "number of grammar productions was expected to be " << gr.GrammarProductions().size() + 1 << ", but was " << rGr.GrammarProductions().size() << "." << std::endl;
00944       throw Exception(name, errstr.str(), 1003);
00945     }
00946     
00947     std::set<GrammarProduction>::const_iterator gpit;
00948     //there must be one production with the start symbol as left-hand side
00949     int i = 0;
00950     for(gpit = rGr.GrammarProductionsBegin(); gpit != rGr.GrammarProductionsEnd(); gpit++){
00951       if(gpit->Lhs() == *nt0dot){
00952         i++;
00953       }
00954     }
00955     if(i != 1){
00956       std::stringstream errstr;
00957       errstr << "number of grammar productions with " << nt0dot->Str() << " as right-hand side was expected to be 1, but was " << i << "." << std::endl;
00958       throw Exception(name, errstr.str(), 1003);
00959     }
00960     
00961     //the new start production must be in the grammar's productions
00962     if(gr.GrammarProductions().find(gp) == rGr.GrammarProductionsEnd()){
00963       std::stringstream errstr;
00964       errstr << "grammar production " << gp.Str() << " was expected to be in the grammar, but was not found." << std::endl;
00965       throw Exception(name, errstr.str(), 1003);
00966     }
00967   }
00968   catch (Exception e){ 
00969   }
00970   TestEnd(name);
00971 }
00972 
00973 /* *****************
00974  * TestDesc11
00975  * *****************/
00976 void TestDesc11(){
00977   TestDesc11Terminal();
00978   TestDesc11Nonterminal();
00979 }
00980 
00981 /* *****************
00982  * TestDesc
00983  * *****************/
00984 void TestDesc(){
00985   TestDescSelectedConfigs();
00986 }
00987 
00988 /* *****************
00989  * TestPassesX
00990  * *****************/
00991 void TestPassesX(){
00992   TestPassesXNonterminal();
00993   TestPassesXTerminal();
00994   TestPassesXNoShift(); 
00995 }
00996 
00997 /* *****************
00998  * TestLrm1
00999  * *****************/
01000 void TestLrm1(){
01001   TestLrm1FindOne();
01002   TestLrm1FindThree();
01003 }
01004 
01005 /* *****************
01006  * TestLrmLoop
01007  * *****************/
01008 void TestLrmLoop(){
01009  TestLrmLoopAnB(); 
01010 }
01011 
01012 /* *****************
01013  * TestDescInitial
01014  * *****************/
01015 void TestDescInitial(){
01016   TestDescInitialFindTwo();
01017 }
01018 
01019 /* *****************
01020  * TestDescInitial
01021  * *****************/
01022 void TestLrm(){
01023   TestLrmGr1(); 
01024   //TestLrmGr2();
01025 }
01026 
01027 /* *****************
01028  * TestAug
01029  * *****************/
01030 void TestAug(){
01031   TestAugSuccess();
01032 }
01033   
01034 } // namespace faudes
01035 

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