Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TestDisjointSet.java
Go to the documentation of this file.
1 // Copyright 2014 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.util;
16 
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20 
21 import java.util.Set;
22 
23 import org.junit.Test;
24 
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Sets;
27 
31 public class TestDisjointSet {
32 
33  @Test
34  public void testMakeSet() throws Exception {
35  DisjointSet<Integer> ds = new DisjointSet<Integer>();
36 
37  // Expect success.
38  ds.makeSet(1);
39  assertTrue(ds.get(1).contains(1));
40  ds.makeSet(2);
41  assertTrue(ds.get(2).contains(2));
42  ds.checkConsistency();
43 
44  Set<Integer> existingSet = ds.get(1);
45  // Manually tamper with the item set for testing purposes.
46  existingSet.add(6);
47  existingSet.add(7);
48  // The item set already exists.
49  try {
50  ds.makeSet(1);
51  fail("makeSet() on an item with an existing item set did not fail");
52  } catch (Exception e) {
53  // Check that the failed makeSet didn't change the existing set.
54  assertTrue(existingSet == ds.get(1));
55  assertTrue(existingSet.contains(1));
56  assertTrue(existingSet.contains(6));
57  assertTrue(existingSet.contains(7));
58  }
59 
60  // Tests detecting the inconsistency due to the manual modifications.
61  try {
62  ds.checkConsistency();
63  fail("Failed to detect an inconsistency in the DisjointSet data structure.");
64  } catch (Exception e) {
65  }
66  }
67 
68  @Test
69  public void testUnion() throws Exception {
70  DisjointSet<Integer> ds = new DisjointSet<Integer>();
71  ds.makeSet(1);
72  // test idempotence
73  assertFalse(ds.union(1, 1));
74  assertTrue(ds.get(1).contains(1) && ds.get(1).size() == 1);
75  ds.checkConsistency();
76 
77  // test creating a new single-item set with union()
78  assertTrue(ds.union(2, 2));
79  assertTrue(ds.get(2).contains(2) && ds.get(2).size() == 1);
80  ds.checkConsistency();
81 
82  // test creating a multi-item set with union()
83  assertTrue(ds.union(3, 4));
84  assertTrue(ds.get(3) == ds.get(4) && ds.get(3).contains(4) && ds.get(4).contains(3));
85  ds.checkConsistency();
86  // test idempotence
87  assertFalse(ds.union(3, 4));
88  assertTrue(ds.get(3) == ds.get(4) && ds.get(3).contains(4) && ds.get(4).contains(3));
89  ds.checkConsistency();
90 
91  // test merging an existing item set with a non-existent item
92  assertTrue(ds.union(4, 5));
93  assertTrue(ds.get(4) == ds.get(5) && ds.get(4).contains(5)
94  && ds.get(4).containsAll(Lists.newArrayList(3, 4, 5)));
95  assertTrue(ds.union(6, 4));
96  assertTrue(ds.get(4) == ds.get(6) && ds.get(6).contains(4)
97  && ds.get(4).containsAll(Lists.newArrayList(3, 4, 5, 6)));
98  // already in the same set
99  assertFalse(ds.union(4, 6));
100  ds.checkConsistency();
101 
102  // test merging two existing single-item item sets
103  assertTrue(ds.union(1, 2));
104  assertTrue(ds.get(1) == ds.get(2) &&
105  ds.get(1).containsAll(Lists.newArrayList(1, 2)));
106  ds.checkConsistency();
107 
108  // test merging two multi-item item sets
109  assertTrue(ds.union(1, 3));
110  assertTrue(ds.get(1) == ds.get(3) &&
111  ds.get(1).containsAll(Lists.newArrayList(1, 2, 3, 4, 5, 6)));
112  // already in the same set
113  for (int i = 1; i <= 6; ++i) {
114  for (int j = 1; j <= 6; ++j) {
115  assertFalse(ds.union(i, j));
116  }
117  }
118  ds.checkConsistency();
119  }
120 
121  @Test
122  public void testBulkUnion() throws Exception {
123  DisjointSet<Integer> ds = new DisjointSet<Integer>();
124 
125  // test creating a new single-item set
126  assertTrue(ds.bulkUnion(Sets.newHashSet(1)));
127  assertTrue(ds.get(1).contains(1) && ds.get(1).size() == 1);
128  ds.checkConsistency();
129 
130  // test creating a new multi-item item set
131  assertTrue(ds.bulkUnion(Sets.newHashSet(2, 3, 4)));
132  assertTrue(ds.get(2) == ds.get(3) && ds.get(2) == ds.get(4)
133  && ds.get(2).containsAll(Lists.newArrayList(2, 3, 4)));
134  // already in the same set
135  for (int i = 2; i <= 4; ++i) {
136  for (int j = 2; j <= 4; ++j) {
137  assertFalse(ds.union(i, j));
138  assertFalse(ds.bulkUnion(Sets.newHashSet(i, j)));
139  }
140  }
141  ds.checkConsistency();
142 
143  // create another new multi-item item set
144  assertTrue(ds.bulkUnion(Sets.newHashSet(5, 6, 7, 8)));
145  assertTrue(ds.get(5) == ds.get(6) && ds.get(6) == ds.get(7) && ds.get(7) == ds.get(8)
146  && ds.get(5).containsAll(Lists.newArrayList(5, 6, 7, 8)));
147  ds.checkConsistency();
148 
149  // merge all existing item sets
150  assertTrue(ds.bulkUnion(Sets.newHashSet(1, 3, 8)));
151  assertTrue(ds.get(1) == ds.get(2) && ds.get(2) == ds.get(3) && ds.get(3) == ds.get(4)
152  && ds.get(4) == ds.get(5) && ds.get(5) == ds.get(6) && ds.get(6) == ds.get(7)
153  && ds.get(7) == ds.get(8)
154  && ds.get(1).containsAll(Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8)));
155  ds.checkConsistency();
156  }
157 }
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)