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.Helpers.copyToList;
020import static com.google.common.collect.testing.features.CollectionSize.ONE;
021import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
022import static com.google.common.collect.testing.features.CollectionSize.ZERO;
023import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows;
024import static java.util.Collections.sort;
025
026import com.google.common.annotations.GwtCompatible;
027import com.google.common.collect.testing.features.CollectionSize;
028import java.util.List;
029import java.util.NoSuchElementException;
030import java.util.SortedSet;
031import org.jspecify.annotations.NullMarked;
032import org.jspecify.annotations.Nullable;
033import org.junit.Ignore;
034
035/**
036 * A generic JUnit test which tests operations on a SortedSet. Can't be invoked directly; please see
037 * {@code SortedSetTestSuiteBuilder}.
038 *
039 * @author Jesse Wilson
040 * @author Louis Wasserman
041 */
042@GwtCompatible
043@Ignore("test runners must not instantiate and run this directly, only via suites we build")
044// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
045@SuppressWarnings("JUnit4ClassUsedInJUnit3")
046@NullMarked
047public class SortedSetNavigationTester<E extends @Nullable Object> extends AbstractSetTester<E> {
048
049  private SortedSet<E> sortedSet;
050  private List<E> values;
051  private @Nullable E a;
052  private @Nullable E b;
053  private @Nullable E c;
054
055  @Override
056  public void setUp() throws Exception {
057    super.setUp();
058    sortedSet = (SortedSet<E>) getSet();
059    values =
060        copyToList(
061            getSubjectGenerator()
062                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
063    sort(values, sortedSet.comparator());
064
065    // some tests assume SEVERAL == 3
066    if (values.size() >= 1) {
067      a = values.get(0);
068      if (values.size() >= 3) {
069        b = values.get(1);
070        c = values.get(2);
071      }
072    }
073  }
074
075  @CollectionSize.Require(ZERO)
076  public void testEmptySetFirst() {
077    assertThrows(NoSuchElementException.class, () -> sortedSet.first());
078  }
079
080  @CollectionSize.Require(ZERO)
081  public void testEmptySetLast() {
082    assertThrows(NoSuchElementException.class, () -> sortedSet.last());
083  }
084
085  @CollectionSize.Require(ONE)
086  public void testSingletonSetFirst() {
087    assertEquals(a, sortedSet.first());
088  }
089
090  @CollectionSize.Require(ONE)
091  public void testSingletonSetLast() {
092    assertEquals(a, sortedSet.last());
093  }
094
095  @CollectionSize.Require(SEVERAL)
096  public void testFirst() {
097    assertEquals(a, sortedSet.first());
098  }
099
100  @CollectionSize.Require(SEVERAL)
101  public void testLast() {
102    assertEquals(c, sortedSet.last());
103  }
104}