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