ts_UnionFind.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #include <icl_core/UnionFind.h>
24 
25 #include <boost/test/unit_test.hpp>
26 
28 
29 BOOST_AUTO_TEST_SUITE(ts_UnionFind)
30 
31 BOOST_AUTO_TEST_CASE(UnionFindTest)
32 {
33  UnionFind uf(5);
34  BOOST_CHECK_EQUAL(uf.size(), 5);
35  BOOST_CHECK_EQUAL(uf.find(0), 0);
36  BOOST_CHECK_EQUAL(uf.find(1), 1);
37  BOOST_CHECK_EQUAL(uf.find(2), 2);
38  BOOST_CHECK_EQUAL(uf.find(3), 3);
39  BOOST_CHECK_EQUAL(uf.find(4), 4);
40 
41  uf.merge(0, 1);
42  uf.merge(2, 3);
43  BOOST_CHECK_EQUAL(uf.find(0), 0);
44  BOOST_CHECK_EQUAL(uf.find(1), 0);
45  BOOST_CHECK_EQUAL(uf.find(2), 2);
46  BOOST_CHECK_EQUAL(uf.find(3), 2);
47  BOOST_CHECK_EQUAL(uf.find(4), 4);
48 
49  uf.merge(2, 4);
50  BOOST_CHECK_EQUAL(uf.find(0), 0);
51  BOOST_CHECK_EQUAL(uf.find(1), 0);
52  BOOST_CHECK_EQUAL(uf.find(2), 2);
53  BOOST_CHECK_EQUAL(uf.find(3), 2);
54  BOOST_CHECK_EQUAL(uf.find(4), 2);
55 
56  // Copying the structure and merging in different directions
57  {
58  UnionFind uf_forward = uf;
59  UnionFind uf_backward = uf;
60  uf_forward.merge(0, 3);
61  BOOST_CHECK_EQUAL(uf_forward.find(0), 0);
62  BOOST_CHECK_EQUAL(uf_forward.find(1), 0);
63  BOOST_CHECK_EQUAL(uf_forward.find(2), 0);
64  BOOST_CHECK_EQUAL(uf_forward.find(3), 0);
65  BOOST_CHECK_EQUAL(uf_forward.find(4), 0);
66 
67  uf_backward.merge(3, 0);
68  BOOST_CHECK_EQUAL(uf_backward.find(0), 2);
69  BOOST_CHECK_EQUAL(uf_backward.find(1), 2);
70  BOOST_CHECK_EQUAL(uf_backward.find(2), 2);
71  BOOST_CHECK_EQUAL(uf_backward.find(3), 2);
72  BOOST_CHECK_EQUAL(uf_backward.find(4), 2);
73  }
74 
75  // Enlarging the structure
76  uf.grow(2);
77  BOOST_CHECK_EQUAL(uf.size(), 7);
78  BOOST_CHECK_EQUAL(uf.find(0), 0);
79  BOOST_CHECK_EQUAL(uf.find(1), 0);
80  BOOST_CHECK_EQUAL(uf.find(2), 2);
81  BOOST_CHECK_EQUAL(uf.find(3), 2);
82  BOOST_CHECK_EQUAL(uf.find(4), 2);
83  BOOST_CHECK_EQUAL(uf.find(5), 5);
84  BOOST_CHECK_EQUAL(uf.find(6), 6);
85 
86  // Merging on the enlarged structure
87  uf.merge(5, 6);
88  uf.merge(4, 5);
89  BOOST_CHECK_EQUAL(uf.find(0), 0);
90  BOOST_CHECK_EQUAL(uf.find(1), 0);
91  BOOST_CHECK_EQUAL(uf.find(2), 2);
92  BOOST_CHECK_EQUAL(uf.find(3), 2);
93  BOOST_CHECK_EQUAL(uf.find(4), 2);
94  BOOST_CHECK_EQUAL(uf.find(5), 2);
95  BOOST_CHECK_EQUAL(uf.find(6), 2);
96 
97  uf.merge(1, 6);
98  BOOST_CHECK_EQUAL(uf.find(0), 2);
99  BOOST_CHECK_EQUAL(uf.find(1), 2);
100  BOOST_CHECK_EQUAL(uf.find(2), 2);
101  BOOST_CHECK_EQUAL(uf.find(3), 2);
102  BOOST_CHECK_EQUAL(uf.find(4), 2);
103  BOOST_CHECK_EQUAL(uf.find(5), 2);
104  BOOST_CHECK_EQUAL(uf.find(6), 2);
105 }
106 
107 BOOST_AUTO_TEST_SUITE_END()
void merge(std::size_t x, std::size_t y)
Definition: UnionFind.h:61
std::size_t find(std::size_t x)
Definition: UnionFind.h:88
void grow(const std::size_t n=1)
Enlarges the data structure by n elements.
Definition: UnionFind.h:47
BOOST_AUTO_TEST_CASE(UnionFindTest)
std::size_t size() const
Definition: UnionFind.h:101


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58