| [906] | 1 | /* -*- C++ -*- | 
|---|
| [1435] | 2 | * test/unionfind_test.cc - Part of LEMON, a generic C++ optimization library | 
|---|
| [906] | 3 | * | 
|---|
| [1875] | 4 | * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport | 
|---|
| [1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). | 
|---|
| [906] | 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] | 23 | using namespace lemon; | 
|---|
| [483] | 24 | using namespace std; | 
|---|
|  | 25 |  | 
|---|
|  | 26 | template <typename T> | 
|---|
|  | 27 | class BaseMap : public StdMap<int,T> {}; | 
|---|
|  | 28 |  | 
|---|
|  | 29 | typedef UnionFindEnum<int, BaseMap> UFE; | 
|---|
|  | 30 |  | 
|---|
|  | 31 | void 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 |  | 
|---|
|  | 48 | int main() { | 
|---|
|  | 49 | UFE::MapType base; | 
|---|
|  | 50 | UFE U(base); | 
|---|
|  | 51 |  | 
|---|
| [1569] | 52 | U.insert(1); | 
|---|
|  | 53 | U.insert(2); | 
|---|
| [483] | 54 |  | 
|---|
| [774] | 55 | check(U.join(1,2),"Test failed."); | 
|---|
| [483] | 56 |  | 
|---|
|  | 57 | U.insert(3); | 
|---|
|  | 58 | U.insert(4); | 
|---|
|  | 59 | U.insert(5); | 
|---|
|  | 60 | U.insert(6); | 
|---|
|  | 61 | U.insert(7); | 
|---|
|  | 62 |  | 
|---|
| [774] | 63 | check(U.join(1,4),"Test failed."); | 
|---|
|  | 64 | check(!U.join(2,4),"Test failed."); | 
|---|
|  | 65 | check(U.join(3,5),"Test failed."); | 
|---|
| [483] | 66 |  | 
|---|
|  | 67 | U.insert(8,5); | 
|---|
|  | 68 |  | 
|---|
| [774] | 69 | check(U.size(4) == 3,"Test failed."); | 
|---|
|  | 70 | check(U.size(5) == 3,"Test failed."); | 
|---|
|  | 71 | check(U.size(6) == 1,"Test failed."); | 
|---|
|  | 72 | check(U.size(2) == 3,"Test failed."); | 
|---|
| [483] | 73 |  | 
|---|
|  | 74 | U.insert(9); | 
|---|
|  | 75 | U.insert(10,9); | 
|---|
| [1569] | 76 | check(U.join(8,10),"Test failed."); | 
|---|
| [483] | 77 |  | 
|---|
| [1569] | 78 | check(U.move(9,4),"Test failed."); | 
|---|
|  | 79 | check(!U.move(9,2),"Test failed."); | 
|---|
| [483] | 80 |  | 
|---|
| [1569] | 81 | check(U.size(4) == 4,"Test failed."); | 
|---|
|  | 82 | check(U.size(9) == 4,"Test failed."); | 
|---|
| [483] | 83 |  | 
|---|
| [1569] | 84 | check(U.move(5,6),"Test failed."); | 
|---|
| [483] | 85 |  | 
|---|
| [774] | 86 | check(U.size(5) == 2,"Test failed."); | 
|---|
|  | 87 | check(U.size(8) == 3,"Test failed."); | 
|---|
| [483] | 88 |  | 
|---|
| [774] | 89 | check(U.move(7,10),"Test failed."); | 
|---|
|  | 90 | check(U.size(7) == 4,"Test failed."); | 
|---|
| [483] | 91 |  | 
|---|
|  | 92 | U.erase(9); | 
|---|
| [1569] | 93 | U.erase(1); | 
|---|
| [483] | 94 |  | 
|---|
| [774] | 95 | check(U.size(4) == 2,"Test failed."); | 
|---|
|  | 96 | check(U.size(2) == 2,"Test failed."); | 
|---|
| [483] | 97 |  | 
|---|
| [1569] | 98 | U.erase(1); | 
|---|
|  | 99 | U.erase(6); | 
|---|
|  | 100 | U.split(8); | 
|---|
| [483] | 101 |  | 
|---|
| [774] | 102 | check(U.size(4) == 2,"Test failed."); | 
|---|
|  | 103 | check(U.size(3) == 1,"Test failed."); | 
|---|
|  | 104 | check(U.size(2) == 2,"Test failed."); | 
|---|
| [483] | 105 |  | 
|---|
| [774] | 106 | check(U.join(3,4),"Test failed."); | 
|---|
|  | 107 | check(!U.join(2,4),"Test failed."); | 
|---|
| [483] | 108 |  | 
|---|
| [774] | 109 | check(U.size(4) == 3,"Test failed."); | 
|---|
|  | 110 | check(U.size(3) == 3,"Test failed."); | 
|---|
|  | 111 | check(U.size(2) == 3,"Test failed."); | 
|---|
| [483] | 112 |  | 
|---|
|  | 113 | U.makeRep(4); | 
|---|
|  | 114 | U.makeRep(3); | 
|---|
|  | 115 | U.makeRep(2); | 
|---|
|  | 116 |  | 
|---|
| [774] | 117 | check(U.size(4) == 3,"Test failed."); | 
|---|
|  | 118 | check(U.size(3) == 3,"Test failed."); | 
|---|
|  | 119 | check(U.size(2) == 3,"Test failed."); | 
|---|
| [483] | 120 |  | 
|---|
|  | 121 |  | 
|---|
|  | 122 | U.eraseClass(4); | 
|---|
| [1569] | 123 | U.eraseClass(7); | 
|---|
| [483] | 124 |  | 
|---|
|  | 125 | } | 
|---|