COIN-OR::LEMON - Graph Library

source: lemon-0.x/test/unionfind_test.cc @ 2553:bfced05fa852

Last change on this file since 2553:bfced05fa852 was 2553:bfced05fa852, checked in by Alpar Juttner, 16 years ago

Happy New Year to LEMON (+ better update-copyright-header script)

File size: 2.5 KB
RevLine 
[906]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
[2553]5 * Copyright (C) 2003-2008
[1956]6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
[483]19#include <iostream>
20
[921]21#include <lemon/maps.h>
22#include <lemon/unionfind.h>
[774]23#include "test_tools.h"
[483]24
[921]25using namespace lemon;
[483]26using namespace std;
27
[2308]28typedef UnionFindEnum<StdMap<int, int> > UFE;
[483]29
30void print(UFE const &ufe) {
[774]31  cout << "Print the classes of the structure:" << endl;
[483]32  int i = 1;
[2205]33  for (UFE::ClassIt cit(ufe); cit != INVALID; ++cit) {
[483]34    cout << "  " << i << " (" << cit << "):" << flush;
[2205]35    for (UFE::ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
[483]36      cout << " " << iit << flush;
37    }
38    cout << endl;
39    i++;
40  }
41  cout << "done" << endl;
42}
43
44
45int main() {
[2205]46  StdMap<int, int> base;
[483]47  UFE U(base);
48
[1569]49  U.insert(1);
50  U.insert(2);
[483]51
[2505]52  check(U.join(1,2) != -1,"Test failed.");
[483]53
54  U.insert(3);
55  U.insert(4);
56  U.insert(5);
57  U.insert(6);
58  U.insert(7);
59
60
[2505]61  check(U.join(1,4) != -1,"Test failed.");
62  check(U.join(2,4) == -1,"Test failed.");
63  check(U.join(3,5) != -1,"Test failed.");
[483]64
[2505]65
66  U.insert(8,U.find(5));
67
68
69  check(U.size(U.find(4)) == 3,"Test failed.");
70  check(U.size(U.find(5)) == 3,"Test failed.");
71  check(U.size(U.find(6)) == 1,"Test failed.");
72  check(U.size(U.find(2)) == 3,"Test failed.");
73
[483]74
75  U.insert(9);
[2505]76  U.insert(10,U.find(9));
[2205]77
[483]78
[2505]79  check(U.join(8,10) != -1,"Test failed.");
[483]80
81
[2505]82  check(U.size(U.find(4)) == 3,"Test failed.");
83  check(U.size(U.find(9)) == 5,"Test failed.");
[483]84
[2505]85  check(U.size(U.find(8)) == 5,"Test failed.");
[483]86
87  U.erase(9);
[1569]88  U.erase(1);
[483]89
[2505]90  check(U.size(U.find(10)) == 4,"Test failed.");
91  check(U.size(U.find(2)) == 2,"Test failed.");
[483]92
[1569]93  U.erase(6);
[2505]94  U.split(U.find(8));
[483]95
96
[2505]97  check(U.size(U.find(4)) == 2,"Test failed.");
98  check(U.size(U.find(3)) == 1,"Test failed.");
99  check(U.size(U.find(2)) == 2,"Test failed.");
[483]100
101
[2505]102  check(U.join(3,4) != -1,"Test failed.");
103  check(U.join(2,4) == -1,"Test failed.");
[483]104
105
[2505]106  check(U.size(U.find(4)) == 3,"Test failed.");
107  check(U.size(U.find(3)) == 3,"Test failed.");
108  check(U.size(U.find(2)) == 3,"Test failed.");
[483]109
[2505]110  U.eraseClass(U.find(4));
111  U.eraseClass(U.find(7));
[483]112
[2505]113  return 0;
[483]114}
Note: See TracBrowser for help on using the repository browser.