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