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.junit.Ignore;
031
032/**
033 * A generic JUnit test which tests operations on a SortedSet. Can't be invoked directly; please see
034 * {@code SortedSetTestSuiteBuilder}.
035 *
036 * @author Jesse Wilson
037 * @author Louis Wasserman
038 */
039@GwtCompatible
040@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
041public class SortedSetNavigationTester<E> extends AbstractSetTester<E> {
042
043  private SortedSet<E> sortedSet;
044  private List<E> values;
045  private E a;
046  private E b;
047  private E c;
048
049  @Override
050  public void setUp() throws Exception {
051    super.setUp();
052    sortedSet = (SortedSet<E>) getSet();
053    values =
054        Helpers.copyToList(
055            getSubjectGenerator()
056                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
057    Collections.sort(values, sortedSet.comparator());
058
059    // some tests assume SEVERAL == 3
060    if (values.size() >= 1) {
061      a = values.get(0);
062      if (values.size() >= 3) {
063        b = values.get(1);
064        c = values.get(2);
065      }
066    }
067  }
068
069  @CollectionSize.Require(ZERO)
070  public void testEmptySetFirst() {
071    try {
072      sortedSet.first();
073      fail();
074    } catch (NoSuchElementException e) {
075    }
076  }
077
078  @CollectionSize.Require(ZERO)
079  public void testEmptySetLast() {
080    try {
081      sortedSet.last();
082      fail();
083    } catch (NoSuchElementException e) {
084    }
085  }
086
087  @CollectionSize.Require(ONE)
088  public void testSingletonSetFirst() {
089    assertEquals(a, sortedSet.first());
090  }
091
092  @CollectionSize.Require(ONE)
093  public void testSingletonSetLast() {
094    assertEquals(a, sortedSet.last());
095  }
096
097  @CollectionSize.Require(SEVERAL)
098  public void testFirst() {
099    assertEquals(a, sortedSet.first());
100  }
101
102  @CollectionSize.Require(SEVERAL)
103  public void testLast() {
104    assertEquals(c, sortedSet.last());
105  }
106}