alpar@906: /* -*- C++ -*- ladanyi@1435: * test/unionfind_test.cc - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@1164: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: beckerjc@483: #include beckerjc@483: alpar@921: #include alpar@921: #include alpar@774: #include "test_tools.h" beckerjc@483: alpar@921: using namespace lemon; beckerjc@483: using namespace std; beckerjc@483: beckerjc@483: template beckerjc@483: class BaseMap : public StdMap {}; beckerjc@483: beckerjc@483: typedef UnionFindEnum UFE; beckerjc@483: beckerjc@483: void print(UFE const &ufe) { beckerjc@483: UFE::ClassIt cit; alpar@774: cout << "Print the classes of the structure:" << endl; beckerjc@483: int i = 1; beckerjc@483: for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) { beckerjc@483: cout << " " << i << " (" << cit << "):" << flush; beckerjc@483: UFE::ItemIt iit; beckerjc@483: for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) { beckerjc@483: cout << " " << iit << flush; beckerjc@483: } beckerjc@483: cout << endl; beckerjc@483: i++; beckerjc@483: } beckerjc@483: cout << "done" << endl; beckerjc@483: } beckerjc@483: beckerjc@483: beckerjc@483: int main() { beckerjc@483: UFE::MapType base; beckerjc@483: UFE U(base); beckerjc@483: alpar@1569: U.insert(1); alpar@1569: U.insert(2); beckerjc@483: alpar@774: check(U.join(1,2),"Test failed."); beckerjc@483: beckerjc@483: U.insert(3); beckerjc@483: U.insert(4); beckerjc@483: U.insert(5); beckerjc@483: U.insert(6); beckerjc@483: U.insert(7); beckerjc@483: alpar@774: check(U.join(1,4),"Test failed."); alpar@774: check(!U.join(2,4),"Test failed."); alpar@774: check(U.join(3,5),"Test failed."); beckerjc@483: beckerjc@483: U.insert(8,5); beckerjc@483: alpar@774: check(U.size(4) == 3,"Test failed."); alpar@774: check(U.size(5) == 3,"Test failed."); alpar@774: check(U.size(6) == 1,"Test failed."); alpar@774: check(U.size(2) == 3,"Test failed."); beckerjc@483: beckerjc@483: U.insert(9); beckerjc@483: U.insert(10,9); alpar@1569: check(U.join(8,10),"Test failed."); beckerjc@483: alpar@1569: check(U.move(9,4),"Test failed."); alpar@1569: check(!U.move(9,2),"Test failed."); beckerjc@483: alpar@1569: check(U.size(4) == 4,"Test failed."); alpar@1569: check(U.size(9) == 4,"Test failed."); beckerjc@483: alpar@1569: check(U.move(5,6),"Test failed."); beckerjc@483: alpar@774: check(U.size(5) == 2,"Test failed."); alpar@774: check(U.size(8) == 3,"Test failed."); beckerjc@483: alpar@774: check(U.move(7,10),"Test failed."); alpar@774: check(U.size(7) == 4,"Test failed."); beckerjc@483: beckerjc@483: U.erase(9); alpar@1569: U.erase(1); beckerjc@483: alpar@774: check(U.size(4) == 2,"Test failed."); alpar@774: check(U.size(2) == 2,"Test failed."); beckerjc@483: alpar@1569: U.erase(1); alpar@1569: U.erase(6); alpar@1569: U.split(8); beckerjc@483: alpar@774: check(U.size(4) == 2,"Test failed."); alpar@774: check(U.size(3) == 1,"Test failed."); alpar@774: check(U.size(2) == 2,"Test failed."); beckerjc@483: alpar@774: check(U.join(3,4),"Test failed."); alpar@774: check(!U.join(2,4),"Test failed."); beckerjc@483: alpar@774: check(U.size(4) == 3,"Test failed."); alpar@774: check(U.size(3) == 3,"Test failed."); alpar@774: check(U.size(2) == 3,"Test failed."); beckerjc@483: beckerjc@483: U.makeRep(4); beckerjc@483: U.makeRep(3); beckerjc@483: U.makeRep(2); beckerjc@483: alpar@774: check(U.size(4) == 3,"Test failed."); alpar@774: check(U.size(3) == 3,"Test failed."); alpar@774: check(U.size(2) == 3,"Test failed."); beckerjc@483: beckerjc@483: beckerjc@483: U.eraseClass(4); alpar@1569: U.eraseClass(7); beckerjc@483: beckerjc@483: }