SortedSetNavigationTester.java revision 7dd252788645e940eada959bdde927426e2531c9
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
17package com.google.common.collect.testing.testers;
18
19import static com.google.common.collect.testing.features.CollectionSize.ONE;
20import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22
23import com.google.common.annotations.GwtCompatible;
24import com.google.common.collect.testing.Helpers;
25import com.google.common.collect.testing.features.CollectionSize;
26
27import java.util.Collections;
28import java.util.List;
29import java.util.NoSuchElementException;
30import java.util.SortedSet;
31
32/**
33 * A generic JUnit test which tests operations on a SortedSet. Can't be
34 * invoked directly; please see {@code SortedSetTestSuiteBuilder}.
35 *
36 * @author Jesse Wilson
37 * @author Louis Wasserman
38 */
39@GwtCompatible
40public class SortedSetNavigationTester<E> extends AbstractSetTester<E> {
41
42  private SortedSet<E> sortedSet;
43  private List<E> values;
44  private E a;
45  private E b;
46  private E c;
47
48  @Override public void setUp() throws Exception {
49    super.setUp();
50    sortedSet = (SortedSet<E>) getSet();
51    values = Helpers.copyToList(getSubjectGenerator().getSampleElements(
52        getSubjectGenerator().getCollectionSize().getNumElements()));
53    Collections.sort(values, sortedSet.comparator());
54
55    // some tests assume SEVERAL == 3
56    if (values.size() >= 1) {
57      a = values.get(0);
58      if (values.size() >= 3) {
59        b = values.get(1);
60        c = values.get(2);
61      }
62    }
63  }
64
65  @CollectionSize.Require(ZERO)
66  public void testEmptySetFirst() {
67    try {
68      sortedSet.first();
69      fail();
70    } catch (NoSuchElementException e) {
71    }
72  }
73
74  @CollectionSize.Require(ZERO)
75  public void testEmptySetLast() {
76    try {
77      sortedSet.last();
78      fail();
79    } catch (NoSuchElementException e) {
80    }
81  }
82
83  @CollectionSize.Require(ONE)
84  public void testSingletonSetFirst() {
85    assertEquals(a, sortedSet.first());
86  }
87
88  @CollectionSize.Require(ONE)
89  public void testSingletonSetLast() {
90    assertEquals(a, sortedSet.last());
91  }
92
93  @CollectionSize.Require(SEVERAL)
94  public void testFirst() {
95    assertEquals(a, sortedSet.first());
96  }
97
98  @CollectionSize.Require(SEVERAL)
99  public void testLast() {
100    assertEquals(c, sortedSet.last());
101  }
102}
103