src/work/johanna/unionfind_test.cc
author alpar
Mon, 26 Apr 2004 09:00:12 +0000
changeset 406 e8377ac921b6
child 462 0ab31578af67
permissions -rw-r--r--
Docs are now divided into modules.
beckerjc@394
     1
#include <iostream>
beckerjc@394
     2
beckerjc@394
     3
#include <maps.h>
beckerjc@394
     4
#include <unionfind.h>
beckerjc@394
     5
beckerjc@394
     6
using namespace hugo;
beckerjc@394
     7
using namespace std;
beckerjc@394
     8
beckerjc@394
     9
bool passed = true;
beckerjc@394
    10
beckerjc@394
    11
void check(bool rc) {
beckerjc@394
    12
  passed = passed && rc;
beckerjc@394
    13
  if(!rc) {
beckerjc@394
    14
    cout << "Test failed!" << endl;
beckerjc@394
    15
  }
beckerjc@394
    16
}
beckerjc@394
    17
beckerjc@394
    18
template <typename T>
beckerjc@394
    19
class BaseMap : public StdMap<int,T> {};
beckerjc@394
    20
beckerjc@394
    21
typedef UnionFindEnum<int, BaseMap> UFE;
beckerjc@394
    22
beckerjc@394
    23
void print(UFE const &ufe) {
beckerjc@394
    24
  UFE::ClassIt cit;
beckerjc@394
    25
  cout << "printing the classes of the structure:" << endl;
beckerjc@394
    26
  int i = 1;
beckerjc@394
    27
  for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) {
beckerjc@394
    28
    cout << "  " << i << " (" << cit << "):" << flush;
beckerjc@394
    29
    UFE::ItemIt iit;
beckerjc@394
    30
    for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) {
beckerjc@394
    31
      cout << " " << iit << flush;
beckerjc@394
    32
    }
beckerjc@394
    33
    cout << endl;
beckerjc@394
    34
    i++;
beckerjc@394
    35
  }
beckerjc@394
    36
  cout << "done" << endl;
beckerjc@394
    37
}
beckerjc@394
    38
beckerjc@394
    39
beckerjc@394
    40
int main() {
beckerjc@394
    41
  UFE::MapType base;
beckerjc@394
    42
  UFE U(base);
beckerjc@394
    43
beckerjc@394
    44
  print(U);
beckerjc@394
    45
beckerjc@394
    46
  cout << "Inserting 1..." << endl;
beckerjc@394
    47
  U.insert(1);
beckerjc@394
    48
  print(U);
beckerjc@394
    49
  
beckerjc@394
    50
  cout << "Inserting 2..." << endl;
beckerjc@394
    51
  U.insert(2);
beckerjc@394
    52
  print(U);
beckerjc@394
    53
  
beckerjc@394
    54
  cout << "Joining 1 and 2..." << endl;
beckerjc@394
    55
  check(U.join(1,2));
beckerjc@394
    56
  print(U);
beckerjc@394
    57
beckerjc@394
    58
  cout << "Inserting 3, 4, 5, 6, 7..." << endl;
beckerjc@394
    59
  U.insert(3);
beckerjc@394
    60
  U.insert(4);
beckerjc@394
    61
  U.insert(5);
beckerjc@394
    62
  U.insert(6);
beckerjc@394
    63
  U.insert(7);
beckerjc@394
    64
  print (U);
beckerjc@394
    65
beckerjc@394
    66
  cout << "Joining 1 - 4, 2 - 4 and 3 - 5 ..." << endl;
beckerjc@394
    67
  check(U.join(1,4));
beckerjc@394
    68
  check(!U.join(2,4));
beckerjc@394
    69
  check(U.join(3,5));
beckerjc@394
    70
  print(U);
beckerjc@394
    71
beckerjc@394
    72
  cout << "size of the class of 4: " << U.size(4) << endl;
beckerjc@394
    73
  check(U.size(4) == 3);
beckerjc@394
    74
  cout << "size of the class of 5: " << U.size(5) << endl;
beckerjc@394
    75
  check(U.size(5) == 2);
beckerjc@394
    76
  cout << "size of the class of 6: " << U.size(6) << endl;
beckerjc@394
    77
  check(U.size(6) == 1);
beckerjc@394
    78
beckerjc@394
    79
  cout <<"erase 1: " << endl;
beckerjc@394
    80
  U.erase(1);
beckerjc@394
    81
  print(U);
beckerjc@394
    82
beckerjc@394
    83
  cout <<"erase 1: " << endl;
beckerjc@394
    84
  U.erase(1);
beckerjc@394
    85
  print(U);
beckerjc@394
    86
beckerjc@394
    87
  cout <<"erase 6: " << endl;
beckerjc@394
    88
  U.erase(6);
beckerjc@394
    89
  print(U);
beckerjc@394
    90
beckerjc@394
    91
  cout << "split the class of 5: " << endl;
beckerjc@394
    92
  U.split(5);
beckerjc@394
    93
  print(U);
beckerjc@394
    94
beckerjc@394
    95
  cout << "Joining 3 - 4 and 2 - 4 ..." << endl;
beckerjc@394
    96
  check(U.join(3,4));
beckerjc@394
    97
  check(!U.join(2,4));
beckerjc@394
    98
  print(U);
beckerjc@394
    99
beckerjc@394
   100
  cout << "eraseClass 4 ..." << endl;
beckerjc@394
   101
  U.eraseClass(4);
beckerjc@394
   102
  print(U);
beckerjc@394
   103
beckerjc@394
   104
  cout << "eraseClass 7 ..." << endl;
beckerjc@394
   105
  U.eraseClass(7);
beckerjc@394
   106
  print(U);
beckerjc@394
   107
beckerjc@394
   108
  cout << "done" << endl;
beckerjc@394
   109
beckerjc@394
   110
  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
beckerjc@394
   111
       << endl;
beckerjc@394
   112
beckerjc@394
   113
  return passed ? 0 : 1;
beckerjc@394
   114
}