aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/collect/SynchronizedNavigableSetTest.java
blob: 9350f6205ba33fa5718bc492413709f4d3ff0c3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (C) 2010 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import com.google.common.collect.Synchronized.SynchronizedNavigableSet;
import com.google.common.collect.Synchronized.SynchronizedSortedSet;
import com.google.common.collect.testing.NavigableSetTestSuiteBuilder;
import com.google.common.collect.testing.SafeTreeSet;
import com.google.common.collect.testing.TestStringSetGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.testing.SerializableTester;

import junit.framework.TestSuite;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Tests for {@link Sets#synchronizedNavigableSet(NavigableSet)}.
 *
 * @author Louis Wasserman
 */
public class SynchronizedNavigableSetTest extends SynchronizedSetTest {
  @SuppressWarnings("unchecked")
      @Override protected <E> NavigableSet<E> create() {
    TestSet<E> inner = new TestSet<E>(
        new TreeSet<E>((Comparator<E>) Ordering.natural().nullsFirst()), mutex);
    NavigableSet<E> outer =
        Synchronized.navigableSet(inner, mutex);
    return outer;
  }

  static class TestSet<E> extends SynchronizedSetTest.TestSet<E>
      implements NavigableSet<E> {

    TestSet(NavigableSet<E> delegate, Object mutex) {
      super(delegate, mutex);
    }

    @Override protected NavigableSet<E> delegate() {
      return (NavigableSet<E>) super.delegate();
    }

    @Override public E ceiling(E e) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().ceiling(e);
    }

    @Override public Iterator<E> descendingIterator() {
      return delegate().descendingIterator();
    }

    @Override public NavigableSet<E> descendingSet() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().descendingSet();
    }

    @Override public E floor(E e) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().floor(e);
    }

    @Override public NavigableSet<E> headSet(E toElement, boolean inclusive) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().headSet(toElement, inclusive);
    }

    @Override public SortedSet<E> headSet(E toElement) {
      return headSet(toElement, false);
    }

    @Override public E higher(E e) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().higher(e);
    }

    @Override public E lower(E e) {
      return delegate().lower(e);
    }

    @Override public E pollFirst() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().pollFirst();
    }

    @Override public E pollLast() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().pollLast();
    }

    @Override public NavigableSet<E> subSet(E fromElement,
        boolean fromInclusive, E toElement, boolean toInclusive) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().subSet(
          fromElement, fromInclusive, toElement, toInclusive);
    }

    @Override public SortedSet<E> subSet(E fromElement, E toElement) {
      return subSet(fromElement, true, toElement, false);
    }

    @Override public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().tailSet(fromElement, inclusive);
    }

    @Override public SortedSet<E> tailSet(E fromElement) {
      return tailSet(fromElement, true);
    }

    @Override public Comparator<? super E> comparator() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().comparator();
    }

    @Override public E first() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().first();
    }

    @Override public E last() {
      assertTrue(Thread.holdsLock(mutex));
      return delegate().last();
    }

    private static final long serialVersionUID = 0;
  }

  public static TestSuite suite() {
    TestSuite suite = new TestSuite();
    suite.addTestSuite(SynchronizedNavigableSetTest.class);
    suite.addTest(
        NavigableSetTestSuiteBuilder.using(new TestStringSetGenerator() {
          private final Object mutex = new Integer(1);

          @Override protected Set<String> create(String[] elements) {
            NavigableSet<String> innermost = new SafeTreeSet<String>();
            innermost.addAll(Arrays.asList(elements));
            TestSet<String> inner = new TestSet<String>(innermost, mutex);
            NavigableSet<String> outer =
                Synchronized.navigableSet(inner, mutex);
            return outer;
          }

          @Override public List<String> order(List<String> insertionOrder) {
            return Ordering.natural().sortedCopy(insertionOrder);
          }
        }).named("Sets.synchronizedNavigableSet[SafeTreeSet]")
            .withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER,
                CollectionFeature.GENERAL_PURPOSE).createTestSuite());

    return suite;
  }

  public void testComparator() {
    create().comparator();
  }

  public void testCeiling() {
    create().ceiling("a");
  }

  public void testFloor() {
    create().floor("a");
  }

  public void testHigher() {
    create().higher("a");
  }

  public void testLower() {
    create().lower("a");
  }

  public void testDescendingSet() {
    NavigableSet<String> map = create();
    NavigableSet<String> descendingSet = map.descendingSet();
    assertTrue(descendingSet instanceof SynchronizedNavigableSet);
    assertSame(mutex, ((SynchronizedNavigableSet<String>) descendingSet).mutex);
  }

  public void testFirst() {
    NavigableSet<String> set = create();
    set.add("a");
    set.first();
  }

  public void testPollFirst() {
    create().pollFirst();
  }

  public void testLast() {
    NavigableSet<String> set = create();
    set.add("a");
    set.last();
  }

  public void testPollLast() {
    create().pollLast();
  }

  public void testHeadSet_E() {
    NavigableSet<String> map = create();
    SortedSet<String> headSet = map.headSet("a");
    assertTrue(headSet instanceof SynchronizedSortedSet);
    assertSame(mutex, ((SynchronizedSortedSet<String>) headSet).mutex);
  }

  public void testHeadSet_E_B() {
    NavigableSet<String> map = create();
    NavigableSet<String> headSet = map.headSet("a", true);
    assertTrue(headSet instanceof SynchronizedNavigableSet);
    assertSame(mutex, ((SynchronizedNavigableSet<String>) headSet).mutex);
  }

  public void testSubSet_E_E() {
    NavigableSet<String> map = create();
    SortedSet<String> subSet = map.subSet("a", "b");
    assertTrue(subSet instanceof SynchronizedSortedSet);
    assertSame(mutex, ((SynchronizedSortedSet<String>) subSet).mutex);
  }

  public void testSubSet_E_B_E_B() {
    NavigableSet<String> map = create();
    NavigableSet<String> subSet = map.subSet("a", false, "b", true);
    assertTrue(subSet instanceof SynchronizedNavigableSet);
    assertSame(mutex, ((SynchronizedNavigableSet<String>) subSet).mutex);
  }

  public void testTailSet_E() {
    NavigableSet<String> map = create();
    SortedSet<String> tailSet = map.tailSet("a");
    assertTrue(tailSet instanceof SynchronizedSortedSet);
    assertSame(mutex, ((SynchronizedSortedSet<String>) tailSet).mutex);
  }

  public void testTailSet_E_B() {
    NavigableSet<String> map = create();
    NavigableSet<String> tailSet = map.tailSet("a", true);
    assertTrue(tailSet instanceof SynchronizedNavigableSet);
    assertSame(mutex, ((SynchronizedNavigableSet<String>) tailSet).mutex);
  }

  @Override public void testSerialization() {
    SerializableTester.reserializeAndAssert(create());
  }
}