pd_alg_lrm_test.cpp
Go to the documentation of this file.
1 /** @file pd_alg_lrm_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_lrm_test.h"
10 
11 namespace faudes {
12 
13  /* *****************
14  * TestDesc11Terminal
15  * *****************/
17  std::string name = "Desc11 Terminal";
18  TestStart(name);
19 
20  //just to populate events and stack symbols
22 
23  Grammar gr = TestGrammar5();
24 
25  //create terminals
27  GrammarSymbolPtr ptrta(ta);
29  GrammarSymbolPtr ptrtlambda(tlambda);
30 
31  //create nonterminals (1, [dot])
32  std::vector<Idx> v;
33  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
34  Nonterminal* nt1dot = new Nonterminal(1,v);
35  GrammarSymbolPtr ptrnt1dot(nt1dot);
36 
37  //test for ((1, [dot]) -> lambda . a, lambda)
38  // ((1, [dot]) -> lambda . a, lambda)
39  GrammarSymbolVector beforeDot, afterDot;
40  beforeDot.push_back(ptrtlambda);
41  afterDot.push_back(ptrta);
42  Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tlambda);
43 
44  std::set<Lr1Configuration> descSet = Desc11(gr, 1, config);
45  try{
46 
47  if(descSet.size() != 1){
48  std::stringstream errstr;
49  errstr << "descendant set of (" << config.Str() << ") was expected to be of size 1, but was of size " << descSet.size() << "." << std::endl;
50  throw Exception(name, errstr.str(), 1003);
51  }
52 
53  if(descSet.find(config) == descSet.end()){
54  std::stringstream errstr;
55  errstr << "descendant set of (" << config.Str() << ") was expected to contain the configuration (" << config.Str() << "), but the configuration was not found." << std::endl;
56  throw Exception(name, errstr.str(), 1003);
57  }
58  }
59  catch (Exception e){
60  }
61 
62  TestEnd(name);
63 }
64 
65 /* *****************
66  * TestDesc11Nonterminal
67  * *****************/
69  std::string name = "Desc11 Nonterminal";
70  TestStart(name);
71 
72  //just to populate events and stack symbols
74 
75  Grammar gr = TestGrammar5();
76 
77  //create terminals
79  GrammarSymbolPtr ptrta(ta);
81  GrammarSymbolPtr ptrtb(tb);
83  GrammarSymbolPtr ptrtlambda(tlambda);
84 
85  //create nonterminals (1, [dot])
86  std::vector<Idx> v;
87  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
88  Nonterminal* nt1dot = new Nonterminal(1,v);
89  GrammarSymbolPtr ptrnt1dot(nt1dot);
90  v.clear();
91  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
92  Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
93  GrammarSymbolPtr ptrnt1dot1(nt1dot1);
94  v.clear();
95  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
96  Nonterminal* nt1square = new Nonterminal(1,v);
97  GrammarSymbolPtr ptrnt1square(nt1square);
98 
99  //test for ((1, [dot]) -> lambda . (1, [dot], 1), b
100  // ((1, [dot]) -> lambda . (1, [dot], 1), b
101  // (1, [dot], 1) -> lambda . (1, [dot]), b
102  // (1, [dot], 1) -> lambda . a(1, [square]), b
103  GrammarSymbolVector beforeDot, afterDot;
104  beforeDot.push_back(ptrtlambda);
105  afterDot.push_back(ptrnt1dot1);
106  Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tb);
107 
108  beforeDot.clear();
109  afterDot.clear();
110  beforeDot.push_back(ptrtlambda);
111  afterDot.push_back(ptrnt1dot);
112  Lr1Configuration descConfig1(*nt1dot1, beforeDot, afterDot, *tb);
113 
114  beforeDot.clear();
115  afterDot.clear();
116  beforeDot.push_back(ptrtlambda);
117  afterDot.push_back(ptrta);
118  afterDot.push_back(ptrnt1square);
119  Lr1Configuration descConfig2(*nt1dot1, beforeDot, afterDot, *tb);
120 
121  std::set<Lr1Configuration> descSet = Desc11(gr, 1, config);
122  try{
123 
124  if(descSet.size() != 3){
125  std::stringstream errstr;
126  errstr << "descendant set of (" << config.Str() << ") was expected to be of size 3, but was of size " << descSet.size() << "." << std::endl;
127  throw Exception(name, errstr.str(), 1003);
128  }
129 
130  if(descSet.find(config) == descSet.end()){
131  std::stringstream errstr;
132  errstr << "descendant set of (" << config.Str() << ") was expected to contain the configuration (" << config.Str() << "), but the configuration was not found." << std::endl;
133  throw Exception(name, errstr.str(), 1003);
134  }
135 
136  if(descSet.find(descConfig1) == descSet.end()){
137  std::stringstream errstr;
138  errstr << "descendant set of (" << config.Str() << ") was expected to contain the configuration (" << descConfig1.Str() << "), but the configuration was not found." << std::endl;
139  throw Exception(name, errstr.str(), 1003);
140  }
141 
142  if(descSet.find(descConfig2) == descSet.end()){
143  std::stringstream errstr;
144  errstr << "descendant set of (" << config.Str() << ") was expected to contain the configuration (" << descConfig2.Str() << "), but the configuration was not found." << std::endl;
145  throw Exception(name, errstr.str(), 1003);
146  }
147  }
148  catch (Exception e){
149  }
150 
151  TestEnd(name);
152 }
153 
154 /* *****************
155  * TestDescSelectedConfigs
156  * *****************/
158  std::string name = "Desc SelectedConfigs";
159  TestStart(name);
160 
161  //just to populate events and stack symbols
162  TestGenerator1();
163 
164  Grammar gr = TestGrammar5();
165 
166  //create terminals
168  GrammarSymbolPtr ptrta(ta);
170  GrammarSymbolPtr ptrtb(tb);
172  GrammarSymbolPtr ptrtlambda(tlambda);
173 
174  //create nonterminals
175  std::vector<Idx> v;
176  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
177  Nonterminal* nt1dot = new Nonterminal(1,v);
178  GrammarSymbolPtr ptrnt1dot(nt1dot);
179  v.clear();
180  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
181  Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
182  GrammarSymbolPtr ptrnt1dot1(nt1dot1);
183  v.clear();
184  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
185  Nonterminal* nt1square = new Nonterminal(1,v);
186  GrammarSymbolPtr ptrnt1square(nt1square);
187 
188  //test for (1, [dot]) -> lambda . (1, [dot], 1)a(1, [square]), b
189  //and (1, [dot]) -> a . a, a
190  //and (1, [dot]) -> lambda . lambda , b
191  // (1, [dot], 1) -> lambda . (1, [dot]), a
192  // (1, [dot], 1) -> lambda . a(1, [square]), a
193  // ((1, [dot]) -> lambda . (1, [dot], 1)a(1, [dot]), a
194  GrammarSymbolVector beforeDot, afterDot;
195  beforeDot.push_back(ptrtlambda);
196  afterDot.push_back(ptrnt1dot1);
197  afterDot.push_back(ptrta);
198  afterDot.push_back(ptrnt1square);
199  Lr1Configuration config1(*nt1dot, beforeDot, afterDot, *tb);
200 
201  beforeDot.clear();
202  afterDot.clear();
203  beforeDot.push_back(ptrta);
204  afterDot.push_back(ptrta);
205  Lr1Configuration config2(*nt1dot, beforeDot, afterDot, *ta);
206 
207  beforeDot.clear();
208  afterDot.clear();
209  beforeDot.push_back(ptrtlambda);
210  afterDot.push_back(ptrtlambda);
211  Lr1Configuration config3(*nt1dot, beforeDot, afterDot, *tb);
212 
213  beforeDot.clear();
214  afterDot.clear();
215  beforeDot.push_back(ptrtlambda);
216  afterDot.push_back(ptrnt1dot);
217  Lr1Configuration descConfig1(*nt1dot1, beforeDot, afterDot, *ta);
218 
219  beforeDot.clear();
220  afterDot.clear();
221  beforeDot.push_back(ptrtlambda);
222  afterDot.push_back(ptrta);
223  afterDot.push_back(ptrnt1square);
224  Lr1Configuration descConfig2(*nt1dot1, beforeDot, afterDot, *ta);
225 
226  beforeDot.clear();
227  afterDot.clear();
228  beforeDot.push_back(ptrtlambda);
229  afterDot.push_back(ptrnt1dot1);
230  afterDot.push_back(ptrta);
231  afterDot.push_back(ptrnt1square);
232  Lr1Configuration descConfig3(*nt1dot, beforeDot, afterDot, *ta);
233 
234 
235  std::set<Lr1Configuration> configs;
236  configs.insert(config1);
237  configs.insert(config2);
238  configs.insert(config3);
239  ;
240  std::set<Lr1Configuration> descSet = Desc(gr, 1, configs);
241 
242  try{
243 
244  if(descSet.size() != 6){
245  std::stringstream errstr;
246  errstr << "descendant set was expected to be of size 5, but was of size " << descSet.size() << "." << std::endl;
247  throw Exception(name, errstr.str(), 1003);
248  }
249 
250  if(descSet.find(config1) == descSet.end()){
251  std::stringstream errstr;
252  errstr << "descendant set was expected to contain the configuration (" << config1.Str() << "), but the configuration was not found." << std::endl;
253  throw Exception(name, errstr.str(), 1003);
254  }
255 
256  if(descSet.find(config2) == descSet.end()){
257  std::stringstream errstr;
258  errstr << "descendant set was expected to contain the configuration (" << config2.Str() << "), but the configuration was not found." << std::endl;
259  throw Exception(name, errstr.str(), 1003);
260  }
261 
262  if(descSet.find(config3) == descSet.end()){
263  std::stringstream errstr;
264  errstr << "descendant set was expected to contain the configuration (" << config3.Str() << "), but the configuration was not found." << std::endl;
265  throw Exception(name, errstr.str(), 1003);
266  }
267 
268  if(descSet.find(descConfig1) == descSet.end()){
269  std::stringstream errstr;
270  errstr << "descendant set was expected to contain the configuration (" << descConfig1.Str() << "), but the configuration was not found." << std::endl;
271  throw Exception(name, errstr.str(), 1003);
272  }
273 
274  if(descSet.find(descConfig2) == descSet.end()){
275  std::stringstream errstr;
276  errstr << "descendant set was expected to contain the configuration (" << descConfig2.Str() << "), but the configuration was not found." << std::endl;
277  throw Exception(name, errstr.str(), 1003);
278  }
279 
280  if(descSet.find(descConfig3) == descSet.end()){
281  std::stringstream errstr;
282  errstr << "descendant set was expected to contain the configuration (" << descConfig3.Str() << "), but the configuration was not found." << std::endl;
283  throw Exception(name, errstr.str(), 1003);
284  }
285  }
286  catch (Exception e){
287  }
288 
289  TestEnd(name);
290 }
291 
292 /* *****************
293  * TestPassesXNonterminal
294  * *****************/
296  std::string name = "PassesX Nonterminal";
297  TestStart(name);
298 
299  //just to populate events and stack symbols
300  TestGenerator1();
301 
302  Grammar gr = TestGrammar5();
303 
304  //create terminals
306  GrammarSymbolPtr ptrta(ta);
308  GrammarSymbolPtr ptrtb(tb);
309 
310  //create nonterminals
311  std::vector<Idx> v;
312  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
313  Nonterminal* nt1dot = new Nonterminal(1,v);
314  GrammarSymbolPtr ptrnt1dot(nt1dot);
315 
316  v.clear();
317  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
318  Nonterminal* nt1square = new Nonterminal(1,v);
319  GrammarSymbolPtr ptrnt1square(nt1square);
320 
321  //test for ((1, [dot]) -> a . (1, [square])b, b)
322  // ((1, [dot]) -> a(1, [square]) . b, b)
323  GrammarSymbolVector beforeDot, afterDot;
324  beforeDot.push_back(ptrta);
325  afterDot.push_back(ptrnt1square);
326  afterDot.push_back(ptrtb);
327  Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tb);
328 
329  beforeDot.clear();
330  afterDot.clear();
331  beforeDot.push_back(ptrta);
332  beforeDot.push_back(ptrnt1square);
333  afterDot.push_back(ptrtb);
334  Lr1Configuration expectedConfig(*nt1dot, beforeDot, afterDot, *tb);
335 
336  std::set<Lr1Configuration> shiftSet = PassesX(config, ptrnt1square);
337  try{
338 
339  if(shiftSet.size() != 1){
340  std::stringstream errstr;
341  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;
342  throw Exception(name, errstr.str(), 1003);
343  }
344 
345  if(shiftSet.find(expectedConfig) == shiftSet.end()){
346  std::stringstream errstr;
347  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;
348  throw Exception(name, errstr.str(), 1003);
349  }
350  }
351  catch (Exception e){
352  }
353 
354  TestEnd(name);
355 }
356 
357 /* *****************
358  * TestPassesXTerminal
359  * *****************/
361  std::string name = "PassesX Terminal";
362  TestStart(name);
363 
364  //just to populate events and stack symbols
365  TestGenerator1();
366 
367  Grammar gr = TestGrammar5();
368 
369  //create terminals
371  GrammarSymbolPtr ptrta(ta);
373  GrammarSymbolPtr ptrtb(tb);
375  GrammarSymbolPtr ptrtlambda(tlambda);
376 
377  //create nonterminals
378  std::vector<Idx> v;
379  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
380  Nonterminal* nt1dot = new Nonterminal(1,v);
381  GrammarSymbolPtr ptrnt1dot(nt1dot);
382 
383  //test for ((1, [dot]) -> lambda . a, b)
384  // ((1, [dot]) -> a . lambda , b)
385  GrammarSymbolVector beforeDot, afterDot;
386  beforeDot.push_back(ptrtlambda);
387  afterDot.push_back(ptrta);
388  Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tb);
389 
390  beforeDot.clear();
391  afterDot.clear();
392  beforeDot.push_back(ptrta);
393  afterDot.push_back(ptrtlambda);
394  Lr1Configuration expectedConfig(*nt1dot, beforeDot, afterDot, *tb);
395 
396  std::set<Lr1Configuration> shiftSet = PassesX(config, ptrta);
397  try{
398 
399  if(shiftSet.size() != 1){
400  std::stringstream errstr;
401  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;
402  throw Exception(name, errstr.str(), 1003);
403  }
404 
405  if(shiftSet.find(expectedConfig) == shiftSet.end()){
406  std::stringstream errstr;
407  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;
408  throw Exception(name, errstr.str(), 1003);
409  }
410  }
411  catch (Exception e){
412  }
413 
414  TestEnd(name);
415 }
416 
417 /* *****************
418  * TestPassesXNoShift
419  * *****************/
421  std::string name = "PassesX No Shift";
422  TestStart(name);
423 
424  //just to populate events and stack symbols
425  TestGenerator1();
426 
427  Grammar gr = TestGrammar5();
428 
429  //create terminals
431  GrammarSymbolPtr ptrta(ta);
433  GrammarSymbolPtr ptrtb(tb);
435  GrammarSymbolPtr ptrtlambda(tlambda);
436 
437  //create nonterminals
438  std::vector<Idx> v;
439  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
440  Nonterminal* nt1dot = new Nonterminal(1,v);
441  GrammarSymbolPtr ptrnt1dot(nt1dot);
442 
443  v.clear();
444  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
445  Nonterminal* nt1square = new Nonterminal(1,v);
446  GrammarSymbolPtr ptrnt1square(nt1square);
447 
448  //test for ((1, [dot]) -> a . lambda , b)
449  // empty
450  GrammarSymbolVector beforeDot, afterDot;
451  beforeDot.push_back(ptrta);
452  afterDot.push_back(ptrtlambda);
453  Lr1Configuration config1(*nt1dot, beforeDot, afterDot, *tb);
454 
455  std::set<Lr1Configuration> shiftSet = PassesX(config1, ptrnt1square);
456  try{
457 
458  if(!shiftSet.empty()){
459  std::stringstream errstr;
460  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;
461  throw Exception(name, errstr.str(), 1003);
462  }
463  }
464  catch (Exception e){
465  }
466 
467  //test for ((1, [dot]) -> a . (1, [dot]), b)
468  // empty
469  beforeDot.clear();
470  afterDot.clear();
471  beforeDot.push_back(ptrta);
472  afterDot.push_back(ptrnt1dot);
473  Lr1Configuration config2(*nt1dot, beforeDot, afterDot, *tb);
474 
475  shiftSet = PassesX(config2, ptrnt1square);
476  try{
477 
478  if(!shiftSet.empty()){
479  std::stringstream errstr;
480  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;
481  throw Exception(name, errstr.str(), 1003);
482  }
483  }
484  catch (Exception e){
485  }
486 
487  TestEnd(name);
488 }
489 
490 /* *****************
491  * TestLrm1FindOne
492  * *****************/
494  std::string name = "Lrm1 Find One";
495  TestStart(name);
496 
497  Grammar gr = TestGrammar7();
498 
499  //create terminals
501  GrammarSymbolPtr ptrta(ta);
503  GrammarSymbolPtr ptrtb(tb);
504  Terminal* tdollar = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("$"));
505  GrammarSymbolPtr ptrtdollar(tdollar);
507  GrammarSymbolPtr ptrtlambda(tlambda);
508 
509  //create nonterminals
510  std::vector<Idx> v;
511  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
512  Nonterminal* nt1dot = new Nonterminal(1,v);
513  GrammarSymbolPtr ptrnt1dot(nt1dot);
514 
515  v.clear();
516  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
517  Nonterminal* nt1square = new Nonterminal(1,v);
518  GrammarSymbolPtr ptrnt1square(nt1square);
519 
520  //test for ((1, [dot]) -> lambda . $(1, [square])$, lambda)
521  // $: ((1, [dot]) -> $ . (1, [square])$, lambda)
522  // ((1, [square]) -> lambda . a(1, [square]), $)
523  // ((1, [square]) -> lambda . b, $)
524  GrammarSymbolVector beforeDot, afterDot;
525  beforeDot.push_back(ptrtlambda);
526  afterDot.push_back(ptrtdollar);
527  afterDot.push_back(ptrnt1square);
528  afterDot.push_back(ptrtdollar);
529  Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tlambda);
530 
531  std::set<Lr1Configuration> expectedConfigs;
532 
533  beforeDot.clear();
534  afterDot.clear();
535  beforeDot.push_back(ptrtdollar);
536  afterDot.push_back(ptrnt1square);
537  afterDot.push_back(ptrtdollar);
538  expectedConfigs.insert(Lr1Configuration(*nt1dot, beforeDot, afterDot, *tlambda));
539 
540  beforeDot.clear();
541  afterDot.clear();
542  beforeDot.push_back(ptrtlambda);
543  afterDot.push_back(ptrta);
544  afterDot.push_back(ptrnt1square);
545  expectedConfigs.insert(Lr1Configuration(*nt1square, beforeDot, afterDot, *tdollar));
546 
547  beforeDot.clear();
548  afterDot.clear();
549  beforeDot.push_back(ptrtlambda);
550  afterDot.push_back(ptrtb);
551  expectedConfigs.insert(Lr1Configuration(*nt1square, beforeDot, afterDot, *tdollar));
552 
553  std::set<Lr1Configuration> configSet;
554  configSet.insert(config);
555 
556  Lr1ConfigurationSetSet configSetSet;
557  configSetSet.insert(configSet);
558 
559  LrmTransitionMap transitionMap = Lrm1(gr, 1, configSetSet);
560  try{
561 
562  //size of the transition map must be one
563  if(transitionMap.size() != 1){
564  std::stringstream errstr;
565  errstr << "number of successor config sets of config set (" << ConfigSetToStr(configSet) << ") was expected to be 1, but there were " << transitionMap.size() << "." << std::endl;
566  throw Exception(name, errstr.str(), 1003);
567  }
568 
569  //the symbol must be dollar
570  if(*transitionMap.begin()->first.second != *ptrtdollar){
571  std::stringstream errstr;
572  errstr << "event to transition out of config set (" << ConfigSetToStr(configSet) << ") was expected $, but was not found." << std::endl;
573  throw Exception(name, errstr.str(), 1003);
574  }
575 
576  //the size of the successor set must be three
577  if(transitionMap.begin()->second.size() != 3){
578  std::stringstream errstr;
579  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;
580  throw Exception(name, errstr.str(), 1003);
581  }
582 
583  std::set<Lr1Configuration>::const_iterator configit;
584  //the three expected configs must be in the successor set
585  for(configit = expectedConfigs.begin(); configit != expectedConfigs.end(); configit++){
586  if(transitionMap.begin()->second.find(*configit) == transitionMap.begin()->second.end()){
587  std::stringstream errstr;
588  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;
589  throw Exception(name, errstr.str(), 1003);
590  }
591  }
592  }
593  catch (Exception e){
594  }
595 
596  TestEnd(name);
597 }
598 
599 /* *****************
600  * TestLrm1FindThree
601  * *****************/
603  std::string name = "Lrm1 Find Three";
604  TestStart(name);
605 
606  Grammar gr = TestGrammar7();
607 
608  //create terminals
610  GrammarSymbolPtr ptrta(ta);
612  GrammarSymbolPtr ptrtb(tb);
613  Terminal* tdollar = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("$"));
614  GrammarSymbolPtr ptrtdollar(tdollar);
616  GrammarSymbolPtr ptrtlambda(tlambda);
617 
618  //create nonterminals
619  std::vector<Idx> v;
620  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
621  Nonterminal* nt1dot = new Nonterminal(1,v);
622  GrammarSymbolPtr ptrnt1dot(nt1dot);
623 
624  v.clear();
625  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
626  Nonterminal* nt1square = new Nonterminal(1,v);
627  GrammarSymbolPtr ptrnt1square(nt1square);
628 
629  //test for ((1, [dot]) -> $ lambda . (1, [square])$, lambda)
630  // ((1, [square]) -> lambda . a(1, [square]), $)
631  // ((1, [square]) -> lambda . b, $)
632  // (1, [square]):
633  // (1, [dot]) -> $(1, [square]) . $, lambda)
634  // a:
635  // ((1, [square]) -> a . (1, [square]), $)
636  // ((1, [square]) -> lambda . a(1, [square]), $)
637  // ((1, [square]) -> lambda . b, $)
638  // b:
639  // (1, [square]) -> b . lambda, $
640  GrammarSymbolVector beforeDot, afterDot;
641  beforeDot.push_back(ptrtlambda);
642  afterDot.push_back(ptrtdollar);
643  afterDot.push_back(ptrnt1square);
644  afterDot.push_back(ptrtdollar);
645  Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tlambda);
646 
647  std::set<Lr1Configuration> configSet;
648 
649  beforeDot.clear();
650  afterDot.clear();
651  beforeDot.push_back(ptrtdollar);
652  afterDot.push_back(ptrnt1square);
653  afterDot.push_back(ptrtdollar);
654  configSet.insert(Lr1Configuration(*nt1dot, beforeDot, afterDot, *tlambda));
655 
656  beforeDot.clear();
657  afterDot.clear();
658  beforeDot.push_back(ptrtlambda);
659  afterDot.push_back(ptrta);
660  afterDot.push_back(ptrnt1square);
661  configSet.insert(Lr1Configuration(*nt1square, beforeDot, afterDot, *tdollar));
662 
663  beforeDot.clear();
664  afterDot.clear();
665  beforeDot.push_back(ptrtlambda);
666  afterDot.push_back(ptrtb);
667  configSet.insert(Lr1Configuration(*nt1square, beforeDot, afterDot, *tdollar));
668 
669  Lr1ConfigurationSetSet configSetSet;
670  configSetSet.insert(configSet);
671 
672  LrmTransitionMap transitionMap = Lrm1(gr, 1, configSetSet);
673 
674  //std::cout << TransitionMapToStr(transitionMap) << std::endl;
675  try{
676 
677  //size of the transition map must be three
678  if(transitionMap.size() != 3){
679  std::stringstream errstr;
680  errstr << "number of successor config sets of config set (" << ConfigSetToStr(configSet) << ") was expected to be 3, but there were " << transitionMap.size() << "." << std::endl;
681  throw Exception(name, errstr.str(), 1003);
682  }
683  }
684  catch (Exception e){
685  }
686 
687  TestEnd(name);
688 }
689 
690 /* *****************
691  * TestLrmLoopAnB
692  * *****************/
694  std::string name = "LrmLoop a^n b";
695  TestStart(name);
696 
697  Grammar gr = TestGrammar7();
698 
699  //create terminals
701  GrammarSymbolPtr ptrta(ta);
703  GrammarSymbolPtr ptrtb(tb);
704  Terminal* tdollar = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("$"));
705  GrammarSymbolPtr ptrtdollar(tdollar);
707  GrammarSymbolPtr ptrtlambda(tlambda);
708 
709  //create nonterminals
710  std::vector<Idx> v;
711  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
712  Nonterminal* nt1dot = new Nonterminal(1,v);
713  GrammarSymbolPtr ptrnt1dot(nt1dot);
714 
715  v.clear();
716  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
717  Nonterminal* nt1square = new Nonterminal(1,v);
718  GrammarSymbolPtr ptrnt1square(nt1square);
719 
720  //test with start state ((1, [dot]) -> lambda . $(1, [square])$, lambda)
721  GrammarSymbolVector beforeDot, afterDot;
722  beforeDot.push_back(ptrtlambda);
723  afterDot.push_back(ptrtdollar);
724  afterDot.push_back(ptrnt1square);
725  afterDot.push_back(ptrtdollar);
726  Lr1Configuration config(*nt1dot, beforeDot, afterDot, *tlambda);
727 
728  std::set<Lr1Configuration> configSet;
729  configSet.insert(config);
730 
731  Lr1ConfigurationSetSet configSetSet;
732  configSetSet.insert(configSet);
733 
734  std::pair<LrmTransitionMap,Lr1ConfigurationSetSet> transitionStates = LrmLoop(gr, 1, LrmTransitionMap(), Lr1ConfigurationSetSet(), configSetSet);
735 
736  LrmTransitionMap transitionMap = transitionStates.first;
737  Lr1ConfigurationSetSet states = transitionStates.second;
738  try{
739 
740  //size of the transition map must be eight
741  if(transitionMap.size() != 8){
742  std::stringstream errstr;
743  errstr << "number of transitions was expected to be 8, but was " << transitionMap.size() << "." << std::endl;
744  throw Exception(name, errstr.str(), 1003);
745  }
746 
747  //the number of states must be seven
748  if(states.size() != 7){
749  std::stringstream errstr;
750  errstr << "number of states was expected to be 7, but there were " << states.size() << "." << std::endl;
751  throw Exception(name, errstr.str(), 1003);
752  }
753  }
754  catch (Exception e){
755  }
756  TestEnd(name);
757 }
758 
759 /* *****************
760  * TestDescInitialFindTwo
761  * *****************/
763  std::string name = "DescInitial Find Two";
764  TestStart(name);
765 
766  Grammar gr = TestGrammar3();
767 
768  //create terminals
770  GrammarSymbolPtr ptrta(ta);
772  GrammarSymbolPtr ptrtb(tb);
774  GrammarSymbolPtr ptrtlambda(tlambda);
775 
776  //create nonterminals
777  std::vector<Idx> v;
778  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
779  Nonterminal* nt1dot = new Nonterminal(1,v);
780  GrammarSymbolPtr ptrnt1dot(nt1dot);
781 
782  v.clear();
783  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
784  Nonterminal* nt2square = new Nonterminal(2,v);
785  GrammarSymbolPtr ptrnt2square(nt2square);
786 
787  //create expected configurations
788  GrammarSymbolVector beforeDot, afterDot;
789  beforeDot.push_back(ptrtlambda);
790  afterDot.push_back(ptrta);
791  afterDot.push_back(ptrnt1dot);
792  Lr1Configuration config1(*nt1dot, beforeDot, afterDot, *tlambda);
793 
794  beforeDot.clear();
795  afterDot.clear();
796  beforeDot.push_back(ptrtlambda);
797  afterDot.push_back(ptrtb);
798  afterDot.push_back(ptrnt2square);
799  Lr1Configuration config2(*nt1dot, beforeDot, afterDot, *tlambda);
800 
801  std::set<Lr1Configuration> expectedConfigSet;
802  expectedConfigSet.insert(config1);
803  expectedConfigSet.insert(config2);
804 
805  std::set<Lr1Configuration> configSet = DescInitial(gr);
806 
807  try{
808 
809  //size of the config set must be two
810  if(configSet.size() != expectedConfigSet.size()){
811  std::stringstream errstr;
812  errstr << "number of configurations was expected to be " << expectedConfigSet.size() << ", but was " << configSet.size() << "." << std::endl;
813  throw Exception(name, errstr.str(), 1003);
814  }
815 
816  std::set<Lr1Configuration>::const_iterator configit;
817  //the expected configurations must be in the config set
818  for(configit = expectedConfigSet.begin(); configit != expectedConfigSet.end(); configit++){
819  if(configSet.find(*configit) == configSet.end()){
820  std::stringstream errstr;
821  errstr << "configurations " << configit->Str() << " was expected to be in the initial configuration set, but was not found" << std::endl;
822  throw Exception(name, errstr.str(), 1003);
823  }
824  }
825  }
826  catch (Exception e){
827  }
828  TestEnd(name);
829 }
830 
831 /* *****************
832  * TestLrmGr1
833  * *****************/
834 void TestLrmGr1(){
835  std::string name = "Lrm Gr1";
836  TestStart(name);
837 
838  Grammar gr = TestGrammar7();
839 
840  GotoGenerator gotoGen = Lrm(gr, 1);
841 
842  try{
843 
844  //number of transitions must be eight
845  if(gotoGen.TransRelSize() != 8){
846  std::stringstream errstr;
847  errstr << "number of transitions was expected to be 8, but was " << gotoGen.TransRelSize() << "." << std::endl;
848  throw Exception(name, errstr.str(), 1003);
849  }
850 
851  //the number of states must be seven
852  if(gotoGen.Size()!= 7){
853  std::stringstream errstr;
854  errstr << "number of states was expected to be 7, but there were " << gotoGen.Size() << "." << std::endl;
855  throw Exception(name, errstr.str(), 1003);
856  }
857  }
858  catch (Exception e){
859  }
860  TestEnd(name);
861 }
862 
863 /* *****************
864  * TestLrmGr2
865  * *****************/
866 void TestLrmGr2(){
867  //TODO dont know what to expect as a result
868 // std::string name = "Lrm Gr2";
869 // TestStart(name);
870 //
871 // Grammar gr = TestGrammar8();
872 //
873 // GotoMachine gotoMachine = Lrm(gr, 1);
874 //
875 // LrmTransitionMap transitions = gotoMachine.Transitions();
876 // Lr1ConfigurationSetSet states = gotoMachine.States();
877 //
878 // std::cout << "transitions: " << transitions.size() << ", states: " << states.size() << std::endl;
879 //
880 // std::cout << ConfigSetSetToStr(states) << std::endl;
881 // std::cout << TransitionMapToStr(transitions) << std::endl;
882 //
883 // try{
884 //
885 // //size of the transition map must be 31
886 // if(transitions.size() != 31){
887 // std::stringstream errstr;
888 // errstr << "number of transitions was expected to be 31, but was " << transitions.size() << "." << std::endl;
889 // throw Exception(name, errstr.str(), 1003);
890 // }
891 //
892 // //the number of states must be 25
893 // if(states.size() != 25){
894 // std::stringstream errstr;
895 // errstr << "number of states was expected to be 25, but there were " << states.size() << "." << std::endl;
896 // throw Exception(name, errstr.str(), 1003);
897 // }
898 // }
899 // catch (Exception e){
900 // }
901 // TestEnd(name);
902 }
903 
904 /* *****************
905  * TestAugSuccess
906  * *****************/
908  std::string name = "Aug Success";
909  TestStart(name);
910 
911  Grammar gr = TestGrammar6();
912 
913  //create terminals
914  PushdownGenerator gen;
915  gen.InsEvent("$");
916  Terminal* tdollar = new Terminal(PushdownGenerator::GlobalEventSymbolTablep()->Index("$"));
917  GrammarSymbolPtr ptrtdollar(tdollar);
918 
919  //create nonterminals
920  std::vector<Idx> v;
921  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
922  Nonterminal* nt1dot = new Nonterminal(1,v);
923  GrammarSymbolPtr ptrnt1dot(nt1dot);
924  v.clear();
925  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
926  Nonterminal* nt0dot = new Nonterminal(0,v);
927  GrammarSymbolPtr ptrnt0dot(nt0dot);
928 
929  //create grammar production
931  rhs.push_back(ptrtdollar);
932  rhs.push_back(ptrnt1dot);
933  rhs.push_back(ptrtdollar);
934  GrammarProduction gp(*nt0dot,rhs);
935 
936  Grammar rGr = Aug(gr, *nt0dot, *tdollar);
937 
938  try{
939 
940  //the must be one grammar production more in the new grammar
941  if(gr.GrammarProductions().size() + 1 != rGr.GrammarProductions().size()){
942  std::stringstream errstr;
943  errstr << "number of grammar productions was expected to be " << gr.GrammarProductions().size() + 1 << ", but was " << rGr.GrammarProductions().size() << "." << std::endl;
944  throw Exception(name, errstr.str(), 1003);
945  }
946 
947  std::set<GrammarProduction>::const_iterator gpit;
948  //there must be one production with the start symbol as left-hand side
949  int i = 0;
950  for(gpit = rGr.GrammarProductionsBegin(); gpit != rGr.GrammarProductionsEnd(); gpit++){
951  if(gpit->Lhs() == *nt0dot){
952  i++;
953  }
954  }
955  if(i != 1){
956  std::stringstream errstr;
957  errstr << "number of grammar productions with " << nt0dot->Str() << " as right-hand side was expected to be 1, but was " << i << "." << std::endl;
958  throw Exception(name, errstr.str(), 1003);
959  }
960 
961  //the new start production must be in the grammar's productions
962  if(gr.GrammarProductions().find(gp) == rGr.GrammarProductionsEnd()){
963  std::stringstream errstr;
964  errstr << "grammar production " << gp.Str() << " was expected to be in the grammar, but was not found." << std::endl;
965  throw Exception(name, errstr.str(), 1003);
966  }
967  }
968  catch (Exception e){
969  }
970  TestEnd(name);
971 }
972 
973 /* *****************
974  * TestDesc11
975  * *****************/
976 void TestDesc11(){
979 }
980 
981 /* *****************
982  * TestDesc
983  * *****************/
984 void TestDesc(){
986 }
987 
988 /* *****************
989  * TestPassesX
990  * *****************/
991 void TestPassesX(){
995 }
996 
997 /* *****************
998  * TestLrm1
999  * *****************/
1000 void TestLrm1(){
1001  TestLrm1FindOne();
1003 }
1004 
1005 /* *****************
1006  * TestLrmLoop
1007  * *****************/
1009  TestLrmLoopAnB();
1010 }
1011 
1012 /* *****************
1013  * TestDescInitial
1014  * *****************/
1017 }
1018 
1019 /* *****************
1020  * TestDescInitial
1021  * *****************/
1022 void TestLrm(){
1023  TestLrmGr1();
1024  //TestLrmGr2();
1025 }
1026 
1027 /* *****************
1028  * TestAug
1029  * *****************/
1030 void TestAug(){
1031  TestAugSuccess();
1032 }
1033 
1034 } // namespace faudes
1035 

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