test/unionfind_test.cc
changeset 106 9ba2d265e191
parent 103 b68a7e348e00
child 171 02f4d5d9bfd7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/unionfind_test.cc	Thu Mar 20 23:06:35 2008 +0000
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <iostream>
    1.23 +
    1.24 +#include <lemon/list_graph.h>
    1.25 +#include <lemon/maps.h>
    1.26 +#include <lemon/unionfind.h>
    1.27 +#include "test_tools.h"
    1.28 +
    1.29 +using namespace lemon;
    1.30 +using namespace std;
    1.31 +
    1.32 +typedef UnionFindEnum<ListGraph::NodeMap<int> > UFE;
    1.33 +
    1.34 +int main() {
    1.35 +  ListGraph g;
    1.36 +  ListGraph::NodeMap<int> base(g);
    1.37 +  UFE U(base);
    1.38 +  vector<ListGraph::Node> n;
    1.39 +  
    1.40 +  for(int i=0;i<20;i++) n.push_back(g.addNode());
    1.41 +
    1.42 +  U.insert(n[1]);
    1.43 +  U.insert(n[2]);
    1.44 +
    1.45 +  check(U.join(n[1],n[2]) != -1,"Test failed.");
    1.46 +
    1.47 +  U.insert(n[3]);
    1.48 +  U.insert(n[4]);
    1.49 +  U.insert(n[5]);
    1.50 +  U.insert(n[6]);
    1.51 +  U.insert(n[7]);
    1.52 +
    1.53 +
    1.54 +  check(U.join(n[1],n[4]) != -1,"Test failed.");
    1.55 +  check(U.join(n[2],n[4]) == -1,"Test failed.");
    1.56 +  check(U.join(n[3],n[5]) != -1,"Test failed.");
    1.57 +
    1.58 +
    1.59 +  U.insert(n[8],U.find(n[5]));
    1.60 +
    1.61 +
    1.62 +  check(U.size(U.find(n[4])) == 3,"Test failed.");
    1.63 +  check(U.size(U.find(n[5])) == 3,"Test failed.");
    1.64 +  check(U.size(U.find(n[6])) == 1,"Test failed.");
    1.65 +  check(U.size(U.find(n[2])) == 3,"Test failed.");
    1.66 +
    1.67 +
    1.68 +  U.insert(n[9]);
    1.69 +  U.insert(n[10],U.find(n[9]));
    1.70 +
    1.71 +
    1.72 +  check(U.join(n[8],n[10]) != -1,"Test failed.");
    1.73 +
    1.74 +
    1.75 +  check(U.size(U.find(n[4])) == 3,"Test failed.");
    1.76 +  check(U.size(U.find(n[9])) == 5,"Test failed.");
    1.77 +
    1.78 +  check(U.size(U.find(n[8])) == 5,"Test failed.");
    1.79 +
    1.80 +  U.erase(n[9]);
    1.81 +  U.erase(n[1]);
    1.82 +
    1.83 +  check(U.size(U.find(n[10])) == 4,"Test failed.");
    1.84 +  check(U.size(U.find(n[2])) == 2,"Test failed.");
    1.85 +
    1.86 +  U.erase(n[6]);
    1.87 +  U.split(U.find(n[8]));
    1.88 +
    1.89 +
    1.90 +  check(U.size(U.find(n[4])) == 2,"Test failed.");
    1.91 +  check(U.size(U.find(n[3])) == 1,"Test failed.");
    1.92 +  check(U.size(U.find(n[2])) == 2,"Test failed.");
    1.93 +
    1.94 +
    1.95 +  check(U.join(n[3],n[4]) != -1,"Test failed.");
    1.96 +  check(U.join(n[2],n[4]) == -1,"Test failed.");
    1.97 +
    1.98 +
    1.99 +  check(U.size(U.find(n[4])) == 3,"Test failed.");
   1.100 +  check(U.size(U.find(n[3])) == 3,"Test failed.");
   1.101 +  check(U.size(U.find(n[2])) == 3,"Test failed.");
   1.102 +
   1.103 +  U.eraseClass(U.find(n[4]));
   1.104 +  U.eraseClass(U.find(n[7]));
   1.105 +
   1.106 +  return 0;
   1.107 +}