pd_alg_nb_sub_a_test.cpp

Go to the documentation of this file.
00001 /** @file pd_alg_nb_sub_a_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_nb_sub_a_test.h"
00010 
00011 namespace faudes {
00012 
00013 /* *****************
00014  * TestFilterMixedGrammarSymbols
00015  * *****************/
00016 void TestFilterMixedGrammarSymbols(){
00017   
00018   std::string name = "Filter Mixed Grammar Symbols";
00019   TestStart(name);
00020   
00021   Grammar gr1 = TestGrammar1();
00022   
00023   GrammarSymbolVector word;
00024   std::set<Terminal>::const_iterator tit;
00025   std::vector<TerminalPtr> tpv;
00026   std::vector<TerminalPtr>::iterator tpvit;
00027   std::set<Nonterminal> rSet;
00028   
00029   //build word to filter
00030   //contains one nonterminal and all terminals
00031   Terminal* t;
00032   Nonterminal* nt = new Nonterminal(*gr1.Nonterminals().begin());
00033   GrammarSymbolPtr ntPtr(nt);
00034   word.push_back(ntPtr);
00035 
00036   for(tit = gr1.Terminals().begin(); tit != gr1.Terminals().end(); tit++){
00037     //create new terminal
00038     t = new Terminal(*tit);
00039     TerminalPtr tPtr(t);
00040     //push terminal into word
00041     word.push_back(tPtr);
00042   }
00043   
00044   rSet = Filter(gr1.Nonterminals(),word);
00045   
00046   try{
00047     //resulting set size has to be one 
00048     if(rSet.size() != 1){
00049       std::stringstream errstr;
00050       errstr << "size of result set is " << rSet.size() << ", but 1 was expected" << std::endl;
00051       throw Exception(name, errstr.str(), 1003);
00052     }
00053     //resulting set has to contain *gr1.Nonterminals().begin()
00054     
00055     if(*rSet.begin() != *gr1.Nonterminals().begin()){
00056       std::stringstream errstr;
00057       errstr << "result set did not contain the expected nonterminal " << gr1.Nonterminals().begin()->Str() << std::endl;
00058       throw Exception(name, errstr.str(), 1003);
00059     }
00060   }
00061    catch(Exception e){
00062       std::cout << (*rSet.begin()).Str() << std::endl;
00063       std::cout << (*gr1.Nonterminals().begin()).Str() << std::endl;
00064   }
00065   
00066   TestEnd(name);
00067 }
00068 
00069 /* *****************
00070  * TestFilterNothing
00071  * *****************/
00072 void TestFilterNothing(){
00073   std::string name = "Filter Nothing";
00074   TestStart(name);
00075   
00076   std::set<Nonterminal> nt;
00077   GrammarSymbolVector gs;
00078   
00079   std::set<Nonterminal> rSet = Filter(nt,gs);
00080   
00081   try{
00082     //resulting set size has to be zero 
00083     if(rSet.size() != 0){
00084       std::stringstream errstr;
00085       errstr << "size of result set is " << rSet.size() << ", but 0 was expected" << std::endl;
00086       throw Exception(name, errstr.str(), 1003);
00087     }
00088   }
00089    catch(Exception e){
00090   }
00091   
00092   TestEnd(name);
00093 }
00094 
00095 /* *****************
00096  * TestRnpp1FindSymbolsEmptySet
00097  * *****************/
00098 void TestRnpp1FindSymbolsEmptySet(){
00099   std::string name = "Rnpp1 Find Symbols Empty Set";
00100   TestStart(name);
00101   
00102   Grammar gr = TestGrammar1();
00103   
00104   //eliminable nonterminal which should be found by Rnpp1
00105   std::vector<Idx> v;
00106   v.push_back(3);
00107   Nonterminal nt(5,v,1);
00108   //the grammar should contain this nonterminal!
00109   if(gr.Nonterminals().find(nt) == gr.Nonterminals().end()){
00110     std::cout << "Warning, test parameters seem to be wrong. The test grammar did not contain Nonterminal (5,[3],1)." << std::endl;
00111     return;
00112   }
00113   
00114   //empty set of already eliminated nonterminals
00115   std::set<Nonterminal> ntSet;
00116   
00117   std::set<Nonterminal> rSet = Rnpp1(gr,ntSet);
00118   
00119   try{
00120     //resulting set size has to be one 
00121     if(rSet.size() != 1){
00122       std::stringstream errstr;
00123       errstr << "size of result set is " << rSet.size() << ", but 1 was expected" << std::endl;
00124       throw Exception(name, errstr.str(), 1003);
00125     }
00126     //resulting set size has to contain nonterminal (3,2)
00127     if(*rSet.begin() != nt){
00128       std::stringstream errstr;
00129       errstr << "result set did not contain the expected nonterminal " << nt.Str() << std::endl;
00130       throw Exception(name, errstr.str(), 1003);
00131     }
00132   }
00133    catch(Exception e){
00134   }
00135   
00136   TestEnd(name);
00137 }
00138 
00139 /* *****************
00140  * TestRnpp1FindSymbolsNonemptySet
00141  * *****************/
00142 void TestRnpp1FindSymbolsNonemptySet(){
00143   std::string name = "Rnpp1 Find Symbols Empty Set";
00144   TestStart(name);
00145   
00146   Grammar gr = TestGrammar1();
00147   
00148   //eliminable nonterminal which should be found
00149   std::vector<Idx> v;
00150   v.push_back(2);
00151   Nonterminal nt(1,v,2);
00152   //the grammar should contain this nonterminal!
00153   if(gr.Nonterminals().find(nt) == gr.Nonterminals().end()){
00154     std::cout << "Warning, test parameters seem to be wrong. The test grammar did not contain Nonterminal (1,[2],2)." << std::endl;
00155     std::cout << gr.Str() << std::endl;
00156     return;
00157   }
00158   
00159   //empty set of already eliminated nonterminals
00160   std::set<Nonterminal> ntSet;
00161   
00162   std::set<Nonterminal> rSet = Rnpp1(gr,Rnpp1(gr,ntSet));
00163   
00164   try{
00165     //resulting set size has to be one 
00166     if(rSet.size() != 2){
00167       std::stringstream errstr;
00168       errstr << "size of result set is " << rSet.size() << ", but 2 was expected" << std::endl;
00169       throw Exception(name, errstr.str(), 1003);
00170     }
00171     //resulting set size has to contain nonterminal (1,2,2)
00172     if(*rSet.begin() != nt){
00173       std::stringstream errstr;
00174       errstr << "result set did not contain the expected nonterminal " << nt.Str() << std::endl;
00175       throw Exception(name, errstr.str(), 1003);
00176     }
00177   }
00178    catch(Exception e){
00179   }
00180   
00181   TestEnd(name);
00182 }
00183 
00184 /* *****************
00185  * TestRnpplFindSymbolsEmptySet
00186  * *****************/
00187 void TestRnpplFindSymbolsEmptySet(){
00188   std::string name = "Rnppl Find Symbols Empty Set";
00189   TestStart(name);
00190   
00191   Grammar gr = TestGrammar1();
00192   
00193   //all nonterminals should be eliminable except (2,3,3)
00194   std::set<Nonterminal> findThis = gr.Nonterminals();
00195   std::vector<Idx> v;
00196   v.push_back(3);
00197   findThis.erase(Nonterminal(2,v,3));
00198   
00199   //empty set of already eliminated nonterminals
00200   std::set<Nonterminal> ntSet;
00201   
00202   std::set<Nonterminal> rSet = Rnppl(gr,ntSet);
00203   
00204   try{
00205     //resulting set size has to be the size of findThis 
00206     if(rSet.size() != findThis.size()){
00207       std::stringstream errstr;
00208       errstr << "size of result set is " << rSet.size() << ", but " << findThis.size() << " was expected" << std::endl;
00209       throw Exception(name, errstr.str(), 1003);
00210     }
00211     //resulting set size has to contain all nonterminals in findThis
00212     std::set<Nonterminal>::const_iterator ntit, findit;
00213     for(ntit = findThis.begin(); ntit != findThis.end(); ntit++){
00214       //look for every nonterminal
00215       findit = rSet.find(*ntit);
00216       //throw error if it was not found
00217       if(findit == findThis.end()){
00218         std::stringstream errstr;
00219         errstr << "result set did not contain the expected nonterminal " << ntit->Str() << std::endl;
00220         throw Exception(name, errstr.str(), 1003);
00221       }
00222     }
00223   }
00224    catch(Exception e){
00225   }
00226   
00227   TestEnd(name);
00228 }
00229 
00230 /* *****************
00231  * TestRnpplFindSymbolsNonemptySet
00232  * *****************/
00233 void TestRnpplFindSymbolsNonemptySet(){
00234   std::string name = "Rnppl Find Symbols Nonempty Set";
00235   TestStart(name);
00236   
00237   Grammar gr = TestGrammar1();
00238   
00239   //all nonterminals should be eliminable except (2,3,3)
00240   std::set<Nonterminal> findThis = gr.Nonterminals();
00241   std::vector<Idx> v;
00242   v.push_back(3);
00243   findThis.erase(Nonterminal(2,v,3));
00244   
00245   //set of already eliminated nonterminals, add any eliminable nonterminals
00246   std::set<Nonterminal> ntSet;
00247   v.clear(); v.push_back(2);
00248   ntSet.insert(Nonterminal(1,v,2));
00249   v.clear(); v.push_back(3);
00250   ntSet.insert(Nonterminal(4,v));
00251   
00252   std::set<Nonterminal> rSet = Rnppl(gr,ntSet);
00253   
00254   try{
00255     //resulting set size has to be the size of findThis
00256     if(rSet.size() != findThis.size()){
00257       std::stringstream errstr;
00258       errstr << "size of result set is " << rSet.size() << ", but " << findThis.size() << " was expected" << std::endl;
00259       throw Exception(name, errstr.str(), 1003);
00260     }
00261     //resulting set size has to contain all nonterminals in findThis
00262     std::set<Nonterminal>::const_iterator ntit, findit;
00263     for(ntit = findThis.begin(); ntit != findThis.end(); ntit++){
00264       //look for every nonterminal
00265       findit = rSet.find(*ntit);
00266       //throw error if it was not found
00267       if(findit == findThis.end()){
00268         std::stringstream errstr;
00269         errstr << "result set did not contain the expected nonterminal " << ntit->Str() << std::endl;
00270         throw Exception(name, errstr.str(), 1003);
00271       }
00272     }
00273   }
00274    catch(Exception e){
00275   }
00276   
00277   TestEnd(name);
00278 }
00279 
00280 /* *****************
00281  * TestRnpplFindSymbolsCompleteSet
00282  * *****************/
00283 void TestRnpplFindSymbolsCompleteSet(){
00284   std::string name = "Rnppl Find Symbols Complete Set";
00285   TestStart(name);
00286   
00287   Grammar gr = TestGrammar1();
00288   
00289   //all nonterminals should be eliminable except (2,3,3)
00290   std::set<Nonterminal> findThis = gr.Nonterminals();
00291   std::vector<Idx> v;
00292   v.push_back(3);
00293   findThis.erase(Nonterminal(2,v,3));
00294   
00295   //set of already eliminated nonterminals, add any eliminable nonterminals
00296   std::set<Nonterminal> ntSet = findThis;
00297   
00298   std::set<Nonterminal> rSet = Rnppl(gr,ntSet);
00299   
00300   try{
00301     //resulting set size has to be the size of findThis
00302     if(rSet.size() != findThis.size()){
00303       std::stringstream errstr;
00304       errstr << "size of result set is " << rSet.size() << ", but " << findThis.size() << " was expected" << std::endl;
00305       throw Exception(name, errstr.str(), 1003);
00306     }
00307     //resulting set size has to contain all nonterminals in findThis
00308     std::set<Nonterminal>::const_iterator ntit, findit;
00309     for(ntit = findThis.begin(); ntit != findThis.end(); ntit++){
00310       //look for every nonterminal
00311       findit = rSet.find(*ntit);
00312       //throw error if it was not found
00313       if(findit == findThis.end()){
00314         std::stringstream errstr;
00315         errstr << "result set did not contain the expected nonterminal " << ntit->Str() << std::endl;
00316         throw Exception(name, errstr.str(), 1003);
00317       }
00318     }
00319   }
00320    catch(Exception e){
00321   }
00322   
00323   TestEnd(name);
00324 }
00325 
00326 /* *****************
00327  * TestRnppGrammar1
00328  * *****************/
00329 void TestRnppGrammar1(){
00330   std::string name = "Rnpp Grammar 1";
00331   TestStart(name);
00332   
00333   Grammar gr = TestGrammar1();
00334   
00335   //all nonterminals should be eliminable except (2,3,3)
00336   std::set<Nonterminal> finalNtSet = gr.Nonterminals();
00337   std::vector<Idx> v;
00338   v.push_back(3);
00339   finalNtSet.erase(Nonterminal(2,v,3));
00340   //only one grammar production contains (2,3,3) and must be removed
00341   std::set<GrammarProduction> finalGpSet = gr.GrammarProductions();
00342   GrammarSymbolVector vg;
00343   Nonterminal* nt = new Nonterminal(2,v,3);
00344   GrammarSymbolPtr ntPtr(nt);
00345   vg.push_back(ntPtr);
00346   v.clear();  v.push_back(2);
00347   finalGpSet.erase(GrammarProduction(Nonterminal(1,v,2), vg));
00348   
00349   Grammar rGr = Rnpp(gr);
00350   
00351   try{
00352     //resulting size of nonterminals has to be the size of finalNtSet
00353     if(rGr.Nonterminals().size() != finalNtSet.size()){
00354       std::stringstream errstr;
00355       errstr << "size of nonterminal set is " << rGr.Nonterminals().size() << ", but " << finalNtSet.size() << " was expected" << std::endl;
00356       throw Exception(name, errstr.str(), 1003);
00357     }
00358     //resulting size of grammar productions has to be the size of finalGpSet
00359     if(rGr.GrammarProductions().size() != finalGpSet.size()){
00360       std::stringstream errstr;
00361       errstr << "size of grammar production set is " << rGr.GrammarProductions().size() << ", but " << finalGpSet.size() << " was expected" << std::endl;
00362       throw Exception(name, errstr.str(), 1003);
00363     }
00364     
00365     //resulting set size has to contain all nonterminals in finalNtSet
00366     std::set<Nonterminal>::const_iterator ntit, findntit;
00367     for(ntit = finalNtSet.begin(); ntit != finalNtSet.end(); ntit++){
00368       //look for every nonterminal
00369       findntit = rGr.Nonterminals().find(*ntit);
00370       //throw error if it was not found
00371       if(findntit == finalNtSet.end()){
00372         std::stringstream errstr;
00373         errstr << "nonterminal set did not contain the expected nonterminal " << ntit->Str() << std::endl;
00374         throw Exception(name, errstr.str(), 1003);
00375       }
00376     }
00377     
00378     //resulting set size has to contain all grammar productions in finalGpSet
00379     std::set<GrammarProduction>::const_iterator gpit, findgpit;
00380     for(gpit = finalGpSet.begin(); gpit != finalGpSet.end(); gpit++){
00381       //look for every nonterminal
00382       findgpit = rGr.GrammarProductions().find(*gpit);
00383       //throw error if it was not found
00384       if(findgpit == finalGpSet.end()){
00385         std::stringstream errstr;
00386         errstr << "grammar productions set did not contain the expected grammar production " << gpit->Str() << std::endl;
00387         throw Exception(name, errstr.str(), 1003);
00388       }
00389     }
00390   }
00391    catch(Exception e){
00392   }
00393   
00394   TestEnd(name);
00395 }
00396 
00397 /* *****************
00398  * TestRnppGrammar2
00399  * *****************/
00400 void TestRnppGrammar2(){
00401   std::string name = "Rnpp Grammar 2";
00402   TestStart(name);
00403   
00404   Grammar gr = TestGrammar2();
00405   
00406   //all nonterminals should be eliminable except (2,3,3) and (3,2)
00407   std::vector<Idx> v2;
00408   v2.push_back(2);
00409   std::vector<Idx> v3;
00410   v3.push_back(3);
00411   std::set<Nonterminal> finalNtSet = gr.Nonterminals();
00412   finalNtSet.erase(Nonterminal(2,v3,3));
00413   finalNtSet.erase(Nonterminal(3,v2));
00414   //only one grammar production contains (2,3,3) and must be removed, no grammar
00415   //production contains (3,2)
00416   std::set<GrammarProduction> finalGpSet = gr.GrammarProductions();
00417   GrammarSymbolVector vg;
00418   Nonterminal* nt = new Nonterminal(2,v3,3);
00419   GrammarSymbolPtr ntPtr(nt);
00420   vg.push_back(ntPtr);
00421   finalGpSet.erase(GrammarProduction(Nonterminal(1,v2,2), vg));
00422   
00423   Grammar rGr = Rnpp(gr);
00424     
00425   try{
00426     //resulting size of nonterminals has to be the size of finalNtSet
00427     if(rGr.Nonterminals().size() != finalNtSet.size()){
00428       std::stringstream errstr;
00429       errstr << "size of nonterminal set is " << rGr.Nonterminals().size() << ", but " << finalNtSet.size() << " was expected" << std::endl;
00430       throw Exception(name, errstr.str(), 1003);
00431     }
00432     //resulting size of grammar productions has to be the size of finalGpSet
00433     if(rGr.GrammarProductions().size() != finalGpSet.size()){
00434       std::stringstream errstr;
00435       errstr << "size of grammar production set is " << rGr.GrammarProductions().size() << ", but " << finalGpSet.size() << " was expected" << std::endl;
00436       throw Exception(name, errstr.str(), 1003);
00437     }
00438     
00439     //resulting set size has to contain all nonterminals in finalNtSet
00440     std::set<Nonterminal>::const_iterator ntit, findntit;
00441     for(ntit = finalNtSet.begin(); ntit != finalNtSet.end(); ntit++){
00442       //look for every nonterminal
00443       findntit = rGr.Nonterminals().find(*ntit);
00444       //throw error if it was not found
00445       if(findntit == finalNtSet.end()){
00446         std::stringstream errstr;
00447         errstr << "nonterminal set did not contain the expected nonterminal " << ntit->Str() << std::endl;
00448         throw Exception(name, errstr.str(), 1003);
00449       }
00450     }
00451     
00452     //resulting set size has to contain all grammar productions in finalGpSet
00453     std::set<GrammarProduction>::const_iterator gpit, findgpit;
00454     for(gpit = finalGpSet.begin(); gpit != finalGpSet.end(); gpit++){
00455       //look for every nonterminal
00456       findgpit = rGr.GrammarProductions().find(*gpit);
00457       //throw error if it was not found
00458       if(findgpit == finalGpSet.end()){
00459         std::stringstream errstr;
00460         errstr << "grammar productions set did not contain the expected grammar production " << gpit->Str() << std::endl;
00461         throw Exception(name, errstr.str(), 1003);
00462       }
00463     }
00464   }
00465    catch(Exception e){
00466   }
00467   
00468   TestEnd(name);
00469 }
00470 
00471 /* *****************
00472  * TestRnppEmptyGrammar
00473  * *****************/
00474 void TestRnppEmptyGrammar(){
00475   std::string name = "Rnpp Empty Grammar";
00476   TestStart(name);
00477   
00478   Grammar gr;
00479   
00480   Grammar rGr = Rnpp(gr);
00481   
00482   try{
00483     //resulting size of nonterminals has to be zero
00484     if(rGr.Nonterminals().size() != 0){
00485       std::stringstream errstr;
00486       errstr << "size of nonterminal set is " << rGr.Nonterminals().size() << ", but 0 was expected" << std::endl;
00487       throw Exception(name, errstr.str(), 1003);
00488     }
00489     //resulting size of grammar productions has to be zero
00490     if(rGr.GrammarProductions().size() != 0){
00491       std::stringstream errstr;
00492       errstr << "size of grammar production set is " << rGr.GrammarProductions().size() << ", but 0 was expected" << std::endl;
00493       throw Exception(name, errstr.str(), 1003);
00494     }
00495   }
00496    catch(Exception e){
00497   }
00498   
00499   TestEnd(name);
00500 }
00501 
00502 /* *****************
00503  * TestSp2LrTerminals
00504  * *****************/
00505 void TestSp2LrTerminals(){
00506   std::string name = "Sp2Lr Terminals";
00507   TestStart(name);
00508   
00509   PushdownGenerator g = TestGenerator8();
00510   
00511   Grammar gr = Sp2Lr(g);
00512   
00513   try{
00514     //size of terminals must be the same as size of events
00515     if(g.Alphabet().Size() != gr.Terminals().size()){
00516       std::stringstream errstr;
00517       errstr << "size of terminal set is " << gr.Terminals().size() << ", but "<< g.Alphabet().Size() << " was expected" << std::endl;
00518       throw Exception(name, errstr.str(), 1003);
00519     }
00520     
00521     EventSet::Iterator eventit;
00522     //terminals must be all events
00523     for(eventit = g.AlphabetBegin(); eventit != g.AlphabetEnd(); eventit++){
00524       if(gr.Terminals().find(*eventit) == gr.Terminals().end()){
00525         std::stringstream errstr;
00526         errstr << "terminal " << g.EventName(*eventit) << " was not found in terminal set" << std::endl;
00527         throw Exception(name, errstr.str(), 1003);
00528       }
00529     }
00530   }
00531    catch(Exception e){
00532   }
00533   
00534   TestEnd(name);
00535 }
00536 
00537 /* *****************
00538  * TestSp2LrNonterminals
00539  * *****************/
00540 void TestSp2LrNonterminals(){
00541   std::string name = "Sp2Lr Nonterminals";
00542   TestStart(name);
00543   
00544   PushdownGenerator g = TestGenerator8();
00545   
00546   Grammar gr = Sp2Lr(g);
00547   
00548   try{
00549     //number of all possible nonterminals
00550     uint expectedNumberNonterminals = g.States().Size() * (g.StackSymbols().Size() - 1) + g.States().Size() * g.States().Size() * (g.StackSymbols().Size() - 1);
00551     //if the size of nonterminals matches the expected size, none can be missing
00552     if(expectedNumberNonterminals != gr.Nonterminals().size()){
00553       std::stringstream errstr;
00554       errstr << "size of nonterminal set is " << gr.Nonterminals().size() << ", but "<< expectedNumberNonterminals << " was expected" << std::endl;
00555       throw Exception(name, errstr.str(), 1003);
00556     }
00557     
00558     //test for the correct starting nonterminal
00559     if(gr.StartSymbol().StartState() != 1 || gr.StartSymbol().EndState() != 0 || gr.StartSymbol().OnStack().front() != g.StackSymbolIndex("square")){
00560       std::stringstream errstr;
00561       errstr << "start symbol was " << gr.StartSymbol().Str() << ", but (2, [square]) was expected" << std::endl;
00562       throw Exception(name, errstr.str(), 1003);
00563     }
00564   }
00565    catch(Exception e){
00566   }
00567   
00568   TestEnd(name);
00569 }
00570 
00571 /* *****************
00572  * TestSp2LrProductions
00573  * *****************/
00574 void TestSp2LrProductions(){
00575   std::string name = "Sp2Lr Productions";
00576   TestStart(name);
00577   
00578   PushdownGenerator g = TestGenerator8();
00579   
00580   Grammar gr = Sp2Lr(g);
00581   
00582   std::vector<Idx> dot, lambda, square;
00583   dot.push_back(g.StackSymbolIndex("dot"));
00584   square.push_back(g.StackSymbolIndex("square"));
00585   lambda.push_back(g.StackSymbolIndex("lambda"));
00586   
00587   Nonterminal* nt1dot = new Nonterminal(1,dot);
00588   GrammarSymbolPtr nt1dotPtr(nt1dot);
00589   Nonterminal* nt1square = new Nonterminal(1,square);
00590   GrammarSymbolPtr nt1squarePtr(nt1square);
00591   Nonterminal* nt2dot = new Nonterminal(2,dot);
00592   GrammarSymbolPtr nt2dotPtr(nt2dot);
00593   Nonterminal* nt2square = new Nonterminal(2,square);
00594   GrammarSymbolPtr nt2squarePtr(nt2square);
00595   Nonterminal* nt1dot1 = new Nonterminal(1,dot,1);
00596   GrammarSymbolPtr nt1dot1Ptr(nt1dot1);
00597   Nonterminal* nt1dot2 = new Nonterminal(1,dot,2);
00598   GrammarSymbolPtr nt1dot2Ptr(nt1dot2);
00599   Nonterminal* nt1square1 = new Nonterminal(1,square,1);
00600   GrammarSymbolPtr nt1square1Ptr(nt1square1);
00601   Nonterminal* nt1square2 = new Nonterminal(1,square,2);
00602   GrammarSymbolPtr nt1square2Ptr(nt1square2);
00603   Nonterminal* nt2dot1 = new Nonterminal(2,dot,1);
00604   GrammarSymbolPtr nt2dot1Ptr(nt2dot1);
00605   Nonterminal* nt2dot2 = new Nonterminal(2,dot,2);
00606   GrammarSymbolPtr nt2dot2Ptr(nt2dot2);
00607   Nonterminal* nt2square1 = new Nonterminal(2,square,1);
00608   GrammarSymbolPtr nt2square1Ptr(nt2square1);
00609   Nonterminal* nt2square2 = new Nonterminal(2,square,2);
00610   GrammarSymbolPtr nt2square2Ptr(nt2square2);
00611   Terminal* ta = new Terminal(g.EventIndex("a"));
00612   GrammarSymbolPtr taPtr(ta);
00613   Terminal* tlambda = new Terminal(g.EventIndex(FAUDES_PD_LAMBDA));
00614   GrammarSymbolPtr tlambdaPtr(tlambda);
00615   
00616   
00617   GrammarSymbolVector v;
00618   //expected read set
00619   std::set<GrammarProduction> read;
00620   v.clear();
00621   v.push_back(taPtr);
00622   v.push_back(nt1dot1Ptr);
00623   read.insert(GrammarProduction(*nt2dot1,v));
00624   v.clear();
00625   v.push_back(taPtr);
00626   v.push_back(nt1dot2Ptr);
00627   read.insert(GrammarProduction(*nt2dot2,v));
00628   v.clear();
00629   v.push_back(taPtr);
00630   v.push_back(nt1dotPtr);
00631   read.insert(GrammarProduction(*nt2dot,v));
00632   
00633   //expected pop set
00634   std::set<GrammarProduction> pop;
00635   v.clear();
00636   v.push_back(tlambdaPtr);
00637   pop.insert(GrammarProduction(*nt1dot2,v));
00638   
00639   //expected push set
00640   std::set<GrammarProduction> push;
00641   v.clear();
00642   v.push_back(nt2dot1Ptr);
00643   v.push_back(nt1square1Ptr);
00644   push.insert(GrammarProduction(*nt1square1,v));
00645   v.clear();
00646   v.push_back(nt2dot2Ptr);
00647   v.push_back(nt2square1Ptr);
00648   push.insert(GrammarProduction(*nt1square1,v));
00649   v.clear();
00650   v.push_back(nt2dot1Ptr);
00651   v.push_back(nt1square2Ptr);
00652   push.insert(GrammarProduction(*nt1square2,v));
00653   v.clear();
00654   v.push_back(nt2dot2Ptr);
00655   v.push_back(nt2square2Ptr);
00656   push.insert(GrammarProduction(*nt1square2,v));
00657   v.clear();
00658   v.push_back(nt2dot1Ptr);
00659   v.push_back(nt1squarePtr);
00660   push.insert(GrammarProduction(*nt1square,v));
00661   v.clear();
00662   v.push_back(nt2dot2Ptr);
00663   v.push_back(nt2squarePtr);
00664   push.insert(GrammarProduction(*nt1square,v));
00665   v.clear();
00666   v.push_back(nt2dotPtr);
00667   push.insert(GrammarProduction(*nt1square,v));
00668   
00669   //expected final state set
00670   std::set<GrammarProduction> final;
00671   v.clear();
00672   v.push_back(tlambdaPtr);
00673   final.insert(GrammarProduction(*nt2dot,v));
00674   v.clear();
00675   v.push_back(tlambdaPtr);
00676   final.insert(GrammarProduction(*nt2square,v));
00677   
00678 
00679   try{
00680     
00681     std::set<GrammarProduction> gp = gr.GrammarProductions();
00682     std::set<GrammarProduction>::const_iterator gpit;
00683     //test for read
00684     for(gpit = read.begin(); gpit != read.end(); gpit++){
00685       if(gp.erase(*gpit) == 0){
00686         std::stringstream errstr;
00687         errstr << "grammar production " << gpit->Str() << " , which is generated by a read transition, was expected but not found in the grammar" << std::endl;
00688         throw Exception(name, errstr.str(), 1003);
00689       }
00690     }
00691     
00692     //test for pop
00693     for(gpit = pop.begin(); gpit != pop.end(); gpit++){
00694       if(gp.erase(*gpit) == 0){
00695         std::stringstream errstr;
00696         errstr << "grammar production " << gpit->Str() << " , which is generated by a pop transition, was expected but not found in the grammar" << std::endl;
00697         throw Exception(name, errstr.str(), 1003);
00698       }
00699     }
00700     
00701     //test for push
00702     for(gpit = push.begin(); gpit != push.end(); gpit++){
00703       if(gp.erase(*gpit) == 0){
00704         std::stringstream errstr;
00705         errstr << "grammar production " << gpit->Str() << " , which is generated by a push transition, was expected but not found in the grammar" << std::endl;
00706         throw Exception(name, errstr.str(), 1003);
00707       }
00708     }
00709     
00710     //test for final states
00711     for(gpit = final.begin(); gpit != final.end(); gpit++){
00712       if(gp.erase(*gpit) == 0){
00713         std::stringstream errstr;
00714         errstr << "grammar production " << gpit->Str() << " , which is generated by a final transition, was expected but not found in the grammar" << std::endl;
00715         throw Exception(name, errstr.str(), 1003);
00716       }
00717     }
00718     
00719     //all productions must have been looked at by now
00720     if(gp.size() != 0){
00721       std::stringstream errstr;
00722       errstr << "the grammar contained" << gp.size() << " more productions than expected" << std::endl;
00723       throw Exception(name, errstr.str(), 1003);
00724     }
00725   }
00726    catch(Exception e){
00727   }
00728   
00729   TestEnd(name);
00730 }
00731 
00732 /* *****************
00733  * TestSp2Lr2Productions
00734  * *****************/
00735 void TestSp2Lr2Productions(){
00736   std::string name = "Sp2Lr2 Productions";
00737   TestStart(name);
00738   
00739   PushdownGenerator g = TestGenerator8();
00740   
00741   Grammar gr = Sp2Lr2(g);
00742   
00743   std::vector<Idx> dot, lambda, square;
00744   dot.push_back(g.StackSymbolIndex("dot"));
00745   square.push_back(g.StackSymbolIndex("square"));
00746   lambda.push_back(g.StackSymbolIndex("lambda"));
00747   
00748   Nonterminal* nt1dot = new Nonterminal(1,dot);
00749   GrammarSymbolPtr nt1dotPtr(nt1dot);
00750   Nonterminal* nt1square = new Nonterminal(1,square);
00751   GrammarSymbolPtr nt1squarePtr(nt1square);
00752   Nonterminal* nt2dot = new Nonterminal(2,dot);
00753   GrammarSymbolPtr nt2dotPtr(nt2dot);
00754   Nonterminal* nt2square = new Nonterminal(2,square);
00755   GrammarSymbolPtr nt2squarePtr(nt2square);
00756   Nonterminal* nt1dot1 = new Nonterminal(1,dot,1);
00757   GrammarSymbolPtr nt1dot1Ptr(nt1dot1);
00758   Nonterminal* nt1dot2 = new Nonterminal(1,dot,2);
00759   GrammarSymbolPtr nt1dot2Ptr(nt1dot2);
00760   Nonterminal* nt1square1 = new Nonterminal(1,square,1);
00761   GrammarSymbolPtr nt1square1Ptr(nt1square1);
00762   Nonterminal* nt1square2 = new Nonterminal(1,square,2);
00763   GrammarSymbolPtr nt1square2Ptr(nt1square2);
00764   Nonterminal* nt2dot1 = new Nonterminal(2,dot,1);
00765   GrammarSymbolPtr nt2dot1Ptr(nt2dot1);
00766   Nonterminal* nt2dot2 = new Nonterminal(2,dot,2);
00767   GrammarSymbolPtr nt2dot2Ptr(nt2dot2);
00768   Nonterminal* nt2square1 = new Nonterminal(2,square,1);
00769   GrammarSymbolPtr nt2square1Ptr(nt2square1);
00770   Nonterminal* nt2square2 = new Nonterminal(2,square,2);
00771   GrammarSymbolPtr nt2square2Ptr(nt2square2);
00772   Terminal* ta = new Terminal(g.EventIndex("a"));
00773   GrammarSymbolPtr taPtr(ta);
00774   Terminal* tlambda = new Terminal(g.EventIndex(FAUDES_PD_LAMBDA));
00775   GrammarSymbolPtr tlambdaPtr(tlambda);
00776   
00777   
00778   GrammarSymbolVector v;
00779   //expected read set
00780   std::set<GrammarProduction> read;
00781   v.clear();
00782   v.push_back(taPtr);
00783   v.push_back(nt1dot2Ptr);
00784   read.insert(GrammarProduction(*nt2dot2,v));
00785   
00786   //expected pop set
00787   std::set<GrammarProduction> pop;
00788   v.clear();
00789   v.push_back(tlambdaPtr);
00790   pop.insert(GrammarProduction(*nt1dot2,v));
00791   
00792   //expected push set
00793   std::set<GrammarProduction> push;
00794   v.clear();
00795   v.push_back(nt2dot2Ptr);
00796   v.push_back(nt2squarePtr);
00797   push.insert(GrammarProduction(*nt1square,v));
00798   v.clear();
00799   v.push_back(nt2dotPtr);
00800   push.insert(GrammarProduction(*nt1square,v));
00801   
00802   //expected final state set
00803   std::set<GrammarProduction> final;
00804   v.clear();
00805   v.push_back(tlambdaPtr);
00806   final.insert(GrammarProduction(*nt2dot,v));
00807   v.clear();
00808   v.push_back(tlambdaPtr);
00809   final.insert(GrammarProduction(*nt2square,v));
00810 
00811   try{
00812     
00813     std::set<GrammarProduction> gp = gr.GrammarProductions();
00814     std::set<GrammarProduction>::const_iterator gpit;
00815     //test for read
00816     for(gpit = read.begin(); gpit != read.end(); gpit++){
00817       if(gp.erase(*gpit) == 0){
00818         std::stringstream errstr;
00819         errstr << "grammar production " << gpit->Str() << " , which is generated by a read transition, was expected but not found in the grammar" << std::endl;
00820         throw Exception(name, errstr.str(), 1003);
00821       }
00822     }
00823     
00824     //test for pop
00825     for(gpit = pop.begin(); gpit != pop.end(); gpit++){
00826       if(gp.erase(*gpit) == 0){
00827         std::stringstream errstr;
00828         errstr << "grammar production " << gpit->Str() << " , which is generated by a pop transition, was expected but not found in the grammar" << std::endl;
00829         throw Exception(name, errstr.str(), 1003);
00830       }
00831     }
00832     
00833     //test for push
00834     for(gpit = push.begin(); gpit != push.end(); gpit++){
00835       if(gp.erase(*gpit) == 0){
00836         std::stringstream errstr;
00837         errstr << "grammar production " << gpit->Str() << " , which is generated by a push transition, was expected but not found in the grammar" << std::endl;
00838         throw Exception(name, errstr.str(), 1003);
00839       }
00840     }
00841     
00842     //test for final states
00843     for(gpit = final.begin(); gpit != final.end(); gpit++){
00844       if(gp.erase(*gpit) == 0){
00845         std::stringstream errstr;
00846         errstr << "grammar production " << gpit->Str() << " , which is generated by a final transition, was expected but not found in the grammar" << std::endl;
00847         throw Exception(name, errstr.str(), 1003);
00848       }
00849     }
00850     
00851     //all productions must have been looked at by now
00852     if(gp.size() != 0){
00853       std::stringstream errstr;
00854       errstr << "the grammar contained" << gp.size() << " more productions than expected" << std::endl;
00855       throw Exception(name, errstr.str(), 1003);
00856     }
00857   }
00858    catch(Exception e){
00859   }
00860   
00861   TestEnd(name);
00862 }
00863 
00864 /* *****************
00865  * TestRupProductions
00866  * *****************/
00867 void TestRupProductions(){
00868   std::string name = "Test Rup Productions";
00869   TestStart(name);
00870   
00871   Grammar gr = TestGrammar3();
00872   Grammar rGr = Rup(gr);
00873   
00874   try{
00875     
00876     //only one production must have been removed
00877     if(gr.GrammarProductions().size() - 1 != rGr.GrammarProductions().size()){
00878       std::stringstream errstr;
00879       errstr << "size of grammar productions was " << rGr.GrammarProductions().size() << ", but " << gr.GrammarProductions().size() - 1 << " was expected" << std::endl;
00880       throw Exception(name, errstr.str(), 1003);
00881     }
00882     
00883     //only the grammar production (2, [dot], 2) -> a(1, [dot]) must have been removed
00884     std::vector<Idx> v;
00885     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00886     Nonterminal* nt1 = new Nonterminal(2,v,2);
00887     GrammarSymbolPtr nt1Ptr(nt1);
00888     Nonterminal* nt2 = new Nonterminal(1,v);
00889     GrammarSymbolPtr nt2Ptr(nt2);
00890     Terminal* t = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("a"));
00891     GrammarSymbolPtr tPtr(t);
00892     GrammarSymbolVector gs;
00893     gs.push_back(tPtr);
00894     gs.push_back(nt2Ptr);
00895     GrammarProduction gp(*nt1,gs);
00896     
00897     if(gr.GrammarProductions().find(gp) != gr.GrammarProductions().end()){
00898       std::stringstream errstr;
00899       errstr << "grammar production (2, [dot], 2) -> a(1, [dot]) was present, but was expected to be deleted" << std::endl;
00900       throw Exception(name, errstr.str(), 1003);
00901     }
00902   }
00903    catch(Exception e){
00904   }
00905   
00906   TestEnd(name);
00907 }
00908 
00909 /* *****************
00910  * TestRupNonterminals
00911  * *****************/
00912 void TestRupNonterminals(){
00913   std::string name = "Test Rup Nonterminals";
00914   TestStart(name);
00915   
00916   Grammar gr = TestGrammar3();
00917   Grammar rGr = Rup(gr);
00918   
00919   try{
00920     
00921     //only one nonterminal must have been removed
00922     if(gr.Nonterminals().size() - 1 != rGr.Nonterminals().size()){
00923       std::stringstream errstr;
00924       errstr << "size of nonterminals was " << rGr.Nonterminals().size() << ", but " << gr.Nonterminals().size() - 1 << " was expected" << std::endl;
00925       throw Exception(name, errstr.str(), 1003);
00926     }
00927     
00928     //only the nonterminal (2, [dot], 2) must have been removed
00929     std::vector<Idx> v;
00930     v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
00931     Nonterminal nt(2,v,2);
00932     if(gr.Nonterminals().find(nt) != gr.Nonterminals().end()){
00933       std::stringstream errstr;
00934       errstr << "nonterminal (2, [dot], 2) was present, but was expected to be deleted" << std::endl;
00935       throw Exception(name, errstr.str(), 1003);
00936     }
00937   }
00938    catch(Exception e){
00939   }
00940 
00941   
00942   TestEnd(name);
00943 }
00944 
00945 /* *****************
00946  * TestFilter
00947  * *****************/
00948 void TestFilter(){
00949   TestFilterMixedGrammarSymbols();
00950   TestFilterNothing();
00951 }
00952 
00953 /* *****************
00954  * TestRnpp1
00955  * *****************/
00956 void TestRnpp1(){
00957   TestRnpp1FindSymbolsEmptySet();
00958   TestRnpp1FindSymbolsNonemptySet();
00959 }
00960 
00961 /* *****************
00962  * TestRnpp1
00963  * *****************/
00964 void TestRnppl(){
00965   TestRnpplFindSymbolsEmptySet();
00966   TestRnpplFindSymbolsNonemptySet();
00967   TestRnpplFindSymbolsCompleteSet();
00968 }
00969 
00970 /* *****************
00971  * TestRnpp
00972  * *****************/
00973 void TestRnpp(){
00974   TestRnppGrammar1();
00975   TestRnppGrammar2();
00976   TestRnppEmptyGrammar();
00977 }
00978 
00979 /* *****************
00980  * Sp2Lr
00981  * *****************/
00982 void TestSp2Lr(){
00983   
00984   TestSp2LrTerminals();
00985   TestSp2LrNonterminals();
00986   TestSp2LrProductions();
00987 }
00988 
00989 /* *****************
00990  * Sp2Lr2
00991  * *****************/
00992 void TestSp2Lr2(){
00993   TestSp2Lr2Productions();
00994 }
00995 
00996 /* *****************
00997  * Rup
00998  * *****************/
00999 void TestRup(){
01000   
01001   TestRupProductions();
01002   TestRupNonterminals();
01003 }
01004 } // namespace faudes
01005 

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