pd_alg_nb_sub_b_test.cpp
Go to the documentation of this file.
1 /** @file pd_alg_nb_sub_b_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_b_test.h"
10 
11 namespace faudes {
12 
13 /* *****************
14  * TestRenqRenaming
15  * *****************/
17  std::string name = "RenQ Renaming";
18  std::cout << "Testing " << name << " ..." << std::endl;
19 
20  std::string renameString = "old";
21 
23  PushdownGenerator g2 = RenQ(renameString,g1);
24 
25  try{
26  StateSet::Iterator its;
27  for(its = g2.StatesBegin(); its != g2.StatesEnd(); its++){
28 
29  const MergeStateAnnotation* msa = dynamic_cast<const MergeStateAnnotation*>(g2.StateAttribute(*its).Merge());
30 
31  if(msa == NULL)
32  throw Exception(name, "MergeStateAnnotation not set.", 1003);
33  if(msa->Annotation().compare(renameString) != 0){
34  std::stringstream errstr;
35  errstr << "Annotation incorrect, was " << msa->Annotation() << ", but " << renameString << " was expected." << std::endl;
36  throw Exception(name, errstr.str(), 1003);
37  }
38  }
39  }
40  catch (Exception e){
41  }
42  std::cout << "Finished " << name << std::endl;
43 }
44 
45 /* *****************
46  * TestRenQNumberOfStates
47  * *****************/
49  std::string name = "RenQ Number of States";
50  std::cout << "Testing " << name << " ..." << std::endl;
51 
52  std::string renameString = "old";
53 
55  PushdownGenerator g2 = RenQ(renameString,g1);
56 
57  try{
58  if(g1.Size() != g2.Size()){
59  std::stringstream errstr;
60  errstr << "Incorrect, number of states, was " << g1.Size() << ", but " << g2.Size() << " was expected." << std::endl;
61  throw Exception(name, errstr.str(), 1003);
62  }
63  }
64  catch (Exception e){
65  }
66  std::cout << "Finished " << name << std::endl;
67 }
68 
69 /* *****************
70  * TestRenGRenamingStackSymbols
71  * *****************/
73  std::string name = "RenG Renaming Stack Symbols";
74  std::cout << "Testing " << name << " ..." <<std::endl;
75 
76  std::string renameString = "old";
77 
79  PushdownGenerator g2 = RenG(renameString,g1);
80 
81  try{
82  StackSymbolSet::Iterator it;
83  for(it = g1.StackSymbolsBegin(); it != g1.StackSymbolsEnd(); it++){
84  //lambda does not get renamed so test for not getting renamed instead
85  if(g2.IsStackSymbolLambda(*it)){
86  if(!g2.StackSymbols().Exists(g2.StackSymbolName(*it))){
87  std::stringstream errstr;
88  errstr << "Annotation incorrect, " << g2.StackSymbolName(*it) << " was expected, but not found in the stack symbol list:\n" ;
89  StackSymbolSet::Iterator iterr;
90  for(iterr = g2.StackSymbolsBegin(); iterr != g2.StackSymbolsEnd(); iterr++){
91  errstr << " " << g2.StackSymbolName(*iterr);
92  }
93  errstr << std::endl;
94  throw Exception(name, errstr.str(), 1003);
95  }
96  }
97  //all other symbols must get renamed
98  else{
99  if(!g2.StackSymbols().Exists(renameString + "-" + g2.StackSymbolName(*it))){
100  std::stringstream errstr;
101  errstr << "Annotation incorrect, " << renameString << "-" << g2.StackSymbolName(*it) << " was expected, but not found in the stack symbol list:\n" ;
102  StackSymbolSet::Iterator iterr;
103  for(iterr = g2.StackSymbolsBegin(); iterr != g2.StackSymbolsEnd(); iterr++){
104  errstr << " " << g2.StackSymbolName(*iterr);
105  }
106  errstr << std::endl;
107  throw Exception(name, errstr.str(), 1003);
108  }
109  }
110 
111  }
112  }
113  catch(Exception e){
114  }
115  std::cout << "Finished " << name << std::endl;
116 }
117 
118 /* *****************
119  * TestRenGNumberOfStackSymbols
120  * *****************/
122  std::string name = "RenG Number of Stack Symbols";
123  std::cout << "Testing " << name << " ..." << std::endl;
124 
125  std::string renameString = "old";
126 
128  PushdownGenerator g2 = RenG(renameString,g1);
129 
130  try{
131  if(g1.StackSymbols().Size() != g2.StackSymbols().Size()){
132  std::stringstream errstr;
133  errstr << "Incorrect number of stack symbols, was " << g1.StackSymbols().Size() << ", but " << g2.StackSymbols().Size() << " was expected." << std::endl;
134  throw Exception(name, errstr.str(), 1003);
135  }
136  }
137  catch (Exception e){
138  }
139  std::cout << "Finished " << name << std::endl;
140 }
141 
142 /* *****************
143  * TestRenGRenamingTransitions
144  * *****************/
146 
147  FAUDES_TEST_DUMP("test 77",true)
148 
149  std::string name = "RenG Renaming Transitions";
150  std::cout << "Testing " << name << " ..." << std::endl;
151 
152  std::string renameString = "old";
153 
155  PushdownGenerator g2 = RenG(renameString,g1);
156 
157  TransSet::Iterator tit;
158  std::vector<Idx> oldPush, oldPop, push, pop;
159  std::vector<Idx>::const_iterator pushit, popit;
160  PopPushSet popPush;
161  PopPushSet::const_iterator ppit;
162 
163  try{
164  for(tit = g2.TransRelBegin(); tit != g2.TransRelEnd(); tit++){
165  //examine all pop/push pairs
166  popPush = g2.PopPush(*tit);
167  for(ppit = popPush.begin(); ppit != popPush.end(); ppit++){
168 
169  //test pop stack symbols
170  oldPop = ppit->first;
171  for(popit = oldPop.begin(); popit != oldPop.end(); popit++){
172 
173  //everything but lambda must be renamed
174  if(!g2.IsStackSymbolLambda(*popit)){
175  std::string annotation = g2.StackSymbolName(*popit).substr(0, renameString.size() + 1);
176  if(annotation.compare(renameString + "-") != 0){
177  std::stringstream errstr;
178  errstr << "Annotation incorrect, " << renameString << "- was expected, but symbol was " << g2.StackSymbolName(*popit) << std::endl;
179  throw Exception(name, errstr.str(), 1003);
180  }
181  }
182  }
183 
184  //test push stack symbols
185  oldPush = ppit->second;
186  for(pushit = oldPush.begin(); pushit != oldPush.end(); pushit++){
187 
188  //everything but lambda must be renamed
189  if(!g2.IsStackSymbolLambda(*pushit)){
190  std::string annotation = g2.StackSymbolName(*pushit).substr(0, renameString.size() + 1);
191  if(annotation.compare(renameString + "-") != 0){
192  std::stringstream errstr;
193  errstr << "Annotation incorrect, " << renameString << "- was expected, but symbol was " << g2.StackSymbolName(*pushit) << std::endl;
194  throw Exception(name, errstr.str(), 1003);
195  }
196  }
197  }
198  }
199 
200  }
201  }
202  catch(Exception e){
203  FAUDES_TEST_DUMP("test 77 ERROR ",false)
204  }
205  std::cout << "Finished " << name << std::endl;
206 }
207 
208 /* *****************
209  * TestRep0NoLambdaPop
210  * *****************/
212  std::string name = "Rep0 No Lambda Pop";
213  std::cout << "Testing " << name << " ..." << std::endl;
214 
216  PushdownGenerator g2 = Rep0(g1);
217 
218  try{
219 
220  //iterate over all transitions
221  TransSet::Iterator tit;
222  PopPushSet::const_iterator ppsit;
223  for(tit = g2.TransRel().Begin(); tit != g2.TransRel().End(); tit++){
224 
225  //get all PopPush pairs, extract pop (first), extract foremost
226  //stack symbol (front), and test for lambda
227  for(ppsit = g2.PopPush(*tit).begin(); ppsit != g2.PopPush(*tit).end(); ppsit++){
228  if(g2.IsStackSymbolLambda(ppsit->first.front())){
229  std::stringstream errstr;
230  errstr << "Lambda popping edges not removed, Lambda pop found in transition" << g2.TransRel().Str(*tit) << std::endl;
231  throw Exception(name, errstr.str(), 1003);
232  }
233  }
234  }
235  }
236  catch(Exception e){
237  }
238  std::cout << "Finished " << name << std::endl;
239 }
240 
241 /* *****************
242  * TestRep0AllExpectingTransition
243  * *****************/
245  std::string name = "Rep0 All Expecting Transition";
246  std::cout << "Testing " << name << " ..." << std::endl;
247 
249  PushdownGenerator g2 = Rep0(g1);
250 
251  //test g1 for lambda transition
252  TransSet::Iterator tit;
253  PopPushSet::const_iterator ppsit;
254  bool hasLambda;
255  for(tit = g1.TransRel().Begin(); tit != g1.TransRel().End(); tit++){
256 
257  //get all PopPush pairs, extract pop (first), extract foremost
258  //stack symbol (front), and test for lambda
259  for(ppsit = g1.PopPush(*tit).begin(); ppsit != g1.PopPush(*tit).end(); ppsit++){
260  if(g2.IsStackSymbolLambda(ppsit->first.front())){
261  hasLambda = true;
262  //lambda was found, no need to search further
263  break;
264  }
265  }
266  }
267 
268  if(!hasLambda){
269  std::cout << "warning: cannot perform test, because there is no lambda pop in the original generator" << std::endl;
270  return;
271  }
272 
273  //test g2 for transition (s1,ev,s2,u,uw), where u is every stack symbol
274  //except for lambda and bottom
275  try{
276 
277  //iterate over all transitions
278  std::vector<Idx> pop, push;
279  std::vector<Idx>::const_iterator popit, pushit;
280  StackSymbolSet::Iterator ssit;
281  bool transFound, symbolFound;
282  for(tit = g2.TransRel().Begin(); tit != g2.TransRel().End(); tit++){
283  transFound = true;
284 
285  //iterate over all relevant stack symbols and see if there is one PopPush
286  //pair with pop u and push wu
287  for(ssit = g2.StackSymbols().Begin(); ssit != g2.StackSymbols().End(); ssit++){
288 
289  //lambda and stack bottom are not relevant
290  if (*ssit == g2.StackBottom() || g2.IsStackSymbolLambda(*ssit)) continue;
291 
292  //test PopPush pairs and find at least one (u,uw) PopPush pair per symbol
293  symbolFound = false;
294  for(ppsit = g2.PopPush(*tit).begin(); ppsit != g2.PopPush(*tit).end(); ppsit++){
295 
296  pop = ppsit->first;
297  push = ppsit->second;
298  //if front of pop or push are identical, the pair is found
299  if(pop.front() == *ssit && *push.rbegin() == *ssit){
300  symbolFound = true;
301  break;
302  }
303  }
304  //if any one symbol has not been found, this transition is not relevant
305  if(!symbolFound){
306  transFound = false;
307  break;
308  }
309  }
310  //no need to look for another such transition
311  if(transFound) break;
312  }
313 
314  if(!transFound){
315  std::stringstream errstr;
316  errstr << "Original generator has lambda popping edge, but result generator has no edge that accepts all stack symbols" << std::endl;
317  throw Exception(name, errstr.str(), 1003);
318  }
319  }
320  catch(Exception e){
321  }
322  std::cout << "Finished " << name << std::endl;
323 }
324 
325 /* ****************
326  * TestRep2NumberOfTransitions
327  * *****************/
329 
330  std::string name = "Rep2 Number of States and Transitions";
331  TestStart(name);
332 
334 
335  //expected resulst for this particular test generator
336  Idx expectedNumberTransitions = 7;
337  Idx expectedNumberStates = 6;
338 
339  PushdownGenerator g2 = Rep2(g1);
340 
341  try{
342 
343  //test for number of states
344  if(g2.Size() != expectedNumberStates){
345  std::stringstream errstr;
346  errstr << "Number of states was " << g2.Size() << ", but " << expectedNumberStates << " was expected." << std::endl;
347  throw Exception(name, errstr.str(), 1003);
348  }
349 
350  //test for number of transitions
351  if(g2.TransRelSize() != expectedNumberTransitions){
352  std::stringstream errstr;
353  errstr << "Number of transitions incorrect" << g2.TransRelSize() << ", but " << expectedNumberTransitions << " was expected." << std::endl;
354  throw Exception(name, errstr.str(), 1003);
355  }
356  }
357  catch (Exception e){
358  }
359 
360  TestEnd(name);
361 }
362 
363 /* *****************
364  * TestRep2Renaming
365  * *****************/
367  std::string name = "Rep2 Renaming";
368  TestStart(name);
369 
370  std::string renameString = "old";
371 
373 
374  PushdownGenerator g2 = Rep2(g1);
375 
376  try{
377  StateSet::Iterator stateit;
378  for(stateit = g2.StatesBegin(); stateit != g2.StatesEnd(); stateit++){
379 
380  const MergeStateAnnotation* msa = dynamic_cast<const MergeStateAnnotation*>(g2.StateAttribute(*stateit).Merge());
381 
382  if(msa == NULL)
383  throw Exception(name, "MergeStateAnnotation not set.", 1003);
384  if(msa->Annotation().compare(renameString) != 0){
385  std::stringstream errstr;
386  errstr << "Annotation incorrect, was " << msa->Annotation() << ", but " << renameString << " was expected." << std::endl;
387  throw Exception(name, errstr.str(), 1003);
388  }
389  }
390  }
391  catch (Exception e){
392  }
393  TestEnd(name);
394 }
395 
396 /* ****************
397  * TestRppReadPopPushOnly
398  * *****************/
400  //TODO dont know what to expect
401 // std::string name = "Rpp Number States Transitions";
402 // TestStart(name);
403 //
404 // PushdownGenerator g1 = TestGenerator6();
405 //
406 // PushdownGenerator g2 = Rpp(g1);
407 // try{
408 // //number of states must be 7
409 // if(g2.Size() != 7){
410 // std::stringstream errstr;
411 // errstr << "Number of states was expected to be 7, but was " << g2.Size() << "." << std::endl;
412 // throw Exception(name, errstr.str(), 1003);
413 // }
414 //
415 // TransSet::Iterator transit;
416 // PopPushSet::const_iterator ppit;
417 // //number of transitions must be 10
418 // int transCount = 0;
419 // for(transit = g2.TransRelBegin(); transit != g2.TransRelEnd(); transit++){
420 // transCount += g2.PopPush(*transit).size();
421 // }
422 //
423 // if(transCount != 10){
424 // std::stringstream errstr;
425 // errstr << "Number of transitions was expected to be 10, but was " << transCount << "." << std::endl;
426 // throw Exception(name, errstr.str(), 1003);
427 // }
428 // }
429 // catch (Exception e){
430 // }
431 // TestEnd(name);
432 }
433 
434 /* ****************
435  * TestRppReadPopPushOnly
436  * *****************/
438  std::string name = "Rpp Read Pop Push Only";
439  TestStart(name);
440 
442 
443  PushdownGenerator g2 = Rpp(g1);
444  try{
445  TransSet::Iterator transit;
446  PopPushSet::const_iterator ppit;
447  std::vector<Idx> pop, push;
448  std::vector<Idx>::const_iterator ssit;
449  for(transit = g2.TransRelBegin(); transit != g2.TransRelEnd(); transit++){
450  for(ppit = g2.PopPushBegin(*transit); ppit != g2.PopPushEnd(*transit); ppit++){
451 
452  //read pop and push for convenience
453  pop = ppit->first;
454  push = ppit->second;
455 
456  //check for read only transition
457  if(!g2.IsEventLambda(transit->Ev) &&
458  pop == push){
459  continue;
460  }
461  //check for pop only transition
462  else if(g2.IsEventLambda(transit->Ev) &&
463  !g2.IsStackSymbolLambda(pop.front()) &&
464  pop.size() == 1 &&
465  g2.IsStackSymbolLambda(push.front())){
466  continue;
467  }
468  //check for push only transition
469  else if(g2.IsEventLambda(transit->Ev) &&
470  push.size() == 2 &&
471  pop.size() == 1 &&
472  pop.front() == push.back()){
473  continue;
474  }
475  //error
476  else{
477  std::stringstream errstr;
478  errstr << "Transition (" << transit->X1 << ", " << g2.EventName(transit->Ev) << ", " << transit->X2 << ") with pop [";
479  for(ssit = pop.begin(); ssit != pop.end(); ssit++){
480  errstr << " " << g2.StackSymbolName(*ssit);
481  }
482  errstr << "] and push [";
483  for(ssit = push.begin(); ssit != push.end(); ssit++){
484  errstr << " " << g2.StackSymbolName(*ssit);
485  }
486  errstr << "] was neither read nor pop nor push." << std::endl;
487  throw Exception(name, errstr.str(), 1003);
488  }
489  }
490  }
491  }
492  catch (Exception e){
493  }
494  TestEnd(name);
495 }
496 
497 /* ****************
498  * TestNdaActivePassive
499  * *****************/
501  std::string name = "Nda Active Passive";
502  TestStart(name);
503 
505  PushdownGenerator g2 = Nda(g1);
506 
507  try{
508  //the number of transitions must have doubled
509  if(2*g1.States().Size() != g2.States().Size()){
510  std::stringstream errstr;
511  errstr << "Number of states incorrect, was" << g2.States().Size() << ", but " << 2*g1.States().Size() << " was expected." << std::endl;
512  throw Exception(name, errstr.str(), 1003);
513  }
514 
515  StateSet::Iterator stateit;
516  int active = 0;
517  int passive = 0;
518  //there must be an equal amount of active and passive states
519  for(stateit = g2.StatesBegin(); stateit != g2.StatesEnd(); stateit++){
520 
521  const MergeStateAnnotation* msa = dynamic_cast<const MergeStateAnnotation*>(g2.StateAttribute(*stateit).Merge());
522 
523  if(msa == NULL)
524  throw Exception(name, "MergeStateAnnotation not set.", 1003);
525 
526  if(msa->Annotation().compare("active") == 0){
527  active++;
528  }
529  else if(msa->Annotation().compare("passive") == 0){
530  passive++;
531  }
532  else{
533  std::stringstream errstr;
534  errstr << "Annotation incorrect, was " << msa->Annotation() << ", but either active or passive was expected." << std::endl;
535  throw Exception(name, errstr.str(), 1003);
536  }
537  }
538  if(active != passive){
539  std::stringstream errstr;
540  errstr << "There were " << active << " active states and " << passive << " passive states, but equal numbers were expected." << std::endl;
541  throw Exception(name, errstr.str(), 1003);
542  }
543  }
544  catch (Exception e){
545  }
546 
547  TestEnd(name);
548 }
549 
550 /* ****************
551  * TestNdaTransitions
552  * *****************/
554  std::string name = "Nda Transitions";
555  TestStart(name);
556 
558  PushdownGenerator g2 = Nda(g1);
559 
560  //put together expected transitions for later comparison
561  std::set<std::pair<Idx,Idx> > expectedTransitions;
562  expectedTransitions.insert(std::make_pair(1,3));
563  expectedTransitions.insert(std::make_pair(1,5));
564  expectedTransitions.insert(std::make_pair(2,3));
565  expectedTransitions.insert(std::make_pair(2,6));
566  expectedTransitions.insert(std::make_pair(3,8));
567  expectedTransitions.insert(std::make_pair(4,8));
568  expectedTransitions.insert(std::make_pair(5,3));
569  expectedTransitions.insert(std::make_pair(6,4));
570  expectedTransitions.insert(std::make_pair(7,6));
571  expectedTransitions.insert(std::make_pair(8,6));
572 
573  try{
574  TransSet::Iterator transit;
575  for(transit = g2.TransRelBegin(); transit != g2.TransRelEnd(); transit++){
576 
577  if(expectedTransitions.erase(std::make_pair(transit->X1, transit->X2)) == 0){
578  std::stringstream errstr;
579  errstr << "Transition from state " << transit->X1 << " to state " << transit->X2 << " found, but was not expected." << std::endl;
580  throw Exception(name, errstr.str(), 1003);
581  }
582  }
583 
584  if(expectedTransitions.size() != 0){
585  std::stringstream errstr;
586  errstr << "Not all expected transitions were found." << std::endl;
587  throw Exception(name, errstr.str(), 1003);
588  }
589  }
590  catch (Exception e){
591  }
592 
593  TestEnd(name);
594 }
595 
596 /* *****************
597  * TestRenQ
598  * *****************/
599 void TestRenQ(){
601  //TestRenQNumberOfStates();
602 }
603 
604 /* *****************
605  * TestRenG
606  * *****************/
607 void TestRenG(){
611 }
612 
613 /* *****************
614  * TestRep0
615  * *****************/
616 void TestRep0(){
619 }
620 
621 /* ****************
622  * TestRpp
623  * *****************/
624 void TestRpp(){
627 }
628 
629 /* *****************
630  * TestRep2
631  * *****************/
632 void TestRep2(){
633 
636 }
637 
638 /* *****************
639  * TestNda
640  * *****************/
641 void TestNda(){
642 
645 }
646 
647 } // namespace faudes
648 

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