src/work/klao/path_test.cc
author ladanyi
Fri, 28 May 2004 07:48:16 +0000
changeset 666 410a1419e86b
parent 369 dc9c19f4ca9a
permissions -rw-r--r--
Added a short tutorial on using graphs.
     1 #include <string>
     2 #include <iostream>
     3 
     4 #include <path.h>
     5 #include <list_graph.h>
     6 
     7 using namespace std;
     8 using namespace hugo;
     9 
    10 bool passed = true;
    11 
    12 void check(bool rc) {
    13   passed = passed && rc;
    14   if(!rc) {
    15     cout << "Test failed!" << endl;
    16   }
    17 }
    18 
    19 #ifdef DEBUG
    20 const bool debug = true;
    21 #else
    22 const bool debug = false;
    23 #endif
    24 
    25 
    26 int main() {
    27 
    28   try {
    29 
    30     typedef ListGraph::Node Node;
    31     typedef ListGraph::Edge Edge;
    32 
    33     ListGraph G;
    34 
    35     Node s=G.addNode();
    36     Node v1=G.addNode();
    37     Node v2=G.addNode();
    38     Node v3=G.addNode();
    39     Node v4=G.addNode();
    40     Node t=G.addNode();
    41   
    42     Edge e1 = G.addEdge(s, v1);
    43     Edge e2 = G.addEdge(s, v2);
    44     Edge e3 = G.addEdge(v1, v2);
    45     Edge e4 = G.addEdge(v2, v1);
    46     Edge e5 = G.addEdge(v1, v3);
    47     Edge e6 = G.addEdge(v3, v2);
    48     Edge e7 = G.addEdge(v2, v4);
    49     Edge e8 = G.addEdge(v4, v3);
    50     Edge e9 = G.addEdge(v3, t);
    51     Edge e10 = G.addEdge(v4, t);
    52 
    53     bool rc;
    54 
    55     {
    56       cout << "\n\n\nDirPath tesztelese...\n";
    57 
    58 
    59       cout << "Ures path letrehozasa" << endl;
    60       typedef DirPath<ListGraph> DPath;
    61       DPath P(G);
    62 
    63       cout << "P.length() == " << P.length() << endl;
    64       check(P.length() == 0);
    65 
    66       cout << "P.from() valid? " << G.valid(P.from()) << endl;
    67       check(! G.valid(P.from()));
    68 
    69       {
    70 	cout << "Builder objektum letrehozasa" << endl;
    71 	DPath::Builder B(P);
    72 
    73 	cout << "Hozzaadunk az elejehez ket elet..." << endl;
    74 	B.pushFront(e6);
    75 	B.pushFront(e5);
    76 	cout << "P.length() == " << P.length() << endl;
    77 	check(P.length() == 0);
    78       
    79 	cout << "Commitolunk..." << endl;
    80 	B.commit();
    81 
    82 	cout << "P.length() == " << P.length() << endl;
    83 	check(P.length() == 2);
    84 	cout << "P.from() valid? " << G.valid(P.from()) << endl;
    85 	check(G.valid(P.from()));
    86 	cout << "P.from()==v1 ? " << (P.from()==v1) << endl;
    87 	check(P.from() == v1);
    88 
    89 	// Na ja, ez igy nem igazi, mindket esetet le kene tesztelni,
    90 	// de legalabb valami:
    91 #ifdef DEBUG
    92 	cout << "Hozzaadunk az elejehez egy nem illeszkedo elet..." << endl;
    93 	rc = false;
    94 	try {
    95 	  B.pushFront(e3);
    96 	}
    97 	catch(const Exception &e) {
    98 	  cout << "E: " << e.what() << endl;
    99 	  rc = true;
   100 	}
   101 	check(rc);
   102 #endif
   103 
   104 	cout << "Hozzaadunk a vegehez ket elet..." << endl;
   105 	B.pushBack(e7);
   106 	B.pushBack(e8);
   107 	cout << "P.length() == " << P.length() << endl;
   108 	check(P.length() == 2);
   109       
   110 	cout << "Es commitolunk...\n";
   111 	B.commit();
   112       }
   113       cout << "P.length() == " << P.length() << endl;
   114       check(P.length() == 4);
   115       cout << "P.to()==v3 ? " << (P.to()==v3) << endl;
   116       check(P.to() == v3);
   117 
   118       cout << "Vegigiteralunk az eleken." << endl;
   119       typedef DPath::NodeIt NodeIt;
   120       typedef DPath::EdgeIt EdgeIt;
   121       EdgeIt e;
   122       int i=1;
   123       for(P.first(e); P.valid(e); P.next(e), ++i) {
   124 	cout << i << ". el: " << e << endl;
   125       }
   126 
   127 
   128       // Na ja, ez igy nem igazi, mindket esetet le kene tesztelni,
   129       // de legalabb valami:
   130       rc = false;
   131       try {
   132 	cout << "Setting an edgeiter to a nonexistant edge." << endl;
   133 	P.nth(e,134);
   134 	rc = !debug;
   135       }
   136       catch(const Exception &e) {
   137 	cout << "E: " << e.what() << endl;
   138 	rc = debug;
   139       }
   140       check(rc);
   141     }
   142 
   143 
   144   }
   145   catch(const std::exception &e) {
   146     cout << "Uncaught exception: " << e.what() << endl;
   147     return 1;
   148   }
   149   catch(...) {
   150     cout << "Something horrible happened: an exception which isn't "
   151 	 << "std::exception" << endl;
   152     return 2;
   153   }
   154 
   155 
   156   cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
   157        << endl;
   158 
   159   return passed ? 0 : 1;
   160 }