• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.collect.Synchronized.SynchronizedNavigableSet;
20 import com.google.common.collect.Synchronized.SynchronizedSortedSet;
21 import com.google.common.collect.testing.NavigableSetTestSuiteBuilder;
22 import com.google.common.collect.testing.SafeTreeSet;
23 import com.google.common.collect.testing.TestStringSortedSetGenerator;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.NavigableSet;
31 import java.util.SortedSet;
32 import java.util.TreeSet;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35 import org.checkerframework.checker.nullness.qual.Nullable;
36 
37 /**
38  * Tests for {@link Sets#synchronizedNavigableSet(NavigableSet)}.
39  *
40  * @author Louis Wasserman
41  */
42 public class SynchronizedNavigableSetTest extends TestCase {
43 
44   @SuppressWarnings("unchecked")
create()45   protected <E> NavigableSet<E> create() {
46     TestSet<E> inner =
47         new TestSet<>(new TreeSet<E>((Comparator<E>) Ordering.natural().nullsFirst()), null);
48     NavigableSet<E> outer = Synchronized.navigableSet(inner, null);
49     inner.mutex = outer;
50     return outer;
51   }
52 
53   static class TestSet<E> extends SynchronizedSetTest.TestSet<E> implements NavigableSet<E> {
54 
TestSet(NavigableSet<E> delegate, @Nullable Object mutex)55     TestSet(NavigableSet<E> delegate, @Nullable Object mutex) {
56       super(delegate, mutex);
57     }
58 
59     @Override
delegate()60     protected NavigableSet<E> delegate() {
61       return (NavigableSet<E>) super.delegate();
62     }
63 
64     @Override
ceiling(E e)65     public @Nullable E ceiling(E e) {
66       assertTrue(Thread.holdsLock(mutex));
67       return delegate().ceiling(e);
68     }
69 
70     @Override
descendingIterator()71     public Iterator<E> descendingIterator() {
72       return delegate().descendingIterator();
73     }
74 
75     @Override
descendingSet()76     public NavigableSet<E> descendingSet() {
77       assertTrue(Thread.holdsLock(mutex));
78       return delegate().descendingSet();
79     }
80 
81     @Override
floor(E e)82     public @Nullable E floor(E e) {
83       assertTrue(Thread.holdsLock(mutex));
84       return delegate().floor(e);
85     }
86 
87     @Override
headSet(E toElement, boolean inclusive)88     public NavigableSet<E> headSet(E toElement, boolean inclusive) {
89       assertTrue(Thread.holdsLock(mutex));
90       return delegate().headSet(toElement, inclusive);
91     }
92 
93     @Override
headSet(E toElement)94     public SortedSet<E> headSet(E toElement) {
95       return headSet(toElement, false);
96     }
97 
98     @Override
higher(E e)99     public @Nullable E higher(E e) {
100       assertTrue(Thread.holdsLock(mutex));
101       return delegate().higher(e);
102     }
103 
104     @Override
lower(E e)105     public @Nullable E lower(E e) {
106       return delegate().lower(e);
107     }
108 
109     @Override
pollFirst()110     public @Nullable E pollFirst() {
111       assertTrue(Thread.holdsLock(mutex));
112       return delegate().pollFirst();
113     }
114 
115     @Override
pollLast()116     public @Nullable E pollLast() {
117       assertTrue(Thread.holdsLock(mutex));
118       return delegate().pollLast();
119     }
120 
121     @Override
subSet( E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)122     public NavigableSet<E> subSet(
123         E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
124       assertTrue(Thread.holdsLock(mutex));
125       return delegate().subSet(fromElement, fromInclusive, toElement, toInclusive);
126     }
127 
128     @Override
subSet(E fromElement, E toElement)129     public SortedSet<E> subSet(E fromElement, E toElement) {
130       return subSet(fromElement, true, toElement, false);
131     }
132 
133     @Override
tailSet(E fromElement, boolean inclusive)134     public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
135       assertTrue(Thread.holdsLock(mutex));
136       return delegate().tailSet(fromElement, inclusive);
137     }
138 
139     @Override
tailSet(E fromElement)140     public SortedSet<E> tailSet(E fromElement) {
141       return tailSet(fromElement, true);
142     }
143 
144     @Override
comparator()145     public Comparator<? super E> comparator() {
146       assertTrue(Thread.holdsLock(mutex));
147       return delegate().comparator();
148     }
149 
150     @Override
first()151     public E first() {
152       assertTrue(Thread.holdsLock(mutex));
153       return delegate().first();
154     }
155 
156     @Override
last()157     public E last() {
158       assertTrue(Thread.holdsLock(mutex));
159       return delegate().last();
160     }
161 
162     private static final long serialVersionUID = 0;
163   }
164 
suite()165   public static TestSuite suite() {
166     TestSuite suite = new TestSuite();
167     suite.addTestSuite(SynchronizedNavigableSetTest.class);
168     suite.addTest(
169         NavigableSetTestSuiteBuilder.using(
170                 new TestStringSortedSetGenerator() {
171 
172                   @Override
173                   protected NavigableSet<String> create(String[] elements) {
174                     NavigableSet<String> innermost = new SafeTreeSet<>();
175                     Collections.addAll(innermost, elements);
176                     TestSet<String> inner = new TestSet<>(innermost, null);
177                     NavigableSet<String> outer = Synchronized.navigableSet(inner, null);
178                     inner.mutex = outer;
179                     return outer;
180                   }
181 
182                   @Override
183                   public List<String> order(List<String> insertionOrder) {
184                     return Ordering.natural().sortedCopy(insertionOrder);
185                   }
186                 })
187             .named("Sets.synchronizedNavigableSet[SafeTreeSet]")
188             .withFeatures(
189                 CollectionSize.ANY,
190                 CollectionFeature.KNOWN_ORDER,
191                 CollectionFeature.GENERAL_PURPOSE,
192                 CollectionFeature.SERIALIZABLE)
193             .createTestSuite());
194 
195     return suite;
196   }
197 
testDescendingSet()198   public void testDescendingSet() {
199     NavigableSet<String> set = create();
200     NavigableSet<String> descendingSet = set.descendingSet();
201     assertTrue(descendingSet instanceof SynchronizedNavigableSet);
202     assertSame(set, ((SynchronizedNavigableSet<String>) descendingSet).mutex);
203   }
204 
testHeadSet_E()205   public void testHeadSet_E() {
206     NavigableSet<String> set = create();
207     SortedSet<String> headSet = set.headSet("a");
208     assertTrue(headSet instanceof SynchronizedSortedSet);
209     assertSame(set, ((SynchronizedSortedSet<String>) headSet).mutex);
210   }
211 
testHeadSet_E_B()212   public void testHeadSet_E_B() {
213     NavigableSet<String> set = create();
214     NavigableSet<String> headSet = set.headSet("a", true);
215     assertTrue(headSet instanceof SynchronizedNavigableSet);
216     assertSame(set, ((SynchronizedNavigableSet<String>) headSet).mutex);
217   }
218 
testSubSet_E_E()219   public void testSubSet_E_E() {
220     NavigableSet<String> set = create();
221     SortedSet<String> subSet = set.subSet("a", "b");
222     assertTrue(subSet instanceof SynchronizedSortedSet);
223     assertSame(set, ((SynchronizedSortedSet<String>) subSet).mutex);
224   }
225 
testSubSet_E_B_E_B()226   public void testSubSet_E_B_E_B() {
227     NavigableSet<String> set = create();
228     NavigableSet<String> subSet = set.subSet("a", false, "b", true);
229     assertTrue(subSet instanceof SynchronizedNavigableSet);
230     assertSame(set, ((SynchronizedNavigableSet<String>) subSet).mutex);
231   }
232 
testTailSet_E()233   public void testTailSet_E() {
234     NavigableSet<String> set = create();
235     SortedSet<String> tailSet = set.tailSet("a");
236     assertTrue(tailSet instanceof SynchronizedSortedSet);
237     assertSame(set, ((SynchronizedSortedSet<String>) tailSet).mutex);
238   }
239 
testTailSet_E_B()240   public void testTailSet_E_B() {
241     NavigableSet<String> set = create();
242     NavigableSet<String> tailSet = set.tailSet("a", true);
243     assertTrue(tailSet instanceof SynchronizedNavigableSet);
244     assertSame(set, ((SynchronizedNavigableSet<String>) tailSet).mutex);
245   }
246 }
247