src/test/unionfind_test.cc
author alpar
Tue, 06 Jul 2004 10:07:48 +0000
changeset 694 2d87cefb35b2
parent 483 ce29ae5b2e1b
child 774 4297098d9677
permissions -rw-r--r--
I moved run() into the body of class Dijkstra, because Doxygen handles
external member function definitions very poorly.
     1 #include <iostream>
     2 
     3 #include <hugo/maps.h>
     4 #include <hugo/unionfind.h>
     5 
     6 using namespace hugo;
     7 using namespace std;
     8 
     9 bool passed = true;
    10 
    11 void check(bool rc) {
    12   passed = passed && rc;
    13   if(!rc) {
    14     cout << "Test failed!" << endl;
    15   }
    16 }
    17 
    18 template <typename T>
    19 class BaseMap : public StdMap<int,T> {};
    20 
    21 typedef UnionFindEnum<int, BaseMap> UFE;
    22 
    23 void print(UFE const &ufe) {
    24   UFE::ClassIt cit;
    25   cout << "printing the classes of the structure:" << endl;
    26   int i = 1;
    27   for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) {
    28     cout << "  " << i << " (" << cit << "):" << flush;
    29     UFE::ItemIt iit;
    30     for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) {
    31       cout << " " << iit << flush;
    32     }
    33     cout << endl;
    34     i++;
    35   }
    36   cout << "done" << endl;
    37 }
    38 
    39 
    40 int main() {
    41   UFE::MapType base;
    42   UFE U(base);
    43 
    44   print(U);
    45 
    46   cout << "Inserting 1..." << endl;
    47   U.insert(1);
    48   print(U);
    49   
    50   cout << "Inserting 2..." << endl;
    51   U.insert(2);
    52   print(U);
    53   
    54   cout << "Joining 1 and 2..." << endl;
    55   check(U.join(1,2));
    56   print(U);
    57 
    58   cout << "Inserting 3, 4, 5, 6, 7..." << endl;
    59   U.insert(3);
    60   U.insert(4);
    61   U.insert(5);
    62   U.insert(6);
    63   U.insert(7);
    64   print (U);
    65 
    66   cout << "Joining 1 - 4, 2 - 4 and 3 - 5 ..." << endl;
    67   check(U.join(1,4));
    68   check(!U.join(2,4));
    69   check(U.join(3,5));
    70   print(U);
    71 
    72   cout << "Inserting 8 to the component of 5 ..." << endl;
    73   U.insert(8,5);
    74   print(U);
    75 
    76   cout << "size of the class of 4: " << U.size(4) << endl;
    77   check(U.size(4) == 3);
    78   cout << "size of the class of 5: " << U.size(5) << endl;
    79   check(U.size(5) == 3);
    80   cout << "size of the class of 6: " << U.size(6) << endl;
    81   check(U.size(6) == 1);
    82   cout << "size of the class of 2: " << U.size(2) << endl;
    83   check(U.size(2) == 3);
    84 
    85   cout << "Inserting 9 ..." << endl;
    86   U.insert(9);
    87   print(U);
    88   cout << "Inserting 10 to the component of 9 ..." << endl;
    89   U.insert(10,9);
    90   print(U);
    91 
    92   cout << "Joining 8 and 10..." << endl;
    93   check(U.join(8,10));
    94   print(U);
    95 
    96   cout << "Move 9 to the class of 4 ..." << endl;
    97   check(U.move(9,4));
    98   print(U);
    99 
   100   cout << "Move 9 to the class of 2 ..." << endl;
   101   check(!U.move(9,2));
   102   print(U);
   103 
   104   cout << "size of the class of 4: " << U.size(4) << endl;
   105   check(U.size(4) == 4);
   106   cout << "size of the class of 9: " << U.size(9) << endl;
   107   check(U.size(9) == 4);
   108   
   109   cout << "Move 5 to the class of 6 ..." << endl;
   110   check(U.move(5,6));
   111   print(U);
   112 
   113   cout << "size of the class of 5: " << U.size(5) << endl;
   114   check(U.size(5) == 2);
   115   cout << "size of the class of 8: " << U.size(8) << endl;
   116   check(U.size(8) == 3);
   117 
   118   cout << "Move 7 to the class of 10 ..." << endl;
   119   check(U.move(7,10));
   120   print(U);
   121 
   122   cout << "size of the class of 7: " << U.size(7) << endl;
   123   check(U.size(7) == 4);
   124 
   125   cout <<"erase 9: " << endl;
   126   U.erase(9);
   127   print(U);
   128 
   129   cout <<"erase 1: " << endl;
   130   U.erase(1);
   131   print(U);
   132 
   133   cout << "size of the class of 4: " << U.size(4) << endl;
   134   check(U.size(4) == 2);
   135   cout << "size of the class of 2: " << U.size(2) << endl;
   136   check(U.size(2) == 2);
   137 
   138 
   139   cout <<"erase 1: " << endl;
   140   U.erase(1);
   141   print(U);
   142 
   143   cout <<"erase 6: " << endl;
   144   U.erase(6);
   145   print(U);
   146 
   147   cout << "split the class of 8: " << endl;
   148   U.split(8);
   149   print(U);
   150 
   151 
   152   cout << "size of the class of 4: " << U.size(4) << endl;
   153   check(U.size(4) == 2);
   154   cout << "size of the class of 3: " << U.size(3) << endl;
   155   check(U.size(3) == 1);
   156   cout << "size of the class of 2: " << U.size(2) << endl;
   157   check(U.size(2) == 2);
   158 
   159 
   160   cout << "Joining 3 - 4 and 2 - 4 ..." << endl;
   161   check(U.join(3,4));
   162   check(!U.join(2,4));
   163   print(U);
   164 
   165 
   166   cout << "size of the class of 4: " << U.size(4) << endl;
   167   check(U.size(4) == 3);
   168   cout << "size of the class of 3: " << U.size(3) << endl;
   169   check(U.size(3) == 3);
   170   cout << "size of the class of 2: " << U.size(2) << endl;
   171   check(U.size(2) == 3);
   172 
   173   cout << "makeRep(4)" << endl;
   174   U.makeRep(4);
   175   print(U);
   176   cout << "makeRep(3)" << endl;
   177   U.makeRep(3);
   178   print(U);
   179   cout << "makeRep(2)" << endl;
   180   U.makeRep(2);
   181   print(U);
   182 
   183   cout << "size of the class of 4: " << U.size(4) << endl;
   184   check(U.size(4) == 3);
   185   cout << "size of the class of 3: " << U.size(3) << endl;
   186   check(U.size(3) == 3);
   187   cout << "size of the class of 2: " << U.size(2) << endl;
   188   check(U.size(2) == 3);
   189 
   190 
   191   cout << "eraseClass 4 ..." << endl;
   192   U.eraseClass(4);
   193   print(U);
   194 
   195   cout << "eraseClass 7 ..." << endl;
   196   U.eraseClass(7);
   197   print(U);
   198 
   199   cout << "done" << endl;
   200 
   201   cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
   202        << endl;
   203 
   204   return passed ? 0 : 1;
   205 }