aboutsummaryrefslogtreecommitdiff
path: root/guava-testlib/src/com/google/common/collect/testing/testers/NavigableSetNavigationTester.java
blob: 354dd608ddb3411de7fc1ef8c1f25415257fca95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (C) 2010 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect.testing.testers;

import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
import static com.google.common.collect.testing.features.CollectionSize.ONE;
import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
import static org.junit.Assert.assertThrows;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
import org.junit.Ignore;

/**
 * A generic JUnit test which tests operations on a NavigableSet. Can't be invoked directly; please
 * see {@code NavigableSetTestSuiteBuilder}.
 *
 * @author Jesse Wilson
 * @author Louis Wasserman
 */
@GwtIncompatible
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
public class NavigableSetNavigationTester<E> extends AbstractSetTester<E> {

  private NavigableSet<E> navigableSet;
  private List<E> values;
  private E a;
  private E b;
  private E c;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    navigableSet = (NavigableSet<E>) getSet();
    values =
        Helpers.copyToList(
            getSubjectGenerator()
                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
    Collections.sort(values, navigableSet.comparator());

    // some tests assume SEVERAL == 3
    if (values.size() >= 1) {
      a = values.get(0);
      if (values.size() >= 3) {
        b = values.get(1);
        c = values.get(2);
      }
    }
  }

  /** Resets the contents of navigableSet to have elements a, c, for the navigation tests. */
  protected void resetWithHole() {
    super.resetContainer(getSubjectGenerator().create(a, c));
    navigableSet = (NavigableSet<E>) getSet();
  }

  @CollectionFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(ZERO)
  public void testEmptySetPollFirst() {
    assertNull(navigableSet.pollFirst());
  }

  @CollectionSize.Require(ZERO)
  public void testEmptySetNearby() {
    assertNull(navigableSet.lower(e0()));
    assertNull(navigableSet.floor(e0()));
    assertNull(navigableSet.ceiling(e0()));
    assertNull(navigableSet.higher(e0()));
  }

  @CollectionFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(ZERO)
  public void testEmptySetPollLast() {
    assertNull(navigableSet.pollLast());
  }

  @CollectionFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(ONE)
  public void testSingletonSetPollFirst() {
    assertEquals(a, navigableSet.pollFirst());
    assertTrue(navigableSet.isEmpty());
  }

  @CollectionSize.Require(ONE)
  public void testSingletonSetNearby() {
    assertNull(navigableSet.lower(e0()));
    assertEquals(a, navigableSet.floor(e0()));
    assertEquals(a, navigableSet.ceiling(e0()));
    assertNull(navigableSet.higher(e0()));
  }

  @CollectionFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(ONE)
  public void testSingletonSetPollLast() {
    assertEquals(a, navigableSet.pollLast());
    assertTrue(navigableSet.isEmpty());
  }

  @CollectionFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(SEVERAL)
  public void testPollFirst() {
    assertEquals(a, navigableSet.pollFirst());
    assertEquals(values.subList(1, values.size()), Helpers.copyToList(navigableSet));
  }

  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
  public void testPollFirstUnsupported() {
    assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollFirst());
  }

  @CollectionSize.Require(SEVERAL)
  public void testLowerHole() {
    resetWithHole();
    assertEquals(null, navigableSet.lower(a));
    assertEquals(a, navigableSet.lower(b));
    assertEquals(a, navigableSet.lower(c));
  }

  @CollectionSize.Require(SEVERAL)
  public void testFloorHole() {
    resetWithHole();
    assertEquals(a, navigableSet.floor(a));
    assertEquals(a, navigableSet.floor(b));
    assertEquals(c, navigableSet.floor(c));
  }

  @CollectionSize.Require(SEVERAL)
  public void testCeilingHole() {
    resetWithHole();
    assertEquals(a, navigableSet.ceiling(a));
    assertEquals(c, navigableSet.ceiling(b));
    assertEquals(c, navigableSet.ceiling(c));
  }

  @CollectionSize.Require(SEVERAL)
  public void testHigherHole() {
    resetWithHole();
    assertEquals(c, navigableSet.higher(a));
    assertEquals(c, navigableSet.higher(b));
    assertEquals(null, navigableSet.higher(c));
  }

  /*
   * TODO(cpovirk): make "too small" and "too large" elements available for better navigation
   * testing. At that point, we may be able to eliminate the "hole" tests, which would mean that
   * ContiguousSet's tests would no longer need to suppress them.
   */
  @CollectionSize.Require(SEVERAL)
  public void testLower() {
    assertEquals(null, navigableSet.lower(a));
    assertEquals(a, navigableSet.lower(b));
    assertEquals(b, navigableSet.lower(c));
  }

  @CollectionSize.Require(SEVERAL)
  public void testFloor() {
    assertEquals(a, navigableSet.floor(a));
    assertEquals(b, navigableSet.floor(b));
    assertEquals(c, navigableSet.floor(c));
  }

  @CollectionSize.Require(SEVERAL)
  public void testCeiling() {
    assertEquals(a, navigableSet.ceiling(a));
    assertEquals(b, navigableSet.ceiling(b));
    assertEquals(c, navigableSet.ceiling(c));
  }

  @CollectionSize.Require(SEVERAL)
  public void testHigher() {
    assertEquals(b, navigableSet.higher(a));
    assertEquals(c, navigableSet.higher(b));
    assertEquals(null, navigableSet.higher(c));
  }

  @CollectionFeature.Require(SUPPORTS_REMOVE)
  @CollectionSize.Require(SEVERAL)
  public void testPollLast() {
    assertEquals(c, navigableSet.pollLast());
    assertEquals(values.subList(0, values.size() - 1), Helpers.copyToList(navigableSet));
  }

  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
  public void testPollLastUnsupported() {
    assertThrows(UnsupportedOperationException.class, () -> navigableSet.pollLast());
  }

  @CollectionSize.Require(SEVERAL)
  public void testDescendingNavigation() {
    List<E> descending = new ArrayList<>();
    for (Iterator<E> i = navigableSet.descendingIterator(); i.hasNext(); ) {
      descending.add(i.next());
    }
    Collections.reverse(descending);
    assertEquals(values, descending);
  }

  public void testEmptySubSet() {
    NavigableSet<E> empty = navigableSet.subSet(e0(), false, e0(), false);
    assertEquals(new TreeSet<E>(), empty);
  }

  /*
   * TODO(cpovirk): more testing of subSet/headSet/tailSet/descendingSet? and/or generate derived
   * suites?
   */

  /**
   * Returns the {@link Method} instances for the test methods in this class that create a set with
   * a "hole" in it so that set tests of {@code ContiguousSet} can suppress them with {@code
   * FeatureSpecificTestSuiteBuilder.suppressing()}.
   */
  /*
   * TODO(cpovirk): or we could make HOLES_FORBIDDEN a feature. Or we could declare that
   * implementations are permitted to throw IAE if a hole is requested, and we could update
   * test*Hole to permit IAE. (But might this ignore genuine bugs?) But see the TODO above
   * testLower, which could make this all unnecessary
   */
  public static Method[] getHoleMethods() {
    return new Method[] {
      Helpers.getMethod(NavigableSetNavigationTester.class, "testLowerHole"),
      Helpers.getMethod(NavigableSetNavigationTester.class, "testFloorHole"),
      Helpers.getMethod(NavigableSetNavigationTester.class, "testCeilingHole"),
      Helpers.getMethod(NavigableSetNavigationTester.class, "testHigherHole"),
    };
  }
}