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.checkerframework.checker.nullness.qual.Nullable; 032import org.junit.Ignore; 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@Ignore("test runners must not instantiate and run this directly, only via suites we build") 043// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 044@SuppressWarnings("JUnit4ClassUsedInJUnit3") 045@ElementTypesAreNonnullByDefault 046public class SortedSetNavigationTester<E extends @Nullable Object> extends AbstractSetTester<E> { 047 048 private SortedSet<E> sortedSet; 049 private List<E> values; 050 private @Nullable E a; 051 private @Nullable E b; 052 private @Nullable E c; 053 054 @Override 055 public void setUp() throws Exception { 056 super.setUp(); 057 sortedSet = (SortedSet<E>) getSet(); 058 values = 059 copyToList( 060 getSubjectGenerator() 061 .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); 062 sort(values, sortedSet.comparator()); 063 064 // some tests assume SEVERAL == 3 065 if (values.size() >= 1) { 066 a = values.get(0); 067 if (values.size() >= 3) { 068 b = values.get(1); 069 c = values.get(2); 070 } 071 } 072 } 073 074 @CollectionSize.Require(ZERO) 075 public void testEmptySetFirst() { 076 assertThrows(NoSuchElementException.class, () -> sortedSet.first()); 077 } 078 079 @CollectionSize.Require(ZERO) 080 public void testEmptySetLast() { 081 assertThrows(NoSuchElementException.class, () -> sortedSet.last()); 082 } 083 084 @CollectionSize.Require(ONE) 085 public void testSingletonSetFirst() { 086 assertEquals(a, sortedSet.first()); 087 } 088 089 @CollectionSize.Require(ONE) 090 public void testSingletonSetLast() { 091 assertEquals(a, sortedSet.last()); 092 } 093 094 @CollectionSize.Require(SEVERAL) 095 public void testFirst() { 096 assertEquals(a, sortedSet.first()); 097 } 098 099 @CollectionSize.Require(SEVERAL) 100 public void testLast() { 101 assertEquals(c, sortedSet.last()); 102 } 103}