guava
572 строки · 19.9 Кб
1/*
2* Copyright (C) 2009 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
17package com.google.common.collect.testing;
18
19import static com.google.common.collect.testing.testers.CollectionSpliteratorTester.getSpliteratorNotImmutableCollectionAllowsAddMethod;
20import static com.google.common.collect.testing.testers.CollectionSpliteratorTester.getSpliteratorNotImmutableCollectionAllowsRemoveMethod;
21import static java.util.Arrays.asList;
22
23import com.google.common.annotations.GwtIncompatible;
24import com.google.common.collect.testing.features.CollectionFeature;
25import com.google.common.collect.testing.features.CollectionSize;
26import com.google.common.collect.testing.features.SetFeature;
27import java.io.Serializable;
28import java.lang.reflect.Method;
29import java.util.AbstractSet;
30import java.util.Collection;
31import java.util.Collections;
32import java.util.Comparator;
33import java.util.EnumSet;
34import java.util.HashSet;
35import java.util.Iterator;
36import java.util.LinkedHashSet;
37import java.util.NavigableSet;
38import java.util.Set;
39import java.util.SortedSet;
40import java.util.TreeSet;
41import java.util.concurrent.ConcurrentSkipListSet;
42import java.util.concurrent.CopyOnWriteArraySet;
43import junit.framework.Test;
44import junit.framework.TestSuite;
45
46/**
47* Generates a test suite covering the {@link Set} implementations in the {@link java.util} package.
48* Can be subclassed to specify tests that should be suppressed.
49*
50* @author Kevin Bourrillion
51*/
52@GwtIncompatible
53public class TestsForSetsInJavaUtil {
54public static Test suite() {
55return new TestsForSetsInJavaUtil().allTests();
56}
57
58public Test allTests() {
59TestSuite suite = new TestSuite("java.util Sets");
60suite.addTest(testsForCheckedNavigableSet());
61suite.addTest(testsForEmptySet());
62suite.addTest(testsForEmptyNavigableSet());
63suite.addTest(testsForEmptySortedSet());
64suite.addTest(testsForSingletonSet());
65suite.addTest(testsForHashSet());
66suite.addTest(testsForLinkedHashSet());
67suite.addTest(testsForEnumSet());
68suite.addTest(testsForSynchronizedNavigableSet());
69suite.addTest(testsForTreeSetNatural());
70suite.addTest(testsForTreeSetWithComparator());
71suite.addTest(testsForCopyOnWriteArraySet());
72suite.addTest(testsForUnmodifiableSet());
73suite.addTest(testsForUnmodifiableNavigableSet());
74suite.addTest(testsForCheckedSet());
75suite.addTest(testsForCheckedSortedSet());
76suite.addTest(testsForAbstractSet());
77suite.addTest(testsForBadlyCollidingHashSet());
78suite.addTest(testsForConcurrentSkipListSetNatural());
79suite.addTest(testsForConcurrentSkipListSetWithComparator());
80
81return suite;
82}
83
84protected Collection<Method> suppressForCheckedNavigableSet() {
85return Collections.emptySet();
86}
87
88protected Collection<Method> suppressForEmptySet() {
89return Collections.emptySet();
90}
91
92protected Collection<Method> suppressForEmptyNavigableSet() {
93return Collections.emptySet();
94}
95
96protected Collection<Method> suppressForEmptySortedSet() {
97return Collections.emptySet();
98}
99
100protected Collection<Method> suppressForSingletonSet() {
101return Collections.emptySet();
102}
103
104protected Collection<Method> suppressForHashSet() {
105return Collections.emptySet();
106}
107
108protected Collection<Method> suppressForLinkedHashSet() {
109return Collections.emptySet();
110}
111
112protected Collection<Method> suppressForEnumSet() {
113return Collections.emptySet();
114}
115
116protected Collection<Method> suppressForSynchronizedNavigableSet() {
117return Collections.emptySet();
118}
119
120protected Collection<Method> suppressForTreeSetNatural() {
121return Collections.emptySet();
122}
123
124protected Collection<Method> suppressForTreeSetWithComparator() {
125return Collections.emptySet();
126}
127
128protected Collection<Method> suppressForCopyOnWriteArraySet() {
129return asList(
130getSpliteratorNotImmutableCollectionAllowsAddMethod(),
131getSpliteratorNotImmutableCollectionAllowsRemoveMethod());
132}
133
134protected Collection<Method> suppressForUnmodifiableSet() {
135return Collections.emptySet();
136}
137
138protected Collection<Method> suppressForUnmodifiableNavigableSet() {
139return Collections.emptySet();
140}
141
142protected Collection<Method> suppressForCheckedSet() {
143return Collections.emptySet();
144}
145
146protected Collection<Method> suppressForCheckedSortedSet() {
147return Collections.emptySet();
148}
149
150protected Collection<Method> suppressForAbstractSet() {
151return Collections.emptySet();
152}
153
154protected Collection<Method> suppressForConcurrentSkipListSetNatural() {
155return Collections.emptySet();
156}
157
158protected Collection<Method> suppressForConcurrentSkipListSetWithComparator() {
159return Collections.emptySet();
160}
161
162public Test testsForCheckedNavigableSet() {
163return SortedSetTestSuiteBuilder.using(
164new TestStringSortedSetGenerator() {
165@Override
166public NavigableSet<String> create(String[] elements) {
167NavigableSet<String> innerSet = new TreeSet<>();
168Collections.addAll(innerSet, elements);
169return Collections.checkedNavigableSet(innerSet, String.class);
170}
171})
172.named("checkedNavigableSet/TreeSet, natural")
173.withFeatures(
174SetFeature.GENERAL_PURPOSE,
175CollectionFeature.KNOWN_ORDER,
176CollectionFeature.SERIALIZABLE,
177CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
178CollectionFeature.RESTRICTS_ELEMENTS,
179CollectionSize.ANY)
180.suppressing(suppressForCheckedNavigableSet())
181.createTestSuite();
182}
183
184public Test testsForEmptySet() {
185return SetTestSuiteBuilder.using(
186new TestStringSetGenerator() {
187@Override
188public Set<String> create(String[] elements) {
189return Collections.emptySet();
190}
191})
192.named("emptySet")
193.withFeatures(CollectionFeature.SERIALIZABLE, CollectionSize.ZERO)
194.suppressing(suppressForEmptySet())
195.createTestSuite();
196}
197
198public Test testsForEmptyNavigableSet() {
199return SetTestSuiteBuilder.using(
200new TestStringSortedSetGenerator() {
201@Override
202public NavigableSet<String> create(String[] elements) {
203return Collections.emptyNavigableSet();
204}
205})
206.named("emptyNavigableSet")
207.withFeatures(CollectionFeature.SERIALIZABLE, CollectionSize.ZERO)
208.suppressing(suppressForEmptyNavigableSet())
209.createTestSuite();
210}
211
212public Test testsForEmptySortedSet() {
213return SetTestSuiteBuilder.using(
214new TestStringSortedSetGenerator() {
215@Override
216public SortedSet<String> create(String[] elements) {
217return Collections.emptySortedSet();
218}
219})
220.named("emptySortedSet")
221.withFeatures(CollectionFeature.SERIALIZABLE, CollectionSize.ZERO)
222.suppressing(suppressForEmptySortedSet())
223.createTestSuite();
224}
225
226public Test testsForSingletonSet() {
227return SetTestSuiteBuilder.using(
228new TestStringSetGenerator() {
229@Override
230public Set<String> create(String[] elements) {
231return Collections.singleton(elements[0]);
232}
233})
234.named("singleton")
235.withFeatures(
236CollectionFeature.SERIALIZABLE,
237CollectionFeature.ALLOWS_NULL_VALUES,
238CollectionSize.ONE)
239.suppressing(suppressForSingletonSet())
240.createTestSuite();
241}
242
243public Test testsForHashSet() {
244return SetTestSuiteBuilder.using(
245new TestStringSetGenerator() {
246@Override
247public Set<String> create(String[] elements) {
248return new HashSet<>(MinimalCollection.of(elements));
249}
250})
251.named("HashSet")
252.withFeatures(
253SetFeature.GENERAL_PURPOSE,
254CollectionFeature.SERIALIZABLE,
255CollectionFeature.ALLOWS_NULL_VALUES,
256CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
257CollectionSize.ANY)
258.suppressing(suppressForHashSet())
259.createTestSuite();
260}
261
262public Test testsForLinkedHashSet() {
263return SetTestSuiteBuilder.using(
264new TestStringSetGenerator() {
265@Override
266public Set<String> create(String[] elements) {
267return new LinkedHashSet<>(MinimalCollection.of(elements));
268}
269})
270.named("LinkedHashSet")
271.withFeatures(
272SetFeature.GENERAL_PURPOSE,
273CollectionFeature.SERIALIZABLE,
274CollectionFeature.ALLOWS_NULL_VALUES,
275CollectionFeature.KNOWN_ORDER,
276CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
277CollectionSize.ANY)
278.suppressing(suppressForLinkedHashSet())
279.createTestSuite();
280}
281
282public Test testsForEnumSet() {
283return SetTestSuiteBuilder.using(
284new TestEnumSetGenerator() {
285@Override
286public Set<AnEnum> create(AnEnum[] elements) {
287return (elements.length == 0)
288? EnumSet.noneOf(AnEnum.class)
289: EnumSet.copyOf(MinimalCollection.of(elements));
290}
291})
292.named("EnumSet")
293.withFeatures(
294SetFeature.GENERAL_PURPOSE,
295CollectionFeature.SERIALIZABLE,
296CollectionFeature.KNOWN_ORDER,
297CollectionFeature.RESTRICTS_ELEMENTS,
298CollectionSize.ANY)
299.suppressing(suppressForEnumSet())
300.createTestSuite();
301}
302
303/**
304* Tests regular NavigableSet behavior of synchronizedNavigableSet(treeSet); does not test the
305* fact that it's synchronized.
306*/
307public Test testsForSynchronizedNavigableSet() {
308return NavigableSetTestSuiteBuilder.using(
309new TestStringSortedSetGenerator() {
310@Override
311public SortedSet<String> create(String[] elements) {
312NavigableSet<String> delegate = new TreeSet<>(MinimalCollection.of(elements));
313return Collections.synchronizedNavigableSet(delegate);
314}
315})
316.named("synchronizedNavigableSet/TreeSet, natural")
317.withFeatures(
318SetFeature.GENERAL_PURPOSE,
319CollectionFeature.SERIALIZABLE,
320CollectionFeature.KNOWN_ORDER,
321CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
322CollectionSize.ANY)
323.suppressing(suppressForSynchronizedNavigableSet())
324.createTestSuite();
325}
326
327public Test testsForTreeSetNatural() {
328return NavigableSetTestSuiteBuilder.using(
329new TestStringSortedSetGenerator() {
330@Override
331public SortedSet<String> create(String[] elements) {
332return new TreeSet<>(MinimalCollection.of(elements));
333}
334})
335.named("TreeSet, natural")
336.withFeatures(
337SetFeature.GENERAL_PURPOSE,
338CollectionFeature.SERIALIZABLE,
339CollectionFeature.KNOWN_ORDER,
340CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
341CollectionSize.ANY)
342.suppressing(suppressForTreeSetNatural())
343.createTestSuite();
344}
345
346public Test testsForTreeSetWithComparator() {
347return NavigableSetTestSuiteBuilder.using(
348new TestStringSortedSetGenerator() {
349@Override
350public SortedSet<String> create(String[] elements) {
351SortedSet<String> set = new TreeSet<>(arbitraryNullFriendlyComparator());
352Collections.addAll(set, elements);
353return set;
354}
355})
356.named("TreeSet, with comparator")
357.withFeatures(
358SetFeature.GENERAL_PURPOSE,
359CollectionFeature.SERIALIZABLE,
360CollectionFeature.ALLOWS_NULL_VALUES,
361CollectionFeature.KNOWN_ORDER,
362CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
363CollectionSize.ANY)
364.suppressing(suppressForTreeSetWithComparator())
365.createTestSuite();
366}
367
368public Test testsForCopyOnWriteArraySet() {
369return SetTestSuiteBuilder.using(
370new TestStringSetGenerator() {
371@Override
372public Set<String> create(String[] elements) {
373return new CopyOnWriteArraySet<>(MinimalCollection.of(elements));
374}
375})
376.named("CopyOnWriteArraySet")
377.withFeatures(
378CollectionFeature.SUPPORTS_ADD,
379CollectionFeature.SUPPORTS_REMOVE,
380CollectionFeature.SERIALIZABLE,
381CollectionFeature.ALLOWS_NULL_VALUES,
382CollectionFeature.KNOWN_ORDER,
383CollectionSize.ANY)
384.suppressing(suppressForCopyOnWriteArraySet())
385.createTestSuite();
386}
387
388public Test testsForUnmodifiableSet() {
389return SetTestSuiteBuilder.using(
390new TestStringSetGenerator() {
391@Override
392public Set<String> create(String[] elements) {
393Set<String> innerSet = new HashSet<>();
394Collections.addAll(innerSet, elements);
395return Collections.unmodifiableSet(innerSet);
396}
397})
398.named("unmodifiableSet/HashSet")
399.withFeatures(
400CollectionFeature.NONE,
401CollectionFeature.SERIALIZABLE,
402CollectionFeature.ALLOWS_NULL_VALUES,
403CollectionSize.ANY)
404.suppressing(suppressForUnmodifiableSet())
405.createTestSuite();
406}
407
408public Test testsForUnmodifiableNavigableSet() {
409return SetTestSuiteBuilder.using(
410new TestStringSortedSetGenerator() {
411@Override
412public NavigableSet<String> create(String[] elements) {
413NavigableSet<String> innerSet = new TreeSet<>();
414Collections.addAll(innerSet, elements);
415return Collections.unmodifiableNavigableSet(innerSet);
416}
417})
418.named("unmodifiableNavigableSet/TreeSet, natural")
419.withFeatures(
420CollectionFeature.KNOWN_ORDER,
421CollectionFeature.RESTRICTS_ELEMENTS,
422CollectionFeature.SERIALIZABLE,
423CollectionSize.ANY)
424.suppressing(suppressForUnmodifiableNavigableSet())
425.createTestSuite();
426}
427
428public Test testsForCheckedSet() {
429return SetTestSuiteBuilder.using(
430new TestStringSetGenerator() {
431@Override
432public Set<String> create(String[] elements) {
433Set<String> innerSet = new HashSet<>();
434Collections.addAll(innerSet, elements);
435return Collections.checkedSet(innerSet, String.class);
436}
437})
438.named("checkedSet/HashSet")
439.withFeatures(
440SetFeature.GENERAL_PURPOSE,
441CollectionFeature.SERIALIZABLE,
442CollectionFeature.ALLOWS_NULL_VALUES,
443CollectionFeature.RESTRICTS_ELEMENTS,
444CollectionSize.ANY)
445.suppressing(suppressForCheckedSet())
446.createTestSuite();
447}
448
449public Test testsForCheckedSortedSet() {
450return SortedSetTestSuiteBuilder.using(
451new TestStringSortedSetGenerator() {
452@Override
453public SortedSet<String> create(String[] elements) {
454SortedSet<String> innerSet = new TreeSet<>();
455Collections.addAll(innerSet, elements);
456return Collections.checkedSortedSet(innerSet, String.class);
457}
458})
459.named("checkedSortedSet/TreeSet, natural")
460.withFeatures(
461SetFeature.GENERAL_PURPOSE,
462CollectionFeature.KNOWN_ORDER,
463CollectionFeature.SERIALIZABLE,
464CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION,
465CollectionFeature.RESTRICTS_ELEMENTS,
466CollectionSize.ANY)
467.suppressing(suppressForCheckedSortedSet())
468.createTestSuite();
469}
470
471public Test testsForAbstractSet() {
472return SetTestSuiteBuilder.using(
473new TestStringSetGenerator() {
474@Override
475protected Set<String> create(String[] elements) {
476final String[] deduped = dedupe(elements);
477return new AbstractSet<String>() {
478@Override
479public int size() {
480return deduped.length;
481}
482
483@Override
484public Iterator<String> iterator() {
485return MinimalCollection.of(deduped).iterator();
486}
487};
488}
489})
490.named("AbstractSet")
491.withFeatures(
492CollectionFeature.NONE,
493CollectionFeature.ALLOWS_NULL_VALUES,
494CollectionFeature.KNOWN_ORDER, // in this case, anyway
495CollectionSize.ANY)
496.suppressing(suppressForAbstractSet())
497.createTestSuite();
498}
499
500public Test testsForBadlyCollidingHashSet() {
501return SetTestSuiteBuilder.using(
502new TestCollidingSetGenerator() {
503@Override
504public Set<Object> create(Object... elements) {
505return new HashSet<>(MinimalCollection.of(elements));
506}
507})
508.named("badly colliding HashSet")
509.withFeatures(
510SetFeature.GENERAL_PURPOSE,
511CollectionFeature.ALLOWS_NULL_VALUES,
512CollectionSize.SEVERAL)
513.suppressing(suppressForHashSet())
514.createTestSuite();
515}
516
517public Test testsForConcurrentSkipListSetNatural() {
518return SetTestSuiteBuilder.using(
519new TestStringSortedSetGenerator() {
520@Override
521public SortedSet<String> create(String[] elements) {
522return new ConcurrentSkipListSet<>(MinimalCollection.of(elements));
523}
524})
525.named("ConcurrentSkipListSet, natural")
526.withFeatures(
527SetFeature.GENERAL_PURPOSE,
528CollectionFeature.SERIALIZABLE,
529CollectionFeature.KNOWN_ORDER,
530CollectionSize.ANY)
531.suppressing(suppressForConcurrentSkipListSetNatural())
532.createTestSuite();
533}
534
535public Test testsForConcurrentSkipListSetWithComparator() {
536return SetTestSuiteBuilder.using(
537new TestStringSortedSetGenerator() {
538@Override
539public SortedSet<String> create(String[] elements) {
540SortedSet<String> set =
541new ConcurrentSkipListSet<>(arbitraryNullFriendlyComparator());
542Collections.addAll(set, elements);
543return set;
544}
545})
546.named("ConcurrentSkipListSet, with comparator")
547.withFeatures(
548SetFeature.GENERAL_PURPOSE,
549CollectionFeature.SERIALIZABLE,
550CollectionFeature.KNOWN_ORDER,
551CollectionSize.ANY)
552.suppressing(suppressForConcurrentSkipListSetWithComparator())
553.createTestSuite();
554}
555
556private static String[] dedupe(String[] elements) {
557Set<String> tmp = new LinkedHashSet<>();
558Collections.addAll(tmp, elements);
559return tmp.toArray(new String[0]);
560}
561
562static <T> Comparator<T> arbitraryNullFriendlyComparator() {
563return new NullFriendlyComparator<>();
564}
565
566private static final class NullFriendlyComparator<T> implements Comparator<T>, Serializable {
567@Override
568public int compare(T left, T right) {
569return String.valueOf(left).compareTo(String.valueOf(right));
570}
571}
572}
573