COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/test/unionfind_test.cc @ 1216:45f65dd610da

Last change on this file since 1216:45f65dd610da was 1164:80bb73097736, checked in by Alpar Juttner, 20 years ago

A year has passed again.

File size: 5.3 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/test/unionfind_test.cc - Part of LEMON, a generic C++ optimization library
[906]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[906]5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
[483]17#include <iostream>
18
[921]19#include <lemon/maps.h>
20#include <lemon/unionfind.h>
[774]21#include "test_tools.h"
[483]22
[921]23using namespace lemon;
[483]24using namespace std;
25
26template <typename T>
27class BaseMap : public StdMap<int,T> {};
28
29typedef UnionFindEnum<int, BaseMap> UFE;
30
31void print(UFE const &ufe) {
32  UFE::ClassIt cit;
[774]33  cout << "Print the classes of the structure:" << endl;
[483]34  int i = 1;
35  for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) {
36    cout << "  " << i << " (" << cit << "):" << flush;
37    UFE::ItemIt iit;
38    for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) {
39      cout << " " << iit << flush;
40    }
41    cout << endl;
42    i++;
43  }
44  cout << "done" << endl;
45}
46
47
48int main() {
49  UFE::MapType base;
50  UFE U(base);
51
[774]52//   print(U);
[483]53
[774]54  cout << "Insert 1..." << endl;
[483]55  U.insert(1);
[774]56//   print(U);
[483]57 
[774]58  cout << "Insert 2..." << endl;
[483]59  U.insert(2);
[774]60//   print(U);
[483]61 
[774]62  cout << "Join 1 and 2..." << endl;
63  check(U.join(1,2),"Test failed.");
64//   print(U);
[483]65
[774]66  cout << "Insert 3, 4, 5, 6, 7..." << endl;
[483]67  U.insert(3);
68  U.insert(4);
69  U.insert(5);
70  U.insert(6);
71  U.insert(7);
[774]72//   print (U);
[483]73
[774]74  cout << "Join 1 - 4, 2 - 4 and 3 - 5 ..." << endl;
75  check(U.join(1,4),"Test failed.");
76  check(!U.join(2,4),"Test failed.");
77  check(U.join(3,5),"Test failed.");
78//   print(U);
[483]79
[774]80  cout << "Insert 8 to the component of 5 ..." << endl;
[483]81  U.insert(8,5);
[774]82//   print(U);
[483]83
[774]84  cout << "Size of the class of 4: " << U.size(4) << endl;
85  check(U.size(4) == 3,"Test failed.");
86  cout << "Size of the class of 5: " << U.size(5) << endl;
87  check(U.size(5) == 3,"Test failed.");
88  cout << "Size of the class of 6: " << U.size(6) << endl;
89  check(U.size(6) == 1,"Test failed.");
90  cout << "Size of the class of 2: " << U.size(2) << endl;
91  check(U.size(2) == 3,"Test failed.");
[483]92
[774]93  cout << "Insert 9 ..." << endl;
[483]94  U.insert(9);
[774]95//   print(U);
96  cout << "Insert 10 to the component of 9 ..." << endl;
[483]97  U.insert(10,9);
[774]98//   print(U);
[483]99
[774]100  cout << "Join 8 and 10..." << endl;
101  check(U.join(8,10),"Test failed.");
102//   print(U);
[483]103
104  cout << "Move 9 to the class of 4 ..." << endl;
[774]105  check(U.move(9,4),"Test failed.");
106//   print(U);
[483]107
108  cout << "Move 9 to the class of 2 ..." << endl;
[774]109  check(!U.move(9,2),"Test failed.");
110//   print(U);
[483]111
[774]112  cout << "Size of the class of 4: " << U.size(4) << endl;
113  check(U.size(4) == 4,"Test failed.");
114  cout << "Size of the class of 9: " << U.size(9) << endl;
115  check(U.size(9) == 4,"Test failed.");
[483]116 
117  cout << "Move 5 to the class of 6 ..." << endl;
[774]118  check(U.move(5,6),"Test failed.");
119//   print(U);
[483]120
[774]121  cout << "Size of the class of 5: " << U.size(5) << endl;
122  check(U.size(5) == 2,"Test failed.");
123  cout << "Size of the class of 8: " << U.size(8) << endl;
124  check(U.size(8) == 3,"Test failed.");
[483]125
126  cout << "Move 7 to the class of 10 ..." << endl;
[774]127  check(U.move(7,10),"Test failed.");
128//   print(U);
[483]129
[774]130  cout << "Size of the class of 7: " << U.size(7) << endl;
131  check(U.size(7) == 4,"Test failed.");
[483]132
[774]133  cout <<"Erase 9... " << endl;
[483]134  U.erase(9);
[774]135//   print(U);
[483]136
[774]137  cout <<"Erase 1... " << endl;
[483]138  U.erase(1);
[774]139//   print(U);
[483]140
[774]141  cout << "Size of the class of 4: " << U.size(4) << endl;
142  check(U.size(4) == 2,"Test failed.");
143  cout << "Size of the class of 2: " << U.size(2) << endl;
144  check(U.size(2) == 2,"Test failed.");
[483]145
146
[774]147  cout <<"Erase 1... " << endl;
[483]148  U.erase(1);
[774]149//   print(U);
[483]150
[774]151  cout <<"Erase 6... " << endl;
[483]152  U.erase(6);
[774]153//   print(U);
[483]154
[774]155  cout << "Split the class of 8... " << endl;
[483]156  U.split(8);
[774]157//   print(U);
[483]158
159
[774]160  cout << "Size of the class of 4: " << U.size(4) << endl;
161  check(U.size(4) == 2,"Test failed.");
162  cout << "Size of the class of 3: " << U.size(3) << endl;
163  check(U.size(3) == 1,"Test failed.");
164  cout << "Size of the class of 2: " << U.size(2) << endl;
165  check(U.size(2) == 2,"Test failed.");
[483]166
167
[774]168  cout << "Join 3 - 4 and 2 - 4 ..." << endl;
169  check(U.join(3,4),"Test failed.");
170  check(!U.join(2,4),"Test failed.");
171//   print(U);
[483]172
173
[774]174  cout << "Size of the class of 4: " << U.size(4) << endl;
175  check(U.size(4) == 3,"Test failed.");
176  cout << "Size of the class of 3: " << U.size(3) << endl;
177  check(U.size(3) == 3,"Test failed.");
178  cout << "Size of the class of 2: " << U.size(2) << endl;
179  check(U.size(2) == 3,"Test failed.");
[483]180
[793]181  cout << "Calling makeRep(4)..." << endl;
[483]182  U.makeRep(4);
[774]183//   print(U);
[793]184  cout << "Calling makeRep(3)..." << endl;
[483]185  U.makeRep(3);
[774]186//   print(U);
[793]187  cout << "Calling makeRep(2)..." << endl;
[483]188  U.makeRep(2);
[774]189//   print(U);
[483]190
[774]191  cout << "Size of the class of 4: " << U.size(4) << endl;
192  check(U.size(4) == 3,"Test failed.");
193  cout << "Size of the class of 3: " << U.size(3) << endl;
194  check(U.size(3) == 3,"Test failed.");
195  cout << "Size of the class of 2: " << U.size(2) << endl;
196  check(U.size(2) == 3,"Test failed.");
[483]197
198
199  cout << "eraseClass 4 ..." << endl;
200  U.eraseClass(4);
[774]201//   print(U);
[483]202
203  cout << "eraseClass 7 ..." << endl;
204  U.eraseClass(7);
[774]205//   print(U);
[483]206
[774]207  cout << "done." << endl;
[483]208}
Note: See TracBrowser for help on using the repository browser.