View Javadoc
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.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  
36  /**
37   * A generic JUnit test which tests operations on a NavigableSet. Can't be
38   * invoked directly; please see {@code NavigableSetTestSuiteBuilder}.
39   *
40   * @author Jesse Wilson
41   * @author Louis Wasserman
42   */
43  @GwtIncompatible
44  public class NavigableSetNavigationTester<E> extends AbstractSetTester<E> {
45  
46    private NavigableSet<E> navigableSet;
47    private List<E> values;
48    private E a;
49    private E b;
50    private E c;
51  
52    @Override
53    public void setUp() throws Exception {
54      super.setUp();
55      navigableSet = (NavigableSet<E>) getSet();
56      values =
57          Helpers.copyToList(
58              getSubjectGenerator()
59                  .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
60      Collections.sort(values, navigableSet.comparator());
61  
62      // some tests assume SEVERAL == 3
63      if (values.size() >= 1) {
64        a = values.get(0);
65        if (values.size() >= 3) {
66          b = values.get(1);
67          c = values.get(2);
68        }
69      }
70    }
71  
72    /**
73     * Resets the contents of navigableSet to have elements a, c, for the
74     * navigation tests.
75     */
76    protected void resetWithHole() {
77      super.resetContainer(getSubjectGenerator().create(a, c));
78      navigableSet = (NavigableSet<E>) getSet();
79    }
80  
81    @CollectionFeature.Require(SUPPORTS_REMOVE)
82    @CollectionSize.Require(ZERO)
83    public void testEmptySetPollFirst() {
84      assertNull(navigableSet.pollFirst());
85    }
86  
87    @CollectionSize.Require(ZERO)
88    public void testEmptySetNearby() {
89      assertNull(navigableSet.lower(e0()));
90      assertNull(navigableSet.floor(e0()));
91      assertNull(navigableSet.ceiling(e0()));
92      assertNull(navigableSet.higher(e0()));
93    }
94  
95    @CollectionFeature.Require(SUPPORTS_REMOVE)
96    @CollectionSize.Require(ZERO)
97    public void testEmptySetPollLast() {
98      assertNull(navigableSet.pollLast());
99    }
100 
101   @CollectionFeature.Require(SUPPORTS_REMOVE)
102   @CollectionSize.Require(ONE)
103   public void testSingletonSetPollFirst() {
104     assertEquals(a, navigableSet.pollFirst());
105     assertTrue(navigableSet.isEmpty());
106   }
107 
108   @CollectionSize.Require(ONE)
109   public void testSingletonSetNearby() {
110     assertNull(navigableSet.lower(e0()));
111     assertEquals(a, navigableSet.floor(e0()));
112     assertEquals(a, navigableSet.ceiling(e0()));
113     assertNull(navigableSet.higher(e0()));
114   }
115 
116   @CollectionFeature.Require(SUPPORTS_REMOVE)
117   @CollectionSize.Require(ONE)
118   public void testSingletonSetPollLast() {
119     assertEquals(a, navigableSet.pollLast());
120     assertTrue(navigableSet.isEmpty());
121   }
122 
123   @CollectionFeature.Require(SUPPORTS_REMOVE)
124   @CollectionSize.Require(SEVERAL)
125   public void testPollFirst() {
126     assertEquals(a, navigableSet.pollFirst());
127     assertEquals(values.subList(1, values.size()), Helpers.copyToList(navigableSet));
128   }
129 
130   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
131   public void testPollFirstUnsupported() {
132     try {
133       navigableSet.pollFirst();
134       fail();
135     } catch (UnsupportedOperationException e) {
136     }
137   }
138 
139   @CollectionSize.Require(SEVERAL)
140   public void testLowerHole() {
141     resetWithHole();
142     assertEquals(null, navigableSet.lower(a));
143     assertEquals(a, navigableSet.lower(b));
144     assertEquals(a, navigableSet.lower(c));
145   }
146 
147   @CollectionSize.Require(SEVERAL)
148   public void testFloorHole() {
149     resetWithHole();
150     assertEquals(a, navigableSet.floor(a));
151     assertEquals(a, navigableSet.floor(b));
152     assertEquals(c, navigableSet.floor(c));
153   }
154 
155   @CollectionSize.Require(SEVERAL)
156   public void testCeilingHole() {
157     resetWithHole();
158     assertEquals(a, navigableSet.ceiling(a));
159     assertEquals(c, navigableSet.ceiling(b));
160     assertEquals(c, navigableSet.ceiling(c));
161   }
162 
163   @CollectionSize.Require(SEVERAL)
164   public void testHigherHole() {
165     resetWithHole();
166     assertEquals(c, navigableSet.higher(a));
167     assertEquals(c, navigableSet.higher(b));
168     assertEquals(null, navigableSet.higher(c));
169   }
170 
171   /*
172    * TODO(cpovirk): make "too small" and "too large" elements available for better navigation
173    * testing. At that point, we may be able to eliminate the "hole" tests, which would mean that
174    * ContiguousSet's tests would no longer need to suppress them.
175    */
176   @CollectionSize.Require(SEVERAL)
177   public void testLower() {
178     assertEquals(null, navigableSet.lower(a));
179     assertEquals(a, navigableSet.lower(b));
180     assertEquals(b, navigableSet.lower(c));
181   }
182 
183   @CollectionSize.Require(SEVERAL)
184   public void testFloor() {
185     assertEquals(a, navigableSet.floor(a));
186     assertEquals(b, navigableSet.floor(b));
187     assertEquals(c, navigableSet.floor(c));
188   }
189 
190   @CollectionSize.Require(SEVERAL)
191   public void testCeiling() {
192     assertEquals(a, navigableSet.ceiling(a));
193     assertEquals(b, navigableSet.ceiling(b));
194     assertEquals(c, navigableSet.ceiling(c));
195   }
196 
197   @CollectionSize.Require(SEVERAL)
198   public void testHigher() {
199     assertEquals(b, navigableSet.higher(a));
200     assertEquals(c, navigableSet.higher(b));
201     assertEquals(null, navigableSet.higher(c));
202   }
203 
204   @CollectionFeature.Require(SUPPORTS_REMOVE)
205   @CollectionSize.Require(SEVERAL)
206   public void testPollLast() {
207     assertEquals(c, navigableSet.pollLast());
208     assertEquals(values.subList(0, values.size() - 1), Helpers.copyToList(navigableSet));
209   }
210 
211   @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
212   public void testPollLastUnsupported() {
213     try {
214       navigableSet.pollLast();
215       fail();
216     } catch (UnsupportedOperationException e) {
217     }
218   }
219 
220   @CollectionSize.Require(SEVERAL)
221   public void testDescendingNavigation() {
222     List<E> descending = new ArrayList<E>();
223     for (Iterator<E> i = navigableSet.descendingIterator(); i.hasNext(); ) {
224       descending.add(i.next());
225     }
226     Collections.reverse(descending);
227     assertEquals(values, descending);
228   }
229 
230   public void testEmptySubSet() {
231     NavigableSet<E> empty = navigableSet.subSet(e0(), false, e0(), false);
232     assertEquals(new TreeSet<E>(), empty);
233   }
234 
235   /*
236    * TODO(cpovirk): more testing of subSet/headSet/tailSet/descendingSet? and/or generate derived
237    * suites?
238    */
239 
240   /**
241    * Returns the {@link Method} instances for the test methods in this class that create a set with
242    * a "hole" in it so that set tests of {@code ContiguousSet} can suppress them with {@code
243    * FeatureSpecificTestSuiteBuilder.suppressing()}.
244    */
245   /*
246    * TODO(cpovirk): or we could make HOLES_FORBIDDEN a feature. Or we could declare that
247    * implementations are permitted to throw IAE if a hole is requested, and we could update
248    * test*Hole to permit IAE. (But might this ignore genuine bugs?) But see the TODO above
249    * testLower, which could make this all unnecessary
250    */
251   public static Method[] getHoleMethods() {
252     return new Method[] {
253       Helpers.getMethod(NavigableSetNavigationTester.class, "testLowerHole"),
254       Helpers.getMethod(NavigableSetNavigationTester.class, "testFloorHole"),
255       Helpers.getMethod(NavigableSetNavigationTester.class, "testCeilingHole"),
256       Helpers.getMethod(NavigableSetNavigationTester.class, "testHigherHole"),
257     };
258   }
259 }