1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import com.google.common.base.Function; 20 import com.google.common.collect.testing.SafeTreeSet; 21 import com.google.common.collect.testing.SetTestSuiteBuilder; 22 import com.google.common.collect.testing.TestStringSetGenerator; 23 import com.google.common.collect.testing.features.CollectionFeature; 24 import com.google.common.collect.testing.features.CollectionSize; 25 import com.google.common.testing.EqualsTester; 26 import com.google.common.testing.ForwardingWrapperTester; 27 import java.util.Arrays; 28 import java.util.Collection; 29 import java.util.Collections; 30 import java.util.List; 31 import java.util.NavigableSet; 32 import java.util.Set; 33 import java.util.SortedSet; 34 import junit.framework.Test; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 import org.checkerframework.checker.nullness.qual.Nullable; 38 39 /** 40 * Tests for {@code ForwardingNavigableSet}. 41 * 42 * @author Louis Wasserman 43 */ 44 public class ForwardingNavigableSetTest extends TestCase { 45 static class StandardImplForwardingNavigableSet<T> extends ForwardingNavigableSet<T> { 46 private final NavigableSet<T> backingSet; 47 StandardImplForwardingNavigableSet(NavigableSet<T> backingSet)48 StandardImplForwardingNavigableSet(NavigableSet<T> backingSet) { 49 this.backingSet = backingSet; 50 } 51 52 @Override delegate()53 protected NavigableSet<T> delegate() { 54 return backingSet; 55 } 56 57 @Override equals(@ullable Object object)58 public boolean equals(@Nullable Object object) { 59 return standardEquals(object); 60 } 61 62 @Override hashCode()63 public int hashCode() { 64 return standardHashCode(); 65 } 66 67 @Override addAll(Collection<? extends T> collection)68 public boolean addAll(Collection<? extends T> collection) { 69 return standardAddAll(collection); 70 } 71 72 @Override clear()73 public void clear() { 74 standardClear(); 75 } 76 77 @Override contains(Object object)78 public boolean contains(Object object) { 79 return standardContains(object); 80 } 81 82 @Override containsAll(Collection<?> collection)83 public boolean containsAll(Collection<?> collection) { 84 return standardContainsAll(collection); 85 } 86 87 @Override remove(Object object)88 public boolean remove(Object object) { 89 return standardRemove(object); 90 } 91 92 @Override removeAll(Collection<?> collection)93 public boolean removeAll(Collection<?> collection) { 94 return standardRemoveAll(collection); 95 } 96 97 @Override retainAll(Collection<?> collection)98 public boolean retainAll(Collection<?> collection) { 99 return standardRetainAll(collection); 100 } 101 102 @Override toArray()103 public Object[] toArray() { 104 return standardToArray(); 105 } 106 107 @Override toArray(T[] array)108 public <T> T[] toArray(T[] array) { 109 return standardToArray(array); 110 } 111 112 @Override toString()113 public String toString() { 114 return standardToString(); 115 } 116 117 @Override subSet(T fromElement, T toElement)118 public SortedSet<T> subSet(T fromElement, T toElement) { 119 return standardSubSet(fromElement, toElement); 120 } 121 122 @Override lower(T e)123 public @Nullable T lower(T e) { 124 return standardLower(e); 125 } 126 127 @Override floor(T e)128 public @Nullable T floor(T e) { 129 return standardFloor(e); 130 } 131 132 @Override ceiling(T e)133 public @Nullable T ceiling(T e) { 134 return standardCeiling(e); 135 } 136 137 @Override higher(T e)138 public @Nullable T higher(T e) { 139 return standardHigher(e); 140 } 141 142 @Override pollFirst()143 public @Nullable T pollFirst() { 144 return standardPollFirst(); 145 } 146 147 @Override pollLast()148 public @Nullable T pollLast() { 149 return standardPollLast(); 150 } 151 152 @Override headSet(T toElement)153 public SortedSet<T> headSet(T toElement) { 154 return standardHeadSet(toElement); 155 } 156 157 @Override tailSet(T fromElement)158 public SortedSet<T> tailSet(T fromElement) { 159 return standardTailSet(fromElement); 160 } 161 } 162 suite()163 public static Test suite() { 164 TestSuite suite = new TestSuite(); 165 166 suite.addTestSuite(ForwardingNavigableSetTest.class); 167 suite.addTest( 168 SetTestSuiteBuilder.using( 169 new TestStringSetGenerator() { 170 @Override 171 protected Set<String> create(String[] elements) { 172 return new StandardImplForwardingNavigableSet<>( 173 new SafeTreeSet<String>(Arrays.asList(elements))); 174 } 175 176 @Override 177 public List<String> order(List<String> insertionOrder) { 178 return Lists.newArrayList(Sets.newTreeSet(insertionOrder)); 179 } 180 }) 181 .named("ForwardingNavigableSet[SafeTreeSet] with standard implementations") 182 .withFeatures( 183 CollectionSize.ANY, 184 CollectionFeature.KNOWN_ORDER, 185 CollectionFeature.GENERAL_PURPOSE) 186 .createTestSuite()); 187 suite.addTest( 188 SetTestSuiteBuilder.using( 189 new TestStringSetGenerator() { 190 @Override 191 protected Set<String> create(String[] elements) { 192 SafeTreeSet<String> set = new SafeTreeSet<>(Ordering.natural().nullsFirst()); 193 Collections.addAll(set, elements); 194 return new StandardImplForwardingNavigableSet<>(set); 195 } 196 197 @Override 198 public List<String> order(List<String> insertionOrder) { 199 return Lists.newArrayList(Sets.newTreeSet(insertionOrder)); 200 } 201 }) 202 .named( 203 "ForwardingNavigableSet[SafeTreeSet[Ordering.natural.nullsFirst]]" 204 + " with standard implementations") 205 .withFeatures( 206 CollectionSize.ANY, 207 CollectionFeature.KNOWN_ORDER, 208 CollectionFeature.GENERAL_PURPOSE, 209 CollectionFeature.ALLOWS_NULL_VALUES) 210 .createTestSuite()); 211 212 return suite; 213 } 214 215 @SuppressWarnings({"rawtypes", "unchecked"}) testForwarding()216 public void testForwarding() { 217 new ForwardingWrapperTester() 218 .testForwarding( 219 NavigableSet.class, 220 new Function<NavigableSet, NavigableSet>() { 221 @Override 222 public NavigableSet apply(NavigableSet delegate) { 223 return wrap(delegate); 224 } 225 }); 226 } 227 testEquals()228 public void testEquals() { 229 NavigableSet<String> set1 = ImmutableSortedSet.of("one"); 230 NavigableSet<String> set2 = ImmutableSortedSet.of("two"); 231 new EqualsTester() 232 .addEqualityGroup(set1, wrap(set1), wrap(set1)) 233 .addEqualityGroup(set2, wrap(set2)) 234 .testEquals(); 235 } 236 wrap(final NavigableSet<T> delegate)237 private static <T> NavigableSet<T> wrap(final NavigableSet<T> delegate) { 238 return new ForwardingNavigableSet<T>() { 239 @Override 240 protected NavigableSet<T> delegate() { 241 return delegate; 242 } 243 }; 244 } 245 } 246