| 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.testing; |
| 18 |
|
| 19 |
import static com.google.common.collect.testing.features.CollectionFeature.DESCENDING_VIEW; |
| 20 |
import static com.google.common.collect.testing.features.CollectionFeature.SUBSET_VIEW; |
| 21 |
|
| 22 |
import com.google.common.annotations.GwtIncompatible; |
| 23 |
import com.google.common.collect.testing.DerivedCollectionGenerators.Bound; |
| 24 |
import com.google.common.collect.testing.DerivedCollectionGenerators.SortedSetSubsetTestSetGenerator; |
| 25 |
import com.google.common.collect.testing.features.Feature; |
| 26 |
import com.google.common.collect.testing.testers.NavigableSetNavigationTester; |
| 27 |
import java.util.ArrayList; |
| 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.TestSuite; |
| 35 |
|
| 36 |
/** |
| 37 |
* Creates, based on your criteria, a JUnit test suite that exhaustively tests a NavigableSet |
| 38 |
* implementation. |
| 39 |
*/ |
| 40 |
@GwtIncompatible |
| 41 |
public final class NavigableSetTestSuiteBuilder<E> extends SortedSetTestSuiteBuilder<E> { |
| 42 |
public static <E> NavigableSetTestSuiteBuilder<E> using(TestSortedSetGenerator<E> generator) { |
| 43 |
NavigableSetTestSuiteBuilder<E> builder = new NavigableSetTestSuiteBuilder<>(); |
| 44 |
builder.usingGenerator(generator); |
| 45 |
return builder; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
protected List<TestSuite> createDerivedSuites( |
| 50 |
FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> |
| 51 |
parentBuilder) { |
| 52 |
List<TestSuite> derivedSuites = new ArrayList<>(super.createDerivedSuites(parentBuilder)); |
| 53 |
|
| 54 |
if (!parentBuilder.getFeatures().contains(SUBSET_VIEW)) { |
| 55 |
// Other combinations are inherited from SortedSetTestSuiteBuilder. |
| 56 |
derivedSuites.add(createSubsetSuite(parentBuilder, Bound.NO_BOUND, Bound.INCLUSIVE)); |
| 57 |
derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.NO_BOUND)); |
| 58 |
derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.EXCLUSIVE)); |
| 59 |
derivedSuites.add(createSubsetSuite(parentBuilder, Bound.EXCLUSIVE, Bound.INCLUSIVE)); |
| 60 |
derivedSuites.add(createSubsetSuite(parentBuilder, Bound.INCLUSIVE, Bound.INCLUSIVE)); |
| 61 |
} |
| 62 |
if (!parentBuilder.getFeatures().contains(DESCENDING_VIEW)) { |
| 63 |
derivedSuites.add(createDescendingSuite(parentBuilder)); |
| 64 |
} |
| 65 |
return derivedSuites; |
| 66 |
} |
| 67 |
|
| 68 |
public static final class NavigableSetSubsetTestSetGenerator<E> |
| 69 |
extends SortedSetSubsetTestSetGenerator<E> { |
| 70 |
public NavigableSetSubsetTestSetGenerator( |
| 71 |
TestSortedSetGenerator<E> delegate, Bound to, Bound from) { |
| 72 |
super(delegate, to, from); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
NavigableSet<E> createSubSet(SortedSet<E> sortedSet, E firstExclusive, E lastExclusive) { |
| 77 |
NavigableSet<E> set = (NavigableSet<E>) sortedSet; |
| 78 |
if (from == Bound.NO_BOUND && to == Bound.INCLUSIVE) { |
| 79 |
return set.headSet(lastInclusive, true); |
| 80 |
} else if (from == Bound.EXCLUSIVE && to == Bound.NO_BOUND) { |
| 81 |
return set.tailSet(firstExclusive, false); |
| 82 |
} else if (from == Bound.EXCLUSIVE && to == Bound.EXCLUSIVE) { |
| 83 |
return set.subSet(firstExclusive, false, lastExclusive, false); |
| 84 |
} else if (from == Bound.EXCLUSIVE && to == Bound.INCLUSIVE) { |
| 85 |
return set.subSet(firstExclusive, false, lastInclusive, true); |
| 86 |
} else if (from == Bound.INCLUSIVE && to == Bound.INCLUSIVE) { |
| 87 |
return set.subSet(firstInclusive, true, lastInclusive, true); |
| 88 |
} else { |
| 89 |
return (NavigableSet<E>) super.createSubSet(set, firstExclusive, lastExclusive); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public NavigableSetTestSuiteBuilder<E> newBuilderUsing( |
| 96 |
TestSortedSetGenerator<E> delegate, Bound to, Bound from) { |
| 97 |
return using(new NavigableSetSubsetTestSetGenerator<E>(delegate, to, from)); |
| 98 |
} |
| 99 |
|
| 100 |
/** Create a suite whose maps are descending views of other maps. */ |
| 101 |
private TestSuite createDescendingSuite( |
| 102 |
FeatureSpecificTestSuiteBuilder<?, ? extends OneSizeTestContainerGenerator<Collection<E>, E>> |
| 103 |
parentBuilder) { |
| 104 |
TestSetGenerator<E> delegate = |
| 105 |
(TestSetGenerator<E>) parentBuilder.getSubjectGenerator().getInnerGenerator(); |
| 106 |
|
| 107 |
List<Feature<?>> features = new ArrayList<>(); |
| 108 |
features.add(DESCENDING_VIEW); |
| 109 |
features.addAll(parentBuilder.getFeatures()); |
| 110 |
|
| 111 |
return NavigableSetTestSuiteBuilder.using( |
| 112 |
new TestSetGenerator<E>() { |
| 113 |
|
| 114 |
@Override |
| 115 |
public SampleElements<E> samples() { |
| 116 |
return delegate.samples(); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public E[] createArray(int length) { |
| 121 |
return delegate.createArray(length); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public Iterable<E> order(List<E> insertionOrder) { |
| 126 |
List<E> list = new ArrayList<>(); |
| 127 |
for (E e : delegate.order(insertionOrder)) { |
| 128 |
list.add(e); |
| 129 |
} |
| 130 |
Collections.reverse(list); |
| 131 |
return list; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public Set<E> create(Object... elements) { |
| 136 |
NavigableSet<E> navigableSet = (NavigableSet<E>) delegate.create(elements); |
| 137 |
return navigableSet.descendingSet(); |
| 138 |
} |
| 139 |
}) |
| 140 |
.named(parentBuilder.getName() + " descending") |
| 141 |
.withFeatures(features) |
| 142 |
.suppressing(parentBuilder.getSuppressedTests()) |
| 143 |
.createTestSuite(); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
protected List<Class<? extends AbstractTester>> getTesters() { |
| 148 |
List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); |
| 149 |
testers.add(NavigableSetNavigationTester.class); |
| 150 |
return testers; |
| 151 |
} |
| 152 |
} |