15 package com.cloudera.impala.util;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Sets;
35 DisjointSet<Integer> ds =
new DisjointSet<Integer>();
39 assertTrue(ds.get(1).contains(1));
41 assertTrue(ds.get(2).contains(2));
42 ds.checkConsistency();
44 Set<Integer> existingSet = ds.get(1);
51 fail(
"makeSet() on an item with an existing item set did not fail");
52 }
catch (Exception e) {
54 assertTrue(existingSet == ds.get(1));
55 assertTrue(existingSet.contains(1));
56 assertTrue(existingSet.contains(6));
57 assertTrue(existingSet.contains(7));
62 ds.checkConsistency();
63 fail(
"Failed to detect an inconsistency in the DisjointSet data structure.");
64 }
catch (Exception e) {
70 DisjointSet<Integer> ds =
new DisjointSet<Integer>();
73 assertFalse(ds.union(1, 1));
74 assertTrue(ds.get(1).contains(1) && ds.get(1).size() == 1);
75 ds.checkConsistency();
78 assertTrue(ds.union(2, 2));
79 assertTrue(ds.get(2).contains(2) && ds.get(2).size() == 1);
80 ds.checkConsistency();
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();
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();
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)));
99 assertFalse(ds.union(4, 6));
100 ds.checkConsistency();
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();
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)));
113 for (
int i = 1; i <= 6; ++i) {
114 for (
int j = 1; j <= 6; ++j) {
115 assertFalse(ds.union(i, j));
118 ds.checkConsistency();
123 DisjointSet<Integer> ds =
new DisjointSet<Integer>();
126 assertTrue(ds.bulkUnion(Sets.newHashSet(1)));
127 assertTrue(ds.get(1).contains(1) && ds.get(1).size() == 1);
128 ds.checkConsistency();
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)));
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)));
141 ds.checkConsistency();
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();
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();
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)