001/*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.features.CollectionSize.ONE;
020import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022
023import com.google.common.annotations.GwtCompatible;
024import com.google.common.collect.testing.Helpers;
025import com.google.common.collect.testing.features.CollectionSize;
026import java.util.Collections;
027import java.util.List;
028import java.util.NoSuchElementException;
029import java.util.SortedSet;
030import org.checkerframework.checker.nullness.qual.Nullable;
031import org.junit.Ignore;
032
033/**
034 * A generic JUnit test which tests operations on a SortedSet. Can't be invoked directly; please see
035 * {@code SortedSetTestSuiteBuilder}.
036 *
037 * @author Jesse Wilson
038 * @author Louis Wasserman
039 */
040@GwtCompatible
041@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
042@SuppressWarnings("JUnit4ClassUsedInJUnit3")
043@ElementTypesAreNonnullByDefault
044public class SortedSetNavigationTester<E extends @Nullable Object> extends AbstractSetTester<E> {
045
046  private SortedSet<E> sortedSet;
047  private List<E> values;
048  private @Nullable E a;
049  private @Nullable E b;
050  private @Nullable E c;
051
052  @Override
053  public void setUp() throws Exception {
054    super.setUp();
055    sortedSet = (SortedSet<E>) getSet();
056    values =
057        Helpers.copyToList(
058            getSubjectGenerator()
059                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
060    Collections.sort(values, sortedSet.comparator());
061
062    // some tests assume SEVERAL == 3
063    if (values.size() >= 1) {
064      a = values.get(0);
065      if (values.size() >= 3) {
066        b = values.get(1);
067        c = values.get(2);
068      }
069    }
070  }
071
072  @CollectionSize.Require(ZERO)
073  public void testEmptySetFirst() {
074    try {
075      sortedSet.first();
076      fail();
077    } catch (NoSuchElementException e) {
078    }
079  }
080
081  @CollectionSize.Require(ZERO)
082  public void testEmptySetLast() {
083    try {
084      sortedSet.last();
085      fail();
086    } catch (NoSuchElementException e) {
087    }
088  }
089
090  @CollectionSize.Require(ONE)
091  public void testSingletonSetFirst() {
092    assertEquals(a, sortedSet.first());
093  }
094
095  @CollectionSize.Require(ONE)
096  public void testSingletonSetLast() {
097    assertEquals(a, sortedSet.last());
098  }
099
100  @CollectionSize.Require(SEVERAL)
101  public void testFirst() {
102    assertEquals(a, sortedSet.first());
103  }
104
105  @CollectionSize.Require(SEVERAL)
106  public void testLast() {
107    assertEquals(c, sortedSet.last());
108  }
109}