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@ElementTypesAreNonnullByDefault
043public class SortedSetNavigationTester<E extends @Nullable Object> extends AbstractSetTester<E> {
044
045  private SortedSet<E> sortedSet;
046  private List<E> values;
047  private @Nullable E a;
048  private @Nullable E b;
049  private @Nullable E c;
050
051  @Override
052  public void setUp() throws Exception {
053    super.setUp();
054    sortedSet = (SortedSet<E>) getSet();
055    values =
056        Helpers.copyToList(
057            getSubjectGenerator()
058                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
059    Collections.sort(values, sortedSet.comparator());
060
061    // some tests assume SEVERAL == 3
062    if (values.size() >= 1) {
063      a = values.get(0);
064      if (values.size() >= 3) {
065        b = values.get(1);
066        c = values.get(2);
067      }
068    }
069  }
070
071  @CollectionSize.Require(ZERO)
072  public void testEmptySetFirst() {
073    try {
074      sortedSet.first();
075      fail();
076    } catch (NoSuchElementException e) {
077    }
078  }
079
080  @CollectionSize.Require(ZERO)
081  public void testEmptySetLast() {
082    try {
083      sortedSet.last();
084      fail();
085    } catch (NoSuchElementException e) {
086    }
087  }
088
089  @CollectionSize.Require(ONE)
090  public void testSingletonSetFirst() {
091    assertEquals(a, sortedSet.first());
092  }
093
094  @CollectionSize.Require(ONE)
095  public void testSingletonSetLast() {
096    assertEquals(a, sortedSet.last());
097  }
098
099  @CollectionSize.Require(SEVERAL)
100  public void testFirst() {
101    assertEquals(a, sortedSet.first());
102  }
103
104  @CollectionSize.Require(SEVERAL)
105  public void testLast() {
106    assertEquals(c, sortedSet.last());
107  }
108}