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 java.util.Collections.sort;
024import static org.junit.Assert.assertThrows;
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;
033
034/**
035 * A generic JUnit test which tests operations on a SortedSet. Can't be invoked directly; please see
036 * {@code SortedSetTestSuiteBuilder}.
037 *
038 * @author Jesse Wilson
039 * @author Louis Wasserman
040 */
041@GwtCompatible
042@NullMarked
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 first;
048  private @Nullable E third;
049
050  @Override
051  public void setUp() throws Exception {
052    super.setUp();
053    sortedSet = (SortedSet<E>) getSet();
054    values =
055        copyToList(
056            getSubjectGenerator()
057                .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
058    sort(values, sortedSet.comparator());
059
060    // some tests assume SEVERAL == 3
061    if (values.size() >= 1) {
062      first = values.get(0);
063      if (values.size() >= 3) {
064        third = values.get(2);
065      }
066    }
067  }
068
069  @CollectionSize.Require(ZERO)
070  public void testEmptySetFirst() {
071    assertThrows(NoSuchElementException.class, () -> sortedSet.first());
072  }
073
074  @CollectionSize.Require(ZERO)
075  public void testEmptySetLast() {
076    assertThrows(NoSuchElementException.class, () -> sortedSet.last());
077  }
078
079  @CollectionSize.Require(absent = ZERO)
080  public void testFirst() {
081    assertEquals(first, sortedSet.first());
082  }
083
084  @CollectionSize.Require(ONE)
085  public void testSingletonSetLast() {
086    assertEquals(first, sortedSet.last());
087  }
088
089  @CollectionSize.Require(SEVERAL)
090  public void testLargerSetLast() {
091    assertEquals(third, sortedSet.last());
092  }
093}