pd_alg_first_test.cpp
Go to the documentation of this file.
1 /** @file pd_alg_first_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_first_test.h"
10 
11 namespace faudes {
12 
13 
14 /* *****************
15  * TestPostClPostfixes
16  * *****************/
18  std::string name = "PostCl Postfixes";
19  TestStart(name);
20 
21  //just to populate events and stack symbols
23 
25  GrammarSymbolWordSet wordSet;
26 
27  //create terminals (a) and (lambda)
30  //create nonterminals (1, [dot]) and (2, [square], 1)
31  std::vector<Idx> v;
32  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
33  Nonterminal* nt1dot = new Nonterminal(1,v);
34  v.clear();
35  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
36  Nonterminal* nt2square1 = new Nonterminal(2,v,1);
37  //create word ((2, [square], 1)a(1, [dot]))
38  GrammarSymbolPtr ptrnt2square1(nt2square1);
39  word.push_back(ptrnt2square1);
40  GrammarSymbolPtr ptrta(ta);
41  word.push_back(ptrta);
42  GrammarSymbolPtr ptrnt1dot(nt1dot);
43  word.push_back(ptrnt1dot);
44 
45  wordSet = PostCl(word);
46 
47  try{
48 
49  //the set must contain::
50  GrammarSymbolWordSet expectedWords;
51  GrammarSymbolWordSet::const_iterator wordit;
52 
53  //(2, [square], 1)a(1, [dot])
54  GrammarSymbolVector v2square1a1dot;
55  v2square1a1dot.push_back(ptrnt2square1);
56  v2square1a1dot.push_back(ptrta);
57  v2square1a1dot.push_back(ptrnt1dot);
58  expectedWords.insert(v2square1a1dot);
59  //a(1, [dot])
60  GrammarSymbolVector va1dot;
61  va1dot.push_back(ptrta);
62  va1dot.push_back(ptrnt1dot);
63  expectedWords.insert(va1dot);
64  //(1, [dot])
65  GrammarSymbolVector v1dot;
66  v1dot.push_back(ptrnt1dot);
67  expectedWords.insert(v1dot);
68  //lambda
69  GrammarSymbolVector v1lambda;
70  GrammarSymbolPtr ptrtlambda(tlambda);
71  v1lambda.push_back(ptrtlambda);
72  expectedWords.insert(v1lambda);
73 
74  //the set must contain 4 items
75  if(wordSet.size() != expectedWords.size()){
76  std::stringstream errstr;
77  errstr << "Word set size was " << wordSet.size() << ", but " << expectedWords.size() << " was expected." << std::endl;
78  throw Exception(name, errstr.str(), 1003);
79  }
80 
81  //check for specific items
82  for(wordit = expectedWords.begin(); wordit != expectedWords.end(); wordit++){
83  if(!ContainsWord(wordSet, *wordit)){
84  std::stringstream errstr;
85  errstr << "Word set did not contain the expected word ";
86 
87  GrammarSymbolVector::const_iterator gsit;
88  for(gsit = wordit->begin(); gsit != wordit->end(); gsit++){
89  errstr << (*gsit)->Str();
90  }
91  errstr << std::endl;
92  throw Exception(name, errstr.str(), 1003);
93  }
94  }
95  }
96  catch (Exception e){
97  }
98 
99  TestEnd(name);
100 
101 }
102 
103 /* *****************
104  * TestFdsWords
105  * *****************/
107  std::string name = "Fds Words";
108  TestStart(name);
109 
110  //just to populate events and stack symbols
111  TestGenerator1();
112 
113  Grammar gr = TestGrammar4();
114 
115  GrammarSymbolWordSet wordSet = Fds(gr);
116 
117  try{
118 
119  //create terminals (a) and (lambda)
121  GrammarSymbolPtr ptrta(ta);
123  GrammarSymbolPtr ptrtlambda(tlambda);
124  //create nonterminals (1, [dot]), (1, [dot], 1) and (2,square)
125  std::vector<Idx> v;
126  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
127  Nonterminal* nt1dot = new Nonterminal(1,v);
128  GrammarSymbolPtr ptrnt1dot(nt1dot);
129  v.clear();
130  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
131  Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
132  GrammarSymbolPtr ptrnt1dot1(nt1dot1);
133  v.clear();
134  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
135  Nonterminal* nt2square = new Nonterminal(2,v);
136  GrammarSymbolPtr ptrnt2square(nt2square);
137 
138  //the set must contain::
139  GrammarSymbolWordSet expectedWords;
140  GrammarSymbolWordSet::const_iterator wordit;
141 
142  //(1, [dot])
143  GrammarSymbolVector v1dot;
144  v1dot.push_back(ptrnt1dot);
145  expectedWords.insert(v1dot);
146  //a(1, [dot])
147  GrammarSymbolVector va1dot;
148  va1dot.push_back(ptrta);
149  va1dot.push_back(ptrnt1dot);
150  expectedWords.insert(va1dot);
151  //lambda
152  GrammarSymbolVector v1lambda;
153  v1lambda.push_back(ptrtlambda);
154  expectedWords.insert(v1lambda);
155  //(2, [square])
156  GrammarSymbolVector v2square;
157  v2square.push_back(ptrnt2square);
158  expectedWords.insert(v2square);
159  //a(2, [square])
160  GrammarSymbolVector va2square;
161  va2square.push_back(ptrta);
162  va2square.push_back(ptrnt2square);
163  expectedWords.insert(va2square);
164  //(1, [dot] ,1)a(2, [square])
165  GrammarSymbolVector v1dot1a2square;
166  v1dot1a2square.push_back(ptrnt1dot1);
167  v1dot1a2square.push_back(ptrta);
168  v1dot1a2square.push_back(ptrnt2square);
169  expectedWords.insert(v1dot1a2square);
170 
171 
172  //the set must contain 6 items
173  if(wordSet.size() != expectedWords.size()){
174  std::stringstream errstr;
175  errstr << "Word set size was " << wordSet.size() << ", but " << expectedWords.size() << " was expected." << std::endl;
176  throw Exception(name, errstr.str(), 1003);
177  }
178 
179  //check for specific items
180  for(wordit = expectedWords.begin(); wordit != expectedWords.end(); wordit++){
181  if(!ContainsWord(wordSet, *wordit)){
182  std::stringstream errstr;
183  errstr << "Word set did not contain the expected word ";
184 
185  GrammarSymbolVector::const_iterator gsit;
186  for(gsit = wordit->begin(); gsit != wordit->end(); gsit++){
187  errstr << (*gsit)->Str();
188  }
189  errstr << std::endl;
190  throw Exception(name, errstr.str(), 1003);
191  }
192  }
193 
194  }
195  catch (Exception e){
196  }
197 
198  TestEnd(name);
199 }
200 
201 /* *****************
202  * TestFirstLMap
203  * *****************/
205  std::string name = "FirstL Map";
206  TestStart(name);
207 
208  //just to populate events and stack symbols
209  TestGenerator1();
210 
211  Grammar gr = TestGrammar5();
212 
214 
215  /*
216  * the expected map is as follows:
217  *
218  * lambda -> lambda
219  * b -> b
220  * (1, dot) -> a
221  * (1, square) -> b, lambda
222  * (1, dot, 1) -> a
223  * a(1, square) -> a
224  * (1, dot, 1)a(1, square) -> a
225  * (2, square) -> a, b
226  * (1, square)(1, dot) -> a, b
227  */
228  try{
229 
230  //create terminals (a), (b) and (lambda)
232  GrammarSymbolPtr ptrta(ta);
234  GrammarSymbolPtr ptrtb(tb);
236  GrammarSymbolPtr ptrtlambda(tlambda);
237  //create nonterminals (1, [dot]), (1, [dot], 1), (1, [square]) and (2,[square])
238  std::vector<Idx> v;
239  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
240  Nonterminal* nt1dot = new Nonterminal(1,v);
241  GrammarSymbolPtr ptrnt1dot(nt1dot);
242  v.clear();
243  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
244  Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
245  GrammarSymbolPtr ptrnt1dot1(nt1dot1);
246  v.clear();
247  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
248  Nonterminal* nt1square = new Nonterminal(1,v);
249  GrammarSymbolPtr ptrnt1square(nt1square);
250  v.clear();
251  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
252  Nonterminal* nt2square = new Nonterminal(2,v);
253  GrammarSymbolPtr ptrnt2square(nt2square);
254 
255  //test size of result set and result set contents for each word
256  //lambda
258  gsv.push_back(ptrtlambda);
259  if(f.find(gsv)->second.size() != 1){
260  std::stringstream errstr;
261  errstr << "Word set of word lambda was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
262  throw Exception(name, errstr.str(), 1003);
263  }
264  if(f.find(gsv)->second.find(*tlambda) == f.find(gsv)->second.end()){
265  std::stringstream errstr;
266  errstr << "Word set of word lambda did not contain the expected word lambda." << std::endl;
267  throw Exception(name, errstr.str(), 1003);
268  }
269 
270  //b
271  gsv.clear();
272  gsv.push_back(ptrtb);
273  if(f.find(gsv)->second.size() != 1){
274  std::stringstream errstr;
275  errstr << "Word set of word b was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
276  throw Exception(name, errstr.str(), 1003);
277  }
278  if(f.find(gsv)->second.find(*tb) == f.find(gsv)->second.end()){
279  std::stringstream errstr;
280  errstr << "Word set of word b did not contain the expected word b." << std::endl;
281  throw Exception(name, errstr.str(), 1003);
282  }
283 
284  //(1, dot)
285  gsv.clear();
286  gsv.push_back(ptrnt1dot);
287  if(f.find(gsv)->second.size() != 1){
288  std::stringstream errstr;
289  errstr << "Word set of word (1, dot) was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
290  throw Exception(name, errstr.str(), 1003);
291  }
292  if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
293  std::stringstream errstr;
294  errstr << "Word set of word (1, dot) did not contain the expected word a." << std::endl;
295  throw Exception(name, errstr.str(), 1003);
296  }
297 
298  //(1, square)
299  gsv.clear();
300  gsv.push_back(ptrnt1square);
301  if(f.find(gsv)->second.size() != 2){
302  std::stringstream errstr;
303  errstr << "Word set of word (1, square) was expected to be of size 2, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
304  throw Exception(name, errstr.str(), 1003);
305  }
306  if(f.find(gsv)->second.find(*tb) == f.find(gsv)->second.end()){
307  std::stringstream errstr;
308  errstr << "Word set of word (1, square) did not contain the expected word b." << std::endl;
309  throw Exception(name, errstr.str(), 1003);
310  }
311  if(f.find(gsv)->second.find(*tlambda) == f.find(gsv)->second.end()){
312  std::stringstream errstr;
313  errstr << "Word set of word (1, square) did not contain the expected word lambda." << std::endl;
314  throw Exception(name, errstr.str(), 1003);
315  }
316 
317  //(1, dot, 1)
318  gsv.clear();
319  gsv.push_back(ptrnt1dot1);
320  if(f.find(gsv)->second.size() != 1){
321  std::stringstream errstr;
322  errstr << "Word set of word (1, dot, 1) was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
323  throw Exception(name, errstr.str(), 1003);
324  }
325  if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
326  std::stringstream errstr;
327  errstr << "Word set of word (1, dot, 1) did not contain the expected word a." << std::endl;
328  throw Exception(name, errstr.str(), 1003);
329  }
330 
331  //a(1, square)
332  gsv.clear();
333  gsv.push_back(ptrta);
334  gsv.push_back(ptrnt1square);
335  if(f.find(gsv)->second.size() != 1){
336  std::stringstream errstr;
337  errstr << "Word set of word a(1, square) was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
338  throw Exception(name, errstr.str(), 1003);
339  }
340  if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
341  std::stringstream errstr;
342  errstr << "Word set of word a(1, square) did not contain the expected word a." << std::endl;
343  throw Exception(name, errstr.str(), 1003);
344  }
345 
346  //(1, dot, 1)a(1, square)
347  gsv.clear();
348  gsv.push_back(ptrnt1dot1);
349  gsv.push_back(ptrta);
350  gsv.push_back(ptrnt1square);
351  if(f.find(gsv)->second.size() != 1){
352  std::stringstream errstr;
353  errstr << "Word set of word (1, dot, 1)a(1, square) was expected to be of size 1, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
354  throw Exception(name, errstr.str(), 1003);
355  }
356  if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
357  std::stringstream errstr;
358  errstr << "Word set of word (1, dot, 1)a(1, square) did not contain the expected word a." << std::endl;
359  throw Exception(name, errstr.str(), 1003);
360  }
361 
362  //(2, square)
363  gsv.clear();
364  gsv.push_back(ptrnt2square);
365  if(f.find(gsv)->second.size() != 2){
366  std::stringstream errstr;
367  errstr << "Word set of word (2, square) was expected to be of size 2, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
368  throw Exception(name, errstr.str(), 1003);
369  }
370  if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
371  std::stringstream errstr;
372  errstr << "Word set of word (2, square) did not contain the expected word a." << std::endl;
373  throw Exception(name, errstr.str(), 1003);
374  }
375  if(f.find(gsv)->second.find(*tb) == f.find(gsv)->second.end()){
376  std::stringstream errstr;
377  errstr << "Word set of word (2, square) did not contain the expected word b." << std::endl;
378  throw Exception(name, errstr.str(), 1003);
379  }
380 
381  //(1, square)(1, dot)
382  gsv.clear();
383  gsv.push_back(ptrnt1square);
384  gsv.push_back(ptrnt1dot);
385  if(f.find(gsv)->second.size() != 2){
386  std::stringstream errstr;
387  errstr << "Word set of word (1, square)(1, dot) was expected to be of size 2, but was of size " << f.find(gsv)->second.size() << "." << std::endl;
388  throw Exception(name, errstr.str(), 1003);
389  }
390  if(f.find(gsv)->second.find(*ta) == f.find(gsv)->second.end()){
391  std::stringstream errstr;
392  errstr << "Word set of word (1, square)(1, dot) did not contain the expected word a." << std::endl;
393  throw Exception(name, errstr.str(), 1003);
394  }
395  if(f.find(gsv)->second.find(*tb) == f.find(gsv)->second.end()){
396  std::stringstream errstr;
397  errstr << "Word set of word (1, square)(1, dot) did not contain the expected word b." << std::endl;
398  throw Exception(name, errstr.str(), 1003);
399  }
400  }
401  catch (Exception e){
402  }
403 
404  TestEnd(name);
405 }
406 
407 /* *****************
408  * TestFirstASelectedWords
409  * *****************/
411  std::string name = "FirstA Selected Words";
412  TestStart(name);
413 
414  //just to populate events and stack symbols
415  TestGenerator1();
416 
417  Grammar gr = TestGrammar5();
418 
419  //create terminals (a), (b) and (lambda)
421  GrammarSymbolPtr ptrta(ta);
423  GrammarSymbolPtr ptrtb(tb);
425  GrammarSymbolPtr ptrtlambda(tlambda);
426  //create nonterminals (1, [dot]) (1, [square])
427  std::vector<Idx> v;
428  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
429  Nonterminal* nt1dot = new Nonterminal(1,v);
430  GrammarSymbolPtr ptrnt1dot(nt1dot);
431  v.clear();
432  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
433  Nonterminal* nt1square = new Nonterminal(1,v);
434  GrammarSymbolPtr ptrnt1square(nt1square);
435 
436  //test for lambda -> lambda
437  GrammarSymbolVector word;
438  word.push_back(ptrtlambda);
439  std::set<Terminal> firstSet = FirstA(gr, word);
440  try{
441 
442  if(firstSet.size() != 1){
443  std::stringstream errstr;
444  errstr << "Word set of word lambda was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
445  throw Exception(name, errstr.str(), 1003);
446  }
447  if(firstSet.find(*tlambda) == firstSet.end()){
448  std::stringstream errstr;
449  errstr << "Word set of word lambda did not contain the expected word lambda." << std::endl;
450  throw Exception(name, errstr.str(), 1003);
451  }
452  }
453  catch (Exception e){
454  }
455 
456  //test for (1, square) -> b, lambda
457  word.clear();
458  word.push_back(ptrnt1square);
459  firstSet = FirstA(gr, word);
460  try{
461 
462  if(firstSet.size() != 2){
463  std::stringstream errstr;
464  errstr << "Word set of word (1, square) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
465  throw Exception(name, errstr.str(), 1003);
466  }
467  if(firstSet.find(*tb) == firstSet.end()){
468  std::stringstream errstr;
469  errstr << "Word set of word (1, square) did not contain the expected word b." << std::endl;
470  throw Exception(name, errstr.str(), 1003);
471  }
472  if(firstSet.find(*tlambda) == firstSet.end()){
473  std::stringstream errstr;
474  errstr << "Word set of word (1, square) did not contain the expected word lambda." << std::endl;
475  throw Exception(name, errstr.str(), 1003);
476  }
477  }
478  catch (Exception e){
479  }
480 
481  //test for (1, square)(1, dot) -> a, b
482  word.clear();
483  word.push_back(ptrnt1square);
484  word.push_back(ptrnt1dot);
485  firstSet = FirstA(gr, word);
486  try{
487 
488  if(firstSet.size() != 2){
489  std::stringstream errstr;
490  errstr << "Word set of word (1, square)(1, dot) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
491  throw Exception(name, errstr.str(), 1003);
492  }
493  if(firstSet.find(*ta) == firstSet.end()){
494  std::stringstream errstr;
495  errstr << "Word set of word (1, square)(1, dot) did not contain the expected word a." << std::endl;
496  throw Exception(name, errstr.str(), 1003);
497  }
498  if(firstSet.find(*tb) == firstSet.end()){
499  std::stringstream errstr;
500  errstr << "Word set of word (1, square)(1, dot) did not contain the expected word b." << std::endl;
501  throw Exception(name, errstr.str(), 1003);
502  }
503  }
504  catch (Exception e){
505  }
506 
507  TestEnd(name);
508 }
509 
510 /* *****************
511  * TestFirstSelectedWords
512  * *****************/
514  std::string name = "First Selected Words";
515  TestStart(name);
516 
517  //just to populate events and stack symbols
518  TestGenerator1();
519 
520  Grammar gr = TestGrammar5();
521 
522  //create terminals (a), (b) and (lambda)
524  GrammarSymbolPtr ptrta(ta);
526  GrammarSymbolPtr ptrtb(tb);
528  GrammarSymbolPtr ptrtlambda(tlambda);
529  //create nonterminals (1, [dot]), (1, [square]) and (2, [square])
530  std::vector<Idx> v;
531  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
532  Nonterminal* nt1dot = new Nonterminal(1,v);
533  GrammarSymbolPtr ptrnt1dot(nt1dot);
534  v.clear();
535  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
536  Nonterminal* nt1square = new Nonterminal(1,v);
537  GrammarSymbolPtr ptrnt1square(nt1square);
538  v.clear();
539  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
540  Nonterminal* nt2square = new Nonterminal(2,v);
541  GrammarSymbolPtr ptrnt2square(nt2square);
542 
543  //test for lambda -> lambda
544  GrammarSymbolVector word;
545  word.push_back(ptrtlambda);
546  std::set<Terminal> firstSet = First(gr, word);
547  try{
548 
549  if(firstSet.size() != 1){
550  std::stringstream errstr;
551  errstr << "Word set of word lambda was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
552  throw Exception(name, errstr.str(), 1003);
553  }
554  if(firstSet.find(*tlambda) == firstSet.end()){
555  std::stringstream errstr;
556  errstr << "Word set of word lambda did not contain the expected word lambda." << std::endl;
557  throw Exception(name, errstr.str(), 1003);
558  }
559  }
560  catch (Exception e){
561  }
562 
563  //test for (2, square)(1, dot) -> a, b
564  word.clear();
565  word.push_back(ptrnt2square);
566  word.push_back(ptrnt1dot);
567  firstSet = First(gr, word);
568  try{
569 
570  if(firstSet.size() != 2){
571  std::stringstream errstr;
572  errstr << "Word set of word (2, square)(1, dot) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
573  throw Exception(name, errstr.str(), 1003);
574  }
575  if(firstSet.find(*tb) == firstSet.end()){
576  std::stringstream errstr;
577  errstr << "Word set of word (2, square)(1, dot) did not contain the expected word b." << std::endl;
578  throw Exception(name, errstr.str(), 1003);
579  }
580  if(firstSet.find(*ta) == firstSet.end()){
581  std::stringstream errstr;
582  errstr << "Word set of word (2, square)(1, dot) did not contain the expected word a." << std::endl;
583  throw Exception(name, errstr.str(), 1003);
584  }
585  }
586  catch (Exception e){
587  }
588 
589  //test for (1, square)(1, dot) -> a, b
590  word.clear();
591  word.push_back(ptrnt1square);
592  word.push_back(ptrnt1dot);
593  firstSet = First(gr, word);
594  try{
595 
596  if(firstSet.size() != 2){
597  std::stringstream errstr;
598  errstr << "Word set of word (1, square)(1, dot) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
599  throw Exception(name, errstr.str(), 1003);
600  }
601  if(firstSet.find(*ta) == firstSet.end()){
602  std::stringstream errstr;
603  errstr << "Word set of word (1, square)(1, dot) did not contain the expected word a." << std::endl;
604  throw Exception(name, errstr.str(), 1003);
605  }
606  if(firstSet.find(*tb) == firstSet.end()){
607  std::stringstream errstr;
608  errstr << "Word set of word (1, square)(1, dot) did not contain the expected word b." << std::endl;
609  throw Exception(name, errstr.str(), 1003);
610  }
611  }
612  catch (Exception e){
613  }
614 
615  TestEnd(name);
616 }
617 
618 /* *****************
619  * TestFirstRedConsistentWord
620  * *****************/
622  std::string name = "FirstRed Consistent Word";
623  TestStart(name);
624 
625  //just to populate events and stack symbols
626  TestGenerator1();
627 
628  Grammar gr = TestGrammar5();
629 
630  //create terminals (b), lambda
632  GrammarSymbolPtr ptrtb(tb);
634  GrammarSymbolPtr ptrtlambda(tlambda);
635  //create nonterminal (1, [square])
636  std::vector<Idx> v;
637  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
638  Nonterminal* nt1square = new Nonterminal(1,v);
639  GrammarSymbolPtr ptrnt1square(nt1square);
640 
641 
642  //test for (1, square) -> b, lambda
643  GrammarSymbolVector word;
644  word.push_back(ptrnt1square);
645  std::set<Terminal> firstSet = FirstRed(gr, word);
646  try{
647 
648  if(firstSet.size() != 2){
649  std::stringstream errstr;
650  errstr << "Word set of word (1, square) was expected to be of size 2, but was of size " << firstSet.size() << "." << std::endl;
651  throw Exception(name, errstr.str(), 1003);
652  }
653  if(firstSet.find(*tb) == firstSet.end()){
654  std::stringstream errstr;
655  errstr << "Word set of word (1, square) did not contain the expected word b." << std::endl;
656  throw Exception(name, errstr.str(), 1003);
657  }
658  if(firstSet.find(*tlambda) == firstSet.end()){
659  std::stringstream errstr;
660  errstr << "Word set of word (1, square) did not contain the expected word lambda." << std::endl;
661  throw Exception(name, errstr.str(), 1003);
662  }
663  }
664  catch (Exception e){
665  }
666 
667  TestEnd(name);
668 }
669 
670 /* *****************
671  * TestFirstRedInconsistentWord
672  * *****************/
674  std::string name = "FirstRed Inconsistent Word";
675  TestStart(name);
676 
677  //just to populate events and stack symbols
678  TestGenerator1();
679 
680  Grammar gr = TestGrammar5();
681 
682  //create terminal (a)
684  GrammarSymbolPtr ptrta(ta);
685 
686  //create nonterminal (1, [dot], 2)
687  std::vector<Idx> v;
688  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
689  Nonterminal* nt1dot2 = new Nonterminal(1,v,2);
690  GrammarSymbolPtr ptrnt1dot2(nt1dot2);
691 
692 
693  //test for (1, dot, 2) -> empty
694  GrammarSymbolVector word;
695  word.push_back(ptrnt1dot2);
696  std::set<Terminal> firstSet = FirstRed(gr, word);
697  try{
698 
699  if(!firstSet.empty()){
700  std::stringstream errstr;
701  errstr << "Word set of word (1, dot, 2) was expected to be empty, but was of size " << firstSet.size() << "." << std::endl;
702  throw Exception(name, errstr.str(), 1003);
703  }
704  }
705  catch (Exception e){
706  }
707 
708  TestEnd(name);
709 
710 }
711 
712 /* *****************
713  * TestFilter1MixedGrammarSymbols
714  * *****************/
716 
717  std::string name = "Filter1 Mixed Grammar Symbols";
718  TestStart(name);
719 
720  Grammar gr1 = TestGrammar1();
721 
722  GrammarSymbolVector word;
723  std::set<Terminal>::const_iterator tit;
724  std::vector<TerminalPtr> tpv;
725  std::vector<TerminalPtr>::iterator tpvit;
726 
727  //build word to filter
728  //contains two nonterminal and all terminals
729  Terminal* t;
730  Nonterminal* nt = new Nonterminal(*gr1.Nonterminals().begin());
731  GrammarSymbolPtr ntPtr(nt);
732  word.push_back(ntPtr);
733  word.push_back(ntPtr);
734 
735  for(tit = gr1.Terminals().begin(); tit != gr1.Terminals().end(); tit++){
736  //create new terminal
737  t = new Terminal(*tit);
738  TerminalPtr tPtr(t);
739  //push terminal into word
740  word.push_back(tPtr);
741  }
742 
743  NonterminalPtr rPtr = Filter1(gr1.Nonterminals(),word);
744 
745  try{
746  //resulting nonterminal needs to be the same the nonterminal in the word
747  if(*rPtr != *ntPtr){
748  std::stringstream errstr;
749  errstr << "result nonterminal is " << rPtr->Str() << ", but " << ntPtr->Str() << " was expected" << std::endl;
750  throw Exception(name, errstr.str(), 1003);
751  }
752  }
753  catch(Exception e){
754  }
755 
756  TestEnd(name);
757 }
758 
759 /* *****************
760  * TestFilter1Nothing
761  * *****************/
763  std::string name = "Filter1 Nothing";
764  TestStart(name);
765 
766  std::set<Nonterminal> nt;
768 
769  NonterminalPtr rPtr = Filter1(nt,gs);
770 
771  try{
772  //resulting pointer has to be NULL
773  if(rPtr != NULL){
774  std::stringstream errstr;
775  errstr << "size of result set is no NULL" << std::endl;
776  throw Exception(name, errstr.str(), 1003);
777  }
778  }
779  catch(Exception e){
780  }
781 
782  TestEnd(name);
783 }
784 
785 
786 /* *****************
787  * TestFirstAllTerminalsOnly
788  * *****************/
790  std::string name = "FirstAll Terminals Only";
791  TestStart(name);
792 
793  //just to populate events and stack symbols
794  TestGenerator1();
795 
796  Grammar gr = TestGrammar6();
797 
798  //create terminals (a), (b)
800  GrammarSymbolPtr ptrta(ta);
802  GrammarSymbolPtr ptrtb(tb);
803 
804  //test for ab -> a
805  GrammarSymbolVector word;
806  word.push_back(ptrta);
807  word.push_back(ptrtb);
808  std::set<Terminal> firstSet = FirstAll(gr, word);
809  try{
810 
811  if(firstSet.size() != 1){
812  std::stringstream errstr;
813  errstr << "Word set of word ab was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
814  throw Exception(name, errstr.str(), 1003);
815  }
816  if(firstSet.find(*ta) == firstSet.end()){
817  std::stringstream errstr;
818  errstr << "Word set of word ab did not contain the expected word a." << std::endl;
819  throw Exception(name, errstr.str(), 1003);
820  }
821  }
822  catch (Exception e){
823  }
824 
825  TestEnd(name);
826 }
827 
828 /* *****************
829  * TestFirstAllNonterminalsIrreducibleWord
830  * *****************/
832  std::string name = "FirstAll Nonterminals Irreducible Word";
833  TestStart(name);
834 
835  //just to populate events and stack symbols
836  TestGenerator1();
837 
838  Grammar gr = TestGrammar6();
839 
840  //create nonterminal (1, [square], 1)
841  std::vector<Idx> v;
842  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
843  Nonterminal* nt1square1 = new Nonterminal(1,v,1);
844  GrammarSymbolPtr ptrnt1square1(nt1square1);
845 
846  //test for (1, square, 1) -> empty
847  GrammarSymbolVector word;
848  word.push_back(ptrnt1square1);
849  std::set<Terminal> firstSet = FirstAll(gr, word);
850  try{
851 
852  if(!firstSet.empty()){
853  std::stringstream errstr;
854  errstr << "Word set of word (1, square, 1) was expected to be empty, but was of size " << firstSet.size() << "." << std::endl;
855  throw Exception(name, errstr.str(), 1003);
856  }
857  }
858  catch (Exception e){
859  }
860 
861  TestEnd(name);
862 
863 }
864 
865 /* *****************
866  * TestFirstAllNonterminalsReducibleWord
867  * *****************/
869  std::string name = "FirstAll Nonterminals Reducible Word";
870  TestStart(name);
871 
872  //just to populate events and stack symbols
873  TestGenerator1();
874 
875  Grammar gr = TestGrammar6();
876 
877  //create terminal (a)
879  GrammarSymbolPtr ptrta(ta);
880 
881  //create nonterminals (1, [dot], 1) and (1, [dot])
882  std::vector<Idx> v;
883  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
884  Nonterminal* nt1dot = new Nonterminal(1,v);
885  GrammarSymbolPtr ptrnt1dot(nt1dot);
886  v.clear();
887  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
888  Nonterminal* nt1dot1 = new Nonterminal(1,v,1);
889  GrammarSymbolPtr ptrnt1dot1(nt1dot1);
890 
891  //test for (1, [dot])(1, [dot], 1) -> a
892  GrammarSymbolVector word;
893  word.push_back(ptrnt1dot);
894  word.push_back(ptrnt1dot1);
895  std::set<Terminal> firstSet = FirstAll(gr, word);
896  try{
897 
898  if(firstSet.size() != 1){
899  std::stringstream errstr;
900  errstr << "Word set of word (1, [dot])(1, [dot], 1) was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
901  throw Exception(name, errstr.str(), 1003);
902  }
903  if(firstSet.find(*ta) == firstSet.end()){
904  std::stringstream errstr;
905  errstr << "Word set of word (1, [dot])(1, [dot], 1) did not contain the expected word a." << std::endl;
906  throw Exception(name, errstr.str(), 1003);
907  }
908  }
909  catch (Exception e){
910  }
911 
912  TestEnd(name);
913 }
914 
915 /* *****************
916  * TestFirstLeq1ZeroEmpty
917  * *****************/
919  std::string name = "FirstLeq1 Zero Empty";
920  TestStart(name);
921 
922  //just to populate events and stack symbols
923  TestGenerator1();
924 
925  Grammar gr = TestGrammar6();
926 
927  //create nonterminals (1, [square], 1)
928  std::vector<Idx> v;
929  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("square"));
930  Nonterminal* nt1square1 = new Nonterminal(1,v,1);
931  GrammarSymbolPtr ptrnt1square1(nt1square1);
932 
933  //test for 0, (1, [square], 1) -> empty
934  GrammarSymbolVector word;
935  word.push_back(ptrnt1square1);
936  std::set<Terminal> firstSet = FirstLeq1(gr, 0, word);
937  try{
938 
939  if(!firstSet.empty()){
940  std::stringstream errstr;
941  errstr << "Word set of k = 0 and word (1, [square], 1) was expected to be empty, but was of size " << firstSet.size() << "." << std::endl;
942  throw Exception(name, errstr.str(), 1003);
943  }
944  }
945  catch (Exception e){
946  }
947 
948  TestEnd(name);
949 }
950 
951 /* *****************
952  * TestFirstLeq1ZeroNonempty
953  * *****************/
955  std::string name = "FirstLeq1 Zero Nonempty";
956  TestStart(name);
957 
958  //just to populate events and stack symbols
959  TestGenerator1();
960 
961  Grammar gr = TestGrammar6();
962 
963  //create terminal lambda
965  GrammarSymbolPtr ptrtlambda(tlambda);
966 
967  //create nonterminals (1, [dot])
968  std::vector<Idx> v;
969  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
970  Nonterminal* nt1dot = new Nonterminal(1,v);
971  GrammarSymbolPtr ptrnt1dot(nt1dot);
972 
973  //test for 0, (1, [dot]) -> lambda
974  GrammarSymbolVector word;
975  word.push_back(ptrnt1dot);
976  std::set<Terminal> firstSet = FirstLeq1(gr, 0, word);
977  try{
978 
979  if(firstSet.size() != 1){
980  std::stringstream errstr;
981  errstr << "Word set of k = 0 and word (1, [dot]) was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
982  throw Exception(name, errstr.str(), 1003);
983  }
984  if(firstSet.find(*tlambda) == firstSet.end()){
985  std::stringstream errstr;
986  errstr << "Word set of k = 0 and word (1, [dot]) did not contain the expected word lambda." << std::endl;
987  throw Exception(name, errstr.str(), 1003);
988  }
989  }
990  catch (Exception e){
991  }
992 
993  TestEnd(name);
994 }
995 
996 /* *****************
997  * TestFirstLeq1OneNonempty
998  * *****************/
1000  std::string name = "FirstLeq1 One Nonempty";
1001  TestStart(name);
1002 
1003  //just to populate events and stack symbols
1004  TestGenerator1();
1005 
1006  Grammar gr = TestGrammar6();
1007 
1008  //create terminal a
1010  GrammarSymbolPtr ptrta(ta);
1011 
1012  //create nonterminals (1, [dot])
1013  std::vector<Idx> v;
1014  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
1015  Nonterminal* nt1dot = new Nonterminal(1,v);
1016  GrammarSymbolPtr ptrnt1dot(nt1dot);
1017 
1018  //test for 1, (1, [dot]) -> a
1019  GrammarSymbolVector word;
1020  word.push_back(ptrnt1dot);
1021  std::set<Terminal> firstSet = FirstLeq1(gr, 1, word);
1022  try{
1023 
1024  if(firstSet.size() != 1){
1025  std::stringstream errstr;
1026  errstr << "Word set of k = 1 and word (1, [dot]) was expected to be of size 1, but was of size " << firstSet.size() << "." << std::endl;
1027  throw Exception(name, errstr.str(), 1003);
1028  }
1029  if(firstSet.find(*ta) == firstSet.end()){
1030  std::stringstream errstr;
1031  errstr << "Word set of k = 1 and word (1, [dot]) did not contain the expected word lambda." << std::endl;
1032  throw Exception(name, errstr.str(), 1003);
1033  }
1034  }
1035  catch (Exception e){
1036  }
1037 
1038  TestEnd(name);
1039 }
1040 
1041 /* *****************
1042  * TestFirstLeq1TwoNonempty
1043  * *****************/
1045  std::string name = "FirstLeq1 Two Nonempty";
1046  TestStart(name);
1047 
1048  //just to populate events and stack symbols
1049  TestGenerator1();
1050 
1051  Grammar gr = TestGrammar6();
1052 
1053  //create nonterminals (1, [dot])
1054  std::vector<Idx> v;
1055  v.push_back(PushdownGenerator::GlobalStackSymbolTablep()->Index("dot"));
1056  Nonterminal* nt1dot = new Nonterminal(1,v);
1057  GrammarSymbolPtr ptrnt1dot(nt1dot);
1058 
1059  //test for 2, (1, [dot]) -> empty
1060  GrammarSymbolVector word;
1061  word.push_back(ptrnt1dot);
1062  std::set<Terminal> firstSet = FirstLeq1(gr, 2, word);
1063  try{
1064 
1065  if(!firstSet.empty()){
1066  std::stringstream errstr;
1067  errstr << "Word set of k = 2 and word (1, [dot]) was expected to be empty, but was of size " << firstSet.size() << "." << std::endl;
1068  throw Exception(name, errstr.str(), 1003);
1069  }
1070  }
1071  catch (Exception e){
1072  }
1073 
1074  TestEnd(name);
1075 }
1076 
1077 /* *****************
1078  * TestPostCl
1079  * *****************/
1080 void TestPostCl(){
1082 }
1083 
1084 /* *****************
1085  * TestFds
1086  * *****************/
1087 void TestFds(){
1088  TestFdsWords();
1089 }
1090 
1091 /* *****************
1092  * TestFirstA
1093  * *****************/
1094 void TestFirstA(){
1096 }
1097 
1098 /* *****************
1099  * TestFirstL
1100  * *****************/
1101 void TestFirstL(){
1102  TestFirstLMap();
1103 }
1104 
1105 /* *****************
1106  * TestFirst
1107  * *****************/
1108 void TestFirst(){
1110 }
1111 
1112 
1113 /* *****************
1114  * TestFirstRed
1115  * *****************/
1119 }
1120 
1121 /* *****************
1122  * TestFilter1
1123  * *****************/
1127 }
1128 
1129 /* *****************
1130  * TestFirstAll
1131  * *****************/
1136 }
1137 
1138 /* *****************
1139  * TestFirstLeq1
1140  * *****************/
1146 }
1147 
1148 } // namespace faudes
1149 

libFAUDES 2.26g --- 2015.08.17 --- c++ api documentaion by doxygen