pd_alg_nb_sub_a_test.cpp
Go to the documentation of this file.
1 /** @file pd_alg_nb_sub_a_test.cpp Unit Tests */
2 
3 
4 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2013 Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess
7 
8 */
9 #include "pd_alg_nb_sub_a_test.h"
10 
11 namespace faudes {
12 
13 /* *****************
14  * TestFilterMixedGrammarSymbols
15  * *****************/
17 
18  std::string name = "Filter Mixed Grammar Symbols";
19  TestStart(name);
20 
21  Grammar gr1 = TestGrammar1();
22 
24  std::set<Terminal>::const_iterator tit;
25  std::vector<TerminalPtr> tpv;
26  std::vector<TerminalPtr>::iterator tpvit;
27  std::set<Nonterminal> rSet;
28 
29  //build word to filter
30  //contains one nonterminal and all terminals
31  Terminal* t;
32  Nonterminal* nt = new Nonterminal(*gr1.Nonterminals().begin());
33  GrammarSymbolPtr ntPtr(nt);
34  word.push_back(ntPtr);
35 
36  for(tit = gr1.Terminals().begin(); tit != gr1.Terminals().end(); tit++){
37  //create new terminal
38  t = new Terminal(*tit);
39  TerminalPtr tPtr(t);
40  //push terminal into word
41  word.push_back(tPtr);
42  }
43 
44  rSet = Filter(gr1.Nonterminals(),word);
45 
46  try{
47  //resulting set size has to be one
48  if(rSet.size() != 1){
49  std::stringstream errstr;
50  errstr << "size of result set is " << rSet.size() << ", but 1 was expected" << std::endl;
51  throw Exception(name, errstr.str(), 1003);
52  }
53  //resulting set has to contain *gr1.Nonterminals().begin()
54 
55  if(*rSet.begin() != *gr1.Nonterminals().begin()){
56  std::stringstream errstr;
57  errstr << "result set did not contain the expected nonterminal " << gr1.Nonterminals().begin()->Str() << std::endl;
58  throw Exception(name, errstr.str(), 1003);
59  }
60  }
61  catch(Exception e){
62  std::cout << (*rSet.begin()).Str() << std::endl;
63  std::cout << (*gr1.Nonterminals().begin()).Str() << std::endl;
64  }
65 
66  TestEnd(name);
67 }
68 
69 /* *****************
70  * TestFilterNothing
71  * *****************/
73  std::string name = "Filter Nothing";
74  TestStart(name);
75 
76  std::set<Nonterminal> nt;
78 
79  std::set<Nonterminal> rSet = Filter(nt,gs);
80 
81  try{
82  //resulting set size has to be zero
83  if(rSet.size() != 0){
84  std::stringstream errstr;
85  errstr << "size of result set is " << rSet.size() << ", but 0 was expected" << std::endl;
86  throw Exception(name, errstr.str(), 1003);
87  }
88  }
89  catch(Exception e){
90  }
91 
92  TestEnd(name);
93 }
94 
95 /* *****************
96  * TestRnpp1FindSymbolsEmptySet
97  * *****************/
99  std::string name = "Rnpp1 Find Symbols Empty Set";
100  TestStart(name);
101 
102  Grammar gr = TestGrammar1();
103 
104  //eliminable nonterminal which should be found by Rnpp1
105  std::vector<Idx> v;
106  v.push_back(3);
107  Nonterminal nt(5,v,1);
108  //the grammar should contain this nonterminal!
109  if(gr.Nonterminals().find(nt) == gr.Nonterminals().end()){
110  std::cout << "Warning, test parameters seem to be wrong. The test grammar did not contain Nonterminal (5,[3],1)." << std::endl;
111  return;
112  }
113 
114  //empty set of already eliminated nonterminals
115  std::set<Nonterminal> ntSet;
116 
117  std::set<Nonterminal> rSet = Rnpp1(gr,ntSet);
118 
119  try{
120  //resulting set size has to be one
121  if(rSet.size() != 1){
122  std::stringstream errstr;
123  errstr << "size of result set is " << rSet.size() << ", but 1 was expected" << std::endl;
124  throw Exception(name, errstr.str(), 1003);
125  }
126  //resulting set size has to contain nonterminal (3,2)
127  if(*rSet.begin() != nt){
128  std::stringstream errstr;
129  errstr << "result set did not contain the expected nonterminal " << nt.Str() << std::endl;
130  throw Exception(name, errstr.str(), 1003);
131  }
132  }
133  catch(Exception e){
134  }
135 
136  TestEnd(name);
137 }
138 
139 /* *****************
140  * TestRnpp1FindSymbolsNonemptySet
141  * *****************/
143  std::string name = "Rnpp1 Find Symbols Empty Set";
144  TestStart(name);
145 
146  Grammar gr = TestGrammar1();
147 
148  //eliminable nonterminal which should be found
149  std::vector<Idx> v;
150  v.push_back(2);
151  Nonterminal nt(1,v,2);
152  //the grammar should contain this nonterminal!
153  if(gr.Nonterminals().find(nt) == gr.Nonterminals().end()){
154  std::cout << "Warning, test parameters seem to be wrong. The test grammar did not contain Nonterminal (1,[2],2)." << std::endl;
155  std::cout << gr.Str() << std::endl;
156  return;
157  }
158 
159  //empty set of already eliminated nonterminals
160  std::set<Nonterminal> ntSet;
161 
162  std::set<Nonterminal> rSet = Rnpp1(gr,Rnpp1(gr,ntSet));
163 
164  try{
165  //resulting set size has to be one
166  if(rSet.size() != 2){
167  std::stringstream errstr;
168  errstr << "size of result set is " << rSet.size() << ", but 2 was expected" << std::endl;
169  throw Exception(name, errstr.str(), 1003);
170  }
171  //resulting set size has to contain nonterminal (1,2,2)
172  if(*rSet.begin() != nt){
173  std::stringstream errstr;
174  errstr << "result set did not contain the expected nonterminal " << nt.Str() << std::endl;
175  throw Exception(name, errstr.str(), 1003);
176  }
177  }
178  catch(Exception e){
179  }
180 
181  TestEnd(name);
182 }
183 
184 /* *****************
185  * TestRnpplFindSymbolsEmptySet
186  * *****************/
188  std::string name = "Rnppl Find Symbols Empty Set";
189  TestStart(name);
190 
191  Grammar gr = TestGrammar1();
192 
193  //all nonterminals should be eliminable except (2,3,3)
194  std::set<Nonterminal> findThis = gr.Nonterminals();
195  std::vector<Idx> v;
196  v.push_back(3);
197  findThis.erase(Nonterminal(2,v,3));
198 
199  //empty set of already eliminated nonterminals
200  std::set<Nonterminal> ntSet;
201 
202  std::set<Nonterminal> rSet = Rnppl(gr,ntSet);
203 
204  try{
205  //resulting set size has to be the size of findThis
206  if(rSet.size() != findThis.size()){
207  std::stringstream errstr;
208  errstr << "size of result set is " << rSet.size() << ", but " << findThis.size() << " was expected" << std::endl;
209  throw Exception(name, errstr.str(), 1003);
210  }
211  //resulting set size has to contain all nonterminals in findThis
212  std::set<Nonterminal>::const_iterator ntit, findit;
213  for(ntit = findThis.begin(); ntit != findThis.end(); ntit++){
214  //look for every nonterminal
215  findit = rSet.find(*ntit);
216  //throw error if it was not found
217  if(findit == findThis.end()){
218  std::stringstream errstr;
219  errstr << "result set did not contain the expected nonterminal " << ntit->Str() << std::endl;
220  throw Exception(name, errstr.str(), 1003);
221  }
222  }
223  }
224  catch(Exception e){
225  }
226 
227  TestEnd(name);
228 }
229 
230 /* *****************
231  * TestRnpplFindSymbolsNonemptySet
232  * *****************/
234  std::string name = "Rnppl Find Symbols Nonempty Set";
235  TestStart(name);
236 
237  Grammar gr = TestGrammar1();
238 
239  //all nonterminals should be eliminable except (2,3,3)
240  std::set<Nonterminal> findThis = gr.Nonterminals();
241  std::vector<Idx> v;
242  v.push_back(3);
243  findThis.erase(Nonterminal(2,v,3));
244 
245  //set of already eliminated nonterminals, add any eliminable nonterminals
246  std::set<Nonterminal> ntSet;
247  v.clear(); v.push_back(2);
248  ntSet.insert(Nonterminal(1,v,2));
249  v.clear(); v.push_back(3);
250  ntSet.insert(Nonterminal(4,v));
251 
252  std::set<Nonterminal> rSet = Rnppl(gr,ntSet);
253 
254  try{
255  //resulting set size has to be the size of findThis
256  if(rSet.size() != findThis.size()){
257  std::stringstream errstr;
258  errstr << "size of result set is " << rSet.size() << ", but " << findThis.size() << " was expected" << std::endl;
259  throw Exception(name, errstr.str(), 1003);
260  }
261  //resulting set size has to contain all nonterminals in findThis
262  std::set<Nonterminal>::const_iterator ntit, findit;
263  for(ntit = findThis.begin(); ntit != findThis.end(); ntit++){
264  //look for every nonterminal
265  findit = rSet.find(*ntit);
266  //throw error if it was not found
267  if(findit == findThis.end()){
268  std::stringstream errstr;
269  errstr << "result set did not contain the expected nonterminal " << ntit->Str() << std::endl;
270  throw Exception(name, errstr.str(), 1003);
271  }
272  }
273  }
274  catch(Exception e){
275  }
276 
277  TestEnd(name);
278 }
279 
280 /* *****************
281  * TestRnpplFindSymbolsCompleteSet
282  * *****************/
284  std::string name = "Rnppl Find Symbols Complete Set";
285  TestStart(name);
286 
287  Grammar gr = TestGrammar1();
288 
289  //all nonterminals should be eliminable except (2,3,3)
290  std::set<Nonterminal> findThis = gr.Nonterminals();
291  std::vector<Idx> v;
292  v.push_back(3);
293  findThis.erase(Nonterminal(2,v,3));
294 
295  //set of already eliminated nonterminals, add any eliminable nonterminals
296  std::set<Nonterminal> ntSet = findThis;
297 
298  std::set<Nonterminal> rSet = Rnppl(gr,ntSet);
299 
300  try{
301  //resulting set size has to be the size of findThis
302  if(rSet.size() != findThis.size()){
303  std::stringstream errstr;
304  errstr << "size of result set is " << rSet.size() << ", but " << findThis.size() << " was expected" << std::endl;
305  throw Exception(name, errstr.str(), 1003);
306  }
307  //resulting set size has to contain all nonterminals in findThis
308  std::set<Nonterminal>::const_iterator ntit, findit;
309  for(ntit = findThis.begin(); ntit != findThis.end(); ntit++){
310  //look for every nonterminal
311  findit = rSet.find(*ntit);
312  //throw error if it was not found
313  if(findit == findThis.end()){
314  std::stringstream errstr;
315  errstr << "result set did not contain the expected nonterminal " << ntit->Str() << std::endl;
316  throw Exception(name, errstr.str(), 1003);
317  }
318  }
319  }
320  catch(Exception e){
321  }
322 
323  TestEnd(name);
324 }
325 
326 /* *****************
327  * TestRnppGrammar1
328  * *****************/
330  std::string name = "Rnpp Grammar 1";
331  TestStart(name);
332 
333  Grammar gr = TestGrammar1();
334 
335  //all nonterminals should be eliminable except (2,3,3)
336  std::set<Nonterminal> finalNtSet = gr.Nonterminals();
337  std::vector<Idx> v;
338  v.push_back(3);
339  finalNtSet.erase(Nonterminal(2,v,3));
340  //only one grammar production contains (2,3,3) and must be removed
341  std::set<GrammarProduction> finalGpSet = gr.GrammarProductions();
343  Nonterminal* nt = new Nonterminal(2,v,3);
344  GrammarSymbolPtr ntPtr(nt);
345  vg.push_back(ntPtr);
346  v.clear(); v.push_back(2);
347  finalGpSet.erase(GrammarProduction(Nonterminal(1,v,2), vg));
348 
349  Grammar rGr = Rnpp(gr);
350 
351  try{
352  //resulting size of nonterminals has to be the size of finalNtSet
353  if(rGr.Nonterminals().size() != finalNtSet.size()){
354  std::stringstream errstr;
355  errstr << "size of nonterminal set is " << rGr.Nonterminals().size() << ", but " << finalNtSet.size() << " was expected" << std::endl;
356  throw Exception(name, errstr.str(), 1003);
357  }
358  //resulting size of grammar productions has to be the size of finalGpSet
359  if(rGr.GrammarProductions().size() != finalGpSet.size()){
360  std::stringstream errstr;
361  errstr << "size of grammar production set is " << rGr.GrammarProductions().size() << ", but " << finalGpSet.size() << " was expected" << std::endl;
362  throw Exception(name, errstr.str(), 1003);
363  }
364 
365  //resulting set size has to contain all nonterminals in finalNtSet
366  std::set<Nonterminal>::const_iterator ntit, findntit;
367  for(ntit = finalNtSet.begin(); ntit != finalNtSet.end(); ntit++){
368  //look for every nonterminal
369  findntit = rGr.Nonterminals().find(*ntit);
370  //throw error if it was not found
371  if(findntit == finalNtSet.end()){
372  std::stringstream errstr;
373  errstr << "nonterminal set did not contain the expected nonterminal " << ntit->Str() << std::endl;
374  throw Exception(name, errstr.str(), 1003);
375  }
376  }
377 
378  //resulting set size has to contain all grammar productions in finalGpSet
379  std::set<GrammarProduction>::const_iterator gpit, findgpit;
380  for(gpit = finalGpSet.begin(); gpit != finalGpSet.end(); gpit++){
381  //look for every nonterminal
382  findgpit = rGr.GrammarProductions().find(*gpit);
383  //throw error if it was not found
384  if(findgpit == finalGpSet.end()){
385  std::stringstream errstr;
386  errstr << "grammar productions set did not contain the expected grammar production " << gpit->Str() << std::endl;
387  throw Exception(name, errstr.str(), 1003);
388  }
389  }
390  }
391  catch(Exception e){
392  }
393 
394  TestEnd(name);
395 }
396 
397 /* *****************
398  * TestRnppGrammar2
399  * *****************/
401  std::string name = "Rnpp Grammar 2";
402  TestStart(name);
403 
404  Grammar gr = TestGrammar2();
405 
406  //all nonterminals should be eliminable except (2,3,3) and (3,2)
407  std::vector<Idx> v2;
408  v2.push_back(2);
409  std::vector<Idx> v3;
410  v3.push_back(3);
411  std::set<Nonterminal> finalNtSet = gr.Nonterminals();
412  finalNtSet.erase(Nonterminal(2,v3,3));
413  finalNtSet.erase(Nonterminal(3,v2));
414  //only one grammar production contains (2,3,3) and must be removed, no grammar
415  //production contains (3,2)
416  std::set<GrammarProduction> finalGpSet = gr.GrammarProductions();
418  Nonterminal* nt = new Nonterminal(2,v3,3);
419  GrammarSymbolPtr ntPtr(nt);
420  vg.push_back(ntPtr);
421  finalGpSet.erase(GrammarProduction(Nonterminal(1,v2,2), vg));
422 
423  Grammar rGr = Rnpp(gr);
424 
425  try{
426  //resulting size of nonterminals has to be the size of finalNtSet
427  if(rGr.Nonterminals().size() != finalNtSet.size()){
428  std::stringstream errstr;
429  errstr << "size of nonterminal set is " << rGr.Nonterminals().size() << ", but " << finalNtSet.size() << " was expected" << std::endl;
430  throw Exception(name, errstr.str(), 1003);
431  }
432  //resulting size of grammar productions has to be the size of finalGpSet
433  if(rGr.GrammarProductions().size() != finalGpSet.size()){
434  std::stringstream errstr;
435  errstr << "size of grammar production set is " << rGr.GrammarProductions().size() << ", but " << finalGpSet.size() << " was expected" << std::endl;
436  throw Exception(name, errstr.str(), 1003);
437  }
438 
439  //resulting set size has to contain all nonterminals in finalNtSet
440  std::set<Nonterminal>::const_iterator ntit, findntit;
441  for(ntit = finalNtSet.begin(); ntit != finalNtSet.end(); ntit++){
442  //look for every nonterminal
443  findntit = rGr.Nonterminals().find(*ntit);
444  //throw error if it was not found
445  if(findntit == finalNtSet.end()){
446  std::stringstream errstr;
447  errstr << "nonterminal set did not contain the expected nonterminal " << ntit->Str() << std::endl;
448  throw Exception(name, errstr.str(), 1003);
449  }
450  }
451 
452  //resulting set size has to contain all grammar productions in finalGpSet
453  std::set<GrammarProduction>::const_iterator gpit, findgpit;
454  for(gpit = finalGpSet.begin(); gpit != finalGpSet.end(); gpit++){
455  //look for every nonterminal
456  findgpit = rGr.GrammarProductions().find(*gpit);
457  //throw error if it was not found
458  if(findgpit == finalGpSet.end()){
459  std::stringstream errstr;
460  errstr << "grammar productions set did not contain the expected grammar production " << gpit->Str() << std::endl;
461  throw Exception(name, errstr.str(), 1003);
462  }
463  }
464  }
465  catch(Exception e){
466  }
467 
468  TestEnd(name);
469 }
470 
471 /* *****************
472  * TestRnppEmptyGrammar
473  * *****************/
475  std::string name = "Rnpp Empty Grammar";
476  TestStart(name);
477 
478  Grammar gr;
479 
480  Grammar rGr = Rnpp(gr);
481 
482  try{
483  //resulting size of nonterminals has to be zero
484  if(rGr.Nonterminals().size() != 0){
485  std::stringstream errstr;
486  errstr << "size of nonterminal set is " << rGr.Nonterminals().size() << ", but 0 was expected" << std::endl;
487  throw Exception(name, errstr.str(), 1003);
488  }
489  //resulting size of grammar productions has to be zero
490  if(rGr.GrammarProductions().size() != 0){
491  std::stringstream errstr;
492  errstr << "size of grammar production set is " << rGr.GrammarProductions().size() << ", but 0 was expected" << std::endl;
493  throw Exception(name, errstr.str(), 1003);
494  }
495  }
496  catch(Exception e){
497  }
498 
499  TestEnd(name);
500 }
501 
502 /* *****************
503  * TestSp2LrTerminals
504  * *****************/
506  std::string name = "Sp2Lr Terminals";
507  TestStart(name);
508 
510 
511  Grammar gr = Sp2Lr(g);
512 
513  try{
514  //size of terminals must be the same as size of events
515  if(g.Alphabet().Size() != gr.Terminals().size()){
516  std::stringstream errstr;
517  errstr << "size of terminal set is " << gr.Terminals().size() << ", but "<< g.Alphabet().Size() << " was expected" << std::endl;
518  throw Exception(name, errstr.str(), 1003);
519  }
520 
521  EventSet::Iterator eventit;
522  //terminals must be all events
523  for(eventit = g.AlphabetBegin(); eventit != g.AlphabetEnd(); eventit++){
524  if(gr.Terminals().find(*eventit) == gr.Terminals().end()){
525  std::stringstream errstr;
526  errstr << "terminal " << g.EventName(*eventit) << " was not found in terminal set" << std::endl;
527  throw Exception(name, errstr.str(), 1003);
528  }
529  }
530  }
531  catch(Exception e){
532  }
533 
534  TestEnd(name);
535 }
536 
537 /* *****************
538  * TestSp2LrNonterminals
539  * *****************/
541  std::string name = "Sp2Lr Nonterminals";
542  TestStart(name);
543 
545 
546  Grammar gr = Sp2Lr(g);
547 
548  try{
549  //number of all possible nonterminals
550  uint expectedNumberNonterminals = g.States().Size() * (g.StackSymbols().Size() - 1) + g.States().Size() * g.States().Size() * (g.StackSymbols().Size() - 1);
551  //if the size of nonterminals matches the expected size, none can be missing
552  if(expectedNumberNonterminals != gr.Nonterminals().size()){
553  std::stringstream errstr;
554  errstr << "size of nonterminal set is " << gr.Nonterminals().size() << ", but "<< expectedNumberNonterminals << " was expected" << std::endl;
555  throw Exception(name, errstr.str(), 1003);
556  }
557 
558  //test for the correct starting nonterminal
559  if(gr.StartSymbol().StartState() != 1 || gr.StartSymbol().EndState() != 0 || gr.StartSymbol().OnStack().front() != g.StackSymbolIndex("square")){
560  std::stringstream errstr;
561  errstr << "start symbol was " << gr.StartSymbol().Str() << ", but (2, [square]) was expected" << std::endl;
562  throw Exception(name, errstr.str(), 1003);
563  }
564  }
565  catch(Exception e){
566  }
567 
568  TestEnd(name);
569 }
570 
571 /* *****************
572  * TestSp2LrProductions
573  * *****************/
575  std::string name = "Sp2Lr Productions";
576  TestStart(name);
577 
579 
580  Grammar gr = Sp2Lr(g);
581 
582  std::vector<Idx> dot, lambda, square;
583  dot.push_back(g.StackSymbolIndex("dot"));
584  square.push_back(g.StackSymbolIndex("square"));
585  lambda.push_back(g.StackSymbolIndex("lambda"));
586 
587  Nonterminal* nt1dot = new Nonterminal(1,dot);
588  GrammarSymbolPtr nt1dotPtr(nt1dot);
589  Nonterminal* nt1square = new Nonterminal(1,square);
590  GrammarSymbolPtr nt1squarePtr(nt1square);
591  Nonterminal* nt2dot = new Nonterminal(2,dot);
592  GrammarSymbolPtr nt2dotPtr(nt2dot);
593  Nonterminal* nt2square = new Nonterminal(2,square);
594  GrammarSymbolPtr nt2squarePtr(nt2square);
595  Nonterminal* nt1dot1 = new Nonterminal(1,dot,1);
596  GrammarSymbolPtr nt1dot1Ptr(nt1dot1);
597  Nonterminal* nt1dot2 = new Nonterminal(1,dot,2);
598  GrammarSymbolPtr nt1dot2Ptr(nt1dot2);
599  Nonterminal* nt1square1 = new Nonterminal(1,square,1);
600  GrammarSymbolPtr nt1square1Ptr(nt1square1);
601  Nonterminal* nt1square2 = new Nonterminal(1,square,2);
602  GrammarSymbolPtr nt1square2Ptr(nt1square2);
603  Nonterminal* nt2dot1 = new Nonterminal(2,dot,1);
604  GrammarSymbolPtr nt2dot1Ptr(nt2dot1);
605  Nonterminal* nt2dot2 = new Nonterminal(2,dot,2);
606  GrammarSymbolPtr nt2dot2Ptr(nt2dot2);
607  Nonterminal* nt2square1 = new Nonterminal(2,square,1);
608  GrammarSymbolPtr nt2square1Ptr(nt2square1);
609  Nonterminal* nt2square2 = new Nonterminal(2,square,2);
610  GrammarSymbolPtr nt2square2Ptr(nt2square2);
611  Terminal* ta = new Terminal(g.EventIndex("a"));
612  GrammarSymbolPtr taPtr(ta);
613  Terminal* tlambda = new Terminal(g.EventIndex(FAUDES_PD_LAMBDA));
614  GrammarSymbolPtr tlambdaPtr(tlambda);
615 
616 
618  //expected read set
619  std::set<GrammarProduction> read;
620  v.clear();
621  v.push_back(taPtr);
622  v.push_back(nt1dot1Ptr);
623  read.insert(GrammarProduction(*nt2dot1,v));
624  v.clear();
625  v.push_back(taPtr);
626  v.push_back(nt1dot2Ptr);
627  read.insert(GrammarProduction(*nt2dot2,v));
628  v.clear();
629  v.push_back(taPtr);
630  v.push_back(nt1dotPtr);
631  read.insert(GrammarProduction(*nt2dot,v));
632 
633  //expected pop set
634  std::set<GrammarProduction> pop;
635  v.clear();
636  v.push_back(tlambdaPtr);
637  pop.insert(GrammarProduction(*nt1dot2,v));
638 
639  //expected push set
640  std::set<GrammarProduction> push;
641  v.clear();
642  v.push_back(nt2dot1Ptr);
643  v.push_back(nt1square1Ptr);
644  push.insert(GrammarProduction(*nt1square1,v));
645  v.clear();
646  v.push_back(nt2dot2Ptr);
647  v.push_back(nt2square1Ptr);
648  push.insert(GrammarProduction(*nt1square1,v));
649  v.clear();
650  v.push_back(nt2dot1Ptr);
651  v.push_back(nt1square2Ptr);
652  push.insert(GrammarProduction(*nt1square2,v));
653  v.clear();
654  v.push_back(nt2dot2Ptr);
655  v.push_back(nt2square2Ptr);
656  push.insert(GrammarProduction(*nt1square2,v));
657  v.clear();
658  v.push_back(nt2dot1Ptr);
659  v.push_back(nt1squarePtr);
660  push.insert(GrammarProduction(*nt1square,v));
661  v.clear();
662  v.push_back(nt2dot2Ptr);
663  v.push_back(nt2squarePtr);
664  push.insert(GrammarProduction(*nt1square,v));
665  v.clear();
666  v.push_back(nt2dotPtr);
667  push.insert(GrammarProduction(*nt1square,v));
668 
669  //expected final state set
670  std::set<GrammarProduction> final;
671  v.clear();
672  v.push_back(tlambdaPtr);
673  final.insert(GrammarProduction(*nt2dot,v));
674  v.clear();
675  v.push_back(tlambdaPtr);
676  final.insert(GrammarProduction(*nt2square,v));
677 
678 
679  try{
680 
681  std::set<GrammarProduction> gp = gr.GrammarProductions();
682  std::set<GrammarProduction>::const_iterator gpit;
683  //test for read
684  for(gpit = read.begin(); gpit != read.end(); gpit++){
685  if(gp.erase(*gpit) == 0){
686  std::stringstream errstr;
687  errstr << "grammar production " << gpit->Str() << " , which is generated by a read transition, was expected but not found in the grammar" << std::endl;
688  throw Exception(name, errstr.str(), 1003);
689  }
690  }
691 
692  //test for pop
693  for(gpit = pop.begin(); gpit != pop.end(); gpit++){
694  if(gp.erase(*gpit) == 0){
695  std::stringstream errstr;
696  errstr << "grammar production " << gpit->Str() << " , which is generated by a pop transition, was expected but not found in the grammar" << std::endl;
697  throw Exception(name, errstr.str(), 1003);
698  }
699  }
700 
701  //test for push
702  for(gpit = push.begin(); gpit != push.end(); gpit++){
703  if(gp.erase(*gpit) == 0){
704  std::stringstream errstr;
705  errstr << "grammar production " << gpit->Str() << " , which is generated by a push transition, was expected but not found in the grammar" << std::endl;
706  throw Exception(name, errstr.str(), 1003);
707  }
708  }
709 
710  //test for final states
711  for(gpit = final.begin(); gpit != final.end(); gpit++){
712  if(gp.erase(*gpit) == 0){
713  std::stringstream errstr;
714  errstr << "grammar production " << gpit->Str() << " , which is generated by a final transition, was expected but not found in the grammar" << std::endl;
715  throw Exception(name, errstr.str(), 1003);
716  }
717  }
718 
719  //all productions must have been looked at by now
720  if(gp.size() != 0){
721  std::stringstream errstr;
722  errstr << "the grammar contained" << gp.size() << " more productions than expected" << std::endl;
723  throw Exception(name, errstr.str(), 1003);
724  }
725  }
726  catch(Exception e){
727  }
728 
729  TestEnd(name);
730 }
731 
732 /* *****************
733  * TestSp2Lr2Productions
734  * *****************/
736  std::string name = "Sp2Lr2 Productions";
737  TestStart(name);
738 
740 
741  Grammar gr = Sp2Lr2(g);
742 
743  std::vector<Idx> dot, lambda, square;
744  dot.push_back(g.StackSymbolIndex("dot"));
745  square.push_back(g.StackSymbolIndex("square"));
746  lambda.push_back(g.StackSymbolIndex("lambda"));
747 
748  Nonterminal* nt1dot = new Nonterminal(1,dot);
749  GrammarSymbolPtr nt1dotPtr(nt1dot);
750  Nonterminal* nt1square = new Nonterminal(1,square);
751  GrammarSymbolPtr nt1squarePtr(nt1square);
752  Nonterminal* nt2dot = new Nonterminal(2,dot);
753  GrammarSymbolPtr nt2dotPtr(nt2dot);
754  Nonterminal* nt2square = new Nonterminal(2,square);
755  GrammarSymbolPtr nt2squarePtr(nt2square);
756  Nonterminal* nt1dot1 = new Nonterminal(1,dot,1);
757  GrammarSymbolPtr nt1dot1Ptr(nt1dot1);
758  Nonterminal* nt1dot2 = new Nonterminal(1,dot,2);
759  GrammarSymbolPtr nt1dot2Ptr(nt1dot2);
760  Nonterminal* nt1square1 = new Nonterminal(1,square,1);
761  GrammarSymbolPtr nt1square1Ptr(nt1square1);
762  Nonterminal* nt1square2 = new Nonterminal(1,square,2);
763  GrammarSymbolPtr nt1square2Ptr(nt1square2);
764  Nonterminal* nt2dot1 = new Nonterminal(2,dot,1);
765  GrammarSymbolPtr nt2dot1Ptr(nt2dot1);
766  Nonterminal* nt2dot2 = new Nonterminal(2,dot,2);
767  GrammarSymbolPtr nt2dot2Ptr(nt2dot2);
768  Nonterminal* nt2square1 = new Nonterminal(2,square,1);
769  GrammarSymbolPtr nt2square1Ptr(nt2square1);
770  Nonterminal* nt2square2 = new Nonterminal(2,square,2);
771  GrammarSymbolPtr nt2square2Ptr(nt2square2);
772  Terminal* ta = new Terminal(g.EventIndex("a"));
773  GrammarSymbolPtr taPtr(ta);
774  Terminal* tlambda = new Terminal(g.EventIndex(FAUDES_PD_LAMBDA));
775  GrammarSymbolPtr tlambdaPtr(tlambda);
776 
777 
779  //expected read set
780  std::set<GrammarProduction> read;
781  v.clear();
782  v.push_back(taPtr);
783  v.push_back(nt1dot2Ptr);
784  read.insert(GrammarProduction(*nt2dot2,v));
785 
786  //expected pop set
787  std::set<GrammarProduction> pop;
788  v.clear();
789  v.push_back(tlambdaPtr);
790  pop.insert(GrammarProduction(*nt1dot2,v));
791 
792  //expected push set
793  std::set<GrammarProduction> push;
794  v.clear();
795  v.push_back(nt2dot2Ptr);
796  v.push_back(nt2squarePtr);
797  push.insert(GrammarProduction(*nt1square,v));
798  v.clear();
799  v.push_back(nt2dotPtr);
800  push.insert(GrammarProduction(*nt1square,v));
801 
802  //expected final state set
803  std::set<GrammarProduction> final;
804  v.clear();
805  v.push_back(tlambdaPtr);
806  final.insert(GrammarProduction(*nt2dot,v));
807  v.clear();
808  v.push_back(tlambdaPtr);
809  final.insert(GrammarProduction(*nt2square,v));
810 
811  try{
812 
813  std::set<GrammarProduction> gp = gr.GrammarProductions();
814  std::set<GrammarProduction>::const_iterator gpit;
815  //test for read
816  for(gpit = read.begin(); gpit != read.end(); gpit++){
817  if(gp.erase(*gpit) == 0){
818  std::stringstream errstr;
819  errstr << "grammar production " << gpit->Str() << " , which is generated by a read transition, was expected but not found in the grammar" << std::endl;
820  throw Exception(name, errstr.str(), 1003);
821  }
822  }
823 
824  //test for pop
825  for(gpit = pop.begin(); gpit != pop.end(); gpit++){
826  if(gp.erase(*gpit) == 0){
827  std::stringstream errstr;
828  errstr << "grammar production " << gpit->Str() << " , which is generated by a pop transition, was expected but not found in the grammar" << std::endl;
829  throw Exception(name, errstr.str(), 1003);
830  }
831  }
832 
833  //test for push
834  for(gpit = push.begin(); gpit != push.end(); gpit++){
835  if(gp.erase(*gpit) == 0){
836  std::stringstream errstr;
837  errstr << "grammar production " << gpit->Str() << " , which is generated by a push transition, was expected but not found in the grammar" << std::endl;
838  throw Exception(name, errstr.str(), 1003);
839  }
840  }
841 
842  //test for final states
843  for(gpit = final.begin(); gpit != final.end(); gpit++){
844  if(gp.erase(*gpit) == 0){
845  std::stringstream errstr;
846  errstr << "grammar production " << gpit->Str() << " , which is generated by a final transition, was expected but not found in the grammar" << std::endl;
847  throw Exception(name, errstr.str(), 1003);
848  }
849  }
850 
851  //all productions must have been looked at by now
852  if(gp.size() != 0){
853  std::stringstream errstr;
854  errstr << "the grammar contained" << gp.size() << " more productions than expected" << std::endl;
855  throw Exception(name, errstr.str(), 1003);
856  }
857  }
858  catch(Exception e){
859  }
860 
861  TestEnd(name);
862 }
863 
864 /* *****************
865  * TestRupProductions
866  * *****************/
868  std::string name = "Test Rup Productions";
869  TestStart(name);
870 
871  Grammar gr = TestGrammar3();
872  Grammar rGr = Rup(gr);
873 
874  try{
875 
876  //only one production must have been removed
877  if(gr.GrammarProductions().size() - 1 != rGr.GrammarProductions().size()){
878  std::stringstream errstr;
879  errstr << "size of grammar productions was " << rGr.GrammarProductions().size() << ", but " << gr.GrammarProductions().size() - 1 << " was expected" << std::endl;
880  throw Exception(name, errstr.str(), 1003);
881  }
882 
883  //only the grammar production (2, [dot], 2) -> a(1, [dot]) must have been removed
884  std::vector<Idx> v;
885  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
886  Nonterminal* nt1 = new Nonterminal(2,v,2);
887  GrammarSymbolPtr nt1Ptr(nt1);
888  Nonterminal* nt2 = new Nonterminal(1,v);
889  GrammarSymbolPtr nt2Ptr(nt2);
891  GrammarSymbolPtr tPtr(t);
893  gs.push_back(tPtr);
894  gs.push_back(nt2Ptr);
895  GrammarProduction gp(*nt1,gs);
896 
897  if(gr.GrammarProductions().find(gp) != gr.GrammarProductions().end()){
898  std::stringstream errstr;
899  errstr << "grammar production (2, [dot], 2) -> a(1, [dot]) was present, but was expected to be deleted" << std::endl;
900  throw Exception(name, errstr.str(), 1003);
901  }
902  }
903  catch(Exception e){
904  }
905 
906  TestEnd(name);
907 }
908 
909 /* *****************
910  * TestRupNonterminals
911  * *****************/
913  std::string name = "Test Rup Nonterminals";
914  TestStart(name);
915 
916  Grammar gr = TestGrammar3();
917  Grammar rGr = Rup(gr);
918 
919  try{
920 
921  //only one nonterminal must have been removed
922  if(gr.Nonterminals().size() - 1 != rGr.Nonterminals().size()){
923  std::stringstream errstr;
924  errstr << "size of nonterminals was " << rGr.Nonterminals().size() << ", but " << gr.Nonterminals().size() - 1 << " was expected" << std::endl;
925  throw Exception(name, errstr.str(), 1003);
926  }
927 
928  //only the nonterminal (2, [dot], 2) must have been removed
929  std::vector<Idx> v;
930  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
931  Nonterminal nt(2,v,2);
932  if(gr.Nonterminals().find(nt) != gr.Nonterminals().end()){
933  std::stringstream errstr;
934  errstr << "nonterminal (2, [dot], 2) was present, but was expected to be deleted" << std::endl;
935  throw Exception(name, errstr.str(), 1003);
936  }
937  }
938  catch(Exception e){
939  }
940 
941 
942  TestEnd(name);
943 }
944 
945 /* *****************
946  * TestFilter
947  * *****************/
948 void TestFilter(){
951 }
952 
953 /* *****************
954  * TestRnpp1
955  * *****************/
956 void TestRnpp1(){
959 }
960 
961 /* *****************
962  * TestRnpp1
963  * *****************/
964 void TestRnppl(){
968 }
969 
970 /* *****************
971  * TestRnpp
972  * *****************/
973 void TestRnpp(){
977 }
978 
979 /* *****************
980  * Sp2Lr
981  * *****************/
982 void TestSp2Lr(){
983 
987 }
988 
989 /* *****************
990  * Sp2Lr2
991  * *****************/
992 void TestSp2Lr2(){
994 }
995 
996 /* *****************
997  * Rup
998  * *****************/
999 void TestRup(){
1000 
1003 }
1004 } // namespace faudes
1005 

libFAUDES 2.24g --- 2014.09.15 --- c++ api documentaion by doxygen