| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.common.collect.testing.testers; |
| 18 |
|
| 19 |
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; |
| 20 |
import static com.google.common.collect.testing.features.CollectionSize.ONE; |
| 21 |
import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; |
| 22 |
import static com.google.common.collect.testing.features.CollectionSize.ZERO; |
| 23 |
|
| 24 |
import com.google.common.annotations.GwtIncompatible; |
| 25 |
import com.google.common.collect.testing.Helpers; |
| 26 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 27 |
import com.google.common.collect.testing.features.CollectionSize; |
| 28 |
import java.lang.reflect.Method; |
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.Collections; |
| 31 |
import java.util.Iterator; |
| 32 |
import java.util.List; |
| 33 |
import java.util.NavigableSet; |
| 34 |
import java.util.TreeSet; |
| 35 |
import org.junit.Ignore; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
@GwtIncompatible |
| 45 |
@Ignore |
| 46 |
public class NavigableSetNavigationTester<E> extends AbstractSetTester<E> { |
| 47 |
|
| 48 |
private NavigableSet<E> navigableSet; |
| 49 |
private List<E> values; |
| 50 |
private E a; |
| 51 |
private E b; |
| 52 |
private E c; |
| 53 |
|
| 54 |
@Override |
| 55 |
public void setUp() throws Exception { |
| 56 |
super.setUp(); |
| 57 |
navigableSet = (NavigableSet<E>) getSet(); |
| 58 |
values = |
| 59 |
Helpers.copyToList( |
| 60 |
getSubjectGenerator() |
| 61 |
.getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); |
| 62 |
Collections.sort(values, navigableSet.comparator()); |
| 63 |
|
| 64 |
|
| 65 |
if (values.size() >= 1) { |
| 66 |
a = values.get(0); |
| 67 |
if (values.size() >= 3) { |
| 68 |
b = values.get(1); |
| 69 |
c = values.get(2); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
protected void resetWithHole() { |
| 76 |
super.resetContainer(getSubjectGenerator().create(a, c)); |
| 77 |
navigableSet = (NavigableSet<E>) getSet(); |
| 78 |
} |
| 79 |
|
| 80 |
@CollectionFeature.Require(SUPPORTS_REMOVE) |
| 81 |
@CollectionSize.Require(ZERO) |
| 82 |
public void testEmptySetPollFirst() { |
| 83 |
assertNull(navigableSet.pollFirst()); |
| 84 |
} |
| 85 |
|
| 86 |
@CollectionSize.Require(ZERO) |
| 87 |
public void testEmptySetNearby() { |
| 88 |
assertNull(navigableSet.lower(e0())); |
| 89 |
assertNull(navigableSet.floor(e0())); |
| 90 |
assertNull(navigableSet.ceiling(e0())); |
| 91 |
assertNull(navigableSet.higher(e0())); |
| 92 |
} |
| 93 |
|
| 94 |
@CollectionFeature.Require(SUPPORTS_REMOVE) |
| 95 |
@CollectionSize.Require(ZERO) |
| 96 |
public void testEmptySetPollLast() { |
| 97 |
assertNull(navigableSet.pollLast()); |
| 98 |
} |
| 99 |
|
| 100 |
@CollectionFeature.Require(SUPPORTS_REMOVE) |
| 101 |
@CollectionSize.Require(ONE) |
| 102 |
public void testSingletonSetPollFirst() { |
| 103 |
assertEquals(a, navigableSet.pollFirst()); |
| 104 |
assertTrue(navigableSet.isEmpty()); |
| 105 |
} |
| 106 |
|
| 107 |
@CollectionSize.Require(ONE) |
| 108 |
public void testSingletonSetNearby() { |
| 109 |
assertNull(navigableSet.lower(e0())); |
| 110 |
assertEquals(a, navigableSet.floor(e0())); |
| 111 |
assertEquals(a, navigableSet.ceiling(e0())); |
| 112 |
assertNull(navigableSet.higher(e0())); |
| 113 |
} |
| 114 |
|
| 115 |
@CollectionFeature.Require(SUPPORTS_REMOVE) |
| 116 |
@CollectionSize.Require(ONE) |
| 117 |
public void testSingletonSetPollLast() { |
| 118 |
assertEquals(a, navigableSet.pollLast()); |
| 119 |
assertTrue(navigableSet.isEmpty()); |
| 120 |
} |
| 121 |
|
| 122 |
@CollectionFeature.Require(SUPPORTS_REMOVE) |
| 123 |
@CollectionSize.Require(SEVERAL) |
| 124 |
public void testPollFirst() { |
| 125 |
assertEquals(a, navigableSet.pollFirst()); |
| 126 |
assertEquals(values.subList(1, values.size()), Helpers.copyToList(navigableSet)); |
| 127 |
} |
| 128 |
|
| 129 |
@CollectionFeature.Require(absent = SUPPORTS_REMOVE) |
| 130 |
public void testPollFirstUnsupported() { |
| 131 |
try { |
| 132 |
navigableSet.pollFirst(); |
| 133 |
fail(); |
| 134 |
} catch (UnsupportedOperationException e) { |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
@CollectionSize.Require(SEVERAL) |
| 139 |
public void testLowerHole() { |
| 140 |
resetWithHole(); |
| 141 |
assertEquals(null, navigableSet.lower(a)); |
| 142 |
assertEquals(a, navigableSet.lower(b)); |
| 143 |
assertEquals(a, navigableSet.lower(c)); |
| 144 |
} |
| 145 |
|
| 146 |
@CollectionSize.Require(SEVERAL) |
| 147 |
public void testFloorHole() { |
| 148 |
resetWithHole(); |
| 149 |
assertEquals(a, navigableSet.floor(a)); |
| 150 |
assertEquals(a, navigableSet.floor(b)); |
| 151 |
assertEquals(c, navigableSet.floor(c)); |
| 152 |
} |
| 153 |
|
| 154 |
@CollectionSize.Require(SEVERAL) |
| 155 |
public void testCeilingHole() { |
| 156 |
resetWithHole(); |
| 157 |
assertEquals(a, navigableSet.ceiling(a)); |
| 158 |
assertEquals(c, navigableSet.ceiling(b)); |
| 159 |
assertEquals(c, navigableSet.ceiling(c)); |
| 160 |
} |
| 161 |
|
| 162 |
@CollectionSize.Require(SEVERAL) |
| 163 |
public void testHigherHole() { |
| 164 |
resetWithHole(); |
| 165 |
assertEquals(c, navigableSet.higher(a)); |
| 166 |
assertEquals(c, navigableSet.higher(b)); |
| 167 |
assertEquals(null, navigableSet.higher(c)); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
@CollectionSize.Require(SEVERAL) |
| 176 |
public void testLower() { |
| 177 |
assertEquals(null, navigableSet.lower(a)); |
| 178 |
assertEquals(a, navigableSet.lower(b)); |
| 179 |
assertEquals(b, navigableSet.lower(c)); |
| 180 |
} |
| 181 |
|
| 182 |
@CollectionSize.Require(SEVERAL) |
| 183 |
public void testFloor() { |
| 184 |
assertEquals(a, navigableSet.floor(a)); |
| 185 |
assertEquals(b, navigableSet.floor(b)); |
| 186 |
assertEquals(c, navigableSet.floor(c)); |
| 187 |
} |
| 188 |
|
| 189 |
@CollectionSize.Require(SEVERAL) |
| 190 |
public void testCeiling() { |
| 191 |
assertEquals(a, navigableSet.ceiling(a)); |
| 192 |
assertEquals(b, navigableSet.ceiling(b)); |
| 193 |
assertEquals(c, navigableSet.ceiling(c)); |
| 194 |
} |
| 195 |
|
| 196 |
@CollectionSize.Require(SEVERAL) |
| 197 |
public void testHigher() { |
| 198 |
assertEquals(b, navigableSet.higher(a)); |
| 199 |
assertEquals(c, navigableSet.higher(b)); |
| 200 |
assertEquals(null, navigableSet.higher(c)); |
| 201 |
} |
| 202 |
|
| 203 |
@CollectionFeature.Require(SUPPORTS_REMOVE) |
| 204 |
@CollectionSize.Require(SEVERAL) |
| 205 |
public void testPollLast() { |
| 206 |
assertEquals(c, navigableSet.pollLast()); |
| 207 |
assertEquals(values.subList(0, values.size() - 1), Helpers.copyToList(navigableSet)); |
| 208 |
} |
| 209 |
|
| 210 |
@CollectionFeature.Require(absent = SUPPORTS_REMOVE) |
| 211 |
public void testPollLastUnsupported() { |
| 212 |
try { |
| 213 |
navigableSet.pollLast(); |
| 214 |
fail(); |
| 215 |
} catch (UnsupportedOperationException e) { |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
@CollectionSize.Require(SEVERAL) |
| 220 |
public void testDescendingNavigation() { |
| 221 |
List<E> descending = new ArrayList<>(); |
| 222 |
for (Iterator<E> i = navigableSet.descendingIterator(); i.hasNext(); ) { |
| 223 |
descending.add(i.next()); |
| 224 |
} |
| 225 |
Collections.reverse(descending); |
| 226 |
assertEquals(values, descending); |
| 227 |
} |
| 228 |
|
| 229 |
public void testEmptySubSet() { |
| 230 |
NavigableSet<E> empty = navigableSet.subSet(e0(), false, e0(), false); |
| 231 |
assertEquals(new TreeSet<E>(), empty); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public static Method[] getHoleMethods() { |
| 251 |
return new Method[] { |
| 252 |
Helpers.getMethod(NavigableSetNavigationTester.class, "testLowerHole"), |
| 253 |
Helpers.getMethod(NavigableSetNavigationTester.class, "testFloorHole"), |
| 254 |
Helpers.getMethod(NavigableSetNavigationTester.class, "testCeilingHole"), |
| 255 |
Helpers.getMethod(NavigableSetNavigationTester.class, "testHigherHole"), |
| 256 |
}; |
| 257 |
} |
| 258 |
} |