1 /* 2 * Copyright (C) 2010 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 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.collect.testing.Helpers; 25 import com.google.common.collect.testing.features.CollectionFeature; 26 import com.google.common.collect.testing.features.CollectionSize; 27 28 import java.util.ArrayList; 29 import java.util.Collections; 30 import java.util.Iterator; 31 import java.util.List; 32 import java.util.NavigableSet; 33 import java.util.NoSuchElementException; 34 35 /** 36 * A generic JUnit test which tests operations on a NavigableSet. Can't be 37 * invoked directly; please see {@code SetTestSuiteBuilder}. 38 * 39 * @author Jesse Wilson 40 * @author Louis Wasserman 41 */ 42 public class SetNavigationTester<E> extends AbstractSetTester<E> { 43 44 private NavigableSet<E> navigableSet; 45 private List<E> values; 46 private E a; 47 private E b; 48 private E c; 49 50 @Override public void setUp() throws Exception { 51 super.setUp(); 52 navigableSet = (NavigableSet<E>) getSet(); 53 values = Helpers.copyToList(getSubjectGenerator().getSampleElements( 54 getSubjectGenerator().getCollectionSize().getNumElements())); 55 Collections.sort(values, navigableSet.comparator()); 56 57 // some tests assume SEVERAL == 3 58 if (values.size() >= 1) { 59 a = values.get(0); 60 if (values.size() >= 3) { 61 b = values.get(1); 62 c = values.get(2); 63 } 64 } 65 } 66 67 /** 68 * Resets the contents of navigableSet to have elements a, c, for the 69 * navigation tests. 70 */ 71 protected void resetWithHole() { 72 super.resetContainer(getSubjectGenerator().create(a, c)); 73 navigableSet = (NavigableSet<E>) getSet(); 74 } 75 76 @CollectionSize.Require(ZERO) 77 public void testEmptySetFirst() { 78 try { 79 navigableSet.first(); 80 fail(); 81 } catch (NoSuchElementException e) { 82 } 83 } 84 85 @CollectionFeature.Require(SUPPORTS_REMOVE) 86 @CollectionSize.Require(ZERO) 87 public void testEmptySetPollFirst() { 88 assertNull(navigableSet.pollFirst()); 89 } 90 91 @CollectionSize.Require(ZERO) 92 public void testEmptySetNearby() { 93 assertNull(navigableSet.lower(samples.e0)); 94 assertNull(navigableSet.floor(samples.e0)); 95 assertNull(navigableSet.ceiling(samples.e0)); 96 assertNull(navigableSet.higher(samples.e0)); 97 } 98 99 @CollectionSize.Require(ZERO) 100 public void testEmptySetLast() { 101 try { 102 navigableSet.last(); 103 fail(); 104 } catch (NoSuchElementException e) { 105 } 106 } 107 108 @CollectionFeature.Require(SUPPORTS_REMOVE) 109 @CollectionSize.Require(ZERO) 110 public void testEmptySetPollLast() { 111 assertNull(navigableSet.pollLast()); 112 } 113 114 @CollectionSize.Require(ONE) 115 public void testSingletonSetFirst() { 116 assertEquals(a, navigableSet.first()); 117 } 118 119 @CollectionFeature.Require(SUPPORTS_REMOVE) 120 @CollectionSize.Require(ONE) 121 public void testSingletonSetPollFirst() { 122 assertEquals(a, navigableSet.pollFirst()); 123 assertTrue(navigableSet.isEmpty()); 124 } 125 126 @CollectionSize.Require(ONE) 127 public void testSingletonSetNearby() { 128 assertNull(navigableSet.lower(samples.e0)); 129 assertEquals(a, navigableSet.floor(samples.e0)); 130 assertEquals(a, navigableSet.ceiling(samples.e0)); 131 assertNull(navigableSet.higher(samples.e0)); 132 } 133 134 @CollectionSize.Require(ONE) 135 public void testSingletonSetLast() { 136 assertEquals(a, navigableSet.last()); 137 } 138 139 @CollectionFeature.Require(SUPPORTS_REMOVE) 140 @CollectionSize.Require(ONE) 141 public void testSingletonSetPollLast() { 142 assertEquals(a, navigableSet.pollLast()); 143 assertTrue(navigableSet.isEmpty()); 144 } 145 146 @CollectionSize.Require(SEVERAL) 147 public void testFirst() { 148 assertEquals(a, navigableSet.first()); 149 } 150 151 @CollectionFeature.Require(SUPPORTS_REMOVE) 152 @CollectionSize.Require(SEVERAL) 153 public void testPollFirst() { 154 assertEquals(a, navigableSet.pollFirst()); 155 assertEquals( 156 values.subList(1, values.size()), Helpers.copyToList(navigableSet)); 157 } 158 159 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 160 public void testPollFirstUnsupported() { 161 try { 162 navigableSet.pollFirst(); 163 fail(); 164 } catch (UnsupportedOperationException e) { 165 } 166 } 167 168 @CollectionSize.Require(SEVERAL) 169 public void testLower() { 170 resetWithHole(); 171 assertEquals(null, navigableSet.lower(a)); 172 assertEquals(a, navigableSet.lower(b)); 173 assertEquals(a, navigableSet.lower(c)); 174 } 175 @CollectionSize.Require(SEVERAL) 176 public void testFloor() { 177 resetWithHole(); 178 assertEquals(a, navigableSet.floor(a)); 179 assertEquals(a, navigableSet.floor(b)); 180 assertEquals(c, navigableSet.floor(c)); 181 } 182 183 @CollectionSize.Require(SEVERAL) 184 public void testCeiling() { 185 resetWithHole(); 186 assertEquals(a, navigableSet.ceiling(a)); 187 assertEquals(c, navigableSet.ceiling(b)); 188 assertEquals(c, navigableSet.ceiling(c)); 189 } 190 191 @CollectionSize.Require(SEVERAL) 192 public void testHigher() { 193 resetWithHole(); 194 assertEquals(c, navigableSet.higher(a)); 195 assertEquals(c, navigableSet.higher(b)); 196 assertEquals(null, navigableSet.higher(c)); 197 } 198 199 @CollectionSize.Require(SEVERAL) 200 public void testLast() { 201 assertEquals(c, navigableSet.last()); 202 } 203 204 @CollectionFeature.Require(SUPPORTS_REMOVE) 205 @CollectionSize.Require(SEVERAL) 206 public void testPollLast() { 207 assertEquals(c, navigableSet.pollLast()); 208 assertEquals( 209 values.subList(0, values.size() - 1), Helpers.copyToList(navigableSet)); 210 } 211 212 @CollectionFeature.Require(absent = SUPPORTS_REMOVE) 213 public void testPollLastUnsupported() { 214 try { 215 navigableSet.pollLast(); 216 fail(); 217 } catch (UnsupportedOperationException e) { 218 } 219 } 220 221 @CollectionSize.Require(SEVERAL) 222 public void testDescendingNavigation() { 223 List<E> descending = new ArrayList<E>(); 224 for (Iterator<E> i = navigableSet.descendingIterator(); i.hasNext();) { 225 descending.add(i.next()); 226 } 227 Collections.reverse(descending); 228 assertEquals(values, descending); 229 } 230 } 231